rohitg00/ai-engineering-from-scratch · error · ValueError

secret name must be an uppercase environment variable

Error message

secret name must be an uppercase environment variable

What it means

Raised in EnvironmentSecrets.require (via its name validation) when a requested secret name is not an uppercase environment-variable-style identifier. The guard rejects lowercase or malformed names before any environment lookup, keeping the secret registry to canonical ENV_VAR keys.

Solutions

  1. Request secrets by their canonical uppercase name (e.g. 'API_KEY', 'DATABASE_URL')
  2. Normalize names with str.upper() at the call site if mixed case is expected
  3. Fix typo'd secret names in configuration that mix case
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at certifications/claude/lessons/13-application-security-and-secrets/code/main.py:107 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/69d3a81dd7e2cd06. Report an issue: GitHub.

Appendix: source

Thrown at certifications/claude/lessons/13-application-security-and-secrets/code/main.py:107

        if not isinstance(url, str):
            return Decision(False, "url must be a string", ("schema_validation",))
        parsed = urlparse(url)
        if parsed.scheme != "https" or parsed.hostname not in self.allowed_hosts:
            return Decision(False, "network destination is not allowed", ("network_allowlist",))
        return Decision(True, "read-only request to allowed host", ("network_allowlist", "response_size_limit"))


def redact(value: str) -> str:
    """Remove common secret assignments before a string reaches logs."""
    return SECRET_VALUE_PATTERN.sub(lambda match: f"{match.group(1)}=[REDACTED]", value)


class EnvironmentSecrets:
    """Return secret values to trusted code without logging or serialization helpers."""

    def require(self, name: str) -> str:
        if not re.fullmatch(r"[A-Z][A-Z0-9_]+", name):
            raise ValueError("secret name must be an uppercase environment variable")
        value = os.environ.get(name)
        if not value:
            raise RuntimeError(f"required secret {name} is not configured")
        return value


def demo() -> dict[str, Any]:
    gate = PolicyGate(["/workspace/project"], ["api.example.test"])
    actions = [
        Action("read_file", {"path": "/workspace/project/README.md"}),
        Action("read_file", {"path": "/workspace/project/.env"}),
        Action("run_command", {"command": "rm -rf /workspace/project"}, approved=True),
        Action("http_get", {"url": "https://api.example.test/status"}),
    ]
    return {"decisions": [decision.__dict__ for decision in map(gate.evaluate, actions)]}


if __name__ == "__main__":

View on GitHub (pinned to 39ea8a1c6d)