Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

Canary tokens in agent sandboxes: tripwires that tell you when a run goes rogue

Most controls around agent code are preventive: isolation, network allowlists, scoped credentials. They limit what a compromised run can do. They do not tell you that a run was compromised. A prompt injection that tries

Most controls around agent code are preventive: isolation, network allowlists, scoped credentials. They limit what a compromised run can do. They do not tell you that a run was compromised. A prompt injection that tries to steal credentials and fails leaves you safe, and also unaware that someone tried.

Canary tokens fill that gap. A canary is a fake secret or address that nothing legitimate should ever use. When something uses it, you get an alert. In an agent sandbox, a canary that fires is strong evidence that the run is doing something nobody asked it to do.

This post uses the free Canarytokens service from Thinkst as the example, because its documentation is public and the tool is open source. The ideas carry over to any canary system, including one you build.

How canaries work

The service gives you something that looks real and is wired to an alert:

  • AWS API keys. The docs describe a set of AWS keys that an attacker will believe are the keys to your cloud; if they are used via the AWS API at any point, you are alerted. The docs note that these alerts pass through Amazon's logging first, which may add a delay of 2 to 30 minutes.
  • DNS names. A unique hostname; according to the docs, anyone attempting to resolve it triggers an alert.
  • HTTP URLs. A unique web address that alerts when fetched.

The key property: nothing in your normal workflow uses these, so the false positive rate can be very low if you place them well.

Where to put them in an agent run

Think about what an injected instruction would tell an agent to do, and put a canary there.

Fake cloud credentials in the usual places

Attackers, and injected prompts written by attackers, look for credentials where tools keep them: an ~/.aws/credentials file, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in the environment, a .env file in the project. Put AWS key canaries there in runs that have no real cloud credentials.

This one has a useful property: it fires wherever the keys are used, not only from inside the sandbox. If a run smuggles the keys out through any channel you missed, the alert comes when the attacker tries them.

A hostname in a tempting config file

Put a DNS canary in a place an exfiltration script might use or a curious agent might call: a config.yaml with an internal_api entry, or a commented-out "backup endpoint" in a README. If anything resolves it, you know the run tried to reach it.

This also tests your network controls. In a sandbox with no network, the lookup should never reach the internet, so a DNS canary should never fire. If it does, name resolution is leaking, which is exactly the gap our post on DNS exfiltration describes.

A URL in fetched content

If your agent reads web pages, issues or documents, a canary URL inside test content you control shows whether the agent follows links it should not. This is a way to test your own prompt-injection defenses: put an instruction to fetch the canary URL in a test document and see whether any run obeys it.

Make each canary point to a run

An alert that says "the AWS canary fired" is much less useful than one that says "the canary from run 8f2c fired". Two ways to get there:

  • One canary per run, or per day of runs. Record which token went into which run, next to your structured logs. Canarytokens lets you add a reminder note to each token; put the run ID or batch in it.
  • Encode the run in the DNS name. The DNS canary docs describe a way to carry a small amount of extra data in the hostname, base32-encoded with a marker, which shows up in the alert. A run ID fits.

Avoiding false alarms

Canaries in agent environments have a failure mode they do not have on a server: the agent might use them in good faith. A model that sees AWS credentials in its environment and a task that mentions S3 may try them. Then your alert fires and nothing malicious happened.

Reduce that by:

  • Never planting canaries the task could plausibly need. No AWS key canaries in a run whose task involves AWS. Use them in runs that should not touch cloud APIs at all.
  • Keeping them out of the prompt. Do not mention them in the system prompt or tool descriptions. They should exist only where something has to go looking.
  • Treating an alert as a prompt to look, not proof. When one fires, pull the run's transcript and commands. Did the model choose to use the key because of the task, or because of text it read? The answer tells you whether you have a prompt-injection problem, a task design problem, or both.

What canaries do not do

  • They do not prevent anything. They tell you after the fact. Keep your isolation, network limits and credential scoping; canaries sit on top.
  • They depend on a third party for alerts, unless you run your own. With a hosted service, the alert path and its delays are out of your hands, as the AWS keys delay shows.
  • They only cover what you planted. An attacker after something else will not trip them.

Where Sandscale fits

In a Sandscale sandbox you can place canaries the same way as anywhere else: write a fake credentials file with writeFile, or pass fake keys as plain env values when you create the sandbox, and use labels to tie the sandbox to the canary you planted. Keep real secrets in Vault and fake ones in plain configuration, so it is always clear which is which. Separately, on every plan, DLP checks prompts and MCP tool calls for credentials before they leave, which can stop a real secret from reaching a model; canaries tell you when something went looking for one. A canary key that looks like a real AWS key may itself be stopped by DLP if it ends up in a prompt, and that block is worth investigating like any other alert.

Originally published on the Sandscale blog.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.