Skip to content

feat(swiftui): load policy-checked media URLs and add opt-in Markdown links - #4123

Draft
nan-yu wants to merge 4 commits into
feat/url-security-swift-corefrom
feat/url-security-swiftui
Draft

nan-yu wants to merge 4 commits into
feat/url-security-swift-corefrom
feat/url-security-swiftui

Conversation

@nan-yu

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

Copy link
Copy Markdown
Collaborator

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, and A2UIAudioPlayer load only the URL the resolver returns after the surface's policy, which may be rewritten. A2UIText keeps stripping Markdown links by default and gains an opt-in that renders links the policy allows.

Reviewer focus

  1. Check-once: media views don't run the policy again on the resolved URL, since a rewritten proxy URL would then need the proxy host on the allowlist. They run only the baseline (URLBaseline.check, with the policy's allowSVGDataURLs from A2UIURLSanitizer.allowsSVGDataURLs), which any policy result already passes, so a catalog that doesn't mark these properties still can't load javascript: URLs. Relative URLs load nothing, since the views have no base URL.
  2. The plan named an a2uiURLSecurityPolicy environment value. This adds a2uiURLSanitizer instead, bound to SurfaceViewModel.sanitizeURL, so views get the baseline, the policy, and URL_BLOCKED reporting in one call rather than the raw policy.
  3. Markdown images stay plain text even with the opt-in, because SwiftUI Text doesn't display Markdown images. Only .links exists for now.

Changes

  1. A2UISwiftUI: A2UIURLSanitizer and the \.a2uiURLSanitizer environment value. Surface binds it to its view model; outside a surface it runs the baseline only.
  2. BasicCatalogSwiftUI: an internal MediaURL helper replaces the deprecated BasicCatalog.sanitizeMediaURL(_:) calls in A2UIImage, A2UIVideo (including posterUrl), and A2UIAudioPlayerModel.
  3. BasicCatalogSwiftUI: A2UIMarkdownURLOptions (.links), the \.a2uiMarkdownURLOptions environment value, and A2UIText(node:markdownURLOptions:). The link policy is threaded through MarkdownBlockView. Allowed links keep their (possibly rewritten) target; blocked ones render as plain text and are reported (synchronously, as documented on A2UIURLSanitizer).
  4. swift/CHANGELOG.md entries.

Impact & risks

  1. Media views no longer trim whitespace around a URL. The resolver already blocks such URLs under the baseline, so this only matters for nodes built by hand.
  2. Link targets are checked as URL.absoluteString after Foundation parses the Markdown, which may percent-encode characters the agent wrote.
  3. No change for hosts that don't opt in to Markdown links.

Testing

  1. Swift isn't available locally, so the Swift Core (iOS) workflow runs on this branch through workflow_dispatch.
  2. New 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.
  3. The Markdown subset adapter suite still passes unchanged.

🤖 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 added a commit that referenced this pull request 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 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.

Comment on lines 252 to 254
if let linkURL {
attributed[run.range].link = links.allowedTarget(for: linkURL)
}

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.

high

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

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

Comment on lines +64 to +67
public init(node: Node, markdownURLOptions: A2UIMarkdownURLOptions? = nil) {
self.node = node
self.markdownURLOptions = markdownURLOptions
}

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.

medium

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.

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.

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

Comment on lines +45 to +49
public init(surface: SurfaceViewModel) {
self.init { url, context, info in
surface.sanitizeURL(url, context: context, info: info)
}
}

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.

medium

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.

Suggested change
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)
}
}

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.

Done in b1c0e76. SurfaceViewModel is @MainActor, and the only caller is Surface's body, so the initializer is now @MainActor too.

🤖 Agent-generated

nan-yu added a commit that referenced this pull request Nov 10, 2026
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch from ba01741 to b1c0e76 Compare November 10, 2026 17:05
pinieb pushed a commit that referenced this pull request Dec 1, 2026
…#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>
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from 354b6e0 to b5bcf67 Compare December 3, 2026 00:36
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch from b1c0e76 to b03dfa4 Compare December 3, 2026 00:38
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from b5bcf67 to 99ecb0e Compare December 3, 2026 00:45
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch 2 times, most recently from 811383b to 09f0159 Compare December 3, 2026 00:49
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from 48b32aa to 3fff54c Compare December 3, 2026 00:49
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch from 09f0159 to e0e3c53 Compare December 3, 2026 00:49
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from 3fff54c to b14e2d4 Compare December 3, 2026 00:54
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch 6 times, most recently from 06f3c35 to 23aec0f Compare December 3, 2026 18:00
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from 86007a7 to aa70192 Compare December 3, 2026 19:09
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch 3 times, most recently from 471b10a to 5e87b06 Compare December 3, 2026 19:27
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from a9b6972 to 67e9551 Compare December 3, 2026 19:27
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch from 5e87b06 to ac8eeaf Compare December 3, 2026 19:27
@nan-yu
nan-yu force-pushed the feat/url-security-swift-core branch from 67e9551 to 9c3f568 Compare December 3, 2026 19:28
… 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.
@nan-yu
nan-yu force-pushed the feat/url-security-swiftui branch from ac8eeaf to 6e1da49 Compare December 3, 2026 19:28

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