Skip to content

feat(catalogs): cap MCP app CSP domains and Vega chart URLs by the URL security policy - #4117

Draft
nan-yu wants to merge 9 commits into
feat/url-security-web-corefrom
feat/url-security-mcp-vega
Draft

nan-yu wants to merge 9 commits into
feat/url-security-web-corefrom
feat/url-security-mcp-vega

Conversation

@nan-yu

@nan-yu nan-yu commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Part of #4045. Stacked on #4104 (base feat/url-security-web-core).

This PR puts the two interactive catalog packages through the URL security policy from #4104:

  1. McpApp caps the _meta.ui.csp domain allowlists before sending them to the sandbox proxy.
  2. McpAppBridge checks ui/open-link URLs as navigation before opening them, reporting blocked requests with {functionName: 'ui/open-link'} and without logging raw malformed URLs.
  3. VegaChart checks every URL path in a spec: data/image/href via a Vega loader, tooltip image fields via a tooltip formatter wrapper, stripped usermeta.embedOptions, disabled globalCursor, an allowlist for parameter bindings (known input types and keys only, applied to the spec and again to the compiled Vega spec through patch), and a DOM MutationObserver scrubbing CSS url(...) / image-set(...), form image/action attributes, and on* handler attributes on form elements while preserving same-document #id paint references. This covers both v0.9 and v1.0 entry points, which share the element.
  4. @a2ui/sandbox documents on SandboxResource.url that hosts using url sandbox mode must mark the property with iframeUrl() or call context.sanitizeUrl(url, 'iframe').

The TypeScript core API is unchanged.

