secrets
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSecrets Management
密钥管理
Core Rules
核心规则
- NEVER hardcode secrets, API keys, OAuth2 client IDs/secrets, tokens, passwords, or credentials in source code
- ALWAYS store secrets in files (or platform-native equivalents like
.env,local.properties).xcconfig - ALWAYS load secrets from environment variables at runtime
- ALWAYS add to
.envbefore first commit.gitignore - ALWAYS provide a documenting required variables (with empty values)
.env.example
- 绝对不要硬编码密钥、API密钥、OAuth2客户端ID/密钥、令牌、密码或凭证到源代码中
- 始终将密钥存储在文件中(或平台原生等效文件,如
.env、local.properties).xcconfig - 始终在运行时从环境变量加载密钥
- 始终在首次提交前将添加到
.env中.gitignore - 始终提供一个文件,记录所需变量(值留空)
.env.example
Workflow
工作流程
When Writing Code That Uses Secrets
编写使用密钥的代码时
- Detect the platform/framework from the project files
- Check if and
.envare set up — if not, create them.gitignore - Load secrets from environment variables using the platform's standard pattern
- Never use string literals for secret values — always reference ,
process.env.*, etc.os.getenv() - Add the variable name to with an empty value and a descriptive comment
.env.example - Run the scan script to verify no secrets leaked:
python3 scripts/scan_secrets.py .
- 从项目文件中识别平台/框架
- 检查和
.env是否已配置——如果没有,创建它们.gitignore - 使用平台的标准方式从环境变量加载密钥
- 切勿使用字符串字面量存储密钥值——始终引用、
process.env.*等os.getenv() - 将变量名添加到中,值留空并添加描述性注释
.env.example - 运行扫描脚本以验证没有密钥泄露:
python3 scripts/scan_secrets.py .
When Setting Up a New Project
搭建新项目时
- Create with required variables
.env - Create mirroring
.env.examplestructure with empty values (use env-example-template as a starting point).env - Add secret-related entries to (use gitignore-secrets as reference)
.gitignore - Install the loading library for the platform
.env - Add loading code at the application entry point
- 创建包含所需变量的文件
.env - 创建与结构一致的
.env文件,值留空(可参考env-example-template作为模板).env.example - 将与密钥相关的条目添加到中(可参考gitignore-secrets)
.gitignore - 安装对应平台的加载库
.env - 在应用入口处添加密钥加载代码
When Reviewing Code
代码审查时
Run to detect:
python3 scripts/scan_secrets.py <project-directory>- Hardcoded API keys, tokens, and passwords
- OAuth2 client secrets in source
- AWS keys, Google API keys, Stripe keys, GitHub tokens
- Embedded private keys
- Connection strings with credentials
- Missing entries for
.gitignore.env - Missing
.env.example
运行以检测:
python3 scripts/scan_secrets.py <项目目录>- 硬编码的API密钥、令牌和密码
- 源代码中的OAuth2客户端密钥
- AWS密钥、Google API密钥、Stripe密钥、GitHub令牌
- 嵌入的私钥
- 包含凭证的连接字符串
- 中缺少
.gitignore的条目.env - 缺少文件
.env.example
Quick Reference by Platform
各平台快速参考
For platform-specific loading patterns (install, load, access, framework variants), see references/platforms.md. Covers:
.env- JavaScript/TypeScript: Node.js, Next.js, Vite, React, Nuxt, Remix, Express, NestJS
- Python: Django, Flask, FastAPI
- Ruby: Rails
- Go: godotenv
- Java/Kotlin: Spring Boot
- PHP: Laravel
- Rust: dotenvy
- Swift/iOS: Xcode .xcconfig, Vapor
- Android/Kotlin: local.properties + BuildConfig
- Flutter/Dart: flutter_dotenv
- C#/.NET: DotNetEnv, User Secrets
- Docker: --env-file, docker-compose env_file
- CI/CD: GitHub Actions, GitLab CI, Vercel, Netlify, AWS, GCP, Azure
有关各平台特定的加载方式(安装、加载、访问、框架变体),请查看references/platforms.md。涵盖以下平台:
.env- JavaScript/TypeScript: Node.js, Next.js, Vite, React, Nuxt, Remix, Express, NestJS
- Python: Django, Flask, FastAPI
- Ruby: Rails
- Go: godotenv
- Java/Kotlin: Spring Boot
- PHP: Laravel
- Rust: dotenvy
- Swift/iOS: Xcode .xcconfig, Vapor
- Android/Kotlin: local.properties + BuildConfig
- Flutter/Dart: flutter_dotenv
- C#/.NET: DotNetEnv, User Secrets
- Docker: --env-file, docker-compose env_file
- CI/CD: GitHub Actions, GitLab CI, Vercel, Netlify, AWS, GCP, Azure
Anti-Patterns to Block
需避免的反模式
Never generate code like:
undefined切勿生成如下代码:
undefinedBAD - hardcoded secrets
BAD - hardcoded secrets
api_key = "sk-1234567890abcdef"
client_secret = "my-oauth-secret"
DATABASE_URL = "postgres://user:password@host/db"
const token = "ghp_xxxxxxxxxxxxxxxxxxxx";
Always generate code like:api_key = "sk-1234567890abcdef"
client_secret = "my-oauth-secret"
DATABASE_URL = "postgres://user:password@host/db"
const token = "ghp_xxxxxxxxxxxxxxxxxxxx";
应始终生成如下代码:GOOD - loaded from environment
GOOD - loaded from environment
api_key = os.getenv("API_KEY")
const token = process.env.GITHUB_TOKEN;
undefinedapi_key = os.getenv("API_KEY")
const token = process.env.GITHUB_TOKEN;
undefinedMobile Platform Notes
移动平台注意事项
- iOS: Use files (gitignored) referenced from Xcode build settings — not
.xcconfigat runtime.env - Android: Use (gitignored by default) injected via
local.properties— notbuildConfigFieldat runtime.env - Flutter: bundles
flutter_dotenvinto the app binary. For truly sensitive secrets, use a backend proxy instead of embedding in the mobile app.env
- iOS:使用文件(已添加到git忽略),从Xcode构建设置中引用——切勿在运行时使用
.xcconfig.env - Android:使用(默认已添加git忽略),通过
local.properties注入——切勿在运行时使用buildConfigField.env - Flutter:会将
flutter_dotenv打包到应用二进制文件中。对于真正敏感的密钥,请使用后端代理,而非嵌入到移动应用中.env