Skip to content

feat(security): add opt-in context-leak canary tokens - #64

Merged
ALikesToCode merged 1 commit into
integrate/gateway-waves-1-5from
feat/context-canary
Oct 9, 2026
Merged

ALikesToCode merged 1 commit into
integrate/gateway-waves-1-5from
feat/context-canary

Conversation

@ALikesToCode

Copy link
Copy Markdown
Owner

Summary

Adds opt-in context-leak canary tokens. An opted-in managed request gets a request-specific marker inside a clearly labelled gateway system annotation. The gateway scans the provider's response for that marker and never lets it reach the client. With CONTEXT_CANARY_MODE unset or off (the default), or with no matching opt-in, prompts, responses, traces and logs are byte-for-byte unchanged, and no canary state is created.

  • Settings:
    • CONTEXT_CANARY_MODE is off, log or block. Empty means off; a malformed value is off with one warning that leaves the value out.
    • CONTEXT_CANARY_POLICY_JSON (default {}) opts in exact routes and authenticated keys. It is validated strictly, and setting the mode alone opts nothing in.
  • Marker:
    • 32 hex characters, HMAC-SHA256 derived from a fresh random 32-byte secret per request. There is no persistent secret or remote classifier.
    • It goes in a system message for chat, the system field for Messages, or instructions for Responses. Existing instructions stay.
    • A marker from another request isn't recognised. Raw provider passthrough never gets or scans markers.
  • Scanning:
    • JSON strings are decoded first, including Unicode escapes.
    • SSE data is reassembled across reads and UTF-8 boundaries, with separate lanes for content, reasoning, text and tool arguments. Each lane keeps at most a 31-character carry.
    • Frames are capped at 64 KiB and non-streaming bodies at 16 MiB. Malformed or oversized opted-in responses fail with context_canary_scan_failed.
  • On detection:
    • log removes the marker and keeps delivering.
    • block returns 502 context_canary_leak before the response is committed. After streaming has begun, it sends a real SSE error event, without a fabricated completion or [DONE].
    • Either way the upstream is cancelled and the cost hold stays ambiguous, never settled as zero. Detection never grants a retry or replay.
  • Logs and traces record only the marker digest, the rule and the trace ID. They never record the marker, the annotation or content, and they respect zero-content retention.
  • Not proof of safety: text sent before detection can't be withdrawn. An absent marker doesn't prove that no context leaked; encoded, altered or partial markers and unrelated context aren't detected.

Details are in docs/context-canary.md.

Wiring

  • Roleplay: wired in worker/roleplay/endpoint.mjs through small named calls into worker/context-canary.mjs, each of which returns its input unchanged while off.
    • The marker is added to dispatch payloads only, never to retained conversation or recovery inputs.
    • Initial and continuation responses are scanned before completion is persisted.
    • Its input tokens are counted in the context budget.
  • Trace journal: worker/roleplay/turn-trace.mjs gains a canary event that accepts only a valid digest, the rule and the trace ID.
  • Managed paths: services/context_canary.py (prepare_request, finalize_response) and the Worker prepareRequest/finalizeResponse are connected to the Flask managed pipeline and native Worker routes with the Wave 6 integration.

Shared registrations

  • tests/test_context_canary_worker.mjs in test:worker.
  • worker/context-canary.mjs in the edge module inventory.
  • CONTEXT_CANARY_MODE and CONTEXT_CANARY_POLICY_JSON in the Container env keys.
  • tests/helpers/load_cloudflare_worker.mjs maps the endpoint's ../context-canary.mjs import.

No migration, D1 tables, routes or browser-readable headers.

Tests

  • tests/test_context_canary.py and tests/test_context_canary_worker.mjs cover:
    • off, empty, malformed and not-opted-in traffic left unchanged, with no state or logs;
    • strict policy validation;
    • marker placement for chat, Messages and Responses, and no markers for passthrough;
    • JSON and SSE detection across chunk and UTF-8 boundaries, escapes and tool arguments;
    • log removal, block before commit (502) and after commit (SSE error), upstream cancellation and ambiguous accounting;
    • scan bounds and context_canary_scan_failed;
    • content-free trace events under zero retention;
    • the roleplay path with the marker excluded from stored conversation.
  • The roleplay and PII Worker suites (25 files, 323 tests) pass with the new call sites.
  • Full Python suite: 4,485 passed, 2 skipped. ruff, mypy and bandit are clean.
  • npm run test:worker: 1,656 passed. npm run test:integration: 7 passed.
  • migrations:check (28 contiguous, additive migrations), test:deploy, free routes, client headers and the wrangler deploy --dry-run bundle all pass.
  • tests/test_frontend_build.mjs fails locally because @tailwindcss/cli is not installed. It fails the same way on main.

CI notes

  • GitHub python/node jobs: the dependency-audit steps fail on every branch, including main. fix(deps): clear the CI dependency audit advisories #42 fixes that.
  • Workers Builds: fails on this stack because the Wave 3 base adds the AdmissionCoordinator Durable Object migration, which wrangler versions upload cannot apply. This is expected until wrangler deploy runs on main.

This is part of Wave 6. It is based on integrate/gateway-waves-1-5, the Wave 5 integration branch (#60). The README line comes with the Wave 6 integration PR.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Oct 9, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4f80756d-a661-4381-9878-41f73c32f2a2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
multillm-proxy 4af6c28 Oct 09 2026, 05:19 AM

@ALikesToCode
ALikesToCode merged commit 12f83c9 into integrate/gateway-waves-1-5 Oct 9, 2026
4 of 7 checks passed
@ALikesToCode
ALikesToCode deleted the feat/context-canary branch October 26, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant