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 implements a comprehensive URL security policy framework for the Swift core SDK. It introduces the URLSecurityPolicy protocol, along with permissive and allowlist-based implementations (PermissiveURLSecurityPolicy and AllowlistURLSecurityPolicy), a baseline check utility (URLBaseline), and related models for context, warnings, and error handling. SurfaceViewModel, MessageProcessor, and NodeResolver have been updated to integrate URL sanitization and publish blocked URL warnings. Additionally, OpenURLFunction now validates URLs against the active security policy, throwing a new URLBlockedError when blocked. Feedback on the changes suggests a potential performance improvement in HTTPAuthority.swift to limit the length of parsed port bytes, preventing potential issues with an excessive number of leading zeros.
| let significant = portBytes.drop { $0 == UInt8(ascii: "0") } | ||
| guard significant.count <= 5 else { return nil } | ||
| let value = significant.reduce(0) { $0 * 10 + Int($1 - UInt8(ascii: "0")) } | ||
| guard value <= 65535 else { return nil } | ||
| port = value == defaultPort ? nil : value |
There was a problem hiding this comment.
The port parsing logic checks if the number of significant digits is less than or equal to 5 (significant.count <= 5). However, if the port string contains an extremely large number of leading zeros (e.g., more than 256 leading zeros), it could potentially cause performance issues or excessive memory allocation when converting or slicing. Consider limiting the maximum length of the input portBytes slice before processing to prevent potential denial-of-service or memory exhaustion issues.
| let significant = portBytes.drop { $0 == UInt8(ascii: "0") } | |
| guard significant.count <= 5 else { return nil } | |
| let value = significant.reduce(0) { $0 * 10 + Int($1 - UInt8(ascii: "0")) } | |
| guard value <= 65535 else { return nil } | |
| port = value == defaultPort ? nil : value | |
| guard portBytes.count <= 10 else { return nil } | |
| let significant = portBytes.drop { $0 == UInt8(ascii: "0") } |
There was a problem hiding this comment.
I left the length check out on purpose. The other SDKs accept any number of leading zeros: the WHATWG URL parser (TypeScript), Python's urlsplit and Dart's Uri all read https://host:00080/ and a port with 200 leading zeros as port 80. Capping portBytes at 10 bytes would make Swift block URLs that the other three allow, and the allowlist verdict should match across SDKs.
The current code is already bounded: drop(while:) is one lazy pass with no copy, the work is linear in a string that has already been read, and the significant.count <= 5 check stops the value from overflowing before it's built. In 354b6e0 I added a comment saying so and a test that a port with 300 leading zeros still matches localhost:8080 (and 008081 doesn't).
🤖 Agent-generated
53f8e27 to
183e38e
Compare
86007a7 to
aa70192
Compare
a9b6972 to
67e9551
Compare
Adds URLSecurityPolicy, the permissive and allowlist policies, the shared baseline, resolver filtering of URL properties (x-a2ui-url-context, the basic catalog built-in list, and the host's property map), redacted URL_BLOCKED warnings, and openUrl checks through the policy. Runs the shared url_security_policy.yaml suite and claims F-CORE-46 for swift-core.
…re IPv6 allowlist entries by address (#4045) Hosts whose last label is a number must be dotted-decimal IPv4 addresses, allowlist entries match ASCII hosts only, and IPv6 hosts are parsed and compared in one written form, as the shared suite now requires.
#4045) Browsers decode %XX in a host before reading it, so 127%2E1 is 127.1 and cdn%2Eexample.com is cdn.example.com. An invalid escape, or a decoded forbidden character such as % or /, makes the URL invalid.
…tes in the policy (#4045) Entries with a leading dot or a percent escape, and wildcards over IP addresses, now throw at construction. AllowlistURLSecurityPolicy checks a rewritten URL against the baseline itself, for hosts that call it directly, and a host percent-decoded to invalid UTF-8 makes the URL invalid.
…4045) Browsers map such hosts with IDNA, which can turn fullwidth digits and dots into a numeric IPv4 address that the dotted-decimal rule would block.
…policy in data contexts (#4045) The resolver now finds an x-a2ui-url-context mark inside allOf, anyOf, and oneOf branches, keeps it when allOf branches list the property again without it, and checks the values of a map whose additionalProperties schema is marked. A DataContext keeps its handler's policy, so a URL checked after the handler is gone still goes through the host's policy. The theme's iconUrl is reported without running the policy twice.
…cting URL marks (#4045) The schema generator marks Image.url, Video.url, Video.posterUrl, and AudioPlayer.url, so derived catalogs keep the checks. Conflicting marks resolve to the stricter context (iframe, navigation, media), an unknown value counts as iframe, and the URL_BLOCKED dedupe key keeps only the URL's length and first 2048 characters.
…openUrl once (#4045) A mark on a list property checks each item and leaves null in place of a blocked one, as in the TypeScript reference. openUrl applies the policy without a URL_BLOCKED warning, since its thrown URLBlockedError already reports the blocked URL; DataContext.sanitizeURL gains reportsBlockedURL.
…ents by their ID (#4045)
67e9551 to
9c3f568
Compare
Summary
Adds the URL security policy to the Swift core (
A2UICoreandBasicCatalog), following the TypeScript reference in #4104 with Swift names. Part of #4045. Stacked on #4102 (basefeat/url-security-spec).A host sets one policy on
MessageProcessor(urlSecurityPolicy:). Every surface uses it to filter URL properties as they resolve, andopenUrlchecks its URL before the host'sOpenURLHandlersees it.Reviewer focus
URLBaselineandHTTPAuthorityparse URLs by hand instead of with Foundation'sURL, so the checks match the proposal byte for byte (no trimming or normalization). Hosts compare as lowercase ASCII, so a non-ASCII host never matches an allowlist entry; it's blocked, not let through. A host whose last label is a number must be dotted-decimal IPv4 (127.1,0x7f.0.0.1, and256.0.0.1are blocked), and IPv6 hosts are parsed with the WHATWG IPv6 parser so[0:0::1]matches a[::1]entry (IPAddressHost).JSONValue.null, at the top level and inside lists and objects, so list positions stay stable. A rewritten value keeps the binding's identity and setter.URLBlockedWarningonSurfaceViewModel.urlBlockedPublisherand an aggregateMessageProcessor.urlBlockedPublisher. Errors still go toActionHandling.FunctionHandlergainsurlSecurityPolicyandsanitizeURL(_:context:info:)with default implementations (baseline only), so existing handlers keep compiling. ADataContextkeeps its handler's policy, so a URL checked after the handler is gone (the context holds it weakly) still goes through the host's policy.Changes
Security/folder inA2UICore:URLSecurityPolicy,PermissiveURLSecurityPolicy,AllowlistURLSecurityPolicy(host lists with*.wildcards and ports,allowRelative, rewrite hooks,allowSVGDataURLs,urlProperties),URLContext,URLContextAnnotation,URLCheckInfo,URLBaseline,URLSchema(helpers forx-a2ui-url-context),URLBlockedWarning, andURLBlockedError.NodeResolverfilters every resolved value of a URL property: the host'surlPropertiesentry wins, then the schema annotation (also insideallOf/anyOf/oneOfbranches; conflicting marks use the stricter context, and an unknown value counts asiframe), then a built-in list for the basic catalogs (Image.url,Video.url,Video.posterUrl,AudioPlayer.url). The schema generator also marks those basic props, so the checks follow the components intoderive()d catalogs. Data-bound,formatString, nested, and markedadditionalPropertiesmap values are covered, and each change resolves and checks again.sanitizeURL(_:context:info:)onSurfaceViewModel,DataContext, andNodeResolver. Blocked URLs are reported once per place (bounded memory of 256), with onlyscheme://host[:port], thedata:media type, or the scheme.MessageProcessorpasses the policy to surfaces and to the RPC handler (for calls without a surface), and removes or rewrites the v0.9 theme'siconUrl.OpenURLFunctionchecks its URL asnavigationand throwsURLBlockedError(codeURL_BLOCKED). The user activation rule and the weakly heldOpenURLHandlerare unchanged.BasicCatalog.sanitizeMediaURL(_:)is deprecated and now runs the baseline.core/url_security_policy.yaml(both actions), andF-CORE-46claimsswift-core.swift/CHANGELOG.mdunder Unreleased.Impact & risks
openUrlthrowsURLBlockedErrorinstead ofFunctionErrorfor blocked URLs, and the baseline is stricter than the old scheme check (https:host, spaces, and control characters are rejected).data:URLs. The SwiftUI views still call the deprecated helper; the stacked SwiftUI PR removes those calls.conformance/README.mdstill says no core harness runs the suite; the TypeScript PR rewrites that sentence, so I left it alone to avoid a conflict.Testing
Swift Core (iOS)workflow runs on this branch throughworkflow_dispatch(the workflow only runs on pull requests tomain).url_security_policy.yamlcases run inURLSecurityPolicyConformanceTests.URLSecurityPolicyTestscover what the suite doesn't: invalid host entries, numeric and IPv6 hosts, non-ASCII hosts under a wildcard, warning redaction and dedupe, the host property map andnone, the theme icon (blocked and rewritten), a rewrittenopenUrlURL reaching the handler, and theDataContextdefault.uv run --project conformance pytest conformance/testsandscripts/fix_licenses.py --checkpass.🤖 Agent-generated