保护您的 API 密钥和数据安全,遵循企业级安全最佳实践
AI Router Platform 采用企业级安全措施保护您的数据和 API 密钥。 所有敏感数据使用 AES-256 加密存储,传输过程使用 HTTPS 加密, 并实施完整的 RBAC 权限控制和审计日志。
所有 Provider API Keys 加密存储,密钥环境变量管理
安全的 Token 认证,24 小时有效期,自动续期
细粒度访问控制,3 种角色,多租户隔离
所有数据传输使用 TLS 1.3 加密
使用参数化查询,防止 SQL 注入
Content Security Policy + 输入过滤
完整的操作日志记录和追踪
硬编码 API Key
// ❌ 错误
const API_KEY = "sk-xxxxx";
提交到 Git
# ❌ .env 文件被提交
git add .env
git commit -m "add config"
在前端暴露
// ❌ 浏览器中可见
const key = process.env.API_KEY;
使用环境变量
// ✅ 正确
const key = process.env.API_KEY;
添加到 .gitignore
# ✅ .gitignore
.env
.env.local
.env.production
只在后端使用
// ✅ 后端 API 路由
app.post('/api/chat', auth, handler);
.env 文件
# ⚠️ 重要:不要提交此文件到 Git
AI_ROUTER_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
AI_ROUTER_BASE_URL=https://api.poeti.ai/v1
# JWT Secret(必须至少 32 字节)
JWT_SECRET=your-super-secret-jwt-key-32-bytes-min
# 数据库密码
POSTGRES_PASSWORD=your-strong-database-password
# Provider API Keys(加密存储)
ENCRYPTION_KEY=your-32-byte-encryption-key-here
# 生成 32 字节随机密钥
openssl rand -base64 32
# 或使用 /dev/urandom
head -c 32 /dev/urandom | base64
Python
import os
from dotenv import load_dotenv
# 加载环境变量
load_dotenv()
# ✅ 安全获取 API Key
api_key = os.getenv("AI_ROUTER_API_KEY")
if not api_key:
raise ValueError("API Key 未设置")
# 使用 API Key
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
Node.js
require('dotenv').config();
// ✅ 安全获取 API Key
const apiKey = process.env.AI_ROUTER_API_KEY;
if (!apiKey) {
throw new Error('API Key 未设置');
}
// 使用 API Key(仅在后端)
const headers = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
};
Go
package main
import (
"os"
"log"
)
func main() {
// ✅ 安全获取 API Key
apiKey := os.Getenv("AI_ROUTER_API_KEY")
if apiKey == "" {
log.Fatal("API Key 未设置")
}
// 使用 API Key
req.Header.Set("Authorization", "Bearer " + apiKey)
}
平台使用 JWT (JSON Web Token) 进行用户认证,Token 有效期为 24 小时, 支持自动刷新机制。
/v1/auth/login 登录,获取 JWT TokenlocalStorage 或 sessionStorageAuthorization header 中携带 TokenHTTP 请求示例
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
推荐:使用密码管理器(如 1Password、LastPass)生成和管理强密码
所有 Provider API Keys 使用 AES-256-GCM 加密存储,
加密密钥通过 ENCRYPTION_KEY 环境变量管理,从不明文存储。
*** 遮掩加密实现(Go)
// internal/crypto/encryption.go
func Encrypt(plaintext, key string) (string, error) {
// 1. 创建 AES cipher
block, _ := aes.NewCipher([]byte(key))
// 2. 创建 GCM mode
gcm, _ := cipher.NewGCM(block)
// 3. 生成随机 nonce
nonce := make([]byte, gcm.NonceSize())
io.ReadFull(rand.Reader, nonce)
// 4. 加密数据
ciphertext := gcm.Seal(nonce, nonce,
[]byte(plaintext), nil)
return base64.StdEncoding.EncodeToString(
ciphertext), nil
}
平台实施 RBAC (Role-Based Access Control) 权限系统, 提供 3 种内置角色,支持细粒度资源访问控制。
| 资源/操作 | Admin | Developer | Viewer |
|---|---|---|---|
| 创建 API Key | ✅ | ✅ | ❌ |
| 查看使用统计 | ✅ | ✅ | ✅ |
| 管理组织 | ✅ | ❌ | ❌ |
| 配置系统设置 | ✅ | ❌ | ❌ |
| 查看所有日志 | ✅ | 仅自己 | ❌ |
⚠️ 立即行动
系统已实施多层 XSS 防护:Content Security Policy (CSP)、输入过滤、输出转义。
开发者注意事项:
textContent 而非 innerHTML 插入用户输入eval() 或 new Function()API Key 支持 IP 白名单功能,只允许特定 IP 地址调用。
# 在控制台配置 API Key 时添加
allowed_ips:
- 203.0.113.0/24
- 198.51.100.42
平台已完成全面安全审计(2026-03-25),修复所有高危和中危漏洞。 审计范围包括:前端、后端、配置、基础设施。
✅ 0
高危漏洞
✅ 0
中危漏洞
✅ 8
已修复问题
完整安全审计报告:SECURITY_AUDIT_REPORT.md