env-var-hygiene
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEnvironment Variable Hygiene
环境变量规范管理
Best practices for managing environment variables across deployment platforms.
跨部署平台管理环境变量的最佳实践。
Triggers
触发场景
Invoke when user mentions:
- "env var", "environment variable", "production deploy"
- "webhook secret", "API key", "token"
- "Invalid character in header", "ERR_INVALID_CHAR"
- "silent failure", "webhook not working"
- "Vercel", "Convex", "deployment", "secrets"
当用户提及以下内容时适用:
- "env var"、"environment variable"(环境变量)、"production deploy"(生产环境部署)
- "webhook secret"(Webhook密钥)、"API key"(API密钥)、"token"(令牌)
- "Invalid character in header"(请求头存在无效字符)、"ERR_INVALID_CHAR"
- "silent failure"(静默失败)、"webhook not working"(Webhook无法正常工作)
- "Vercel"、"Convex"、"deployment"(部署)、"secrets"(保密信息)
Core Principles
核心原则
1. Trailing Whitespace Kills
1. 尾随空格是隐形杀手
Env vars with or trailing spaces cause cryptic errors:
\n- "Invalid character in header content" (HTTP headers)
- Webhook signature mismatch
- Silent authentication failures
Root Cause: Copy-paste or introduces invisible characters.
echoPrevention:
bash
undefined带有或尾随空格的环境变量会导致难以排查的错误:
\n- HTTP请求头报错“Invalid character in header content”
- Webhook签名不匹配
- 认证静默失败
根本原因: 复制粘贴或命令会引入不可见字符。
echo预防方案:
bash
undefined✅ Use printf, not echo
✅ 使用printf而非echo
printf '%s' 'sk_live_xxx' | vercel env add STRIPE_SECRET_KEY production
printf '%s' 'sk_live_xxx' | vercel env add STRIPE_SECRET_KEY production
✅ Trim when setting
✅ 设置时去除多余字符
npx convex env set --prod KEY "$(echo 'value' | tr -d '\n')"
npx convex env set --prod KEY "$(echo 'value' | tr -d '\n')"
❌ Don't use echo directly
❌ 不要直接使用echo
echo "sk_live_xxx" | vercel env add KEY production # May add \n
undefinedecho "sk_live_xxx" | vercel env add KEY production # 可能会添加\n
undefined2. Cross-Platform Parity
2. 跨平台一致性
Shared tokens (webhook secrets, auth tokens) must be identical across platforms:
- Vercel ↔ Convex
- Frontend ↔ Backend
- Dev ↔ Staging ↔ Prod (within each platform)
Common Pitfall: Set token on one platform, forget the other.
Prevention:
bash
undefined共享令牌(如Webhook密钥、认证令牌)必须在所有平台保持一致:
- Vercel ↔ Convex
- 前端 ↔ 后端
- 开发环境 ↔ 预发布环境 ↔ 生产环境(同一平台内)
常见误区: 在一个平台设置了令牌,却忘记在其他平台同步。
预防方案:
bash
undefinedGenerate token once
一次性生成令牌
TOKEN=$(openssl rand -hex 32)
TOKEN=$(openssl rand -hex 32)
Set on ALL platforms
在所有平台设置
npx convex env set --prod CONVEX_WEBHOOK_TOKEN "$(printf '%s' "$TOKEN")"
printf '%s' "$TOKEN" | vercel env add CONVEX_WEBHOOK_TOKEN production
undefinednpx convex env set --prod CONVEX_WEBHOOK_TOKEN "$(printf '%s' "$TOKEN")"
printf '%s' "$TOKEN" | vercel env add CONVEX_WEBHOOK_TOKEN production
undefined3. Validate Format Before Use
3. 使用前验证格式
API keys have specific formats. Validate before deployment:
| Service | Pattern | Example |
|---|---|---|
| Stripe Secret | | |
| Stripe Public | | |
| Stripe Webhook | | |
| Stripe Price | | |
| Clerk Secret | | |
API密钥有特定格式,部署前需验证:
| 服务 | 格式规则 | 示例 |
|---|---|---|
| Stripe Secret | | |
| Stripe Public | | |
| Stripe Webhook | | |
| Stripe Price | | |
| Clerk Secret | | |
4. Dev ≠ Prod
4. 开发环境≠生产环境
Separate deployments have separate env var stores:
- Setting doesn't affect production
.env.local - Convex dev and prod are separate deployments
- Vercel has per-environment variables
Always verify prod separately:
bash
undefined不同部署环境拥有独立的环境变量存储:
- 设置不会影响生产环境
.env.local - Convex的开发和生产环境是独立部署实例
- Vercel支持按环境配置变量
务必单独验证生产环境:
bash
undefinedConvex
Convex
npx convex env list --prod
npx convex env list --prod
Vercel
Vercel
vercel env ls --environment=production
undefinedvercel env ls --environment=production
undefined5. CLI Environment Gotcha
5. CLI环境的陷阱
CONVEX_DEPLOYMENT=prod:xxx npx convex dataAlways use explicit flags:
bash
undefined使用可能返回开发环境数据。
CONVEX_DEPLOYMENT=prod:xxx npx convex data始终使用显式标志:
bash
undefined❌ Unreliable
❌ 不可靠
CONVEX_DEPLOYMENT=prod:xxx npx convex data
CONVEX_DEPLOYMENT=prod:xxx npx convex data
✅ Reliable
✅ 可靠
npx convex run --prod module:function
npx convex env list --prod
undefinednpx convex run --prod module:function
npx convex env list --prod
undefinedQuick Reference
快速参考
Setting Env Vars Safely
安全设置环境变量
Convex:
bash
undefinedConvex:
bash
undefinedDev
开发环境
npx convex env set KEY "value"
npx convex env set KEY "value"
Prod (use --prod flag)
生产环境(使用--prod标志)
npx convex env set --prod KEY "$(printf '%s' 'value')"
**Vercel:**
```bashnpx convex env set --prod KEY "$(printf '%s' 'value')"
**Vercel:**
```bashProduction
生产环境
printf '%s' 'value' | vercel env add KEY production
printf '%s' 'value' | vercel env add KEY production
With explicit environment
指定环境
vercel env add KEY production --force
undefinedvercel env add KEY production --force
undefinedChecking Env Vars
查看环境变量
Convex:
bash
npx convex env list # dev
npx convex env list --prod # prodVercel:
bash
vercel env ls # all
vercel env ls --environment=production # prod onlyConvex:
bash
npx convex env list # 开发环境
npx convex env list --prod # 生产环境Vercel:
bash
vercel env ls # 所有环境
vercel env ls --environment=production # 仅生产环境Detecting Issues
问题检测
Trailing whitespace:
bash
undefined尾随空格检测:
bash
undefinedCheck Convex prod
检查Convex生产环境
npx convex env list --prod | while IFS= read -r line; do
if [[ "$line" =~ [[:space:]]$ ]]; then
echo "WARNING: $(echo "$line" | cut -d= -f1) has trailing whitespace"
fi
done
**Format validation:**
```bashnpx convex env list --prod | while IFS= read -r line; do
if [[ "$line" =~ [[:space:]]$ ]]; then
echo "WARNING: $(echo "$line" | cut -d= -f1) has trailing whitespace"
fi
done
**格式验证:**
```bashValidate Stripe key format
验证Stripe密钥格式
value=$(npx convex env list --prod | grep "^STRIPE_SECRET_KEY=" | cut -d= -f2-)
[[ "$value" =~ ^sk_(test|live)_[A-Za-z0-9]+$ ]] || echo "Invalid format"
undefinedvalue=$(npx convex env list --prod | grep "^STRIPE_SECRET_KEY=" | cut -d= -f2-)
[[ "$value" =~ ^sk_(test|live)_[A-Za-z0-9]+$ ]] || echo "Invalid format"
undefinedReferences
参考资料
See directory:
references/- - Regex patterns for common services
format-patterns.md - - Vercel, Convex, Railway platform details
platform-specifics.md - - Pre-deployment validation checklist
hygiene-checklist.md - - Cross-platform token verification
parity-verification.md
请查看目录:
references/- - 常见服务的正则格式规则
format-patterns.md - - Vercel、Convex、Railway等平台的详细配置说明
platform-specifics.md - - 部署前验证清单
hygiene-checklist.md - - 跨平台令牌一致性校验指南
parity-verification.md
Related Commands
相关命令
- - Comprehensive pre-deployment checklist
/pre-deploy - - Cross-platform token verification
/env-parity-check - - Stripe-specific environment audit
/stripe-check
Based on 2026-01-17 incident: Trailing in caused "Invalid character in header" error. Token mismatch between Vercel and Convex caused silent webhook failures.
\nSTRIPE_SECRET_KEY- - 全面的部署前检查清单
/pre-deploy - - 跨平台令牌一致性校验
/env-parity-check - - Stripe专属环境配置审计
/stripe-check
基于2026-01-17的故障案例:中的尾随导致“Invalid character in header”错误;Vercel与Convex间的令牌不匹配导致Webhook静默失败。
STRIPE_SECRET_KEY\n