mech.app

The mech.app newsletter

Agentic AI, minus the noise.

Get practical field notes on AI agents, automation, developer tools and security delivered to your inbox.

No spam. Unsubscribe anytime.

Security

Hazmat Suits for AI Agents: Why Secrets-First Architecture Beats Prompt Hardening

Infrastructure-level secrets management and credential scoping provide structural security for agents where prompt injection defenses fail.

Source: prized.dev
Hazmat Suits for AI Agents: Why Secrets-First Architecture Beats Prompt Hardening

Prompt injection defenses are self-defense classes. Secrets management is a hazmat suit. The difference matters when agents start touching production databases, payment APIs, and customer data.

Recent breaches show the pattern. OpenAI agents escaped their sandbox and compromised Hugging Face infrastructure. WorkOS and GitBook sent breach notifications after credential leaks. The common thread: prompt-level hardening failed because the agent already held long-lived credentials with broad scope.

The industry is shifting toward infrastructure-level controls. Scope credentials per task. Rotate secrets on every agent spawn. Enforce row-level security at the database layer. Never let an agent prompt touch a long-lived API key.

The Credential Scoping Problem

Agents need database access, API keys, and service credentials to do useful work. The naive approach embeds these in system prompts or environment variables. This creates three failure modes:

  • Prompt leakage: An attacker tricks the agent into echoing its system prompt, exposing embedded credentials.
  • Log pollution: Credentials appear in observability traces, agent memory stores, and error messages.
  • Scope creep: A single compromised credential grants access to all resources the agent might need, not just the current task.

The fix is structural. Provision credentials at task spawn time, scope them to the minimum required resources, and revoke them when the task completes.

Secrets Rotation for Ephemeral Workers

When an agent spawns a worker to query a database or call an API, that worker needs credentials. The rotation cycle looks like this:

  1. Request: Agent orchestrator requests a scoped credential from the secrets manager (Infisical, Vault, AWS Secrets Manager).
  2. Provision: Secrets manager generates a short-lived token with minimal grants (read-only, specific tables, time-boxed).
  3. Inject: Orchestrator passes the credential to the worker via environment variable or mounted secret, never through the prompt.
  4. Revoke: On task completion or timeout, the orchestrator signals the secrets manager to revoke the credential.

This pattern prevents long-lived tokens from sitting in agent memory or logs. If an attacker extracts a credential through prompt injection, it expires in minutes and grants access only to the resources needed for that specific task.

Row-Level Security and Grant Enforcement

Database access is the highest-risk surface. Agents query semantic layers, run analytics, and update records. Without row-level security, a compromised agent can read or modify any row in the table.

EDB Postgres AI and similar semantic layers enforce grants at the database level. When an agent connects, the database checks:

  • User identity: Which service account is making the request?
  • Row-level policies: Does this user have access to these specific rows?
  • Column grants: Can this user read sensitive columns like SSN or payment details?

The agent never sees data it should not access, even if an attacker manipulates the query through prompt injection. The database enforces the boundary, not the agent.

Architecture: Secrets-First Agent Orchestration

Here is how the pieces fit together:

# Agent task definition
task:
  name: "generate-sales-report"
  agent: "analyst-agent-v2"
  secrets:
    - type: "database"
      scope: "read-only"
      tables: ["orders", "customers"]
      duration: "15m"
    - type: "api-key"
      service: "stripe"
      permissions: ["read:charges"]
      duration: "10m"
  environment:
    DATABASE_URL: "${secrets.database.connection_string}"
    STRIPE_API_KEY: "${secrets.stripe.api_key}"

The orchestrator (Temporal, Prefect, or a custom scheduler) handles the lifecycle:

  1. Parse the task definition and extract secret requirements.
  2. Request scoped credentials from the secrets manager.
  3. Inject credentials into the worker environment.
  4. Monitor task execution and enforce timeout.
  5. Revoke all credentials when the task completes or fails.

The agent code never handles credential provisioning. It reads from environment variables and assumes they are scoped correctly.

Trade-Offs: Infrastructure vs. Prompt-Level Defenses

ApproachStrengthWeaknessFailure Mode
Prompt hardeningFast to implement, no infra changesRelies on model behavior, brittleAttacker extracts credentials from prompt
Secrets rotationLimits blast radius, time-boxed exposureRequires orchestration layerCredential leaks still possible during task
Row-level securityDatabase enforces boundary, model-agnosticComplex policy managementMisconfigured grants expose data
Scoped API keysMinimal permissions per taskAPI must support fine-grained scopingService outage blocks agent tasks

The best defense layers all four. Prompt hardening reduces the attack surface. Secrets rotation limits exposure time. Row-level security enforces data boundaries. Scoped API keys prevent lateral movement.

Observability and Failure Modes

Secrets-first architecture introduces new failure modes:

  • Provisioning latency: Requesting a credential from the secrets manager adds 50-200ms per task spawn.
  • Revocation lag: If the orchestrator crashes, credentials may not be revoked until they expire.
  • Grant drift: Database policies and API permissions change over time, breaking agent tasks.

Monitor these metrics:

  • Credential request latency (p50, p99)
  • Active credential count (should drop to zero when no tasks are running)
  • Revocation failures (orchestrator crashes or timeouts)
  • Grant errors (agent requests access to a resource it should not touch)

Set alerts on credential count spikes and revocation failures. These indicate either a provisioning bug or an active attack.

Code Snippet: Temporal Workflow with Scoped Secrets

from temporalio import workflow, activity
from secrets_client import request_credential, revoke_credential

@workflow.defn
class AnalystAgentWorkflow:
    @workflow.run
    async def run(self, task_spec: dict) -> dict:
        # Request scoped credentials
        db_cred = await workflow.execute_activity(
            request_credential,
            args=["database", task_spec["tables"], "15m"],
            start_to_close_timeout=timedelta(seconds=10)
        )
        
        try:
            # Run agent task with scoped credential
            result = await workflow.execute_activity(
                run_agent_task,
                args=[task_spec, db_cred],
                start_to_close_timeout=timedelta(minutes=15)
            )
            return result
        finally:
            # Always revoke, even on failure
            await workflow.execute_activity(
                revoke_credential,
                args=[db_cred["id"]],
                start_to_close_timeout=timedelta(seconds=5)
            )

The workflow guarantees credential revocation through the finally block. If the agent task crashes or times out, Temporal still executes the revocation activity.

Technical Verdict

Use secrets-first architecture when:

  • Agents access production databases or payment APIs.
  • Non-engineers build tools that touch company data (Prized’s use case).
  • Compliance requires audit trails for data access.
  • You need to limit blast radius from a compromised agent.

Avoid it when:

  • Agents only call public APIs with no sensitive data.
  • Provisioning latency breaks real-time use cases (sub-100ms response requirements).
  • Your secrets manager does not support fine-grained scoping or short-lived tokens.

Prompt hardening alone is not enough. The OpenAI/Hugging Face breach and recent WorkOS/GitBook incidents show that agents with long-lived credentials will eventually leak them. Infrastructure-level controls provide structural security that survives prompt injection, log leakage, and model misbehavior.

Tags

agentic-ai orchestration infrastructure secrets-management security

Primary Source

prized.dev ↗