SPB Git

Repository Public MIT

WebAndFileToolTests.swift 18.8 KB 319 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Tests/AstraAgentsTests/WebAndFileToolTests.swift10// Module           : AstraAgentsTests11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Testing90import AstraCore91@testable import AstraAgents9293@Suite struct WebHostPolicyTests {94    let tool = WebFetchTool(fetcher: FakeWeb(html: "<p>ok</p>"))9596    func refusal(_ url: String, tool: WebFetchTool? = nil) -> String? {97        do { try (tool ?? self.tool).preflight(["url": .string(url)]); return nil }98        catch { return (error as? ToolError)?.message ?? "\(error)" }99    }100101    @Test(arguments: [102        "http://example.com/", "ftp://example.com/file", "file:///etc/passwd", "javascript:alert(1)", "data:text/html,hi",103        "https://localhost/", "https://LOCALHOST/admin", "https://app.localhost/", "https://printer.local/", "https://db.internal/",104        "https://router.lan/", "https://nas.home.arpa/", "https://intranet/", "https://127.0.0.1/", "https://127.1.2.3/",105        "https://10.0.0.5/", "https://10.255.255.255/x", "https://192.168.1.1/", "https://172.16.0.1/", "https://172.31.255.254/",106        "https://169.254.169.254/latest/meta-data/", "https://100.64.0.1/", "https://100.127.255.255/", "https://0.0.0.0/",107        "https://224.0.0.1/", "https://255.255.255.255/", "https://198.18.0.1/", "https://192.0.0.8/",108        "https://[::1]/", "https://[::]/", "https://[fe80::1]/", "https://[fd00::1]/", "https://[fc00::abcd]/", "https://[ff02::1]/",109        "https://[::ffff:127.0.0.1]/", "https://[::ffff:10.0.0.1]/", "https://[::ffff:c0a8:0101]/", "https://[64:ff9b::7f00:1]/",110        "https://[2002:7f00:1::]/", "https://[::127.0.0.1]/",111        "https://2130706433/", "https://0x7f000001/", "https://0x7f.0.0.1/", "https://0177.0.0.1/", "https://127.1/", "https://017700000001/",112        "https://user:pass@example.com/", "https://admin@example.com/", "https://example.com:8443/", "https://example.com:22/",113        "https://example.com:80/", "https:///nohost", "not a url", "example.com", "",114    ])115    func refuses(_ url: String) { #expect(refusal(url) != nil, "\(url) must be refused") }116117    @Test(arguments: [118        "https://example.com/", "https://example.com:443/path?q=1", "https://sub.domain.example.co.uk/a/b", "https://93.184.216.34/",119        "https://8.8.8.8/dns", "https://[2606:2800:220:1:248:1893:25c8:1946]/", "https://EXAMPLE.com/", "https://example.com./",120        "https://172.32.0.1/", "https://100.128.0.1/", "https://11.0.0.1/", "https://192.169.0.1/", "https://xn--bcher-kva.example/",121    ])122    func allows(_ url: String) { #expect(refusal(url) == nil, "\(url) should be allowed") }123124    @Test func reasonsAreSpecific() {125        #expect(refusal("http://example.com/") == "Only https URLs can be fetched.")126        #expect(refusal("https://127.0.0.1/") == "Private, loopback and link-local addresses cannot be fetched.")127        #expect(refusal("https://localhost/") == "Local hosts cannot be fetched.")128        #expect(refusal("https://example.com:8443/") == "Port 8443 is not allowed; only the default https port is.")129        #expect(refusal("https://u:p@example.com/") == "URLs that carry credentials are not allowed.")130    }131132    @Test func extraPortsOnlyWhenExplicitlyAllowed() {133        let permissive = WebFetchTool(fetcher: FakeWeb(html: ""), policy: WebHostPolicy(allowedPorts: [8443]))134        #expect(refusal("https://example.com:8443/", tool: permissive) == nil)135        #expect(refusal("https://example.com:9000/", tool: permissive) != nil)136        #expect(refusal("https://127.0.0.1:8443/", tool: permissive) != nil)        // a port exception never unlocks a private host137    }138139    @Test func blockedAddressHelperForResolvedAddresses() {140        for address in ["127.0.0.1", "10.1.2.3", "192.168.0.10", "172.20.1.1", "169.254.1.1", "100.100.100.100", "0.0.0.0", "::1", "fe80::1%en0",141                        "fd12:3456::1", "::ffff:192.168.1.1", "not-an-address", ""] {142            #expect(WebHostPolicy.isBlockedAddress(address), "\(address)")143        }144        for address in ["93.184.216.34", "1.1.1.1", "2606:4700:4700::1111", "172.15.0.1"] { #expect(!WebHostPolicy.isBlockedAddress(address), "\(address)") }145    }146147    @Test func scopeIsTheNormalisedHost() {148        #expect(tool.approvalScope(["url": "https://News.Example.com./a?b=1"]) == "news.example.com")149        #expect(tool.approvalScope(["url": "nonsense"]) == nil)150        #expect(tool.risk == .sendsDataExternally)151        #expect(tool.approvalSummary(["url": "https://example.com/a?api_key=abcdef123456"])?.contains("abcdef123456") == false)152    }153}154155@Suite struct WebFetchToolTests {156    static let page = """157    <!doctype html><html><head><title>Rates &amp; Fees — Bank</title><style>body { color: red; } .x::after { content: "<b>"; }</style>158    <script>var secret = "</p><p>injected"; if (a < b) { fetch("https://evil.example/") }</script></head>159    <body><!-- hidden <p>comment</p> --><h1>Rates</h1><p>The   rate is&nbsp;<b>4.5&#37;</b> &lt;fixed&gt; &eacute;t&#xE9;.</p>160    <ul><li>First</li><li>Second <a href="https://example.com/?a=1&amp;b=2" title="a > b">link</a></li></ul>161    <table><tr><td>A</td><td>B</td></tr></table><noscript>enable js</noscript><svg><text>vector</text></svg>162    <p>5 < 6 and 7 > 3</p></body></html>163    """164165    @Test func htmlBecomesReadableText() {166        let text = HTMLText.extract(Self.page)167        #expect(text == "Rates\n\nThe rate is 4.5% <fixed> été.\n\n• First\n• Second link\n\nA B\n\n5 < 6 and 7 > 3")168        #expect(HTMLText.title(Self.page) == "Rates & Fees — Bank")169        for hidden in ["secret", "injected", "color", "comment", "enable js", "vector", "evil.example", "Bank"] { #expect(!text.contains(hidden), "\(hidden)") }170    }171172    @Test func htmlEdgeCases() {173        #expect(HTMLText.extract("plain text, no tags") == "plain text, no tags")174        #expect(HTMLText.extract("<script>never closed") == "")175        #expect(HTMLText.extract("a<br>b<BR/>c") == "a\nb\nc")176        #expect(HTMLText.extract("<p>unterminated <b") == "unterminated")177        #expect(HTMLText.extract("&amp;amp; &unknown; &#0; &#x1F600; &") == "&amp; &unknown; \u{0} 😀 &")178        #expect(HTMLText.extract("<SCRIPT type='x'>a</SCRIPT>visible<STYLE>b</STYLE>") == "visible")179        #expect(HTMLText.extract("<div>\n\n\n  a  \n\n\n\n  b  </div>") == "a\n\nb")180        #expect(HTMLText.extract(String(repeating: "<div>", count: 50_000) + "deep").hasSuffix("deep"))      // linear, no recursion181        #expect(HTMLText.title("<p>no title</p>") == nil)182    }183184    @Test func fetchesAndExtracts() async throws {185        let web = FakeWeb(html: Self.page)186        let out = try await runTool(WebFetchTool(fetcher: web), ["url": "https://bank.example.com/rates"])187        #expect(out.hasPrefix("URL: https://bank.example.com/rates\nTitle: Rates & Fees — Bank\n\nRates\n\nThe rate is 4.5%"))188        #expect(web.urls.map(\.absoluteString) == ["https://bank.example.com/rates"])189    }190191    @Test func privateTargetsNeverReachTheFetcher() async {192        let web = FakeWeb(html: "x")193        let tool = WebFetchTool(fetcher: web)194        for url in ["https://127.0.0.1/", "http://example.com/", "https://169.254.169.254/latest/meta-data/", "https://router.local/"] {195            #expect(await toolError(tool, ["url": .string(url)]) != nil)196            // even when a caller skips preflight197            await #expect(throws: ToolError.self) { try await tool.run(["url": .string(url)], context: ToolContext()) }198        }199        #expect(web.urls.isEmpty)200    }201202    @Test func redirectsAreRevalidated() async {203        // An implementation that follows the contract asks the validator before following a redirect.204        let obedient = FakeWeb { request in205            let target = URL(string: "https://192.168.1.1/admin")!206            if let reason = request.validate(target) { throw ToolError("Redirect refused. \(reason)") }207            return WebFetchResponse(status: 200, contentType: "text/plain", body: Data("router".utf8), finalURL: target)208        }209        #expect(await toolError(WebFetchTool(fetcher: obedient), ["url": "https://example.com/r"])?.contains("Redirect refused. Private, loopback") == true)210        // One that does not is still caught by the check on the final URL.211        let careless = FakeWeb { _ in WebFetchResponse(status: 200, contentType: "text/plain", body: Data("router".utf8), finalURL: URL(string: "https://10.0.0.1/")!) }212        let message = await toolError(WebFetchTool(fetcher: careless), ["url": "https://example.com/r"])213        #expect(message?.contains("redirected to an address that is not allowed") == true)214        #expect(message?.contains("router") == false)215        let downgraded = FakeWeb { _ in WebFetchResponse(status: 200, contentType: "text/plain", body: Data("x".utf8), finalURL: URL(string: "http://example.com/")!) }216        #expect(await toolError(WebFetchTool(fetcher: downgraded), ["url": "https://example.com/r"])?.contains("Only https") == true)217        // A redirect to another public https host is the validator's call: it accepts it.218        let moved = FakeWeb { request in219            let target = URL(string: "https://www.example.org/new")!220            #expect(request.validate(target) == nil)221            return WebFetchResponse(status: 200, contentType: "text/plain", body: Data("moved".utf8), finalURL: target)222        }223        #expect(await toolError(WebFetchTool(fetcher: moved), ["url": "https://example.com/r"]) == nil)224    }225226    @Test func statusContentTypeAndSizeCaps() async throws {227        let notFound = FakeWeb { WebFetchResponse(status: 404, contentType: "text/html", body: Data(), finalURL: $0.url) }228        #expect(await toolError(WebFetchTool(fetcher: notFound), ["url": "https://example.com/x"]) == "The server answered HTTP 404.")229        let binary = FakeWeb { WebFetchResponse(status: 200, contentType: "application/zip", body: Data([0, 1, 2]), finalURL: $0.url) }230        #expect(await toolError(WebFetchTool(fetcher: binary), ["url": "https://example.com/x"])?.contains("not text") == true)231232        let big = FakeWeb { request in233            #expect(request.maxBytes == 1_000)234            #expect(request.timeout == 5)235            return WebFetchResponse(status: 200, contentType: "text/plain", body: Data(String(repeating: "é", count: 5_000).utf8), finalURL: request.url)236        }237        let tool = WebFetchTool(fetcher: big, maxBytes: 1_000, maxCharacters: 300, timeout: 5)238        let output = try await tool.run(["url": "https://example.com/big"], context: ToolContext())239        #expect(output.truncated)240        #expect(output.text.hasSuffix("[… page truncated]"))241        #expect(output.text.count < 400)242        #expect(!output.text.contains("Ã"))            // a cut UTF-8 sequence is not misread as Latin-1243244        let json = FakeWeb { WebFetchResponse(status: 200, contentType: "application/json", body: Data(#"{"a": "<b>kept</b>"}"#.utf8), finalURL: $0.url) }245        #expect(try await runTool(WebFetchTool(fetcher: json), ["url": "https://api.example.com/v1"]).hasSuffix(#"{"a": "<b>kept</b>"}"#))246        let failing = FakeWeb { _ in throw AstraError.timeout }247        await #expect(throws: AstraError.timeout) { try await runTool(WebFetchTool(fetcher: failing), ["url": "https://example.com/"]) }248    }249}250251@Suite struct FileToolTests {252    func files() -> FakeFiles {253        FakeFiles(items: [GrantedItem(id: "f1", name: "Notes.md", byteCount: 11, kind: "Markdown"), GrantedItem(id: "f2", name: "data.csv")],254                  contents: ["f1": "hello notes", "f2": "a,b\n1,2"])255    }256257    @Test func listsAndReadsOnlyGrantedItems() async throws {258        let store = files()259        let tool = ReadFileTool(files: store)260        #expect(tool.risk == .readsUserData)261        #expect(try await runTool(tool, [:]) == "Shared files (2):\n- id: f1 | name: Notes.md | 11 bytes | Markdown\n- id: f2 | name: data.csv")262        #expect(try await runTool(tool, ["item": "f1"]) == "File: Notes.md\n\nhello notes")263        #expect(try await runTool(tool, ["item": "data.csv"]) == "File: data.csv\n\na,b\n1,2")264        #expect(try await runTool(tool, ["item": "notes.MD"]).hasSuffix("hello notes"))265        #expect(store.reads == ["f1", "f2", "f1"])266    }267268    @Test func pathsAndUnknownNamesNeverReachTheAccessor() async {269        let store = files()270        let tool = ReadFileTool(files: store)271        for attempt in ["/etc/passwd", "../../Library/Preferences/x.plist", "~/Documents/secret.txt", "file:///private/var/mobile", "f3", "Notes.md/../x"] {272            #expect(await toolError(tool, ["item": .string(attempt)])?.contains("No shared file is called") == true, "\(attempt)")273        }274        #expect(store.reads.isEmpty)275        #expect(await toolError(tool, ["path": "/etc/passwd"])?.contains("not a known property") == true)276        #expect(try! await runTool(ReadFileTool(files: FakeFiles()), [:]).contains("No file has been shared"))277    }278279    @Test func readIsCapped() async throws {280        let store = FakeFiles(items: [GrantedItem(id: "big", name: "big.txt")], contents: ["big": String(repeating: "x", count: ReadFileTool.maxBytes + 100)])281        let output = try await ReadFileTool(files: store).run(["item": "big"], context: ToolContext())282        #expect(output.truncated)283        #expect(output.text.hasSuffix("[… file truncated at \(ReadFileTool.maxBytes) bytes]"))284    }285286    @Test func writeGoesThroughTheExporterWithAPlainName() async throws {287        let store = files()288        let tool = WriteFileTool(files: store)289        #expect(tool.risk == .writesUserData)290        #expect(try await runTool(tool, ["file_name": " summary.md ", "content": "# Hi"]) == "Saved as “summary.md” (4 bytes).")291        #expect(store.writes.count == 1 && store.writes[0].0 == "summary.md" && store.writes[0].1 == "# Hi")292        for bad in ["../evil.txt", "/etc/cron.d/x", "a/b.txt", "a\\b.txt", ".hidden", "~root", "x..y", "C:evil", "line\nbreak.txt", "   ", "nul\u{0}.txt"] {293            #expect(await toolError(tool, ["file_name": .string(bad), "content": "x"]) != nil, "\(bad)")294        }295        #expect(await toolError(tool, ["file_name": .string(String(repeating: "a", count: 130)), "content": "x"])?.contains("at most 120") == true)296        #expect(store.writes.count == 1)297        #expect(tool.approvalSummary(["file_name": "k.txt", "content": "password=hunter2hunter2 and more"])?.contains("hunter2") == false)298    }299300    @Test func cancelledExportIsAnError() async throws {301        let tool = WriteFileTool(files: FakeFiles(writeResult: .cancelled))302        let output = try await tool.run(["file_name": "a.txt", "content": "x"], context: ToolContext())303        #expect(output.isError && output.text.contains("not saved"))304    }305306    @Test func documentSearchNumbersAndCapsPassages() async throws {307        let hits = [DocumentHit(id: "1", title: "Lease.pdf", text: "  Rent is due on the first.  ", location: "p. 3", score: 0.9),308                    DocumentHit(id: "2", title: "Notes", text: String(repeating: "z", count: 5_000))]309        let tool = DocumentSearchTool(documents: FakeDocuments(hits: hits))310        let out = try await runTool(tool, ["query": "rent", "limit": 5])311        #expect(out.hasPrefix("[1] Lease.pdf, p. 3\nRent is due on the first.\n\n[2] Notes\nzzz"))312        #expect(out.contains("[… output truncated: 1500 of 5000 characters shown]"))313        #expect(try await runTool(DocumentSearchTool(documents: FakeDocuments(hits: [])), ["query": "x"]) == "No passage matched “x”.")314        #expect(try await runTool(tool, ["query": "rent", "limit": 1]).contains("[2]") == false)315        #expect(await toolError(tool, ["query": "x", "limit": 50])?.contains("at most 10") == true)316        #expect(await toolError(tool, ["query": "   "]) == "The query is empty.")317    }318}319
Files main
Menu

Theme