Skip to content

Security hardening: PII filter expansion and attack chain remediation#2

Open
borng wants to merge 2 commits intomainfrom
feature/security
Open

Security hardening: PII filter expansion and attack chain remediation#2
borng wants to merge 2 commits intomainfrom
feature/security

Conversation

@borng
Copy link
Copy Markdown
Collaborator

@borng borng commented Apr 4, 2026

Summary

  • Expanded PII filter with structured formats (IBAN, PEM, JWT, credential URLs) and keyword-gated patterns (EIN, DOB, driver license, passport)
  • Reordered pattern list to reduce false positives
  • Comprehensive test coverage for all new patterns
  • Builds on prior security hardening (injection blocking, draft filtering, HKDF key derivation, encrypted OAuth tokens)

Test plan

  • Verify all existing PII tests still pass
  • Confirm new patterns redact correctly (IBAN, JWT, PEM keys, credential URLs, EIN, DOB, DL, passport)
  • Validate pattern ordering does not cause regressions on credit card / SSN detection

…harden all layers

Comprehensive security remediation based on 83-finding audit (11 critical, 22 high,
24 medium, 14 low, 12 info). Closes the critical attack chain where a crafted email
could bypass injection filters, expose unredacted PII, and exfiltrate data via drafts.

Phase 1 — Break the attack chain:
- Injection filter now blocks (was: warn only)
- create_draft filtered through pipeline (recipient domain blocking, CRLF strip)
- list_threads PII-redacts snippet/subject via pipeline.redactText()
- PII/injection filters enforced as always-on (server-side floors in api.ts)
- MCP endpoint body size limited to 1MB
- .env.bak deleted, .gitignore hardened with *.bak, *.pem, *.key, etc.

Phase 2 — Harden auth, crypto, and infrastructure:
- OAuth account takeover prevention (password-based accounts reject OAuth login)
- HKDF key derivation separates session/credential/OAuth keys from master secret
- Per-credential random salt for IMAP/GAS encryption (legacy fallback preserved)
- OAuth tokens encrypted at rest (EncryptedOAuthTokens type)
- Rate limiter: per-email + per-IP dual-key limiting, MCP endpoint rate limiting
- Session verified on OAuth connect callback
- Logout changed to POST with Origin header CSRF protection
- IMAP SSRF prevention via DNS resolution + comprehensive IP validation
- .dockerignore created, Dockerfile hardened (non-root user, healthcheck)
- docker-compose hardened (localhost bind, no-new-privileges, read-only)
- Security headers added (X-Content-Type-Options, X-Frame-Options, HSTS, etc.)
- Wildcard CORS removed, scoped to configured origin

Phase 3 — Deepen defenses:
- PII patterns expanded: phone numbers, Mastercard 2-series, addresses, dollar amounts
- Unicode NFKC normalization in sanitizer (collapses homoglyphs)
- PII redaction applied to from/to/cc name fields
- ReDoS protection for user-supplied regex patterns
- Sessions invalidated on password change
- Login error messages whitelisted (no URL param reflection)
- CI/CD security scanning (pnpm audit, TruffleHog)
- Structured audit logging for security events

Build: all 4 packages clean. Tests: 55/55 pass.

Remaining manual actions:
- Rotate GOOGLE_CLIENT_SECRET and SESSION_SECRET in production
- pnpm update hono (4.11.9 -> 4.12.x for timing-safe comparison fix)
- pnpm add safe-regex2 --filter @agentcloak/server

Created by gregb and his home-grown crew of builders 🦜 🤖
Tests cover the full security hardening surface: filter pipeline integration
(chain ordering, redactText, folder/attachment filtering), MCP tool security
(create_draft recipient domain blocking, CRLF injection prevention,
list_threads PII redaction and thread blocking), HKDF key derivation
(context separation, determinism), IMAP credential encryption (per-credential
salt, legacy fallback, tamper detection), and expanded PII patterns (phone,
MC 2-series, SSN variants, address name fields).

103 tests passing across 9 test files.

Created by gregb and his home-grown crew of builders 🦜 🤖
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