diff --git a/.github/workflows/native-links-live.yml b/.github/workflows/native-links-live.yml new file mode 100644 index 0000000..73e47fa --- /dev/null +++ b/.github/workflows/native-links-live.yml @@ -0,0 +1,30 @@ +name: Native link public HTTP acceptance + +on: + deployment_status: + workflow_dispatch: + +permissions: + contents: read + +jobs: + public-http: + if: >- + github.event_name == 'workflow_dispatch' || + (github.event.deployment_status.state == 'success' && + github.event.deployment.environment == 'Production' && + github.event.deployment.creator.login == 'vercel[bot]') + runs-on: ubuntu-24.04 + timeout-minutes: 3 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + ref: ${{ github.event.deployment.sha || github.sha }} + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: 22 + - name: Verify probe failure controls + run: node --test scripts/native-links-live.test.mjs + - name: Verify public association headers, bytes and fallbacks + run: node scripts/native-links-live.mjs diff --git a/.github/workflows/native-links.yml b/.github/workflows/native-links.yml index 3e47d0e..15c98b7 100644 --- a/.github/workflows/native-links.yml +++ b/.github/workflows/native-links.yml @@ -9,6 +9,9 @@ on: - vercel.json - scripts/native-links.test.mjs - scripts/android-development-association.test.mjs + - scripts/native-links-live.mjs + - scripts/native-links-live.test.mjs + - .github/workflows/native-links-live.yml - .github/workflows/native-links.yml push: branches: [main] @@ -19,6 +22,9 @@ on: - vercel.json - scripts/native-links.test.mjs - scripts/android-development-association.test.mjs + - scripts/native-links-live.mjs + - scripts/native-links-live.test.mjs + - .github/workflows/native-links-live.yml - .github/workflows/native-links.yml permissions: @@ -55,4 +61,4 @@ jobs: working-directory: website env: KEEPER_ANDROID_SOURCE: ${{ github.workspace }}/keeper-android-source - run: node --test scripts/native-links.test.mjs scripts/android-development-association.test.mjs + run: node --test scripts/native-links.test.mjs scripts/android-development-association.test.mjs scripts/native-links-live.test.mjs diff --git a/scripts/native-links-live.mjs b/scripts/native-links-live.mjs new file mode 100644 index 0000000..99a18b7 --- /dev/null +++ b/scripts/native-links-live.mjs @@ -0,0 +1,64 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { readFile } from "node:fs/promises"; +import { pathToFileURL } from "node:url"; + +const origin = "https://www.bitcoinkeeper.app"; +const dummyKey = "0123456789abcdef01234567"; + +export function verifyResponse(response, bytes, { path, status = 200, expectedBytes, json = false, html = false }) { + assert.equal(response.status, status, `${path}: HTTP status`); + assert.equal(response.redirected, false, `${path}: unexpected redirect`); + assert.equal(response.headers.get("location"), null, `${path}: unexpected Location header`); + assert.equal(response.url, `${origin}${path}`, `${path}: unexpected response URL`); + if (json) { + assert.match(response.headers.get("content-type") ?? "", /^application\/json(?:\s*;|$)/i, `${path}: JSON Content-Type`); + JSON.parse(bytes.toString("utf8")); + } + if (html) assert.match(response.headers.get("content-type") ?? "", /^text\/html(?:\s*;|$)/i, `${path}: HTML Content-Type`); + if (expectedBytes) assert.ok(bytes.equals(expectedBytes), `${path}: deployed bytes differ from source`); + return { + path, + status: response.status, + contentType: response.headers.get("content-type"), + redirected: response.redirected, + bytes: bytes.length, + sha256: createHash("sha256").update(bytes).digest("hex"), + ...(expectedBytes ? { sourceBytesMatch: true } : {}), + }; +} + +async function request(check) { + const response = await fetch(`${origin}${check.path}`, { + redirect: "manual", + signal: AbortSignal.timeout(15000), + headers: { "User-Agent": "Keeper-R4-native-link-acceptance" }, + }); + const bytes = Buffer.from(await response.arrayBuffer()); + const expectedBytes = check.file + ? await readFile(new URL(`../client/public/${check.file}`, import.meta.url)) + : undefined; + return verifyResponse(response, bytes, { ...check, expectedBytes }); +} + +async function main() { + const checks = [ + { path: "/.well-known/apple-app-site-association", file: ".well-known/apple-app-site-association", json: true }, + { path: "/.well-known/assetlinks.json", file: ".well-known/assetlinks.json", json: true }, + { path: `/app/prod/remote/${dummyKey}`, file: "remote-link.html", html: true }, + { path: `/app/dev/remote/${dummyKey}`, file: "remote-link-dev.html", html: true }, + { path: "/app/prod/remote/not-a-key", status: 404 }, + { path: `/app/prod/remote/${dummyKey}/extra`, status: 404 }, + { path: "/features", html: true }, + { path: "/ask-keeper", html: true }, + ]; + const results = await Promise.all(checks.map(request)); + console.log(JSON.stringify({ schema: 1, origin, state: "PUBLIC_HTTP_ACCEPTANCE_PASS", results }, null, 2)); +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + main().catch(error => { + console.error(error.message); + process.exitCode = 1; + }); +} diff --git a/scripts/native-links-live.test.mjs b/scripts/native-links-live.test.mjs new file mode 100644 index 0000000..fa4aaba --- /dev/null +++ b/scripts/native-links-live.test.mjs @@ -0,0 +1,42 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { verifyResponse } from "./native-links-live.mjs"; + +const path = "/.well-known/assetlinks.json"; +const bytes = Buffer.from('[{"public":"fixture"}]\n'); +const check = { path, json: true, expectedBytes: bytes }; +function response(overrides = {}) { + return { + status: 200, + redirected: false, + url: `https://www.bitcoinkeeper.app${path}`, + headers: new Headers({ "content-type": "application/json; charset=utf-8" }), + ...overrides, + }; +} + +test("live probe accepts direct JSON with exact source bytes", () => { + assert.equal(verifyResponse(response(), bytes, check).sourceBytesMatch, true); +}); +test("live probe rejects redirects and a different final origin", () => { + assert.throws(() => verifyResponse(response({ status: 307 }), bytes, check)); + assert.throws(() => verifyResponse(response({ redirected: true }), bytes, check)); + assert.throws(() => verifyResponse(response({ headers: new Headers({ location: "https://example.com" }) }), bytes, check)); + assert.throws(() => verifyResponse(response({ url: `https://example.com${path}` }), bytes, check)); +}); +test("live probe rejects HTML, invalid JSON and stale deployment bytes", () => { + assert.throws(() => verifyResponse(response({ headers: new Headers({ "content-type": "text/html" }) }), bytes, check)); + assert.throws(() => verifyResponse(response(), Buffer.from("not JSON"), check)); + assert.throws(() => verifyResponse(response(), Buffer.from("[]"), check)); +}); +test("live probe requires actual404 for malformed routes", () => { + assert.equal(verifyResponse(response({ status: 404 }), bytes, { path, status: 404 }).status, 404); + assert.throws(() => verifyResponse(response(), bytes, { path, status: 404 })); +}); +test("live probe requires HTML content type for browser fallbacks", () => { + const html = { path, html: true, expectedBytes: bytes }; + assert.equal(verifyResponse(response({ headers: new Headers({ "content-type": "text/html; charset=utf-8" }) }), bytes, html).sourceBytesMatch, true); + for (const contentType of ["text/plain", "application/octet-stream", "application/json"]) { + assert.throws(() => verifyResponse(response({ headers: new Headers({ "content-type": contentType }) }), bytes, html)); + } +});