env-validator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEnv Validator
环境变量验证器
Validates environment variable configurations by cross-referencing files against
project requirements. Catches missing variables, type errors, insecure defaults, and
orphaned entries before they cause runtime failures.
.env通过将 文件与项目要求交叉比对来验证环境变量配置,在缺失变量、类型错误、不安全默认值和孤立配置项导致运行时故障之前提前发现问题。
.envReference Files
参考文件
| File | Contents | Load When |
|---|---|---|
| Built-in validation rules and severity definitions | Always |
| 文件 | 内容 | 加载时机 |
|---|---|---|
| 内置验证规则和严重级别定义 | 始终加载 |
Prerequisites
前置要求
- A file (or equivalent) in the project
.env - Optionally: ,
.env.example, or deployment manifests for cross-referencingdocker-compose.yml
- 项目中存在 文件(或同等作用的环境变量文件)
.env - 可选:、
.env.example或部署清单,用于交叉比对docker-compose.yml
Workflow
工作流程
Phase 1: Discovery
阶段1:资源发现
Locate environment configuration sources in the project:
- Primary file: Find in the project root. If absent, check for
.env,.env.local,.env.development.env.production - Schema file: Find or
.env.example— this defines the expected variables.env.template - Code references: Grep for ,
os.environ,process.env,env::varpatterns to find variables referenced in codeos.Getenv - Deployment manifests: Check ,
docker-compose.yml,Dockerfilemanifests fork8s/or${VAR}patternsENV VAR
Report what was found before proceeding.
定位项目中的环境配置源:
- 主配置文件: 查找项目根目录下的 ,如果不存在,再检查
.env、.env.local、.env.development.env.production - Schema 文件: 查找 或
.env.example—— 这类文件定义了预期的变量.env.template - 代码引用: 检索 、
os.environ、process.env、env::var模式,找到代码中引用的变量os.Getenv - 部署清单: 检查 、
docker-compose.yml、Dockerfile清单中的k8s/或${VAR}模式ENV VAR
继续处理前先报告已发现的资源。
Phase 2: Schema Extraction
阶段2:Schema 提取
Build the expected variable schema from discovered sources:
For each variable found across all sources, record:
| Field | Source |
|---|---|
| Name | Variable name (e.g., |
| Required | Present in code references or marked required in example |
| Type hint | Inferred from usage (URL, integer, boolean, string, path) |
| Default | Value in |
| Used in | List of files that reference this variable |
从已发现的数据源中构建预期的变量 Schema:
对所有来源中发现的每个变量,记录以下信息:
| 字段 | 来源 |
|---|---|
| 名称 | 变量名(例如 |
| 是否必填 | 在代码中被引用,或在示例文件中标记为必填 |
| 类型提示 | 从使用方式推断(URL、整数、布尔值、字符串、路径) |
| 默认值 | 若 |
| 使用位置 | 引用该变量的文件列表 |
Phase 3: Validation
阶段3:验证
Run these checks against the primary file:
.env-
Missing required variables (CRITICAL)
- Variable referenced in code but absent from
.env - Variable in without a default but absent from
.env.example.env
- Variable referenced in code but absent from
-
Type mismatches (HIGH)
- when code does
PORT=abcint(os.environ["PORT"]) - when code expects boolean (
DEBUG=yes/true)false - URL variables without valid URL format
-
Insecure defaults (HIGH)
- ,
SECRET_KEY=changeme,PASSWORD=passwordAPI_KEY=xxx - or
DEBUG=truein production-targeted filesDEBUG=1 - Empty values for security-critical variables
-
Unreferenced variables (MEDIUM)
- Variables in not referenced anywhere in code or manifests
.env - May indicate stale configuration
- Variables in
-
Format issues (LOW)
- Lines without format
KEY=VALUE - Trailing whitespace in values
- Inconsistent quoting (mixing single/double/no quotes)
- Duplicate variable definitions (last wins, but likely a mistake)
- Lines without
See for the complete rule catalog.
references/validation-rules.md对主 文件执行以下检查:
.env-
缺失必填变量(严重)
- 代码中引用了该变量,但 中不存在
.env - 中存在该变量且无默认值,但
.env.example中不存在.env
- 代码中引用了该变量,但
-
类型不匹配(高优先级)
- 代码中执行 但配置为
int(os.environ["PORT"])PORT=abc - 代码期望布尔值(/
true)但配置为falseDEBUG=yes - URL 变量不符合合法 URL 格式
- 代码中执行
-
不安全默认值(高优先级)
- 、
SECRET_KEY=changeme、PASSWORD=password这类配置API_KEY=xxx - 面向生产环境的文件中配置了 或
DEBUG=trueDEBUG=1 - 安全关键变量为空值
-
未引用变量(中优先级)
- 中存在的变量未在代码或部署清单中任何位置引用
.env - 可能是过期配置
-
格式问题(低优先级)
- 不符合 格式的行
KEY=VALUE - 值末尾有尾随空格
- 引号使用不一致(混合使用单引号/双引号/无引号)
- 重复定义变量(最后定义的生效,但大概率是错误)
- 不符合
完整规则目录请查看 。
references/validation-rules.mdPhase 4: Report
阶段4:报告
Produce a structured validation report:
markdown
undefined生成结构化的验证报告:
markdown
undefinedEnvironment Validation Report
Environment Validation Report
File:
Schema: + code references
Verdict: PASS | FAIL
.env.env.exampleFile:
Schema: + code references
Verdict: PASS | FAIL
.env.env.exampleSummary
Summary
| Severity | Count |
|---|---|
| CRITICAL | N |
| HIGH | N |
| MEDIUM | N |
| LOW | N |
| Severity | Count |
|---|---|
| CRITICAL | N |
| HIGH | N |
| MEDIUM | N |
| LOW | N |
CRITICAL
CRITICAL
[ENV-001] Missing required variable: DATABASE_URL
[ENV-001] Missing required variable: DATABASE_URL
- Referenced in: ,
src/db.py:12docker-compose.yml:8 - Expected type: URL (postgresql://...)
- Fix: Add to
DATABASE_URL=postgresql://user:pass@localhost:5432/dbname.env
- Referenced in: ,
src/db.py:12docker-compose.yml:8 - Expected type: URL (postgresql://...)
- Fix: Add to
DATABASE_URL=postgresql://user:pass@localhost:5432/dbname.env
HIGH
HIGH
...
...
Unreferenced Variables
Unreferenced Variables
| Variable | In .env | In Code | In Manifests | Status |
|---|---|---|---|---|
| LEGACY_API_KEY | Yes | No | No | Unreferenced |
| Variable | In .env | In Code | In Manifests | Status |
|---|---|---|---|---|
| LEGACY_API_KEY | Yes | No | No | Unreferenced |
Recommendations
Recommendations
- [Highest priority fix]
- [Second fix]
undefined- [Highest priority fix]
- [Second fix]
undefinedError Handling
错误处理
| Error | Resolution |
|---|---|
| No .env file found | Report absence; check for alternative env sources |
| No .env.example or schema | Validate based on code references only |
| Binary or very large .env | Skip; report as unsupported format |
| No code references found | Validate format and security only; skip completeness |
| 错误 | 解决方案 |
|---|---|
| 未找到 .env 文件 | 报告缺失情况,检查是否有其他替代的环境变量源 |
| 未找到 .env.example 或 Schema | 仅基于代码引用进行验证 |
| 二进制格式或体积过大的 .env 文件 | 跳过处理,报告为不支持的格式 |
| 未找到代码引用 | 仅验证格式和安全性,跳过完整性检查 |
Limitations
限制
- Cannot validate runtime-injected variables (from vault, AWS SSM, etc.)
- Type inference is heuristic — may misclassify complex values
- Does not check variable values against external services (e.g., valid API key format)
- Production vs. development distinction requires file naming conventions
- 无法验证运行时注入的变量(来自 vault、AWS SSM 等)
- 类型推断是启发式的,可能会对复杂值分类错误
- 不会对照外部服务检查变量值(例如合法 API 密钥格式)
- 生产与开发环境的区分依赖文件命名规范