procurement-ingest/lambdas/wo
seahaven-openswe[bot] e86ea970a8
Some checks are pending
Deploy / deploy (push) Waiting to run
fix: add fail-closed validation gate and XML-delimited prompt on ai_fallback path (#104)
* fix: add fail-closed validation gate and XML-delimited prompt on ai_fallback path

The ai_fallback parse path applied no validation gate to raw Bedrock/LLM
output before DynamoDB writes, and the extraction prompt concatenated the
untrusted email body directly with no instructions-vs-data delimiter. A
DKIM-passing attacker could prompt-inject arbitrary field values into the
work-order store.

Changes:
- wrap untrusted email in \<email\> XML block with prompt instructing the
  model to treat its contents as data only
- add validate_ai_fallback() in template_parser that enforces the same
  contract keys, enums, and patterns as the template path before any write
- call validate_ai_fallback() in handler() dispatch; emit an
  ai_fallback_rejected EMF metric on failure and skip the record
- add 17 unit tests covering every gate rule and two end-to-end dispatch
  tests (injected email_type, injected status)

Refs #101

* style: apply ruff formatting to fix CI check

* harden ai_fallback gate: review fixes + security-review findings

Review follow-up on the ai_fallback validation gate (PR #104), plus
findings from a fan-out /sh-security-review of the change surface.

Reviewer FIX items:
- Neutralize forged <email> delimiters in the untrusted body before
  wrapping, so an in-body </email> cannot escape the data block.
- Fail closed on non-dict model output instead of crashing the handler
  into async retries; count ai_fallback_rejected parses in the
  fallback-rate alarm and add a dedicated rejected-parse alarm so a
  gate-rejection drift outage is not silent.
- Return a distinct invalid_status reason (was malformed_site_code);
  validate ISO-8601 dates; README + docstring updates.

Security-review findings (detector fan-out + proof-or-kill verifier):
- ReDoS (confirmed, medium): the tag neutralizer used two \s* around an
  optional /, backtracking quadratically on "<" + a long whitespace run
  (~32s at 100k chars -- one email could time out the Lambda). Collapse
  to a single [\s/]* class: linear, same defanging.
- Unhashable-type crash (confirmed): a JSON list/dict for email_type or
  status made `x in <set>` raise TypeError, escaping the gate into
  retries. Guard with isinstance(str) before membership.
- Unicode/newline regex (confirmed): _WO_ID_RE/_SITE_CODE_RE used ^..$
  with \d, admitting fullwidth digits ("12345" as a lookalike
  partition key) and trailing newlines. Switch to \A[0-9]+\Z (and the
  handler's inline recheck to [0-9]) so neither passes.
- Alarm comment (confirmed, low): corrected the "slow trickle still
  pages" wording -- rejections >~25-30 min apart page on neither alarm,
  the same knowingly-accepted residual as sender-auth-rejected.

Refuted: residual free-text prompt injection is inherent to trusting
allowlisted senders, not a new primitive; no DynamoDB key-poisoning
bypass survives both gates ('#' can never enter work_order_id).

7 new regression tests. All 260 tests pass; ruff clean; cdk synth OK.

---------

Co-authored-by: amoussa1229 <166072409+amoussa1229@users.noreply.github.com>
Co-authored-by: Adam Moussa <adam@seahavenind.com>
2026-07-16 16:23:28 -04:00
..
email_processor fix: add fail-closed validation gate and XML-delimited prompt on ai_fallback path (#104) 2026-07-16 16:23:28 -04:00
web_ui Land safe fixes from 2026-06-17 security sweep (#97) 2026-07-15 20:17:46 -04:00