diff --git a/README.md b/README.md index d4955c6..6b0feaa 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,14 @@ a text editor. The short domain must use Cloudflare as its authoritative DNS pro Follow the [documentation](https://vanityurls.link/en/docs/quickstart) to spin your vanityURLs' instance. +## Remote link submission (optional) + +GitLab-hosted instances can add short links from a browser at `/add`, without a local checkout: submitting a URL there +picks a collision-free random slug (or a self-chosen one), returns it immediately, and triggers a GitLab pipeline that +runs `v8s-lnk` to append the link, commit, and push. It's opt-in (`links.remote_submission.enabled` in site config, off +by default) and the page should be placed behind its own Cloudflare Access Application, since it grants write access to +the repository. See `defaults/gitlab/.gitlab-ci.yml` for the required secrets and CI/CD variables. + ## Contributions [Contributions](.github/CONTRIBUTING.md) are welcome! We recognize diff --git a/defaults/gitlab/.gitlab-ci.yml b/defaults/gitlab/.gitlab-ci.yml new file mode 100644 index 0000000..a8416c5 --- /dev/null +++ b/defaults/gitlab/.gitlab-ci.yml @@ -0,0 +1,37 @@ +# vanityURLs remote link submission (/add) +# +# This job is only ever started by the Worker's /add/submit handler via the GitLab +# Pipeline Trigger API, which passes SUBMIT_URL and SUBMIT_SLUG as trigger variables. +# The workflow rule below stops a pipeline from being created at all for an ordinary +# push — including the push this job itself makes — since without it GitLab still spins +# up an empty pipeline for that commit (a job-level `rules:` only skips the job, not the +# pipeline shell). +# +# Setup: +# - A Pipeline Trigger Token, stored as the Cloudflare Worker secret GITLAB_TRIGGER_TOKEN. +# - A Project Access Token with `write_repository` scope, allowed to push to the default +# branch, stored as a masked CI/CD variable named GIT_PUSH_TOKEN. Only mark it "protected" +# too if the default branch is itself a protected branch — otherwise a protected variable +# is withheld from pipelines running on it and the push step fails with an empty token. +# - Optionally, a V8S_LINKS_OWNER CI/CD variable to attribute these commits to your usual +# owner tag (defaults to "bhd" otherwise, same as running v8s-lnk locally without it set). +workflow: + rules: + - if: "$SUBMIT_URL && $SUBMIT_SLUG" + +add-link: + image: node:24 + rules: + - if: "$SUBMIT_URL && $SUBMIT_SLUG" + script: + - npm ci + - git config user.email "vanityurls-bot@${CI_SERVER_HOST}" + - git config user.name "vanityURLs bot" + - git remote set-url origin "https://oauth2:${GIT_PUSH_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" + # GitLab Runner checks out a detached commit, not a branch. v8s-lnk's own `git push` (run + # below) expects a normal tracked branch, so set that up first: `push.default current` lets + # a plain `git push` push the current branch to a same-named remote branch with no upstream + # tracking metadata required. + - git checkout -B "$CI_COMMIT_REF_NAME" + - git config push.default current + - node scripts/v8s-lnk "$SUBMIT_URL" "$SUBMIT_SLUG" diff --git a/defaults/public/add/index.html b/defaults/public/add/index.html new file mode 100644 index 0000000..7eaa3f9 --- /dev/null +++ b/defaults/public/add/index.html @@ -0,0 +1,67 @@ + + +
+ + +${escapeHtml(LABELS.shortcode)}
++ ${escapeHtml(shortLink)} + +
+ + `; + + const copyButton = result.querySelector(".submit-copy"); + copyButton?.addEventListener("click", () => { + navigator.clipboard?.writeText(copyButton.dataset.copy || "").then(() => { + copyButton.textContent = LABELS.copied; + setTimeout(() => { + copyButton.textContent = LABELS.copy; + }, 2000); + }); + }); + } + + function renderMessage(result, message, isError) { + result.classList.toggle("is-error", Boolean(isError)); + result.classList.add("is-visible"); + result.innerHTML = ``; + } + + function escapeHtml(value) { + return String(value ?? "") + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); + } + + function escapeAttr(value) { + return escapeHtml(value); + } +})(); diff --git a/defaults/v8s-site-config.json b/defaults/v8s-site-config.json index e26aa60..c6aed74 100644 --- a/defaults/v8s-site-config.json +++ b/defaults/v8s-site-config.json @@ -10,6 +10,13 @@ "tag_random_slug_lengths": { "training": 4, "debug": 2 + }, + "remote_submission": { + "enabled": false, + "gitlab_project": "", + "gitlab_ref": "main", + "gitlab_api_base": "https://gitlab.com/api/v4", + "eta_minutes": 2 } }, "targets": { diff --git a/package.json b/package.json index 84981a5..eabbbc8 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "smoke": "npm run smoke:analytics", "smoke:analytics": "npm run build && node scripts/smoke-analytics.mjs", "test": "npm run test:all", - "test:all": "npm run test:worker && npm run test:runtime-registry && npm run test:install && npm run test:install-core && npm run test:detach && npm run test:maintenance && npm run test:upstream-release && npm run test:upgrade-source && npm run test:build-core && npm run test:build-html-core && npm run test:lookup-page && npm run test:target-normalizers && npm run test:first-party-links && npm run test:check-targets", + "test:all": "npm run test:worker && npm run test:runtime-registry && npm run test:install && npm run test:install-core && npm run test:detach && npm run test:maintenance && npm run test:upstream-release && npm run test:upgrade-source && npm run test:build-core && npm run test:build-html-core && npm run test:lookup-page && npm run test:target-normalizers && npm run test:first-party-links && npm run test:check-targets && npm run test:slug", + "test:slug": "node scripts/workers/lib/slug.test.mjs", "test:build-core": "node scripts/build-site-core.test.mjs", "test:build-html-core": "node scripts/build-html-core.test.mjs", "test:install": "node scripts/install.test.mjs", diff --git a/scripts/v8s-lnk b/scripts/v8s-lnk index 01a2d32..0543302 100755 --- a/scripts/v8s-lnk +++ b/scripts/v8s-lnk @@ -1,6 +1,5 @@ #!/usr/bin/env node -import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -9,6 +8,13 @@ import { stdin as input, stdout as output } from "node:process"; import { mergeSiteConfig } from "./lib/build-assets.mjs"; import { flattenRuntimeRegistry } from "./lib/runtime-registry.mjs"; import { commandLine, runCommand } from "./lib/run-command.mjs"; +import { + DEFAULT_RANDOM_SLUG_LENGTH, + DEFAULT_RANDOM_SLUG_ALPHABET, + randomSlug, + normalizeRandomSlugLength, + normalizeRandomSlugAlphabet +} from "./workers/lib/slug.mjs"; const ROOT = resolveRoot(); process.chdir(ROOT); @@ -18,9 +24,6 @@ const DEFAULT_OWNER = process.env.V8S_LINKS_OWNER || "bhd"; const LINKS_FILE = resolveLinksFile(); const DEFAULT_SITE_CONFIG_FILE = "defaults/v8s-site-config.json"; const CUSTOM_SITE_CONFIG_FILE = "custom/v8s-site-config.json"; -const DEFAULT_RANDOM_SLUG_LENGTH = 3; -const MAX_RANDOM_SLUG_LENGTH = 64; -const DEFAULT_RANDOM_SLUG_ALPHABET = "34789abcdefghjkmnpqrstvwxy"; const VALID_STATES = new Set(["permanent", "ephemeral", "expired", "disabled", "maintenance", "deactivated"]); function usage() { @@ -374,44 +377,6 @@ function replaceLine(filePath, index, line) { fs.writeFileSync(filePath, `${rows.join("\n").replace(/\n*$/g, "")}${hasTrailingNewline ? "\n" : ""}`); } -function randomSlug(length, alphabet) { - const size = normalizeRandomSlugLength(length, "random slug length"); - const characters = normalizeRandomSlugAlphabet(alphabet); - let slug = ""; - for (let index = 0; index < size; index += 1) { - slug += characters[crypto.randomInt(characters.length)]; - } - return slug; -} - -function normalizeRandomSlugLength(value, label = "random slug length") { - const number = Number.parseInt(String(value || ""), 10); - if (!Number.isInteger(number) || number < 1 || number > MAX_RANDOM_SLUG_LENGTH) { - throw new Error(`${label} must be an integer from 1 to ${MAX_RANDOM_SLUG_LENGTH}.`); - } - return number; -} - -function normalizeRandomSlugAlphabet(value) { - const alphabet = String(value || DEFAULT_RANDOM_SLUG_ALPHABET).trim(); - if (!alphabet) { - throw new Error("random_slug_alphabet cannot be empty."); - } - - const unique = [...new Set([...alphabet])].join(""); - if (unique !== alphabet) { - throw new Error("random_slug_alphabet must not contain duplicate characters."); - } - - for (const character of alphabet) { - if (!/^[A-Za-z0-9._~-]$/.test(character)) { - throw new Error(`Invalid random_slug_alphabet character: ${character}`); - } - } - - return alphabet; -} - function normalizeTag(value) { const tag = String(value || "") .trim() diff --git a/scripts/validate-lnk.mjs b/scripts/validate-lnk.mjs index 973b2b6..da6bed0 100644 --- a/scripts/validate-lnk.mjs +++ b/scripts/validate-lnk.mjs @@ -11,6 +11,7 @@ const allowedStates = new Set(["permanent", "ephemeral", "expired", "disabled", const targetRedirectStates = new Set(["permanent", "ephemeral"]); const reservedTopLevel = new Set([ "_stats", + "add", "assets", "lookup", "404", diff --git a/scripts/workers/lib/slug.mjs b/scripts/workers/lib/slug.mjs new file mode 100644 index 0000000..4682e86 --- /dev/null +++ b/scripts/workers/lib/slug.mjs @@ -0,0 +1,55 @@ +export const DEFAULT_RANDOM_SLUG_LENGTH = 3; +export const MAX_RANDOM_SLUG_LENGTH = 64; +export const DEFAULT_RANDOM_SLUG_ALPHABET = "34789abcdefghjkmnpqrstvwxy"; + +// Uses the Web Crypto `crypto.getRandomValues` global (available in both Node and the +// Workers runtime) instead of node:crypto's `randomInt`, so this module runs unchanged +// in scripts/v8s-lnk (Node) and scripts/workers/worker.mjs (Workers runtime). +function randomIndex(max) { + const range = 256 - (256 % max); + const bytes = new Uint8Array(1); + let value; + do { + crypto.getRandomValues(bytes); + value = bytes[0]; + } while (value >= range); + return value % max; +} + +export function randomSlug(length, alphabet) { + const size = normalizeRandomSlugLength(length, "random slug length"); + const characters = normalizeRandomSlugAlphabet(alphabet); + let slug = ""; + for (let index = 0; index < size; index += 1) { + slug += characters[randomIndex(characters.length)]; + } + return slug; +} + +export function normalizeRandomSlugLength(value, label = "random slug length") { + const number = Number.parseInt(String(value || ""), 10); + if (!Number.isInteger(number) || number < 1 || number > MAX_RANDOM_SLUG_LENGTH) { + throw new Error(`${label} must be an integer from 1 to ${MAX_RANDOM_SLUG_LENGTH}.`); + } + return number; +} + +export function normalizeRandomSlugAlphabet(value) { + const alphabet = String(value || DEFAULT_RANDOM_SLUG_ALPHABET).trim(); + if (!alphabet) { + throw new Error("random_slug_alphabet cannot be empty."); + } + + const unique = [...new Set([...alphabet])].join(""); + if (unique !== alphabet) { + throw new Error("random_slug_alphabet must not contain duplicate characters."); + } + + for (const character of alphabet) { + if (!/^[A-Za-z0-9._~-]$/.test(character)) { + throw new Error(`Invalid random_slug_alphabet character: ${character}`); + } + } + + return alphabet; +} diff --git a/scripts/workers/lib/slug.test.mjs b/scripts/workers/lib/slug.test.mjs new file mode 100644 index 0000000..5f22e76 --- /dev/null +++ b/scripts/workers/lib/slug.test.mjs @@ -0,0 +1,50 @@ +#!/usr/bin/env node + +import assert from "node:assert/strict"; +import { + DEFAULT_RANDOM_SLUG_ALPHABET, + MAX_RANDOM_SLUG_LENGTH, + normalizeRandomSlugAlphabet, + normalizeRandomSlugLength, + randomSlug +} from "./slug.mjs"; + +{ + assert.equal(normalizeRandomSlugLength("5"), 5); + assert.equal(normalizeRandomSlugLength(MAX_RANDOM_SLUG_LENGTH), MAX_RANDOM_SLUG_LENGTH); + assert.throws(() => normalizeRandomSlugLength(undefined), /random slug length must be an integer/); + assert.throws(() => normalizeRandomSlugLength("0"), /random slug length must be an integer/); + assert.throws( + () => normalizeRandomSlugLength(String(MAX_RANDOM_SLUG_LENGTH + 1)), + /random slug length must be an integer/ + ); + assert.throws(() => normalizeRandomSlugLength("abc"), /random slug length must be an integer/); +} + +{ + assert.equal(normalizeRandomSlugAlphabet(undefined), DEFAULT_RANDOM_SLUG_ALPHABET); + assert.equal(normalizeRandomSlugAlphabet(""), DEFAULT_RANDOM_SLUG_ALPHABET); + assert.equal(normalizeRandomSlugAlphabet("abc"), "abc"); + assert.throws(() => normalizeRandomSlugAlphabet(" "), /cannot be empty/); + assert.throws(() => normalizeRandomSlugAlphabet("aab"), /duplicate characters/); + assert.throws(() => normalizeRandomSlugAlphabet("ab|"), /Invalid random_slug_alphabet character/); +} + +{ + const alphabet = "ab"; + for (let attempt = 0; attempt < 50; attempt += 1) { + const slug = randomSlug(4, alphabet); + assert.equal(slug.length, 4); + assert.match(slug, /^[ab]{4}$/); + } +} + +{ + const slug = randomSlug(3, DEFAULT_RANDOM_SLUG_ALPHABET); + assert.equal(slug.length, 3); + for (const character of slug) { + assert.ok(DEFAULT_RANDOM_SLUG_ALPHABET.includes(character), `${character} should be in the default alphabet`); + } +} + +console.log("slug tests ok"); diff --git a/scripts/workers/worker.mjs b/scripts/workers/worker.mjs index 87875de..208f587 100644 --- a/scripts/workers/worker.mjs +++ b/scripts/workers/worker.mjs @@ -1,4 +1,11 @@ import { detectBot } from "./lib/analytics-policy.mjs"; +import { + DEFAULT_RANDOM_SLUG_ALPHABET, + DEFAULT_RANDOM_SLUG_LENGTH, + normalizeRandomSlugAlphabet, + normalizeRandomSlugLength, + randomSlug +} from "./lib/slug.mjs"; const ASSET_EXT_RE = /\.(html|css|js|mjs|map|json|png|svg|ico|webmanifest|txt|xml|woff2?|ttf|otf|eot)$/i; @@ -21,6 +28,7 @@ const WEEKDAY_ALIASES = { // Cached per Worker isolate; reset on failed reads so a later request can recover after a fixed deployment. let runtimeBlocklistPromise; +let siteConfigPromise; // JWKS is cached per isolate with a TTL so Access signing-key rotation is picked // up without waiting for isolate recycling. Successful fetches are cached; // failures are not, so transient cert endpoint errors can recover on retry. @@ -55,6 +63,14 @@ const SECURITY_HEADERS = { let customAssetsPromise; +// Test-only: forces the next request to refetch per-isolate caches instead of reusing whatever +// was cached by an earlier request in the same test process. +export function __resetWorkerCachesForTests() { + runtimeBlocklistPromise = null; + siteConfigPromise = null; + customAssetsPromise = null; +} + export default { async fetch(request, env, ctx) { const response = await handleRequest({ request, env, ctx }); @@ -88,11 +104,20 @@ async function withSecurityHeaders(response, context) { async function isCustomHtmlAsset(assetPath, response, { request, env }) { if (!assetPath || !isHtmlResponse(response)) return false; + // The submit page's fetch() to /add/submit must run same-origin so the browser sends the + // Cloudflare Access session cookie with it. A sandboxed (opaque-origin) CSP would turn that + // same-host fetch into a cross-origin one, whose preflight Access always rejects (preflight + // requests never carry cookies), breaking submission entirely. + if (isAddPageAssetPath(assetPath)) return false; const customAssets = await loadCustomAssets(request, env); return customAssets.has(assetPath); } +function isAddPageAssetPath(assetPath) { + return assetPath === "/add/index.html" || /^\/[a-z]{2}\/add\/index\.html$/.test(assetPath); +} + function isHtmlResponse(response) { const contentType = response.headers.get("content-type") || ""; return contentType.toLowerCase().startsWith("text/html"); @@ -173,7 +198,7 @@ async function handleRequest(context) { } if (isProtectedPath(slug)) { - const accessResponse = await requireCloudflareAccess(request, env); + const accessResponse = await requireCloudflareAccess(request, env, accessAudFor(slug, env)); if (accessResponse) return accessResponse; } @@ -185,6 +210,10 @@ async function handleRequest(context) { return renderLookupResponse(request, env); } + if (slug === "add/submit") { + return handleLinkSubmission(request, env); + } + if (request.method !== "GET" && request.method !== "HEAD") { return methodNotAllowedResponse(); } @@ -207,6 +236,10 @@ async function handleRequest(context) { return renderAsset(request, env, statsAssetPath, 200, ctx); } + if (slug === "add") { + return renderSubmitPage(request, env, ctx, correlationId); + } + const scannerProbe = await findScannerProbe(request, env); if (scannerProbe) { @@ -441,7 +474,21 @@ function isSecurityTxtPath(slug) { } function isProtectedPath(slug) { - return Boolean(statsPageAssetPath(slug)) || Boolean(localizedStatsApiEndpoint(slug)) || isTestsPath(slug); + return ( + Boolean(statsPageAssetPath(slug)) || + Boolean(localizedStatsApiEndpoint(slug)) || + isTestsPath(slug) || + isSubmitPath(slug) + ); +} + +function isSubmitPath(slug) { + return slug === "add" || slug === "add/submit"; +} + +function accessAudFor(slug, env) { + if (isSubmitPath(slug)) return env.CF_ACCESS_SUBMIT_AUD || env.CF_ACCESS_AUD; + return env.CF_ACCESS_AUD; } function isTestsPath(slug) { @@ -528,6 +575,189 @@ async function readJsonBody(request) { } } +async function renderSubmitPage(request, env, ctx, correlationId) { + const config = await loadSiteConfig(request, env); + if (!config.links?.remote_submission?.enabled) { + return render404(request, env, { slug: "add", correlationId }); + } + + return renderAsset(request, env, "/add/index.html", 200, ctx); +} + +async function handleLinkSubmission(request, env) { + const headers = { + "content-type": "application/json; charset=utf-8", + "cache-control": "no-store", + "x-robots-tag": "noindex, nofollow" + }; + + if (request.method !== "POST") { + return new Response(JSON.stringify({ error: "Method not allowed" }), { status: 405, headers }); + } + + const config = await loadSiteConfig(request, env); + const remoteSubmission = config.links?.remote_submission; + + if (!remoteSubmission?.enabled) { + return new Response(JSON.stringify({ error: "Not found" }), { status: 404, headers }); + } + + if (!env.GITLAB_READ_TOKEN || !env.GITLAB_TRIGGER_TOKEN || !remoteSubmission.gitlab_project) { + return new Response(JSON.stringify({ error: "Remote submission is not fully configured" }), { + status: 503, + headers + }); + } + + const body = await readJsonBody(request); + const targetUrl = String(body.url || "").trim(); + const requestedSlug = String(body.slug || "").trim(); + + if (!isSafeSubmissionUrl(targetUrl)) { + return new Response(JSON.stringify({ error: "Enter a valid http(s) URL" }), { status: 400, headers }); + } + + let slug; + try { + slug = requestedSlug + ? await reserveCustomSlug(env, remoteSubmission, requestedSlug) + : await pickUnusedSlug(env, config, remoteSubmission); + } catch (error) { + return new Response(JSON.stringify({ error: error.message }), { status: error.status || 502, headers }); + } + + let pipeline; + try { + pipeline = await triggerGitLabPipeline(env, remoteSubmission, { url: targetUrl, slug }); + } catch (error) { + return new Response(JSON.stringify({ error: error.message }), { status: 502, headers }); + } + + return new Response( + JSON.stringify({ + slug, + pipelineUrl: pipeline.web_url || null, + etaMinutes: remoteSubmission.eta_minutes || 2 + }), + { status: 200, headers } + ); +} + +function isSafeSubmissionUrl(value) { + try { + const parsed = new URL(value); + return SAFE_REDIRECT_PROTOCOLS.has(parsed.protocol); + } catch { + return false; + } +} + +// Reserved so a self-service submission can never shadow one of the site's own functional +// routes (the link would just become unreachable behind the route, not a security issue, but +// confusing). Kept in sync with the slugs isProtectedPath/isPublicCorsEndpoint/isSecurityTxtPath +// treat specially. +const RESERVED_SUBMIT_SLUG_SEGMENTS = new Set(["add", "lookup", "_stats", "_tests", "_analytics", ".well-known"]); + +async function reserveCustomSlug(env, remoteSubmission, rawSlug) { + const slug = normalizeSubmittedSlug(rawSlug); + + if (!isValidSubmittedSlug(slug)) { + throw submissionError("Use only letters, numbers, and - . _ ~ (no leading/trailing slash)", 400); + } + + if (RESERVED_SUBMIT_SLUG_SEGMENTS.has(slug.split("/")[0])) { + throw submissionError("This short link is reserved, please choose another one", 400); + } + + const existingSlugs = await loadGitLabLinkSlugs(env, remoteSubmission); + if (existingSlugs.has(slug)) { + throw submissionError("This short link is already taken", 409); + } + + return slug; +} + +function normalizeSubmittedSlug(value) { + return String(value || "") + .trim() + .replace(/^\/+/, "") + .replace(/\/+$/, ""); +} + +function isValidSubmittedSlug(slug) { + if (!slug || slug.includes("//") || slug.includes("?") || slug.includes("#") || slug.includes("|")) return false; + return slug.split("/").every((part) => /^[A-Za-z0-9][A-Za-z0-9._~-]*$/.test(part)); +} + +function submissionError(message, status) { + const error = new Error(message); + error.status = status; + return error; +} + +async function pickUnusedSlug(env, config, remoteSubmission) { + const existingSlugs = await loadGitLabLinkSlugs(env, remoteSubmission); + const length = normalizeRandomSlugLength(config.links?.random_slug_length || DEFAULT_RANDOM_SLUG_LENGTH); + const alphabet = normalizeRandomSlugAlphabet(config.links?.random_slug_alphabet || DEFAULT_RANDOM_SLUG_ALPHABET); + + for (let attempt = 0; attempt < 5; attempt += 1) { + const candidate = randomSlug(length, alphabet); + if (!existingSlugs.has(candidate)) return candidate; + } + + throw new Error("Could not find an unused short code, please try again"); +} + +async function loadGitLabLinkSlugs(env, remoteSubmission) { + const projectPath = encodeURIComponent(remoteSubmission.gitlab_project); + const filePath = encodeURIComponent("custom/v8s-links.txt"); + const ref = encodeURIComponent(remoteSubmission.gitlab_ref || "main"); + const apiUrl = `${remoteSubmission.gitlab_api_base}/projects/${projectPath}/repository/files/${filePath}/raw?ref=${ref}`; + + const response = await fetch(apiUrl, { + headers: { "PRIVATE-TOKEN": env.GITLAB_READ_TOKEN } + }); + + if (!response.ok) { + throw new Error(`Unable to read the current links file (GitLab responded ${response.status})`); + } + + const text = await response.text(); + const slugs = new Set(); + + for (const line of text.split(/\r?\n/)) { + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith("#")) continue; + const [slug] = trimmed.split("|"); + if (slug) slugs.add(slug); + } + + return slugs; +} + +async function triggerGitLabPipeline(env, remoteSubmission, { url, slug }) { + const projectPath = encodeURIComponent(remoteSubmission.gitlab_project); + const apiUrl = `${remoteSubmission.gitlab_api_base}/projects/${projectPath}/trigger/pipeline`; + + const form = new URLSearchParams(); + form.set("token", env.GITLAB_TRIGGER_TOKEN); + form.set("ref", remoteSubmission.gitlab_ref || "main"); + form.set("variables[SUBMIT_URL]", url); + form.set("variables[SUBMIT_SLUG]", slug); + + const response = await fetch(apiUrl, { + method: "POST", + headers: { "content-type": "application/x-www-form-urlencoded" }, + body: form.toString() + }); + + if (!response.ok) { + throw new Error(`Unable to start the publish pipeline (GitLab responded ${response.status})`); + } + + return response.json(); +} + async function findScannerProbe(request, env) { const policy = await loadRuntimeBlocklist(request, env); const keywords = scannerKeywords(policy); @@ -554,6 +784,21 @@ async function loadRuntimeBlocklist(request, env) { } } +async function loadSiteConfig(request, env) { + siteConfigPromise ||= (async () => { + const response = await fetchAsset(request, env, "/v8s-site-config.json"); + if (!response.ok) return {}; + return response.json(); + })(); + + try { + return await siteConfigPromise; + } catch { + siteConfigPromise = null; + return {}; + } +} + function scannerKeywords(policy) { const entries = Array.isArray(policy.blocked_keywords) ? policy.blocked_keywords : []; @@ -1092,9 +1337,8 @@ async function renderTestsPage(request, env, ctx) { return renderAsset(request, env, assetPath || "/_tests/index.html", 200, ctx); } -async function requireCloudflareAccess(request, env) { +async function requireCloudflareAccess(request, env, expectedAud = env.CF_ACCESS_AUD) { const teamDomain = normalizeAccessTeamDomain(env.CF_ACCESS_TEAM_DOMAIN); - const expectedAud = env.CF_ACCESS_AUD; if (!teamDomain || !expectedAud) { return protectedPathResponse("Cloudflare Access is not configured", 503); diff --git a/scripts/workers/worker.test.mjs b/scripts/workers/worker.test.mjs index 63a67e1..54c10e3 100644 --- a/scripts/workers/worker.test.mjs +++ b/scripts/workers/worker.test.mjs @@ -1,4 +1,4 @@ -import worker from "./worker.mjs"; +import worker, { __resetWorkerCachesForTests } from "./worker.mjs"; let analyticsCalls = []; const originalFetch = globalThis.fetch; @@ -122,6 +122,7 @@ const assets = { "/fr/maintenance.html": html("