# 安全政策 | Security Policy [English](#english) | [中文](#中文) --- ## 中文 ### 🔒 支持的版本 我们为以下版本提供安全更新: | 版本 | 支持状态 | | ------- | ------------------ | | 1.x | :white_check_mark: | | < 1.0 | :x: | ### 📢 报告漏洞 Zhin.js 团队和社区非常重视安全问题。我们感谢您帮助我们保护项目的安全。 #### 如何报告 如果您发现了安全漏洞,请**不要**通过 GitHub Issues 公开报告。请通过以下方式私下向我们披露: 1. **主要方式**:通过 GitHub Security Advisory 私下报告 - 访问项目的 [Security Advisories](https://github.com/zhinjs/zhin/security/advisories) 页面 - 点击 "Report a vulnerability" #### 报告内容应包括 为了帮助我们更快地理解和修复问题,请在报告中包含: - 漏洞的详细描述 - 重现步骤(如果可能,提供 PoC) - 受影响的版本 - 潜在影响和严重程度 - 您认为可能的修复方案(可选) - 您的联系方式 #### 响应时间表 - **确认接收**:我们会在 48 小时内确认收到您的报告 - **初步评估**:5 个工作日内提供初步评估和预计修复时间 - **修复发布**:根据严重程度,我们将尽快发布修复版本 - 严重漏洞:7-14 天 - 中等漏洞:14-30 天 - 低危漏洞:30-60 天 #### 漏洞奖励计划 目前我们没有正式的漏洞奖励计划,但我们会: - 在修复公告中感谢报告者(如果您希望) - 将您的贡献记录在项目的安全致谢列表中 ### 🛡️ 安全最佳实践 #### 对于用户 1. **保持更新** - 始终使用最新的稳定版本 - 定期检查并应用安全更新 ```bash pnpm update @zhin.js/* --latest ``` 2. **保护凭证** - 永远不要将 `.env` 文件提交到版本控制 - `HTTP_TOKEN` 是 Console 初始化令牌:用随机值,在开放端口前用它(或 `zhin console init`)创建第一个所有者,之后它在所有接口上失效;勿将 Token 写入 URL 或提交到版本库 - Console 账号用强密码;自动化用账号签发的 API 令牌(`ZHIN_CONSOLE_TOKEN`),权限不超过签发人 - 定期轮换 API 密钥和令牌 3. **访问控制** - 限制 Host API(`:8068`)的暴露范围;Remote Console 见 [docs/console/index.md](docs/console/index.md) - 在生产环境中使用 HTTPS - 考虑使用反向代理(如 Nginx)增加安全层 4. **依赖安全** - 定期运行安全审计 ```bash pnpm audit pnpm audit --fix # 自动修复低风险漏洞 ``` 5. **配置安全** - 审查插件权限 - 禁用不需要的功能 - 使用最小权限原则配置机器人账号 6. **生产环境配置** - 生产环境关闭文件监听与热重载,使用环境变量区分开发和生产配置 ```bash # 生产环境启动(脚手架项目的 pnpm start 即此命令) zhin start --foreground --mode production --no-watch ``` - 只在 `zhin.config.yml` 的 `plugins` 中声明实际需要的插件;未声明的包不会被加载 ```yaml plugins: sandbox: "@zhin.js/adapter-sandbox" # ✅ 只声明用到的插件 ``` #### 对于插件开发者 1. **输入验证** - 始终验证和清理用户输入 - 使用 Schema 系统进行类型检查 ```typescript import { Schema } from '@zhin.js/schema' const Input = Schema.object({ url: Schema.string().pattern(/^https?:\/\//), // 验证 URL 格式 count: Schema.number().min(1).max(100) // 限制数值范围 }) const input = Input(untrustedInput) ``` 2. **防止注入攻击** - 使用参数化查询,避免 SQL 注入 ```typescript // ✅ 正确:使用参数化查询 await db.model('users').findOne({ where: { id: userId } }) // ❌ 错误:拼接 SQL await db.query(`SELECT * FROM users WHERE id = ${userId}`) ``` 3. **敏感数据处理** - 不要在日志中记录敏感信息 - 使用环境变量存储密钥 - 加密存储敏感数据 ```typescript // ✅ 正确:使用环境变量 const apiKey = process.env.API_KEY // ❌ 错误:硬编码密钥 const apiKey = 'sk-1234567890abcdef' ``` 4. **权限控制** - 实现适当的权限检查 - 使用中间件进行访问控制 ```typescript // commands/admin/index.ts —— 用 permit 声明谁可以执行,运行时在 execute 之前拦截 import { defineCommand } from 'zhin.js/command' export default defineCommand({ description: '管理员操作', permit: ['admin'], async execute() { // 管理员操作 return '已执行' }, }) ``` ```typescript // middlewares/blocklist/index.ts —— 入站中间件:不调用 next() 即终止分发 import { defineMiddleware } from 'zhin.js/middleware' import type { Message } from '@zhin.js/core/runtime' export default defineMiddleware({ target: 'inbound', async handle(context, next) { const senderId = context.input.sender?.id if (senderId && context.config.blocked?.includes(senderId)) return await next() }, }) ``` 5. **依赖管理** - 仅使用可信赖的依赖包 - 定期更新依赖到安全版本 - 审查依赖的安全公告 6. **错误处理** - 不要泄露敏感的错误信息 - 实现适当的错误边界 ```typescript try { // 操作 } catch (error) { logger.error('操作失败', error) // 仅在日志中记录详细信息 return '操作失败,请稍后重试' // 向用户返回通用信息 } ``` ### ⚠️ 已知安全注意事项 1. **Remote Console / Host API** - Host HTTP 默认 `127.0.0.1:8068`(仅 API);管理界面在 Remote Console,勿把该端口当作公网 Web UI 暴露 - 生产环境限制 API 来源;先创建所有者再暴露端口(所有者创建前,初始化令牌 `HTTP_TOKEN` 等于全权凭据);Remote Console 登录与权限见 [docs/console/access-control.md](docs/console/access-control.md) 2. **插件系统** - 插件在同一进程中运行,具有完整的系统访问权限 - 仅安装来自可信来源的插件 - 审查插件代码,特别是涉及文件系统和网络访问的部分 3. **配置文件** - Root Runtime 只读取 YAML/JSON 数据文件,不执行配置代码 - 密钥使用 `${ENV_VAR}` 引用并存放在环境变量中,不要写入配置文件 4. **热重载功能** - 开发环境功能,不建议在生产环境中启用 - 可能导致未经授权的代码执行 5. **文件监听性能** - 避免监听 `node_modules` 等大型目录 - 在生产环境中禁用文件监听(`zhin start --foreground --mode production --no-watch`) - 监听大量文件可能导致服务器资源耗尽(inotify 限制) - 开发时用 `--watch source config` 只监听项目源码与配置 ### 🔐 安全功能路线图 我们计划在未来版本中引入: - [ ] 插件权限系统(细粒度权限控制) - [ ] 代码签名验证(验证插件完整性) - [ ] 审计日志(记录敏感操作) - [ ] 双因素认证(增强控制台安全) - [ ] 沙箱环境(隔离插件执行) - [x] 自动安全扫描(CI/CD 集成) ### 📞 联系方式 - **安全问题**:[security@zhin.dev](mailto:security@zhin.dev) - **一般问题**:[GitHub Issues](https://github.com/zhinjs/zhin/issues) - **讨论**:[GitHub Discussions](https://github.com/zhinjs/zhin/discussions) --- ## English ### 🔒 Supported Versions We provide security updates for the following versions: | Version | Support Status | | ------- | ------------------ | | 1.x | :white_check_mark: | | < 1.0 | :x: | ### 📢 Reporting a Vulnerability The Zhin.js team and community take security issues seriously. We appreciate your efforts to responsibly disclose your findings. #### How to Report If you discover a security vulnerability, please **DO NOT** report it publicly via GitHub Issues. Instead, please disclose it privately through one of the following methods: 1. **Preferred Method**: Email us at [security@zhin.dev](mailto:security@zhin.dev) 2. **Alternative**: Report privately via GitHub Security Advisory - Visit the project's [Security Advisories](https://github.com/zhinjs/zhin/security/advisories) page - Click "Report a vulnerability" #### What to Include To help us understand and fix the issue faster, please include: - Detailed description of the vulnerability - Steps to reproduce (provide PoC if possible) - Affected versions - Potential impact and severity - Possible fix suggestions (optional) - Your contact information #### Response Timeline - **Acknowledgment**: We will acknowledge receipt within 48 hours - **Initial Assessment**: Initial assessment and estimated fix timeline within 5 business days - **Fix Release**: Depending on severity, we will release a fix as soon as possible - Critical: 7-14 days - Medium: 14-30 days - Low: 30-60 days #### Bug Bounty Program We currently do not have a formal bug bounty program, but we will: - Credit you in the security advisory (if you wish) - List your contribution in the project's security acknowledgments ### 🛡️ Security Best Practices #### For Users 1. **Keep Updated** - Always use the latest stable version - Regularly check and apply security updates ```bash pnpm update @zhin.js/* --latest ``` 2. **Protect Credentials** - Never commit `.env` files to version control - Use strong passwords for Console accounts; `HTTP_TOKEN` is only a bootstrap token — use a random value, create the first owner with it (or `zhin console init`) before exposing the port, after which it is refused everywhere - Give automation an API token issued by an account (`ZHIN_CONSOLE_TOKEN`), never more than that account's permissions - Regularly rotate API keys and tokens 3. **Access Control** - Restrict Web console access (use firewall rules) - Use HTTPS in production - Consider using a reverse proxy (e.g., Nginx) for additional security 4. **Dependency Security** - Regularly run security audits ```bash pnpm audit pnpm audit --fix # Auto-fix low-risk vulnerabilities ``` 5. **Configuration Security** - Review plugin permissions - Disable unnecessary features - Use least privilege principle for bot accounts 6. **Production Environment Configuration** - Turn off file watching and hot reload in production; use environment variables to distinguish dev and production configs ```bash # Production startup (this is what `pnpm start` runs in a scaffolded project) zhin start --foreground --mode production --no-watch ``` - Declare only the plugins you need under `plugins` in `zhin.config.yml`; undeclared packages are not loaded ```yaml plugins: sandbox: "@zhin.js/adapter-sandbox" # ✅ Declare only the plugins in use ``` #### For Plugin Developers 1. **Input Validation** - Always validate and sanitize user input - Use Schema system for type checking ```typescript import { Schema } from '@zhin.js/schema' const Input = Schema.object({ url: Schema.string().pattern(/^https?:\/\//), // Validate URL format count: Schema.number().min(1).max(100) // Limit numeric range }) const input = Input(untrustedInput) ``` 2. **Prevent Injection Attacks** - Use parameterized queries to prevent SQL injection ```typescript // ✅ Correct: Use parameterized queries await db.model('users').findOne({ where: { id: userId } }) // ❌ Wrong: Concatenate SQL await db.query(`SELECT * FROM users WHERE id = ${userId}`) ``` 3. **Sensitive Data Handling** - Don't log sensitive information - Store secrets in environment variables - Encrypt sensitive data at rest ```typescript // ✅ Correct: Use environment variables const apiKey = process.env.API_KEY // ❌ Wrong: Hardcode secrets const apiKey = 'sk-1234567890abcdef' ``` 4. **Permission Control** - Implement proper permission checks - Use middleware for access control ```typescript // commands/admin/index.ts — declare who may run it with `permit`; the runtime rejects others before `execute` import { defineCommand } from 'zhin.js/command' export default defineCommand({ description: 'Admin operation', permit: ['admin'], async execute() { // Admin operations return 'Done' }, }) ``` ```typescript // middlewares/blocklist/index.ts — inbound middleware: not calling next() stops dispatch import { defineMiddleware } from 'zhin.js/middleware' import type { Message } from '@zhin.js/core/runtime' export default defineMiddleware({ target: 'inbound', async handle(context, next) { const senderId = context.input.sender?.id if (senderId && context.config.blocked?.includes(senderId)) return await next() }, }) ``` 5. **Dependency Management** - Only use trusted dependency packages - Regularly update dependencies to secure versions - Review security advisories for dependencies 6. **Error Handling** - Don't leak sensitive error information - Implement proper error boundaries ```typescript try { // Operations } catch (error) { logger.error('Operation failed', error) // Log details only in logs return 'Operation failed, please try again later' // Generic message to users } ``` ### ⚠️ Known Security Considerations 1. **Web Console Access** - By default, Web console listens on all interfaces (0.0.0.0) - Recommend restricting access sources in production - Use strong passwords and consider enabling 2FA (future version) 2. **Plugin System** - Plugins run in the same process with full system access - Only install plugins from trusted sources - Review plugin code, especially filesystem and network access 3. **Configuration Files** - Root Runtime reads YAML/JSON data files only and does not execute configuration code - Keep secrets in environment variables and reference them as `${ENV_VAR}` 4. **Hot Reload Feature** - Development environment feature, not recommended for production - May lead to unauthorized code execution 5. **File Watching Performance** - Avoid watching large directories like `node_modules` - Disable file watching in production (`zhin start --foreground --mode production --no-watch`) - Watching too many files can exhaust server resources (inotify limits) - In development, `--watch source config` watches only project sources and configuration ### 🔐 Security Roadmap We plan to introduce in future versions: - [ ] Plugin permission system (fine-grained permission control) - [ ] Code signing verification (verify plugin integrity) - [ ] Audit logging (record sensitive operations) - [ ] Two-factor authentication (enhance console security) - [ ] Sandbox environment (isolate plugin execution) - [x] Automated security scanning (CI/CD integration) ### 📞 Contact - **Security Issues**: [security@zhin.dev](mailto:security@zhin.dev) - **General Issues**: [GitHub Issues](https://github.com/zhinjs/zhin/issues) - **Discussions**: [GitHub Discussions](https://github.com/zhinjs/zhin/discussions) --- **Last Updated**: November 2025