Test iOS rich-text outbound link security on a cloud Mac

Test iOS rich-text outbound link security on a cloud Mac

Customer support messages, help documents, and comments can contain rich text that says “View report” but links somewhere else. When QA runs iOS regression tests on a cloud Mac, checking that a link turns blue and opens a page is not enough: tests also need to verify the URL actually passed to the system. The approach below separates outbound link checks into a pure Swift policy and UI acceptance tests. First, establish the boundaries on the command line; then integrate the policy into the app.

Define which destinations are allowed

This example implements a closed set of rules for outbound links: HTTPS only, exact host matches only, no username or password, and either no explicit port or port 443. Relative paths, custom schemes, and all other hosts are rejected. If the product genuinely needs additional destinations, extend the rules explicitly rather than falling back to “open anything that parses.”

The example uses this site's host as a test fixture and makes no network requests. In a real project, the allowlist should come from version-controlled configuration, not from the same untrusted rich text.

A URL parser answers “How should this string be interpreted?” A security policy answers “Is this destination allowed to open?” Neither can replace the other.

Define the failure behavior as well: do not open a page, and show an understandable message. Do not automatically retry a rejected destination in another browser component; that would bypass the same rule.

Implement a standalone destination validator

On the cloud Mac, confirm that a working development toolchain is selected, then save the following as LinkPolicy.swift. The function has no UI dependencies and makes no network requests, so it can run independently in each test pipeline.

import Foundation

func approvedLink(
    _ raw: String,
    hosts: Set<String>
) -> URL? {
    let controls = CharacterSet.controlCharacters
    let rawForbidden = controls.union(.whitespacesAndNewlines)

    guard raw.utf8.count <= 4096,
          !raw.unicodeScalars.contains(where: rawForbidden.contains),
          let decoded = raw.removingPercentEncoding,
          !decoded.unicodeScalars.contains(where: controls.contains),
          !decoded.contains("\\"),
          let parts = URLComponents(string: raw),
          parts.scheme?.lowercased() == "https",
          parts.user == nil,
          parts.password == nil,
          let host = parts.host?.lowercased(),
          hosts.contains(host),
          parts.port == nil || parts.port == 443,
          let url = parts.url
    else {
        return nil
    }

    return url
}

The 4096 limit is a project-defined input budget, not the system's maximum URL length. Unencoded whitespace is rejected outright; valid encoded spaces in a path can remain. The decoded value is used only to screen for control characters and backslashes. The returned URL is still the result of parsing the original input.

Hosts in the allowlist must already be lowercase. Do not check hosts with string prefixes, and do not treat displayed host text as the actual destination. If the product needs internationalized domain names, define separate normalization rules and fixtures; this example does not automatically broaden its acceptance rules.

Prove the rules work with rejection fixtures

Append the following assertions to the same file. A successful run produces no output. If any assertion fails, the process exits with a failure status, making it easy to integrate into existing checks.

let hosts: Set<String> = ["sudomini.com"]

let accepted = [
    "https://sudomini.com/report",
    "HTTPS://SUDOMINI.COM/report",
    "https://sudomini.com:443/report?q=a%20b"
]

let rejected = [
    "//sudomini.com/report",
    "javascript:alert(1)",
    "https://user:support@sudomini.com/report",
    "https://sudomini.com:444/report",
    "https://127.0.0.1/report",
    "https://sudomini.com/report%0A",
    "https://sudomini.com/%5Cprivate",
    "https://sudomini.com/report%"
]

for raw in accepted {
    precondition(approvedLink(raw, hosts: hosts) != nil)
}

for raw in rejected {
    precondition(approvedLink(raw, hosts: hosts) == nil)
}

Run:

xcrun swift LinkPolicy.swift

Do not stop at checking for a non-nil result. Project tests should also assert the returned URL's scheme, host, port, and query contents. The goal is not to prove that “every strange URL is blocked,” but to show that parsing details cannot bypass the explicit rules.

Add separate fixtures for double encoding, too. A single screening pass does not recursively interpret every percent-encoded sequence. The essential constraint is that, after validation, the destination must not be decoded again, concatenated with other content, or rewritten. Server-side redirects are a separate boundary: this function does not guarantee that redirects subsequently returned by the destination server will remain within the allowlist.

Use the same policy for rendering and taps

When rendering rich text, extract each link destination and call the policy function before making it clickable. Rejected links can remain as plain text, but must not leave behind an invisible tap target.

At tap time, check the actual destination supplied by the callback again. Do not decide where to open based on the text shown in the UI, and do not return to the original raw string after validation succeeds. Wrap the system's URL-opening action in a replaceable dependency so tests can record the URL passed to it without actually launching a browser.

For mutable message models, add a regression test: display a valid link, update the message content, and then trigger a tap. Assert that the current valid destination is opened or that the action is explicitly rejected. The app must not keep using a stale cached destination.

If the component has its own default link-opening behavior, intercept it according to its callback contract. Otherwise, the test may record a rejection while the component still opens the page on its own.

Add security assertions to UI acceptance tests

Once the command-line checks pass, test three groups of scenarios in the iOS simulator: a valid link triggers exactly one open call; a rejected link triggers no open calls; and long-press menus, accessibility activation, and ordinary taps all follow the same rules. If copying a link address is a product feature, specify that it copies the actual destination rather than the display text.

Also prepare samples whose display text differs from their destination, and confirm that the UI does not treat a friendly label as evidence of safety. Error messages must be accessible to assistive technologies, and logs must not include complete sensitive query parameters.

For each regression run, retain the toolchain version, fixture version, and failing sample IDs; there is no need to retain users' original messages. Finally, report pure-function tests and UI checks separately: a failure in the former indicates a policy regression, while a failure in the latter indicates an integration bypass. This distinguishes policy errors from errors in the tap-handling path instead of grouping everything under “link won't open.”

Frequently asked questions

Is a URL safe to open if Foundation can parse it?

No. Validate the scheme, exact approved host, user information and port separately, and reject control characters in the input.

Should a link be checked again when tapped?

Yes. Validate the actual tap target and open only the URL returned by the policy. Do not reuse the raw string or decode it again afterward.

A dedicated Mac for every test lane

Rent a full physical Mac mini by the day, week, month, or quarter, with isolated test environments. Track setup progress in the console; service stops at the end of your term unless you renew. Submit a support ticket in the console if you need help.

Rent a Cloud Mac

Set up a Mac for your next test lane

Get an entire physical Mac mini to yourself and rent it for your test cycle. Track setup progress in the console; service stops at the end of your term unless you renew.