Skip to content

fix(bridge,shell): harden v1.0 MCP filtering, cross-frame validation, and prompt boundaries - #340

Open
jgindin wants to merge 6 commits into
mainfrom
fix/security-and-prompt-hardening
Open

jgindin wants to merge 6 commits into
mainfrom
fix/security-and-prompt-hardening

Conversation

@jgindin

@jgindin jgindin commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Hardens untrusted boundary handling across the v1.0 MCP bridge, cross-frame message validation, payload healing/normalization, and system prompt construction:

  1. Bridge v1.0 MCP Response Filtering (bba63d74):

    • Export hasPrototypePollution(val, maxDepth = 64) in bridge/src/mcp/dynamic-value-strategy.ts reusing isPrototypePollutionKey and checking Object.getPrototypeOf, Object.getOwnPropertyNames, nested objects/arrays, and recursion depth.
    • Wrap rawProcessor in createMcpFunctionsCatalog (bridge/src/mcp/v1-0-mcp-catalog.ts) with filterAllowedV10McpMessages / isAllowedV10McpMessage so untrusted MCP tool and resource responses are restricted to single-operation v1.0 surface-update messages (createSurface, updateComponents, updateDataModel, deleteSurface) free of prototype pollution and extra top-level keys, blocking injected RPC control messages (callRendererFunction, agentFunctionResponse, callAgentFunctionResponse) while keeping v0.9 and v1.0 paths separate.
  2. Shell Cross-Frame & Host Communication Validation (3ffce3a6):

    • Add a hasPrototypePollution(item) guard at the entry of V09WireValidator.validateRenderItem (shell/src/app/shell/cross-frame-validator/v0-9-wire-validator.ts), matching V10WireValidator.validateRenderItem.
    • Return early in HostCommunication.messageListener (shell/src/app/shell/host-communication/host-communication.ts) when an incoming SEND_TO_SERVER payload fails CrossFrameValidator.validateIncomingMessage so invalid envelopes never reach recordEnvelope, messageStream$, latestEnvelope(), or getEnvelopeHistory().
  3. Prompt Code-Fence & Delimiter Hardening (23ef0b0e):

    • Escape backticks as \u0060 across single- and multi-catalog JSON serialization (serializeCatalogForPrompt and serializeCatalogsForPrompt) to prevent Markdown code-fence breakouts.
    • Wrap MCP tool metadata in <mcp_tools>...</mcp_tools> with single-line sanitization, backtick escaping, case-preserving closing-tag escaping (HTML and JSON modes, covering trailing attributes and self-closing slashes), and a reinforcing data-only postamble.
    • Wrap user-supplied customInstructions in <custom_instructions>...</custom_instructions> with closing-tag escaping and a precedence postamble across both V09PromptStrategy and V10PromptStrategy.
    • Add unit and E2E adversarial breakout coverage.
  4. Bridge resolveDynamicTree Fail-Closed Depth Limit & Unchanged-Branch Sanitization (5ffcacac):

    • Fail closed in resolveDynamicTree (bridge/src/mcp/dynamic-value-strategy.ts) when depth >= MAX_RESOLUTION_DEPTH, returning [] for arrays and {} for non-null objects instead of returning unstripped deep structures verbatim.
    • On the unchanged-expression short-circuit (exprResult.unchanged || exprResult.value === value), recurse into resolveDynamicTree with NO_OP_RESOLVE_EXPRESSION so passthrough args and extra v0.9 own properties have prototype-pollution keys stripped and MAX_RESOLUTION_DEPTH enforced without re-evaluating expressions.
    • Add depth-limit and unchanged-passthrough sanitization unit tests in bridge/src/mcp/v0-9-dynamic-value-strategy.spec.ts and bridge/src/mcp/v1-0-dynamic-value-strategy.spec.ts.
  5. Shell component-name-healer Prototype-Lookup Guard & a2ui-rpc-envelope Closure-Safe Deletion (d29d24fd):

    • Back SYNONYM_MAP and buildComponentHealMap with null-prototype objects (Object.create(null)) and guard all lookups on componentsObj, healMap, and SYNONYM_MAP in healComponentInstance (shell/src/app/chat/a2ui-payload-parser/component-name-healer.ts) with Object.hasOwn, rejecting Object.prototype keys (toString, valueOf, hasOwnProperty, constructor, Constructor, __proto__) even on externally supplied plain objects.
    • Add shell/src/app/chat/a2ui-payload-parser/component-name-healer.spec.ts covering positive healing/synonym behavior and prototype-key rejection.
    • Extract takeWireProp in shell/src/app/shared/a2ui-rpc-envelope.ts to replace unquoted object rest-destructuring in normalizeV10RpcEnvelopeItem with shallow copies, bracket property reads, and delete so Closure property renaming cannot break normalization.

