An open source design system that's fully customizable and agent ready
TypeScript
fix(native): a Swift Avatar never links to a script URL or an HTML data URL (#9343)
Cindy Zhang9efcc3a40062
diff --git a/docs/specs/components/Avatar/Avatar.swift.spec.md b/docs/specs/components/Avatar/Avatar.swift.spec.mdindex 7d8206a34d..ea7eebc1e5 100644--- a/docs/specs/components/Avatar/Avatar.swift.spec.md+++ b/docs/specs/components/Avatar/Avatar.swift.spec.md@@ -90,7 +90,7 @@ Consumer migration instructions belong in consumer docs and release notes.| FR2 | Initials MUST follow core's rule: the first letter, digit, or emoji of the first and last words that have one, uppercased, skipping leading punctuation, keeping a whole emoji, flag, or joined emoji sequence. A name with none shows the person symbol and still names the avatar. | core `Avatar` initials | verify || FR3 | The avatar MUST be a square of 20, 24, 36, 48, or 128 points for `xsm` to `xl` at the default text size, grown with Dynamic Type up to 1.6 times and never shrunk, so it and its initials grow with the name beside them without swallowing a row (`spec:AST-082` FR11). It is clipped to a circle, to the theme's `radius.element`, or to a square. | core `Avatar` sizes and shapes | verify || FR4 | Initials and the person symbol MUST draw in `color.textSecondary` on `color.neutral`; initials use the theme's body family at medium weight and 40% of the side. | core `Avatar` fallback styles; `spec:AST-093` FR4 | verify |-| FR5 | With an `href` that is an absolute URL (it has a scheme), the avatar MUST be a SwiftUI `Link`; with `onClick` too, `onClick` runs as it opens and the URL then goes to the app's own URL handler, or the system's. An `href` that is not an absolute URL, such as a web path, MUST stop a development build with a message naming it; a release build draws the avatar without a link, as a button with `onClick` or static. With only `onClick`, a SwiftUI `Button`. Either draws exactly as the static avatar, and the avatar has no disabled state. | Swift component API A4 | verify |+| FR5 | With an `href` that is an absolute URL (it has a scheme), the avatar MUST be a SwiftUI `Link`, except that a script URL (`javascript:`, `vbscript:`) or an HTML document in a `data:` URL MUST leave it without a link, as a button with `onClick` or static (`family:navigation-destinations` FR4); with `onClick` too, `onClick` runs as it opens and the URL then goes to the app's own URL handler, or the system's. An `href` that is not an absolute URL, such as a web path, MUST stop a development build with a message naming it; a release build draws the avatar without a link, as a button with `onClick` or static. With only `onClick`, a SwiftUI `Button`. Either draws exactly as the static avatar, and the avatar has no disabled state. | Swift component API A4 | verify || FR6 | Both initializers MUST build the same `AvatarProps`; `name` is the unlabeled first parameter, a single trailing closure is the status, and `onClick` comes last. Base props come through `AvatarProps`: with `isHidden` the view draws nothing; `id` becomes the accessibility identifier. | Swift component API A2–A4, A7; `spec:AST-082` FR7 | verify || FR7 | The `status` slot MUST draw its content, usually one `AvatarStatusDot`, outside the avatar's clip, placed from the content's own size, in the same layout pass, as core places any status: on a circle centered on the circle's edge at the bottom trailing 45° point, on a rounded or square avatar overhanging the bottom trailing corner by a quarter of its size, mirrored in right-to-left layouts. It MUST NOT change the avatar's size or layout, gives a dot the avatar's side for its size tier, and lets other content grow with Dynamic Type no faster than the avatar, up to the text size at its 1.6 times growth: the content still grows with Dynamic Type (`spec:AST-082` FR11), up to its owner's growth, since the owner sizes what it holds (Swift slots S4). | core `Avatar` status position; Swift slots S4 | verify || FR8 | `tooltip` MUST resolve, as core does, to the name for `nil` or `true`, the given words for a string, and nothing for `false` or blank words, and MUST show through the Astryx tooltip (`architecture:native-swift-library-tooltip`), with the focus and touch the Tooltip by input table below states: a static avatar is a tab stop exactly when it shows a tooltip, as core gives its root a tab stop only then, and opens on a tap; a link or button keeps its focus and its tap. A focused avatar shows the platform's focus ring, as core shows its focus-visible ring. Given words are the VoiceOver hint. On the wire it is the web value, a string or a Bool. | core `Avatar` `tooltip`; core `Tooltip`; `spec:AST-082` FR3 | verify |@@ -194,16 +194,16 @@ Theme values read: `color.neutral`, `color.textSecondary`, `radius.element`,## Verification map-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |-| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |-| FR1, FR2 | `AvatarTests.swift` initials and fallback tests | core's initials cases; blank and symbol names | a wrong initial or a missing person symbol fails | `audit:swift/Avatar/picture` |-| FR3, FR4 | `AvatarTests.swift` size, text-size growth, shape, and two-theme renders | each size; xx-large and the largest text size; circle and square; light, dark, butter | a wrong side, no growth or growth past 1.6 times, an unclipped corner, or a color of its own fails | `audit:swift/Avatar/look` |-| FR5, AR2, AR4 | `AvatarTests.swift` link and button renders; tap, trait, tap-area, warning, `linkURL`, and web-path tests | static, link, button; malformed, relative, and empty `href` | a link or button that draws differently, a malformed `href` made a link, a web path not flagged, or an unflagged unnamed tap fails | `audit:swift/Avatar/control` |-| FR8 | `AvatarTests.swift` tooltip words, focus and touch, VoiceOver hint, and wire tests; `TooltipPresenterTests.swift` for each input; `contracts.test.ts`; `parseDocType.test.ts` | name, `true`, words, `false`, blank, no name; static, link, button | a wrong tooltip, a static avatar that is not a tab stop or does not open on a tap, a control that loses its tap, or a wire value that is not the web's fails; not measured until a test exists: a static avatar that shows no tooltip and takes focus, or a focused avatar with no focus ring | `audit:swift/Avatar/tooltip` |-| FR6 | `AvatarTests.swift` initializer and hidden tests; `contracts.test.ts` | both initializers; `isHidden`; named sizes | differing props, a drawn hidden avatar, or a pixel size in Swift fails | `audit:swift/Avatar/api` |-| FR7, AR6 | `AvatarStatusDotTests.swift` placement renders of a dot and of other content at its measured place, size, an icon's growth at the largest text size, dot-label-wins, and name tests; the label preference that carries a label into the rendered name is not measured (AR3) | circle and square at `lg`; name with and without a status | a dot that changes the avatar's size, sits off the edge, or leaves the name without its label fails | `audit:swift/Avatar/status` |-| AR1 | `AvatarTests.swift` naming and trait tests | `alt`, `name`, blank, none | a wrong name or a named decorative avatar fails | `audit:swift/Avatar/a11y` |-| AR5 | `AvatarTests.swift` two-theme renders draw the initials in core's tokens | default and neutral, light and dark | initials in tokens other than core's fail | `audit:swift/Avatar/look` |+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |+| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |+| FR1, FR2 | `AvatarTests.swift` initials and fallback tests | core's initials cases; blank and symbol names | a wrong initial or a missing person symbol fails | `audit:swift/Avatar/picture` |+| FR3, FR4 | `AvatarTests.swift` size, text-size growth, shape, and two-theme renders | each size; xx-large and the largest text size; circle and square; light, dark, butter | a wrong side, no growth or growth past 1.6 times, an unclipped corner, or a color of its own fails | `audit:swift/Avatar/look` |+| FR5, AR2, AR4 | `AvatarTests.swift` link and button renders; tap, trait, tap-area, warning, `linkURL`, web-path, and refused-destination tests | static, link, button; malformed, relative, and empty `href`; `javascript:`, `vbscript:`, and `data:text/html` hrefs | a link or button that draws differently, a malformed `href` made a link, a web path not flagged, an unflagged unnamed tap, or a refused destination made a link fails | `audit:swift/Avatar/control` |+| FR8 | `AvatarTests.swift` tooltip words, focus and touch, VoiceOver hint, and wire tests; `TooltipPresenterTests.swift` for each input; `contracts.test.ts`; `parseDocType.test.ts` | name, `true`, words, `false`, blank, no name; static, link, button | a wrong tooltip, a static avatar that is not a tab stop or does not open on a tap, a control that loses its tap, or a wire value that is not the web's fails; not measured until a test exists: a static avatar that shows no tooltip and takes focus, or a focused avatar with no focus ring | `audit:swift/Avatar/tooltip` |+| FR6 | `AvatarTests.swift` initializer and hidden tests; `contracts.test.ts` | both initializers; `isHidden`; named sizes | differing props, a drawn hidden avatar, or a pixel size in Swift fails | `audit:swift/Avatar/api` |+| FR7, AR6 | `AvatarStatusDotTests.swift` placement renders of a dot and of other content at its measured place, size, an icon's growth at the largest text size, dot-label-wins, and name tests; the label preference that carries a label into the rendered name is not measured (AR3) | circle and square at `lg`; name with and without a status | a dot that changes the avatar's size, sits off the edge, or leaves the name without its label fails | `audit:swift/Avatar/status` |+| AR1 | `AvatarTests.swift` naming and trait tests | `alt`, `name`, blank, none | a wrong name or a named decorative avatar fails | `audit:swift/Avatar/a11y` |+| AR5 | `AvatarTests.swift` two-theme renders draw the initials in core's tokens | default and neutral, light and dark | initials in tokens other than core's fail | `audit:swift/Avatar/look` |## Decision logdiff --git a/packages/native/apple/Sources/Astryx/Avatar/Avatar.swift b/packages/native/apple/Sources/Astryx/Avatar/Avatar.swiftindex 1b17fd4ada..f271838cde 100644--- a/packages/native/apple/Sources/Astryx/Avatar/Avatar.swift+++ b/packages/native/apple/Sources/Astryx/Avatar/Avatar.swift@@ -302,9 +302,13 @@ public struct Avatar<Status: View>: View {/// The URL the avatar opens, or `nil` when it has no usable `href`.var linkURL: URL? {- // Only an absolute URL the system can open; a malformed or relative- // `href` leaves the avatar static or a button.- guard let url = url(props.href), url.scheme != nil else { return nil }+ // Only an absolute URL the system can open, and never one core's+ // navigation destinations refuse (a script URL or an HTML `data:` URL);+ // a malformed, relative, or refused `href` leaves the avatar static or a+ // button.+ guard let href = props.href, astryxSafeDestination(href) != nil,+ let url = url(href), url.scheme != nil+ else { return nil }return url}diff --git a/packages/native/apple/Tests/AstryxTests/AvatarTests.swift b/packages/native/apple/Tests/AstryxTests/AvatarTests.swiftindex 124ff18c39..40d3fc4aa8 100644--- a/packages/native/apple/Tests/AstryxTests/AvatarTests.swift+++ b/packages/native/apple/Tests/AstryxTests/AvatarTests.swift@@ -125,6 +125,20 @@ import Testing#expect(Avatar("Ada").linkURL == nil)}+// family:navigation-destinations FR4: a script URL or an HTML document in a+// data URL never makes the avatar a link; it stays a button with onClick, or+// static, as Swift Button and Item refuse them.+@MainActor+@Test func aRefusedDestinationLeavesTheAvatarWithoutALink() {+ for refused in ["javascript:alert(1)", " JavaScript: alert(1)", "vbscript:msgbox", "data:text/html,<p>", "data: text/html,<p>"] {+ #expect(Avatar("Ada", href: refused).linkURL == nil, "\(refused)")+ #expect(Avatar("Ada", href: refused).tap == .none, "\(refused)")+ #expect(Avatar("Ada", href: refused, onClick: {}).tap == .button, "\(refused)")+ }+ // Other schemes still open, as before.+ #expect(Avatar("Ada", href: "data:image/png;base64,AAAA").linkURL != nil)+}+@MainActor@Test func avatarsReadTheirTapAndTraits() {#expect(Avatar("Ada").tap == .none)