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 new A2UIURLSanitizer mechanism to check URLs against a surface's security policy, integrates it into Surface, and updates A2UIText to optionally render allowed Markdown links. It also migrates media views (A2UIImage, A2UIVideo, and A2UIAudioPlayer) to use the new MediaURL baseline check instead of the deprecated sanitization method. The review feedback highlights critical safety and concurrency improvements: mutating an AttributedString while iterating over its runs is unsafe and should be refactored to avoid runtime crashes or security bypasses, and both A2UIText and A2UIURLSanitizer.init(surface:) should be annotated with @MainActor to prevent isolation mismatch errors under strict concurrency checking.
| if let linkURL { | ||
| attributed[run.range].link = links.allowedTarget(for: linkURL) | ||
| } |
There was a problem hiding this comment.
Mutating an AttributedString (such as setting attributed[run.range].link or attributed[run.range].imageURL) while iterating over its runs is unsafe. Since runs is a dynamic view, modifying attributes can split or merge runs, which invalidates the iterator and can cause runtime crashes or skip runs entirely. Skipping runs could lead to security bypasses if a blocked link is not processed.
To resolve this safely, collect the ranges and targets first, then apply the mutations:
let runs = attributed.runs.map { ($0.range, $0.link, $0.imageURL) }
for (range, linkURL, imageURL) in runs {
if let linkURL {
attributed[range].link = links.allowedTarget(for: linkURL)
}
if imageURL != nil {
attributed[range].imageURL = nil
}
}There was a problem hiding this comment.
Fixed in b1c0e76: the loop now maps attributed.runs to (range, link, imageURL) first and then edits attributed. With AttributedString's value semantics, the old loop read from a copy of the runs, so it didn't skip runs. Still, collecting first is clearer and doesn't depend on that.
🤖 Agent-generated
| public init(node: Node, markdownURLOptions: A2UIMarkdownURLOptions? = nil) { | ||
| self.node = node | ||
| self.markdownURLOptions = markdownURLOptions | ||
| } |
There was a problem hiding this comment.
Since A2UIText contains properties and methods (like linkPolicy and renderedMarkdown) that interact with @MainActor isolated types and functions (such as attributedMarkdown), the entire A2UIText struct should be annotated with @MainActor.
Without this, compiling under Swift 6 or with strict concurrency checking enabled will produce isolation mismatch errors, as non-isolated contexts cannot synchronously call @MainActor functions.
There was a problem hiding this comment.
No change here. A2UIText conforms to View, which is @MainActor in the SDKs this package builds with, so the struct and its members already run on the main actor. The package is built in Swift 6 language mode (swift-tools-version: 6.1) with strict concurrency checking, and none of the other Basic Catalog views (A2UIImage, A2UIButton and the rest) repeat the annotation, so I kept that convention.
🤖 Agent-generated
| public init(surface: SurfaceViewModel) { | ||
| self.init { url, context, info in | ||
| surface.sanitizeURL(url, context: context, info: info) | ||
| } | ||
| } |
There was a problem hiding this comment.
The initializer init(surface:) should be marked with @MainActor because it accepts a @MainActor-isolated SurfaceViewModel and is intended to be called from the main actor context (such as during view rendering). Marking it explicitly prevents isolation mismatch warnings or errors under strict concurrency checking.
| public init(surface: SurfaceViewModel) { | |
| self.init { url, context, info in | |
| surface.sanitizeURL(url, context: context, info: info) | |
| } | |
| } | |
| @MainActor | |
| public init(surface: SurfaceViewModel) { | |
| self.init { url, context, info in | |
| surface.sanitizeURL(url, context: context, info: info) | |
| } | |
| } |
There was a problem hiding this comment.
Done in b1c0e76. SurfaceViewModel is @MainActor, and the only caller is Surface's body, so the initializer is now @MainActor too.
🤖 Agent-generated
ba01741 to
b1c0e76
Compare
…#4361) ## Problem #3257 asks for a way to restyle the SwiftUI Basic Catalog components. #4024 added `A2UIButtonStyle` and `A2UICardStyle`, and the issue stays open for `Text`, `TextField` and `Image`. `A2UITextField` still hard-codes its look: 10 pt padding, a gray fill, an 8 pt corner radius, and a border and label that turn red while a check fails. To change that, an app has to replace the whole component, copying the data binding, the variants and the accessibility modifiers. Part of #3257. This PR covers `TextField` only. `Text` and `Image` are also changed by the open #4123. ## Changes - Adds `A2UITextFieldStyle`, `A2UITextFieldStyleConfiguration` and `View.a2uiTextFieldStyle(_:)` to `BasicCatalogSwiftUI`, built the same way as `A2UIButtonStyle` in #4024. The protocol refines `DynamicProperty` and the style is resolved through a generic wrapper view, so a style's `@Environment` values (such as `\.colorScheme`) resolve. - The configuration has: - `label`: the label text, or `nil` when the `label` property is empty. It stays hidden from assistive technologies, as before. - `field`: the input control for the variant (`SecureField` for `obscured`, a vertical `TextField` for `longText`, the decimal pad on iOS for `number`), bound to `value` and carrying the accessibility identifier, label and hint. - `variant` (`shortText`, `longText`, `number`, `obscured`; unknown values resolve to `shortText`), `errorMessage` (the first failing `error` check) with `hasError`, `isDisabled` (from the environment's `isEnabled`), and `node`. - `DefaultA2UITextFieldStyle` (`.automatic`) draws what `A2UITextField` drew before, with the same values. - `A2UITextField` still renders `A2UICheckMessage` below the style's view, so errors and warning/info hints look the same under any style. The accessibility modifiers moved from the padded box onto the input control, so a custom style can't drop them. They still reach the same native text field. - The style is read in a private host view inside `A2UITextField.body`, so tests that call `.body` outside a hosted view don't read the environment. - New tests in `ComponentStyleTests`: - a custom style gets the label, variant, error message, disabled state and node; - an unknown variant resolves to `shortText`, and an ancestor `.disabled(true)` sets `isDisabled`; - a style resolves `\.colorScheme` in Dark Mode inside a `Surface`; - the default style renders every variant with a failing check; - under a custom style, the `a2ui-<id>` element in the platform accessibility tree has the explicit accessibility label and hint, or the `label` property when there is no explicit label. The existing `A2UITextField`, check message, accessibility and adapter conformance tests pass unchanged. - A "Styling Basic Catalog components" section in `swift/README.md` with a text field style example, and a `swift/CHANGELOG.md` entry. Verified on macOS (Xcode toolchain, Swift 6.1 package): - `swift build` - `swift test --enable-swift-testing --no-parallel --filter A2UISwiftUITests`: 203 tests. All pass except `A2UIModalTests.keyboardActivationOfButtonTriggerRunsActionOnceAndPresentsSheet`, which also fails on `main` when run headless. - `swift test --enable-swift-testing --no-parallel --skip A2UISwiftUITests`: 809 tests pass. - `scripts/fix_format.sh --check --only-swift` and `swift-format lint --strict` on the changed files. ## Pre-launch Checklist One time: - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I read the [Style Guide]. - [ ] I have built and run at least one [sample]. For this PR: - [x] I have updated the relevant CHANGELOG.md file. - [x] I updated/added relevant documentation. - [x] My code changes (if any) have tests. - [ ] If my branch is on a fork, I have verified that [scripts/e2e_test.sh](https://github.com/a2ui-project/a2ui/blob/main/scripts/e2e_test.sh) passes. If you need help, consider asking for advice on the [discussion board]. <!-- Links --> [CLA]: https://cla.developers.google.com/ [Contributors Guide]: https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md [discussion board]: https://github.com/a2ui-project/a2ui/discussions [Style Guide]: https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md#coding-style [sample]: https://github.com/a2ui-project/a2ui/blob/main/samples/README.md#samples --------- Co-authored-by: Polina Cherkasova <polina.c@live.com>
354b6e0 to
b5bcf67
Compare
b1c0e76 to
b03dfa4
Compare
b5bcf67 to
99ecb0e
Compare
811383b to
09f0159
Compare
48b32aa to
3fff54c
Compare
09f0159 to
e0e3c53
Compare
3fff54c to
b14e2d4
Compare
06f3c35 to
23aec0f
Compare
86007a7 to
aa70192
Compare
471b10a to
5e87b06
Compare
a9b6972 to
67e9551
Compare
5e87b06 to
ac8eeaf
Compare
67e9551 to
9c3f568
Compare
… links (#4045) Media views load the URL the resolver returns after the surface's URL security policy and only run the baseline on it. A2UIText can render Markdown links whose targets the policy allows, through the new a2uiURLSanitizer environment value that Surface binds to the surface.
… media URLs (#4045) Media views pass the surface policy's allowSVGDataURLs to the baseline they run, through A2UIURLSanitizer.allowsSVGDataURLs, and load nothing for a relative URL, which they have no base URL for. The sanitizer documents that it reports blocked URLs synchronously.
…date and drop links without a scheme (#4045)
ac8eeaf to
6e1da49
Compare
Summary
Makes the SwiftUI views use the URL security policy from #4121. Part of #4045. Stacked on #4121 (base
feat/url-security-swift-core).A2UIImage,A2UIVideo, andA2UIAudioPlayerload only the URL the resolver returns after the surface's policy, which may be rewritten.A2UITextkeeps stripping Markdown links by default and gains an opt-in that renders links the policy allows.Reviewer focus
URLBaseline.check, with the policy'sallowSVGDataURLsfromA2UIURLSanitizer.allowsSVGDataURLs), which any policy result already passes, so a catalog that doesn't mark these properties still can't loadjavascript:URLs. Relative URLs load nothing, since the views have no base URL.a2uiURLSecurityPolicyenvironment value. This addsa2uiURLSanitizerinstead, bound toSurfaceViewModel.sanitizeURL, so views get the baseline, the policy, andURL_BLOCKEDreporting in one call rather than the raw policy.Textdoesn't display Markdown images. Only.linksexists for now.Changes
A2UISwiftUI:A2UIURLSanitizerand the\.a2uiURLSanitizerenvironment value.Surfacebinds it to its view model; outside a surface it runs the baseline only.BasicCatalogSwiftUI: an internalMediaURLhelper replaces the deprecatedBasicCatalog.sanitizeMediaURL(_:)calls inA2UIImage,A2UIVideo(includingposterUrl), andA2UIAudioPlayerModel.BasicCatalogSwiftUI:A2UIMarkdownURLOptions(.links), the\.a2uiMarkdownURLOptionsenvironment value, andA2UIText(node:markdownURLOptions:). The link policy is threaded throughMarkdownBlockView. Allowed links keep their (possibly rewritten) target; blocked ones render as plain text and are reported (synchronously, as documented onA2UIURLSanitizer).swift/CHANGELOG.mdentries.Impact & risks
URL.absoluteStringafter Foundation parses the Markdown, which may percent-encode characters the agent wrote.Testing
Swift Core (iOS)workflow runs on this branch throughworkflow_dispatch.URLSecurityViewTests: links stripped by default, allowed links kept and blocked ones (javascript:,data:, protocol-relative) dropped with the opt-in, the surface sanitizer applying the policy and reporting a redacted warning, the baseline sanitizer, and the audio model refusing URLs that fail the baseline.🤖 Agent-generated