Skip to content

feat(pii): add opt-in PII redaction with streaming rehydration - #58

Merged
ALikesToCode merged 1 commit into
integrate/gateway-waves-1-4from
feat/pii-rehydration
Oct 9, 2026
Merged

ALikesToCode merged 1 commit into
integrate/gateway-waves-1-4from
feat/pii-rehydration

Conversation

@ALikesToCode

Copy link
Copy Markdown
Owner

Summary

Adds opt-in PII redaction with streaming rehydration. With PII_REDACTION_ENABLED off and PII_REDACTION_POLICY_JSON={} (the defaults), and for any route or key scope that isn't opted in, requests and responses are unchanged.

  • Opt-in:
    • The policy names exact routes and/or authenticated key scopes, the detectors to run (email, phone, card) and a mode (required or best_effort).
    • Unknown fields or invalid values turn the feature off with one warning per field, without logging the value.
  • Routes: managed /v1/chat/completions, /v1/messages, /v1/responses and /intelligence/v1/chat/completions in Flask, plus /v1/roleplay/chat/completions in the Worker. Raw provider passthrough is never transformed.
  • Detection:
    • Detectors walk JSON string values and keep keys and non-string values.
    • Emails use bounded ASCII parts.
    • Phones need 10–15 digits with formatting; bare digit runs don't count.
    • Cards are 13–19 digits that pass Luhn.
    • There is no remote or semantic classifier.
  • Order: the credential secret firewall runs first. Its findings, including values in secret-named fields, never enter the reversible map.
  • Placeholders:
    • __MLPII_<64 hex>__: HMAC-SHA-256 of the value under a random 32-byte secret made for that request.
    • Only exact tokens issued in the current request are restored. Unknown or altered tokens pass through unchanged.
    • The map stays in memory and is cleared on completion, error or disconnect. It is never logged, cached, stored or reused.
  • Limits: 512 distinct values, 1 MiB of input, JSON depth 32 and 32,768 nodes per request.
  • Responses:
    • JSON bodies (up to 1 MiB) and SSE text are restored.
    • Streams handle arbitrary chunk boundaries, UTF-8 splits, JSON escapes and placeholders split across deltas. At most 128 bytes are held across 16 text lanes.
  • Failures:
    • In required mode (the default), a redaction failure returns 422 pii_redaction_failed before dispatch.
    • In best_effort mode, the unchanged, firewall-checked payload is sent instead.
    • A non-stream response that can't be restored returns 502 pii_rehydration_failed.
    • A stream that has already started ends with an error. The gateway never invents a completion.
  • Caches and replay:
    • Redacted Flask requests switch to zero-content retention for that request, so the exact and shared caches are bypassed.
    • A redacted request with Idempotency-Key returns 400 pii_idempotency_unsupported before dispatch. Requests with no PII keep their existing behavior.
    • Redacted roleplay requests drop Idempotency-Key upstream, send Cache-Control: no-store, and report cacheable: false, replayable: false.

Details are in docs/pii-rehydration.md.

Shared registrations

  • tests/test_pii_rehydration_worker.mjs in test:worker, plus test loader mappings for the two new roleplay transport imports.
  • worker/pii-redaction.mjs and worker/pii-rehydration.mjs in the edge module inventory.
  • The firewall inventory now counts 4 dispatches in worker/roleplay/transport.mjs, up from 3. The new one is the Container fetch in the PII submit callback, which runs inside firewallFetch.
  • PII_REDACTION_ENABLED and PII_REDACTION_POLICY_JSON in the Container env keys.
  • No routes, tables or browser-readable headers.

Left for the Wave 5 integration

  • services/gateway_extensions.py: register register_pii_redaction after retention and deadline, before the idempotency claim. Until then, the Flask hook isn't active.

  • worker/roleplay/endpoint.mjs:

    • pass the authenticated piiKeyScope;
    • honor onPIIDecision, so redacted turns aren't cached or replayed;
    • keep no-store on the final response.

    Until then, roleplay redaction applies only through route opt-in.

  • README line.

Tests

  • tests/test_pii_rehydration.py and tests/test_pii_rehydration_worker.mjs cover:
    • defaults, invalid policies and unknown fields;
    • detectors, Luhn and false positives;
    • firewall-first ordering;
    • secret fields never entering the map;
    • per-request secrets;
    • unknown tokens and JSON escaping of restored values;
    • every byte split of a placeholder, UTF-8 splits and JSON escapes across deltas;
    • bounds;
    • required vs best-effort failures, including HMAC collisions;
    • cache bypass and the idempotency rejection through the registered routes;
    • map cleanup on disconnect and abort;
    • native roleplay redaction after the firewall.
  • Full Python suite: 3,984 passed, 2 skipped. ruff, mypy and bandit are clean.
  • npm run test:worker: 1,504 passed. npm run test:integration: 7 passed.
  • 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 5. It is based on integrate/gateway-waves-1-4, the Wave 4 integration branch. The README line comes with the Wave 5 integration PR.

Opted-in routes and key scopes replace detected emails, phones and cards with request-local HMAC placeholders before dispatch and restore them in JSON and SSE responses. Maps live only in request memory, redacted requests bypass caches and idempotency replay, and both settings default to off.
@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: c7159c15-8fe9-497d-a1df-e3e7d8a65a33

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
  • Autopilot · 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 84d76ff Oct 09 2026, 02:49 AM

@ALikesToCode
ALikesToCode merged commit 7198a88 into integrate/gateway-waves-1-4 Oct 9, 2026
4 of 7 checks passed
@ALikesToCode
ALikesToCode deleted the feat/pii-rehydration branch October 26, 2026 20:16
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