Repository navigation
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
/gemini review 🤖 Agent-generated |
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive URL security policy framework for the A2UI core, adding APIs like UrlSecurityPolicy, PermissiveUrlSecurityPolicy, and AllowlistUrlSecurityPolicy to control media loading, navigation, and iframe embedding. It integrates URL sanitization across MessageProcessor, SurfaceModel, DataContext, and ComponentContext, and updates the basic catalogs to enforce these policies. Feedback on the changes highlights two important issues: first, a potential crash in surface_model.py if an unhashable URL type is used as a dictionary key when reporting blocked URLs; second, the use of the deprecated loop parameter in asyncio.ensure_future within _open_url.py which should be removed.
| ) -> None: | ||
| if self._disposed: | ||
| return | ||
| key = (url, context, info.component_id, info.property_name, info.function_name) |
There was a problem hiding this comment.
If url is not a hashable type (for example, if a custom component or host dynamically passes a dict or list to sanitize_url), creating a tuple key with url will raise a TypeError: unhashable type and crash the message processor. To prevent this, we should ensure the key uses a hashable representation of the URL, such as converting non-string URLs to their string representation.
url_key = url if isinstance(url, str) else str(url)
key = (url_key, context, info.component_id, info.property_name, info.function_name)There was a problem hiding this comment.
Fixed in 013e65a. The baseline already blocks non-string URLs (as the TS SDK does), so they still return None and report redactedUrl: "invalid"; the de-duplication key now uses the URL for strings and <type name> otherwise, so an unhashable value such as a list from a data binding no longer raises. DataContext and ComponentContext delegate to SurfaceModel.sanitize_url, so they share the fix. Tests cover direct calls, a data-bound list, and both contexts.
🤖 Agent-generated
| except Exception: # pylint: disable=broad-exception-caught | ||
| logger.exception("The open-URL handler failed.") | ||
| return | ||
| task = asyncio.ensure_future(awaitable, loop=loop) |
There was a problem hiding this comment.
The loop parameter in asyncio.ensure_future has been deprecated since Python 3.8 and can be safely omitted. Since loop is already the running loop of the current thread, asyncio.ensure_future(awaitable) is sufficient and avoids deprecation warnings in newer Python versions.
| task = asyncio.ensure_future(awaitable, loop=loop) | |
| task = asyncio.ensure_future(awaitable) |
There was a problem hiding this comment.
Fixed in 013e65a. asyncio.ensure_future is only reached when running_loop() returned the running loop, so the loop= argument was redundant; it's dropped and the check calls running_loop() directly.
🤖 Agent-generated
53f8e27 to
183e38e
Compare
013e65a to
8c9862a
Compare
8c9862a to
80770b7
Compare
80770b7 to
be5a99b
Compare
be5a99b to
b987053
Compare
14f301b to
0f6cc36
Compare
d71aac4 to
68dce69
Compare
Adds the UrlSecurityPolicy contract, the permissive and allowlist policies, the processor options url_security_policy and open_url_handler, sanitize_url on surfaces and contexts, resolver filtering of props marked with x-a2ui-url-context (plus the basic catalog built-in list), redacted URL_BLOCKED warnings, and openUrl through the policy. Runs the shared url_security_policy conformance suite and claims python-core for F-CORE-46.
…rop the loop argument (#4045) A non-string URL, such as a list from a data binding, is blocked by the baseline but was used in the report de-duplication key, which raised for unhashable values. asyncio.ensure_future already uses the running loop.
… and parsed IPv6 addresses in allowlists (#4045)
…rewrites in direct sanitize_url calls (#4045)
…ues in the binder (#4045)
…ies, and report a blocked theme icon without re-running the policy (#4045)
…catalog and bound the blocked-URL report keys (#4045)
…keep the stricter of two marks (#4045)
…, and fall back to the catchall for host URL overrides (#4045)
…and reject percent escapes in IPv6 hosts (#4045)
… reference to open-URL handler tasks (#4045)
…without a warning (#4045)
…me and run the allowlist entry and redaction conformance cases (#4045)
…ocked-URL report keys (#4045)
68dce69 to
99ea6eb
Compare
Summary
Adds the URL security policy from the proposal in #4102 to the Python core SDK (
a2ui_core), with the same shape as the TypeScript reference in #4104. Part of #4045, stacked on #4102 (basefeat/url-security-spec).A host sets one policy on the processor. Every surface then checks URL props marked with
x-a2ui-url-context(plus the basic catalog's built-in list) as they resolve,openUrlchecks its URL before the host's handler sees it, and blocked URLs are reported once per surface as a redactedURL_BLOCKEDwarning. The Python harness now runsconformance/core/url_security_policy.yaml(32/32) and claimspython-corefor F-CORE-46.Reviewer focus
security/url_security_policy.py: the baseline and host parser. Hosts come fromurllib.parse.urlsplitand are normalized for ASCII, dotted-decimal IPv4, compressed IPv6, and percent-decoding ([::%31], numeric shorthands such as127.1, and non-ASCII hosts are blocked). A host the normalizer can't read is blocked.resolution/generic_binder.py:BehaviorNode.url_contextandcatchall, read from the schema keyword at any depth (surviving unions,allOfmerges, list marks, and code-definedadditionalProperties, with the stricter context winning on conflict), plus top-level overrides from the built-in list andurl_properties. A STATIC URL prop falls back to DYNAMIC when its value is a binding.catalog/catalog.py:Catalog.from_jsonrejects unknownx-a2ui-url-contextvalues and non-nonemarks underadditionalProperties,patternProperties, orunevaluatedProperties.basic_catalog/_open_url.py: the headlessopenUrl. Without anopen_url_handler, an allowed call does nothing (Python has no browser to open). An async handler runs on the surface's running loop, and the SDK keeps a reference to its task until it finishes.Changes
a2ui.core.securitypackage, re-exported froma2ui.core:UrlSecurityPolicy(protocol withsanitize_url(url, context, info=None)),PermissiveUrlSecurityPolicy,AllowlistUrlSecurityPolicy,UrlContext,UrlContextAnnotation,URL_CONTEXTS,URL_CONTEXT_KEYWORD,UrlCheckInfo,UrlPropertyMap,UrlRewriteHook,OpenUrlHandler,url_property(),url_context_of,apply_url_security_policy,redact_url,BASIC_CATALOG_URL_PROPERTIES.MessageProcessorOptions.url_security_policyandopen_url_handler, passed to every surface the processor creates, including the stand-in surfaces for agent calls (RpcHandlertakes the same keyword arguments).sanitize_urlonSurfaceModel,DataContextandComponentContext. The surface reports a blocked URL once (bounded FIFO memory of 256 keys, keyed by URL length and first 2048 characters) withredactedUrl,contextand the camelCaseinfofields; the full URL never leaves the surface. A non-string URL (such as an unset optional prop) resolves toNonewithout a warning.redact_urlfollows the shared rule:scheme://host[:port]only for anhttp(s)URL without unsafe characters whose host passes the baseline, otherwise the scheme alone.GenericBinderfilters every resolved value of a marked prop (literal, data-bound,formatString, nested in arrays, objects, unions,allOfmerges, and code-defined maps; a data-bound list is filtered item by item). Blocked values resolve toNone. The basic catalog URL props are marked by catalog ID and also by component model class, so they stay marked in catalogs with other IDs.iconUrlgoes through the policy: blocked is removed, rewritten is replaced, and the policy runs once even when blocked.openUrl(v0.9 and v1.0) runs the policy asnavigation; a blocked URL raisesA2uiUrlBlockedError, reported once with error codeURL_BLOCKED(without a duplicate warning). This replaces the scheme-only check, which accepted hostless URLs such ashttps:evil.com. The user-activation rule is unchanged.tests/url_security_policy_runner.pyimplementsurl_policy_check,url_policy_resolution,url_policy_entries, andurl_redact; the suite skip is removed;python-coreadded toF-CORE-46.claimedBy.tests/test_url_security_policy.pycoversurl_propertyon Pydantic models, hosturl_properties(including arrays and catchalls), catalog load rejections, warning redaction and bounds,ComponentContextandRpcHandlerfallback info, policies that raise, invalid host entries, themeiconUrl, and sync/async open-URL handlers.Impact & risks
javascript:, SVGdata:URLs, URLs with spaces or backslashes, numeric shorthand hosts, or non-ASCII hosts now resolve toNone, andopenUrlerrors useURL_BLOCKEDinstead ofEXPRESSION_ERROR. RelativeopenUrlURLs are now allowed, as in the proposal.UrlSecurityPolicyis aProtocol;allow_svg_data_urlsandurl_propertiesare optional attributes read withgetattr, matching the optional fields in TypeScript.F-CORE-46.claimedBy; whichever merges second needs a one-line merge.Testing
uv run --all-packages pytest(core, agent, a2a, adk, conformance/tests): 6608 passed (with 412 skipped, 13 deselected, 3 xfailed, 33 subtests passed).core/url_security_policy.yaml32/32 in the Python harness;check_executed_cases.py --harness python-coreverified 922 cases across 30 active suites.uv run pyink --check .,uv run --all-packages mypy ., license check on all modified and added files.🤖 Agent-generated