import test from "node:test"; import assert from "node:assert/strict"; import { artifactBrowserLinkProps, isLocalArtifactLink } from "../src/artifactNavigation.js"; const base = "https://pipelines.test/runs/18"; const environment = (userAgent, mode = "browser") => ({ location: { href: base }, navigator: { userAgent }, matchMedia: (query) => ({ matches: query === `(display-mode: ${mode})` }), }); test("ordinary browsers and iOS request a separate isolated context", () => { for (const env of [environment("Desktop Chrome"), environment("Android Chrome"), { ...environment("iPhone Safari"), navigator: { userAgent: "iPhone Safari", standalone: true } }]) { const props = artifactBrowserLinkProps("/artifacts/18/2/public/report.html", env); assert.equal(props.href, "https://pipelines.test/artifacts/18/2/public/report.html"); assert.equal(props.target, "_blank"); for (const relation of ["external", "noopener", "noreferrer"]) assert.ok(props.rel.includes(relation)); } }); test("installed Android apps request a browser VIEW hand-off, not a captured app navigation", () => { for (const mode of ["standalone", "fullscreen"]) { const props = artifactBrowserLinkProps("/artifacts/18/2/public/report.html?download=0#section", environment("Android Chrome", mode)); assert.match(props.href, /^intent:\/\//); const marker = props.href.lastIndexOf("#Intent;"); const fields = props.href.slice(marker); assert.ok(fields.includes("action=android.intent.action.VIEW;")); assert.ok(fields.includes("category=android.intent.category.BROWSABLE;")); assert.ok(fields.includes("package=com.android.chrome;"), "do not resolve back into the installed WebAPK"); const original = "https:" + props.href.slice("intent:".length, marker); assert.equal(original, "https://pipelines.test/artifacts/18/2/public/report.html?download=0#section"); const fallback = fields.match(/S\.browser_fallback_url=([^;]+);/)[1]; assert.equal(decodeURIComponent(fallback), original); assert.equal(props.target, "_blank"); } }); test("document fragments cannot inject intent actions or packages", () => { const props = artifactBrowserLinkProps("/artifacts/a.html#Intent;package=bad.app;action=unsafe;end", environment("Android", "standalone")); const fields = props.href.slice(props.href.lastIndexOf("#Intent;")); assert.ok(fields.includes(";package=com.android.chrome;")); assert.ok(!fields.includes(";package=bad.app;")); assert.ok(!fields.includes(";action=unsafe;")); assert.ok(fields.includes(";action=android.intent.action.VIEW;")); }); test("identified Android browser families do not get sent into the WebAPK", () => { for (const [agent,pkg] of [["Android EdgA/130","com.microsoft.emmx"],["Android SamsungBrowser/25","com.sec.android.app.sbrowser"],["Android OPR/80","com.opera.browser"],["Android Firefox/130","org.mozilla.firefox"]]) { const props=artifactBrowserLinkProps("/artifacts/a.html",environment(agent,"standalone")); assert.ok(props.href.includes(`;package=${pkg};`)); } }); test("artifact hand-offs reject blank, executable and credential-bearing URLs", () => { for (const url of [null, "", " ", "javascript:alert(1)", "data:text/html,bad", "file:///secret", "https://user:pass@pipelines.test/artifacts/a"]) { assert.equal(artifactBrowserLinkProps(url, environment("Android", "standalone")), null); } }); test("chat recognizes only genuine same-origin artifact links", () => { for (const url of ["/artifacts/18/2/public/report.html", "https://pipelines.test/artifacts/a.html"]) assert.equal(isLocalArtifactLink(url, base), true); for (const url of ["/runs/18", "//evil.test/artifacts/a", "/artifacts/../../admin", "https://pipelines.test.evil/artifacts/a", "javascript:alert(1)"]) assert.equal(isLocalArtifactLink(url, base), false); });