Test Your Prompt Path: Canaries and a Secret Gate

Leader ●1 ●3 ●7
calendar_today ago • schedule3 min read

Dragos seeded about two million prompts with unique canary credentials and watched who used them. Three hosting providers were breached. Some stolen requests were marked Zero Data Retention. Separately, Together AI disclosed that a coding agent committed a Hugging Face credential to a public repo, which exposed stored prompt samples.

Two controls would have helped. Here they are in code. Both run as written.

1. Canary seeder and listener

Each canary is a unique URL on a host you control. The ledger records which route carried it. A hit means something read that prompt body, and the lag tells you how long after sending.

"""Seed unique canaries into prompts and log every callback.

Run the listener on a host you control, then send each canary prompt through
one route (provider, router, model, privacy setting). A hit on a canary URL
means something read that prompt body.
"""
import json
import secrets
import sys
import time
from http.server import BaseHTTPRequestHandler, HTTPServer

LEDGER = "canary_ledger.jsonl"


def make_canary(base_url, route):
    """Return a prompt snippet and record which route it was sent through."""
    token = secrets.token_urlsafe(16)
    entry = {"token": token, "route": route, "sent_at": time.time()}
    with open(LEDGER, "a") as f:
        f.write(json.dumps(entry) + "\n")
    return f"Reference doc: {base_url}/c/{token}"


def load_ledger():
    with open(LEDGER) as f:
        return {e["token"]: e for e in map(json.loads, f)}


class Listener(BaseHTTPRequestHandler):
    def do_GET(self):
        token = self.path.rsplit("/", 1)[-1]
        sent = load_ledger().get(token)
        if sent:
            lag = time.time() - sent["sent_at"]
            print(f"HIT route={sent['route']} lag={lag:.0f}s "
                  f"ua={self.headers.get('User-Agent')} ip={self.client_address[0]}")
        self.send_response(200)
        self.end_headers()

    def log_message(self, *a):
        pass


if __name__ == "__main__":
    if sys.argv[1:] == ["serve"]:
        HTTPServer(("0.0.0.0", 8080), Listener).serve_forever()
    else:
        route = {"provider": "example", "router": "direct",
                 "model": "m1", "zdr": True}
        print(make_canary("https://canary.example.com", route))

Run python canary.py serve on your host. Send each generated line through one route only: one provider, one router, one model, one privacy setting. Never reuse a token across routes.

A hit looks like this:

HIT route={'provider': 'x', 'zdr': True} lag=0s ua=Python-urllib/3.13 ip=127.0.0.1

A hit on a route marked ZDR is the finding. Dragos saw hits from fetches days and weeks after the prompt was sent, so keep the listener running.

2. Prompt gate

The Together incident started with a credential in an agent's reach. Refuse to send a prompt that carries one.

"""Pre flight gate: refuse to send a prompt that carries a secret."""
import re

PATTERNS = {
    "aws_access_key": re.compile(r"\b(AKIA|ASIA)[0-9A-Z]{16}\b"),
    "github_token": re.compile(r"\bgh[pousr]_[A-Za-z0-9]{36,}\b"),
    "hf_token": re.compile(r"\bhf_[A-Za-z0-9]{30,}\b"),
    "private_key": re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----"),
    "bearer": re.compile(r"(?i)\bbearer\s+[A-Za-z0-9._\-]{20,}"),
}


class SecretInPrompt(Exception):
    pass


def gate(prompt):
    hits = [name for name, rx in PATTERNS.items() if rx.search(prompt)]
    if hits:
        raise SecretInPrompt(f"blocked: {', '.join(hits)}")
    return prompt
import pytest
from gate import gate, SecretInPrompt

def test_clean_passes():
    assert gate("summarize this ticket") == "summarize this ticket"

@pytest.mark.parametrize("bad", [
    "key AKIAABCDEFGHIJKLMNOP here",
    "token hf_" + "a" * 34,
    "ghp_" + "b" * 36,
    "-----BEGIN RSA PRIVATE KEY-----",
    "Authorization: Bearer " + "c" * 30,
])
def test_secret_blocked(bad):
    with pytest.raises(SecretInPrompt):
        gate(bad)
$ pytest -q test_gate.py
......                                                                   [100%]
6 passed

Pattern matching catches known formats only. Provider side secret filtering has the same limit, which Dragos notes. The stronger control is structural: the agent holds a reference to a value and a gate resolves it after the model finishes. That design is in the closed value handoff contract post: https://coderlegion.com/35393/closed-values-hand-off-contracts-for-untrusted-agents

Sources

Repos: https://github.com/AndMaverick

Want more #ai & #devops deep dives?

Join 5,986+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 2 of 2 in Agents

1 Comment

0 votes

More Posts

A Textile Mill's Dyehouse Ramp Curve: Get One Step Wrong and the Sheet Belongs to the Master

aiomniu - Oct 18

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

MCP Is the USB-C of AI. So Why Are You Plugging Everything In?

Ken W. Algerverified - Jun 10

Your AI Doesn't Just Write Tests. It Runs Them Too.

Kevin Martinez - May 12

Dozens of email accounts: learning AI coding tools as a student

rahuldk1105 - Oct 27
chevron_left
956 Points • 11 Badges
4Posts
6Comments
2Connections
I was a product architect at OpenAI from 2016, then built the onboarding and documentation systems b... Show more

Related Jobs

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!