Verification

  • corepack yarn prettier:check — passed
  • corepack yarn tsc — passed
  • corepack yarn lint — passed
  • corepack yarn build — passed
  • corepack yarn test — passed (root scripts/*.test.mjs: 48 tests; a2ui-bridge: 18 files / 353 tests; lit-basic-catalog: 3 files / 26 tests; ng-basic-catalog: 4 files / 34 tests; react-basic-catalog: 4 files / 26 tests; react-slack-catalog: 14 files / 197 tests; a2ui-composer-shell: 123 files / 3048 tests, 94.25% stmts / 87.99% branches / 95.2% funcs / 94.96% lines)
  • E2E_HEADLESS=true corepack yarn --cwd shell playwright test --project=e2e e2e/cross-frame-security.e2e.ts e2e/user-journey.e2e.ts — 13 passed (pre-helper-extraction on 7e211ce0)

Export `hasPrototypePollution` in `dynamic-value-strategy.ts` and wrap
the v1.0 MCP catalog processor with `filterAllowedV10McpMessages` so
only valid, unpolluted v1.0 surface update messages reach
`processMessages`.
Add a `hasPrototypePollution` guard at the entry of
`V09WireValidator.validateRenderItem` to match the v1.0 wire validator.

Return early in `HostCommunication.messageListener` when an incoming
`SEND_TO_SERVER` payload fails validation so invalid envelopes are
never emitted on `messageStream$` or stored in `latestEnvelope` /
`messageHistoryBuffer`.
Escape backticks as `\u0060` across single- and multi-catalog prompt
serialization to prevent Markdown code-fence breakouts.

Wrap MCP tool metadata in `<mcp_tools>...</mcp_tools>` and user custom
instructions in `<custom_instructions>...</custom_instructions>` with
closing-tag escaping, single-line tool metadata sanitization, and
reinforcing data/precedence postambles.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces robust prototype pollution checks and prompt injection hardening across the codebase. Specifically, it adds a hasPrototypePollution utility to inspect nested structures, filters incoming MCP tool response messages to drop disallowed or polluted payloads, wraps custom user instructions and MCP tools in XML-style delimiters with closing-tag escaping, and escapes backticks in serialized catalogs to prevent markdown code-fence breakouts. The reviewer pointed out a security improvement in hasPrototypePollution where the prototype of arrays should also be validated to prevent prototype pollution via array subclasses.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces robust security hardening against prototype pollution and prompt injection. It adds a hasPrototypePollution utility to inspect nested structures, integrates message filtering in the MCP catalog to drop disallowed or polluted messages, and escapes backticks and XML-style tags in prompt strategies to prevent code-fence breakout. Additionally, it validates incoming host communication messages for prototype pollution. The review feedback correctly identifies two important issues: a potential security bypass in hasPrototypePollution where recursion uses Object.keys instead of Object.getOwnPropertyNames (missing non-enumerable properties), and a bug in isAllowedV10McpMessage that incorrectly rejects valid messages containing explicit undefined values.

Comment thread bridge/src/mcp/v1-0-mcp-catalog.ts Resolved
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

⚡ A2UI Composer PR Preview

Your automated preview is successfully live (commit 8d4b82f):
👉 Launch PR Preview

Note: This environment will be wiped automatically when the PR is merged or closed.

Fail closed in `resolveDynamicTree` when `depth >= MAX_RESOLUTION_DEPTH`
by returning `{}` for objects and `[]` for arrays instead of the raw
unstripped input.

On the unchanged-result short-circuit, recurse into `resolveDynamicTree`
with `NO_OP_RESOLVE_EXPRESSION` so passthrough `args` and extra v0.9 own
properties are sanitized without re-evaluating expressions.
Back `SYNONYM_MAP` and `buildComponentHealMap` with null-prototype
objects and guard all `componentsObj`, `healMap`, and `SYNONYM_MAP`
lookups in `healComponentInstance` with `Object.hasOwn` so
`Object.prototype` keys (`toString`, `valueOf`, `hasOwnProperty`,
`constructor`, `__proto__`) are rejected.

Extract `takeWireProp` to replace unquoted object rest-destructuring in
`normalizeV10RpcEnvelopeItem` with shallow copies, bracket access, and
`delete` so property renaming cannot break normalization.
Validate array prototypes against Array.prototype or null and iterate
Object.getOwnPropertyNames(val) for both arrays and objects in
hasPrototypePollution so array subclasses, own pollution keys on
arrays, and pollution nested inside non-enumerable properties are
rejected.

Ignore top-level keys with undefined values in isAllowedV10McpMessage
so valid v1.0 messages with explicit undefined sibling update keys are
accepted while non-undefined disallowed keys continue to be dropped.

This branch has not been deployed

No deployments
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