Skip to content

feat(python): add an optional pre-send URL check to the Python agent SDK - #4122

Draft
nan-yu wants to merge 16 commits into
feat/url-security-python-corefrom
feat/url-security-python-agent
Draft

nan-yu wants to merge 16 commits into
feat/url-security-python-corefrom
feat/url-security-python-agent

Conversation

@nan-yu

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

Copy link
Copy Markdown
Collaborator

Summary

Part of #4045 (plan Wave 4). Stacked on #4119 (feat/url-security-python-core).

Adds check_payload_urls to the Python agent SDK (a2ui.utils), the same opt-in pre-send check as checkPayloadUrls in the TypeScript agent SDK (#4116). An agent can check a payload's URLs against a URL security policy before sending it, and report or strip blocked values. The renderer still enforces its own policy; this is an earlier, optional line.

Reviewer focus

  1. In-order payload simulation in url_check.py: replays createSurface, updateComponents, updateDataModel, and deleteSurface using a2ui.core.DataModel, skips a second createSurface on a surface the payload created and messages after deleteSurface until recreated, checks a createSurface that follows updates to its surface as a new surface while keeping the data and components those updates sent (both renderer readings), checks components against their v1.0 @catalogId (or surface catalog), and re-checks live bound properties whenever an updateDataModel writes at, above, or below their resolved path. Bindings and data writes are indexed by path, so a re-check costs the depth of the path, and a write under a bound list checks only the written items.
  2. Template-child pre-pass: isolates template child IDs per (surface_id, epoch) lifetime and collects componentId values only at child-list schema positions (ChildList, ComponentId, Child, TemplateChildList, or {componentId, path|dataBinding} / explicitList / template), so decoy componentId keys elsewhere cannot silence top-level relative bindings.
  3. Schema walk and strip mode: reads x-a2ui-url-context through $defs refs, allOf/anyOf/oneOf, nested properties, array items, list-level marks, and code-defined additionalProperties (skipping envelope keys and sibling allOf properties), applies the strictest mark on conflict, keeps built-in basic marks on derived basic catalogs, and applies host url_properties overrides (including arrays). Strip mode replaces blocked values with "" at their source (createSurface.dataModel, updateDataModel.value, or the component property).

Changes

  1. a2ui.utils.check_payload_urls(catalogs, payload, *, policy=None, on_blocked="report") returns a PayloadUrlCheckResult (messages, blocked). Each BlockedPayloadUrl carries the message index (of the component or the data write), surface, catalog, component, property path (items[].imageUrl, theme.iconUrl), context, redact_url output, the bound data path, if any, and function_name ("openUrl") for an openUrl URL.
  2. Checks literal values, list items, literal openUrl URLs in actions (as navigation, with the same policy info the renderer passes), bindings (@path in v1.0, path in v0.9) to values the same payload sets (bound lists item by item, including root relative paths on non-template children), components inlined in createSurface, and the basic catalogs' theme.iconUrl. Each value goes through apply_url_security_policy from a2ui.core.
  3. README section and CHANGELOG entry.

Impact & risks

  1. Additive. Agents that don't call it behave as before.
  2. Function calls (formatString), relative bindings inside list templates, and data from earlier payloads aren't resolved here; the renderer checks those. The README says so.

Testing

  1. New python/a2ui_agent/tests/utils/test_url_check.py (53 tests): baseline default, allowlist, redaction, custom marks, nested props, none, policy info, data bindings and update order, DataModel parity (forbidden keys, empty segments, out-of-range indexes), strip at component/data sources (caller's payload unchanged), v1.0 inline components and @catalogId, v0.9 theme.iconUrl, surface lifecycle (deleteSurface, recreated surfaces, template-child position and lifetime isolation), derived basic components, allOf / list marks / code-defined maps, host url_properties overrides, unknown messages, throwing policy, bound lists and item writes, createSurface after updates, literal openUrl URLs (v0.9 and v1.0), re-check order, and a policy-call count for item writes.
  2. uv run --all-packages pytest (6652 passed, 412 skipped, 13 deselected, 3 xfailed, 33 subtests passed), mypy (0 issues), pyink --check (0 changes), isolated a2ui-agent-sdk import check, and license check on all touched files.

🤖 Agent-generated

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-project-automation github-project-automation Bot moved this to Todo in A2UI Nov 10, 2026
@nan-yu

nan-yu commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

/gemini review

🤖 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 introduces a new URL security checking utility, check_payload_urls, in a2ui.utils to validate payload URLs against a security policy before sending. It includes updates to the changelog, documentation, and a comprehensive suite of unit tests. The review identified two important security improvements: first, in _set_child, calling container.extend with an arbitrary index from untrusted payloads can cause memory exhaustion, so a limit on the list index should be enforced; second, in _find_catalog, the fallback behavior when a specified catalog_id is not found could lead to a security bypass, and the function should instead return None immediately.

Comment on lines +563 to +569
def _set_child(container: Any, segment: str, value: Any) -> None:
if isinstance(container, list):
index = int(segment)
container.extend([None] * (index + 1 - len(container)))
container[index] = value
else:
container[segment] = value

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

Calling container.extend with an arbitrary index parsed from an untrusted payload can lead to memory exhaustion and Denial of Service (DoS). If a payload contains a very large list index (e.g., /items/999999999/url), this will attempt to allocate a massive list, causing a MemoryError and crashing the agent. We should enforce a reasonable upper bound on the list index before extending the container.

Suggested change
def _set_child(container: Any, segment: str, value: Any) -> None:
if isinstance(container, list):
index = int(segment)
container.extend([None] * (index + 1 - len(container)))
container[index] = value
else:
container[segment] = value
def _set_child(container: Any, segment: str, value: Any) -> None:
if isinstance(container, list):
index = int(segment)
if index > 10000:
return
container.extend([None] * (index + 1 - len(container)))
container[index] = value
else:
container[segment] = value

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.

Fixed in 00cdfe1. The renderer's data model (Python and TS) rejects any list index above MAX_ARRAY_INDEX (10,000), so such an update never reaches the surface. The mirror now ignores an updateDataModel with any index segment above that bound, which caps list growth and keeps bindings resolving as the renderer would (a binding to the rejected path reads nothing in both). Same fix in the TS port in #4116 (3baf1e6), with tests in both for index 10000 (applied), 1000000000 on an existing list, and on a newly created one (ignored).

🤖 Agent-generated

Comment on lines +368 to +378
def _find_catalog(
catalogs: Sequence[CatalogApi], catalog_id: str | None, component_type: str
) -> CatalogApi | None:
if catalog_id is not None:
for catalog in catalogs:
if catalog.id == catalog_id:
return catalog if catalog.get_component(component_type) else None
for catalog in catalogs:
if catalog.get_component(component_type) is not None:
return catalog
return None

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-medium medium

If catalog_id is specified but not found in catalogs, the function currently falls back to searching all catalogs for the first one that defines the component type. This fallback can lead to checking the component against the wrong catalog schema, potentially bypassing URL security checks if the fallback catalog does not mark the property as a URL. We should return None immediately if the specified catalog_id is not found.

Suggested change
def _find_catalog(
catalogs: Sequence[CatalogApi], catalog_id: str | None, component_type: str
) -> CatalogApi | None:
if catalog_id is not None:
for catalog in catalogs:
if catalog.id == catalog_id:
return catalog if catalog.get_component(component_type) else None
for catalog in catalogs:
if catalog.get_component(component_type) is not None:
return catalog
return None
def _find_catalog(
catalogs: Sequence[CatalogApi], catalog_id: str | None, component_type: str
) -> CatalogApi | None:
if catalog_id is not None:
for catalog in catalogs:
if catalog.id == catalog_id:
return catalog if catalog.get_component(component_type) else None
return None
for catalog in catalogs:
if catalog.get_component(component_type) is not None:
return catalog
return None

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.

Fixed in 00cdfe1. When createSurface names a catalog that the agent's catalogs don't include, the check no longer falls back to another catalog that defines the same type. It checks only the top-level properties that the built-in list (for a basic catalog ID) or the policy's url_properties name for that catalog ID, without a schema; anything else is left to the renderer. A surface the payload only updates (no catalog ID known) still uses the first catalog with the type, as documented. Same behavior in the TS checkPayloadUrls in #4116 (3baf1e6), with tests in both.

🤖 Agent-generated

@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 775d639 to 00cdfe1 Compare November 10, 2026 17:09
@nan-yu
nan-yu force-pushed the feat/url-security-python-core branch from 013e65a to 8c9862a Compare December 3, 2026 00:36
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 00cdfe1 to d21e261 Compare December 3, 2026 00:40
@nan-yu
nan-yu force-pushed the feat/url-security-python-core branch 3 times, most recently from be5a99b to b987053 Compare December 3, 2026 00:59
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from d21e261 to 0dcdc11 Compare December 3, 2026 01:25
@nan-yu
nan-yu force-pushed the feat/url-security-python-core branch from 14f301b to 0f6cc36 Compare December 3, 2026 19:10
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 0dcdc11 to 4572caa Compare December 3, 2026 19:13
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 4572caa to 1c072c6 Compare December 3, 2026 19:14
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 4adc937 to 37ba2f7 Compare December 3, 2026 19:22
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 1828474 to abb593e Compare December 3, 2026 19:25
@nan-yu
nan-yu force-pushed the feat/url-security-python-core branch from d71aac4 to 68dce69 Compare December 3, 2026 19:28
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from abb593e to 7e43cc3 Compare December 3, 2026 19:29
@nan-yu
nan-yu force-pushed the feat/url-security-python-core branch from 68dce69 to 99ea6eb Compare December 3, 2026 19:29
…SDK (#4045)

check_payload_urls checks a payload's URL properties against a URL security
policy before the agent sends it, and reports or strips blocked values,
matching checkPayloadUrls in the TypeScript agent SDK.
…ema in the URL check (#4045)

Ignore an updateDataModel whose list index is above the renderer's
MAX_ARRAY_INDEX, as the renderer rejects it. When the surface's named
catalog isn't among the agent's catalogs, check only the built-in list and
the policy's url_properties for it instead of another catalog's schema.
nan-yu added 13 commits December 3, 2026 19:30
…ed basic components and array overrides, and respect allOf properties in the URL check (#4045)
…dates to its surface, and literal openUrl URLs, and cache catalog schemas in the URL check (#4045)
…and pass openUrl's policy only the renderer's info in the URL check (#4045)
… written items of a bound list in the URL check (#4045)
@nan-yu
nan-yu force-pushed the feat/url-security-python-agent branch from 7e43cc3 to aa66240 Compare December 3, 2026 19:30

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