import Foundation import XCTest @testable import CottonUI /// A saved web page (a Claude artifact) runs in a sealed web view: it reads /// its own file and nothing else on the device, and reaches the network only /// on the CDN hosts an artifact is allowed to draw from. That seal is a /// security rule (lane artifacts, 21 Oct 2026), so it is a guard. final class ViewerSealTests: XCTestCase { /// RULE: Every host a sealed page may load from is named here with the reason it is let in; adding one to `ViewerSeal.hosts` without adding it (and its reason) here fails this test. func testTheSealLetsInOnlyTheReasonedHosts() { let reasoned: [String: String] = [ // An artifact's libraries (React, D3, Chart.js…) — claude.ai's own allowlist. "cdnjs.cloudflare.com": "libraries", // npm packages served as files — claude.ai's own allowlist. "cdn.jsdelivr.net": "libraries", // npm packages served as files — claude.ai's own allowlist. "unpkg.com": "libraries", // Tailwind's play script — claude.ai's own allowlist. "cdn.tailwindcss.com": "styling", // jQuery — claude.ai's own allowlist. "code.jquery.com": "libraries", // Google Fonts stylesheets — the only stylesheets an artifact may load. "fonts.googleapis.com": "fonts", // The font files those stylesheets name. "fonts.gstatic.com": "fonts", ] XCTAssertEqual(Set(ViewerSeal.hosts), Set(reasoned.keys), "a sealed host without its reason here — write the reason, or take the host out") XCTAssertEqual(ViewerSeal.hosts.count, Set(ViewerSeal.hosts).count, "a host is listed twice") // The rule list blocks everything first, then lets back in only the // page's own file, inline data, and https to those hosts. let data = Data(ViewerSeal.ruleList.utf8) let rules = (try? JSONSerialization.jsonObject(with: data)) as? [[String: [String: String]]] ?? [] XCTAssertEqual(rules.first?["trigger"]?["url-filter"], ".*") XCTAssertEqual(rules.first?["action"]?["type"], "block") let letIn = rules.dropFirst().compactMap { $0["action"]?["type"] == "ignore-previous-rules" ? $0["trigger"]?["url-filter"] : nil } XCTAssertEqual(letIn.count, rules.count - 1, "a rule after the block that is not a let-in") let expected = ["^file:", "^data:", "^blob:"] + ViewerSeal.hosts.map { "^https://\($0.replacingOccurrences(of: ".", with: "\\."))[:/]" } XCTAssertEqual(letIn, expected) XCTAssertTrue(ViewerSeal.allows(URL(string: "https://cdn.jsdelivr.net/npm/react@18.3.1"))) XCTAssertFalse(ViewerSeal.allows(URL(string: "http://cdn.jsdelivr.net/npm/react@18.3.1")), "plain http is never let in") XCTAssertFalse(ViewerSeal.allows(URL(string: "https://evil.example/cdn.jsdelivr.net"))) XCTAssertFalse(ViewerSeal.allows(URL(string: "https://cdn.jsdelivr.net.evil.example/x"))) } /// RULE: The sealed web view loads the page from its local file only — `loadFileURL` with read access to that one file, a non-persistent data store and the rule list added before the load — and never from bytes, a string or an address. func testTheSealedPageLoadsItsOwnFileOnly() throws { let sources = try AppSources.all() let text = try XCTUnwrap(sources.first { $0.name.hasSuffix("CottonUI/Viewer/ViewerWeb.swift") }?.text, "ViewerWeb.swift moved — move this guard with it") let code = SourceSweep.withoutComments(text) XCTAssertTrue(code.contains("loadFileURL(file, allowingReadAccessTo: file)"), "the page no longer loads its own file with read access to that file alone") for forbidden in ["load(URLRequest", "loadHTMLString", ".load(_", "load(data", "loadFileRequest", "allowingReadAccessTo: file.deletingLastPathComponent"] { XCTAssertFalse(code.contains(forbidden), "the sealed page loads another way: \(forbidden)") } XCTAssertTrue(code.contains("websiteDataStore = .nonPersistent()"), "the sealed page keeps cookies or storage") let added = try XCTUnwrap(code.range(of: "userContentController.add(rules)"), "the rule list is no longer added") let loaded = try XCTUnwrap(code.range(of: "loadFileURL(")) XCTAssertLessThan(added.lowerBound, loaded.lowerBound, "the page loads before its seal is in place") } }