r/AskNetsec • • 22d ago

Other AI agent runtime security: how to sanitize prompts of secrets before agents store/use it

I used Claude Code (Anthropic) at our org. And sometimes the agents/claude code use env tokens or stored secrets.

Is there a way where we can strip the prompts of the secrets/access keys before it reaches the agent. Something like prompt going into a lambda and getting sanitized before AI using it.

I know some repos on GitHub that help find leaked secrets in prompt logs but those are when the prompt logs are stored. I’m seriously thinking about setting up a Capsule or Prompt Security POC, but I want to know if there is a way where the llm/agent doesn’t even see the secret, if it goes into the prompt (by stripping it beforehand)

11 Upvotes

20 comments sorted by

19

u/ericbythebay 22d ago

Don’t have the secrets where the agent can get to them in the first place. If you are storing secrets in plaintext, you are doing it wrong.

Update your system prompts to never pass secrets to inference. Secrets should only be accessed through scripts/runtime injection and never written to logs, disk, or console.

2

u/SpannuthVerlenia22 20d ago

spot on, sanitization after the fact is just a bandaid for bad secret management.

6

u/sidusnare 22d ago

What are you doing where this is needed?

My Claude user has it's own ssh key, and I setnthe agent up before it runs, and it's own account my internal git host. I have it put in PRs.

Toasters don't need secrets.

5

u/LockedVibe 22d ago

Short answer: Claude Code's built-in hooks can't strip a secret out of a prompt before the model sees it. A UserPromptSubmit hook can block a prompt (exit 2, or decision: block), but it can't rewrite it. The docs also say a blocked prompt's text can still land in the local transcript and prompt history, so blocking doesn't keep the secret off disk either.

What does work is layering:

Block on submit. Have a UserPromptSubmit hook run secret regexes (AWS keys, GitHub tokens, private key headers, your internal token formats) and block on a match. Set suppressOriginalPrompt: true in hookSpecificOutput so the block message doesn't echo the key back. Then rotate the key anyway, because it's already in local files.

Redact tool traffic. In practice most leaks don't come from what people type. They come from the agent running env, reading a config file, or printing a token in command output. PreToolUse can deny or rewrite a tool's input (updatedInput), and PostToolUse can replace a tool's result (updatedToolOutput). The docs point there for redaction.

Keep secrets out of reach. Add Read deny rules such as Read(./.env) and Read(./secrets/**). Know the limit: they cover the built-in file tools and shell commands Claude Code recognises (cat, head, tail, redirects), but not a Python or Node script that opens the file itself. For that you need the sandbox, which enforces at the OS level.

Fix the source. Agree with the others here: the agent shouldn't inherit long-lived credentials from the developer's shell. Give it its own scoped credentials, and have scripts fetch secrets at runtime so values never get printed to stdout.

A gateway POC can still make sense if you want org-wide logging across tools, since hooks only cover Claude Code sessions.

8

u/Ginden 22d ago

That's a symptom treatment, how does AI access these secrets? They are lying unguarded on developer machine, right?

Secrets should be accessible only behind 2FA, and agent shouldn't be able to get them in first place.

2

u/Spare-Ant7119 21d ago

The solution to your problem is to not have the problem arise. Never put secrets in prompts.

2

u/samc621 19d ago

sanitizing prompts helps, but with claude code most leaks don't come from the prompt. they come from the agent reading .env, running env or printenv, or catting a config file, and the value landing in the transcript. i'd layer it:

  1. don't keep real secrets where the agent runs. inject them at runtime from a secrets manager or short-lived creds, and give the agent a .env with placeholders.
  2. block the reads: deny rules in settings.json for .env*, ~/.aws and ~/.ssh, plus a PreToolUse hook on Bash, since Read deny rules don't catch cat .env.
  3. scan transcripts afterwards (gitleaks or trufflehog over ~/.claude/projects) as a backstop. a sanitizing proxy is still a fine extra layer for what users paste in.

i work on agenttrail. guard is a free open-source hook pack that covers step 2: https://github.com/agenttrailhq/guard

1

u/Silent-Suspect1062 22d ago

Injection of secrets into prompt at agent gateway

1

u/Mundane-Presence-896 21d ago

Use a Docker container to run the agent in and don’t mount any secrets in it.

1

u/Immediate-Welder999 21d ago

this solves your problem and it's open source!

1

u/subrosa-john 20d ago

Short answer: you can, but treat prompt scrubbing as a backstop, not the main control. Once a secret is in an env var or a file the agent can read, the agent can pull it into context through a tool call, and anything that only scrubs the prompt never sees that path.

A few layers that work together with Claude Code specifically:

  1. Keep secrets out of the agent's environment. Launch it from a shell that doesn't have production tokens exported, and give it scoped, short-lived credentials (its own service account or deploy key) for only what it needs.
  2. Use permission deny rules in settings.json to block reads of .env files, key files and credential paths, and to block commands like printenv or reading ~/.aws/credentials.
  3. Use hooks. A UserPromptSubmit hook can run a secret scanner (gitleaks or trufflehog style patterns, or your own regex) on the prompt and block it before it's sent. A PreToolUse hook can check tool inputs the same way.
  4. If you want org-wide enforcement that doesn't depend on each developer's local config, put a gateway or egress proxy in front of the model API. That's the layer Prompt Security and similar tools cover, so the POC makes sense there.
  5. Rotate anything that has already shown up in a prompt or transcript, and check where local session transcripts are stored.

The hook gets you the "block it before it reaches the model" behavior you asked about, but the bigger win is making sure nothing sensitive is within reach to leak in the first place.

Disclosure: I'm the CEO of SubRosa, which does security and AI governance work, so I'm not neutral here.

1

u/Terrible_Rich1447 18d ago

totally agree with redacting the secrets before they hit the model but still what happens to everything that the agent can still access? ( because apparently now giving ai the same permissions as a developer was such a good idea) and even wiyh all the cyera-style data visibility work the hard is still making out what the agent can access

1

u/SpartanHydrazine117 1d ago edited 1d ago

The gateway/proxy approach in the other comments is the right call. Strip at the egress layer with regex plus entropy checks before anything hits inference, so the model literally never sees it. Capsule and Prompt Security both do this fine for the prompt path. For the broader problem of agents touching env tokens, we also looked at runtime tooling like Upwind and Sweet to catch when a workload actually accesses a secret it shouldn't, which covers the cases regex misses. Belt and suspenders basically.