Reviewer focus

  1. How a CSP source maps to the URLs the policy checks (mcp_app_csp.ts). The policy API takes URLs, not source expressions, so each source becomes one or more probe URLs:
    • https:// and wss:// sources check https; http://, ws://, and scheme-less hosts check both http and https, because CSP allows scheme upgrades and scheme-less hosts follow the frame origin.
    • Wildcards stay in the host: *.example.com checks http://*.example.com/ and https://*.example.com/. The URL parser accepts * as a host label, and AllowlistUrlSecurityPolicy compares it literally, so a wildcard source passes only a wildcard entry that covers it (*.example.com, *.com), never an exact entry such as cdn.example.com. *, http:, and ws: check http://*/ and https://*/; https: and wss: check https://*/.
    • A port wildcard (:*) is checked with the default port and port 1 per scheme, so it passes only when the policy allows any port.
    • data: and blob: load nothing from a server and are kept, as the allowlist policy does for host-less URLs.
    • A source the policy rewrites to another scheme or host (a proxy) is dropped: the app would still connect to the original host directly.
    • Anything else ('self', other schemes) is dropped; the sandbox already dropped quoted keywords.
  2. VegaChart loader, tooltip, binding, and DOM CSS coverage (url_policy_loader.ts, url_policy_tooltip.ts, url_policy_dom_css.ts, vegaChart.ts):
    • createUrlPolicyLoader checks the input URL and re-checks any URL resolved by the base loader (such as via baseURL), rejecting with a constant message that never leaks the URL.
    • createUrlPolicyTooltipFormatter wraps vega-tooltip's formatValue to check value.image as media (omitting blocked images, replacing rewritten ones).
    • transformSpec removes usermeta.embedOptions and disables globalCursor and external bind selectors (config.events / eventConfig). Parameter bindings go through an allowlist, because Vega's generic input generator copies every binding key onto the <input> as an attribute (so onfocus/autofocus would run script): 'scales', 'legend', legend bindings, input bindings with a known input type keeping only input, name, options, labels, min, max, step, debounce, placeholder, autocomplete, and Vega-Lite per-field maps of those. Anything else (including element targets and input: 'image') removes the binding. The walk covers params, signals, and the deprecated selection at any depth, including config, and skips data.
    • The same sanitizer runs on the compiled Vega spec through the patch embed option, which catches every binding form Vega-Lite compiles into signals / config.signals.
    • A superseded render only releases its own CSS guard, so a late rejection or resolution of an older embed can't disconnect the current chart's observers or leak them.
    • observeCssUrls watches wrapper (and document.body.style.cursor) via MutationObserver before embed(...) runs, resetting cursor with url( / image-set( and removing external or backslash-escaped url(...) values from SVG paint attributes and form src/formaction/type=image, while keeping same-document #id and <location>#... gradient and clip-path refs.

Changes

  1. @a2ui/catalog-mcp
    • New mcp_app_csp.ts: cspSourceToUrls, isCspSourceAllowed, capCspByUrlPolicy (package-internal).
    • McpApp.resolveResource caps csp before the resource goes to the sandbox proxy (connectDomains/resourceDomains as media, frameDomains as iframe, baseUriDomains as navigation). An element without a component context uses PermissiveUrlSecurityPolicy.
    • McpAppBridge takes sanitizeUrl (ui/open-link checked as navigation, {functionName: 'ui/open-link'} from McpApp, and malformed URL warnings omit the raw URL).
    • FrameTestHarness takes MessageProcessorOptions for tests.
    • Updated README and CHANGELOG.
  2. @a2ui/catalog-vegachart
    • New url_policy_loader.ts, url_policy_tooltip.ts, and url_policy_dom_css.ts; loader types exported from root src/index.ts.
    • Added vega-tooltip dependency (^0.35.2, matching vega-embed).
    • Updated A2uiVegaChartElement, transformSpec, VegaEmbedOptions, README, and CHANGELOG.
  3. @a2ui/sandbox
    • Documented URL policy requirement on SandboxResource.url.

Impact & risks

  1. Without a configured policy, McpApp keeps every CSP source it kept before except sources with schemes other than http(s), ws(s), data, and blob.
  2. VegaChart now applies the shared baseline to spec URLs even without a policy: protocol-relative URLs (//host/data.csv) and data: URLs that aren't images, audio, or video (such as data:text/csv,...) are blocked, usermeta.embedOptions and external CSS url(...) values in cursor/SVG paint attributes are stripped, and globalCursor/image/external bind inputs are disabled. Inline data.values is the replacement. Hosts should still keep an img-src CSP as defense in depth.
  3. A custom VegaEmbedFn that doesn't forward options.loader, options.tooltip, and options.patch to vega-embed bypasses those checks (the spec pre-pass still removes unsafe bindings). The README says so.
  4. Each blocked source or URL reports a redacted URL_BLOCKED warning on the surface, once per dedupe entry.
  5. Specs that bind parameters with other input types (input: 'image', submit, ...) or extra attributes lose those bindings.

Testing

  1. @a2ui/catalog-mcp: Karma suite (102/102) and Node suite (196/196) pass, covering mcp_app_csp.test.ts (scheme upgrades, scheme-less hosts, *, port wildcards, rewrites, unmappable sources), mcp_app.test.ts (capped CSP and redacted URL_BLOCKED warning with functionName: 'ui/open-link'), and mcp_app_bridge.test.ts (navigation checks, rewrites, non-http(s) skip, redacted malformed-URL warnings).
  2. @a2ui/catalog-vegachart: all 54 Node tests pass (new: binding allowlist, bindings in config/selection/nested views with inline data untouched, the patch option on a compiled Vega spec, on* attribute scrubbing, and both superseded-render races; each fails on the previous code) (url_policy_loader.test.ts, url_policy_dom_css.test.ts, vegaChart.test.ts with real vega-embed tests for loader, tooltip, usermeta.embedOptions, CSS cursor/fill scrubbing, globalCursor, bind, and preserved local clip-path/gradient refs, plus v0_9/v1).
  3. @a2ui/sandbox: yarn test and ESLint pass.
  4. tsc, ESLint (0 errors), and Prettier pass across all modified packages.

🤖 Agent-generated

@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 implements URL security policy enforcement for MCP applications and Vega charts. For MCP apps, it caps CSP domain lists and sanitizes 'ui/open-link' requests. For Vega charts, it introduces a custom loader wrapper to validate data, image, and navigation URLs against the surface's security policy. The feedback highlights a critical security bypass in the Vega chart loader where relative URLs resolved against a custom 'baseURL' could evade validation; resolving the URL via the base loader before applying the security policy check is recommended to mitigate this vulnerability.

Comment on lines +85 to +92
async sanitize(uri, options) {
const context: UrlContext = options?.context === 'href' ? 'navigation' : 'media';
const allowed = typeof uri === 'string' ? sanitizeUrl(uri, context) : undefined;
if (allowed === undefined) {
throw new Error(BLOCKED_CHART_URL_MESSAGE);
}
return (await ready).sanitize(allowed, options);
},

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.

security-high high

There is a potential security bypass when handling relative URLs or specs with a custom baseURL. If sanitizeUrl is called on the raw relative uri (e.g., 'data.csv'), it will likely be allowed by the policy or baseline checks. However, when it is subsequently passed to (await ready).sanitize(allowed, options), the base Vega loader will resolve it against the configured baseURL (which could point to an untrusted external domain), fetching the resource from the resolved absolute URL without ever validating it against the security policy.

To prevent this bypass, we should first resolve the URL using the base loader's sanitize method, and then apply the security policy check on the fully resolved absolute URL.

    async sanitize(uri, options) {
      const resolved = await ready;
      const sanitized = await resolved.sanitize(uri, options);
      const context: UrlContext = options?.context === 'href' ? 'navigation' : 'media';
      const allowed = sanitizeUrl(sanitized.href, context);
      if (allowed === undefined) {
        throw new Error(BLOCKED_CHART_URL_MESSAGE);
      }
      sanitized.href = allowed;
      return sanitized;
    },

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. In this component the base loader is a plain vega.loader() with no baseURL, and the loader we pass as options.loader takes precedence over a spec's usermeta.embedOptions.loader, so a spec can't set one today. Still, the check shouldn't depend on that. Fixed in a7dcb8a: the policy still checks the raw URI first (so relative-URL rules and rewrites apply to what the spec wrote), and when the base loader changes the URL, for example by resolving it against baseURL, the resolved URL is checked again before Vega fetches or opens it. An unchanged URL is checked once, so a rewritten proxy URL isn't re-checked. Covered by a new test in url_policy_loader.test.ts.

🤖 Agent-generated

@nan-yu
nan-yu force-pushed the feat/url-security-web-core branch 3 times, most recently from cde736f to 76b0957 Compare December 3, 2026 00:55
@nan-yu
nan-yu force-pushed the feat/url-security-mcp-vega branch 3 times, most recently from 44347f9 to 8894707 Compare December 3, 2026 01:25
@nan-yu
nan-yu force-pushed the feat/url-security-web-core branch from b813633 to e38ab5e Compare December 3, 2026 19:12
…L security policy

McpApp checks each _meta.ui.csp source against the surface's URL security
policy (connect/resource domains as media, frame domains as iframe, base URI
domains as navigation) and drops the ones it doesn't allow before the CSP is
built. ui/open-link URLs are checked as navigation.

VegaChart passes Vega a loader that checks data URLs and image marks as
media and href links as navigation before Vega fetches or opens them.

Part of #4045.
…4045)

vega-tooltip renders the image field of an object tooltip as an <img> without the Vega loader, and vega-embed merges usermeta.embedOptions from the spec into the embed options, so a spec could turn the actions menu back on with its own editorUrl. The chart now passes tooltip options whose formatter checks the image as media, and removes usermeta.embedOptions in transformSpec.
…4045)

- Scrub external CSS url()/image-set() references (cursor signals and SVG paint attributes) via MutationObserver on the Vega chart container while preserving local same-document #id gradient and clip-path refs.
- Check upgraded http/https and scheme-less variants for MCP CSP host/scheme sources (*, http:, ws:, http://h, ws://h, and scheme-less hosts).
- Stop logging raw malformed URLs in McpAppBridge ui/open-link and pass {functionName: 'ui/open-link'} to surface warnings.
… inputs (#4045)

- Force config.events/eventConfig globalCursor: false and bind: 'container', and strip element/src/formaction/style/input:'image' from Vega/VL bindings.
- Watch document.body.style.cursor and form src/formaction/type=image attributes in observeCssUrls, preserve valid same-document url(#...) inside style, and clean up observer on embed errors.
- Update mcp README CSP scheme matching description.
@nan-yu
nan-yu force-pushed the feat/url-security-mcp-vega branch from 0385508 to e092412 Compare December 3, 2026 19:13

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