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 @@ + + + + + + Add a short link | VanityURLs + + + + + + + + +
+
+ +

VanityURLs

+
+ +
+ + + + + +
+ +
+
+
+ + + diff --git a/defaults/public/v8s-submit.css b/defaults/public/v8s-submit.css new file mode 100644 index 0000000..433291e --- /dev/null +++ b/defaults/public/v8s-submit.css @@ -0,0 +1,94 @@ +.search-box--slug { + min-height: 46px; + margin-top: 12px; + padding-left: 18px; +} + +.search-box-prefix { + flex: 0 0 auto; + color: var(--subtle); + font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace; + font-size: 0.95rem; +} + +.search-box--slug input { + font-size: 0.95rem; +} + +.submit-result { + display: none; + width: min(100%, 620px); + border: 1px solid var(--border); + border-radius: 8px; + background: var(--panel); + padding: 18px 20px; + box-shadow: var(--shadow); + text-align: left; +} + +.submit-result.is-visible { + display: block; +} + +.submit-result.is-error { + border-color: var(--danger, #c0392b); +} + +.submit-label { + margin: 0 0 8px; + color: var(--muted); + font-size: 0.82rem; + font-weight: 700; + letter-spacing: 0; + text-transform: uppercase; +} + +.submit-shortcode { + display: flex; + align-items: center; + gap: 10px; + margin: 0; +} + +.submit-shortcode a { + overflow-wrap: anywhere; + color: var(--brand); + font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace; + font-size: 1.1rem; + font-weight: 650; + text-decoration: underline; + text-decoration-thickness: 1px; + text-underline-offset: 3px; +} + +.submit-copy { + border: 1px solid var(--border); + border-radius: 6px; + background: transparent; + color: var(--text); + font-size: 0.78rem; + padding: 4px 10px; + cursor: pointer; +} + +.submit-copy:hover { + background: var(--panel); +} + +.submit-meta { + margin: 12px 0 0; + color: var(--muted); + font-size: 0.9rem; + line-height: 1.55; +} + +.submit-meta a { + color: var(--brand); +} + +.submit-message { + margin: 0; + color: var(--text); + font-size: 0.95rem; + line-height: 1.55; +} diff --git a/defaults/public/v8s-submit.js b/defaults/public/v8s-submit.js new file mode 100644 index 0000000..01dd658 --- /dev/null +++ b/defaults/public/v8s-submit.js @@ -0,0 +1,165 @@ +(function submitBrowserCode() { + // Only en/fr are translated so far, unlike v8s-lookup.js's full en/fr/es/it/de set — es/it/de + // instances fall back to English below. Translation contributions welcome. + const SUBMIT_LABELS = { + en: { + empty: "Paste a URL to shorten", + invalid: "Enter a valid http(s) URL", + invalidSlug: "Use only letters, numbers, and - . _ ~", + error: "Unable to create the short link", + pending: "Creating your short link…", + shortcode: "Your new short link", + track: "Track the pipeline", + copy: "Copy", + copied: "Copied!", + eta: (minutes) => `Typically live within ~${minutes} minute${minutes === 1 ? "" : "s"}.` + }, + fr: { + empty: "Collez une URL à raccourcir", + invalid: "Entrez une URL http(s) valide", + invalidSlug: "Utilisez seulement des lettres, chiffres et - . _ ~", + error: "Impossible de créer le lien court", + pending: "Création du lien court…", + shortcode: "Votre nouveau lien court", + track: "Suivre le pipeline", + copy: "Copier", + copied: "Copié!", + eta: (minutes) => `Généralement en ligne d'ici ~${minutes} minute${minutes === 1 ? "" : "s"}.` + } + }; + + const submitLanguage = String(document.documentElement.lang || "en") + .toLowerCase() + .split("-")[0]; + const LABELS = SUBMIT_LABELS[submitLanguage] || SUBMIT_LABELS.en; + + document.addEventListener("DOMContentLoaded", () => { + const form = document.getElementById("submitForm"); + const input = document.getElementById("submitUrl"); + const slugInput = document.getElementById("submitSlug"); + const result = document.getElementById("submitResult"); + + if (!form || !input || !result) return; + + form.addEventListener("submit", async (event) => { + event.preventDefault(); + await submitUrl(form, input, slugInput, result); + }); + }); + + async function submitUrl(form, input, slugInput, result) { + const url = input.value.trim(); + + if (!url) { + renderMessage(result, LABELS.empty, true); + return; + } + + if (!isHttpUrl(url)) { + renderMessage(result, LABELS.invalid, true); + return; + } + + const slug = normalizeSlugInput(slugInput?.value); + + if (slug && !isValidSlugFormat(slug)) { + renderMessage(result, LABELS.invalidSlug, true); + return; + } + + const submitButton = form.querySelector("button[type=submit]"); + submitButton?.setAttribute("disabled", "disabled"); + renderMessage(result, LABELS.pending, false); + + try { + const response = await fetch("/add/submit", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(slug ? { url, slug } : { url }), + cache: "no-store" + }); + + const body = await response.json().catch(() => ({})); + + if (!response.ok || !body.slug) { + renderMessage(result, body.error || LABELS.error, true); + return; + } + + renderShortcode(result, body); + form.reset(); + } catch { + renderMessage(result, LABELS.error, true); + } finally { + submitButton?.removeAttribute("disabled"); + } + } + + function isHttpUrl(value) { + try { + const parsed = new URL(value); + return parsed.protocol === "http:" || parsed.protocol === "https:"; + } catch { + return false; + } + } + + function normalizeSlugInput(value) { + return String(value || "") + .trim() + .replace(/^\/+/, "") + .replace(/\/+$/, ""); + } + + function isValidSlugFormat(slug) { + if (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 renderShortcode(result, { slug, pipelineUrl, etaMinutes }) { + const shortLink = `${window.location.origin}/${slug}`; + + result.classList.remove("is-error"); + result.classList.add("is-visible"); + result.innerHTML = ` +

${escapeHtml(LABELS.shortcode)}

+

+ ${escapeHtml(shortLink)} + +

+

+ ${escapeHtml(etaMinutes ? LABELS.eta(etaMinutes) : "")} + ${pipelineUrl ? `${escapeHtml(LABELS.track)}` : ""} +

+ `; + + 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 = `

${escapeHtml(message)}

`; + } + + 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("
maintenance fr
"), "/fr/404.html": html("
fr {{SLUG_MESSAGE}}{{REFERENCE_LINE}}
"), "/_tests/index.html": html("
tests
"), + "/add/index.html": html("
add a link
"), "/en/_stats/index.html": html("
stats en
"), "/fr/_stats/index.html": html("
stats fr
"), "/.well-known/security.txt": new Response("Contact: mailto:security@example.com\n", { @@ -150,7 +151,7 @@ const assets = { "/v8s.json": Response.json(registry), "/v8s-custom-assets.json": Response.json({ schema_version: 1, - paths: ["/custom-page.html", "/custom-default-csp.html", "/v8s-style.css"] + paths: ["/custom-page.html", "/custom-default-csp.html", "/v8s-style.css", "/add/index.html"] }), "/v8s-blocklist.json": Response.json({ blocked_keywords: [ @@ -1738,4 +1739,314 @@ await run("throttles repeated unknown-kid refresh attempts", async () => { ); }); +function remoteSubmissionSiteConfig(overrides = {}) { + return { + links: { + random_slug_length: 1, + random_slug_alphabet: "b", + remote_submission: { + enabled: true, + gitlab_project: "felleg/f-l.ca3", + gitlab_ref: "main", + gitlab_api_base: "https://gitlab.example/api/v4", + eta_minutes: 2, + ...overrides + } + } + }; +} + +async function withSiteConfigAsset(config, fn) { + const previous = assets["/v8s-site-config.json"]; + assets["/v8s-site-config.json"] = config ? Response.json(config) : undefined; + __resetWorkerCachesForTests(); + + try { + return await fn(); + } finally { + assets["/v8s-site-config.json"] = previous; + __resetWorkerCachesForTests(); + } +} + +async function withGitLabFetch( + { + linksFileText = "", + readStatus = 200, + triggerStatus = 201, + triggerResponse = { id: 42, web_url: "https://gitlab.example/pipelines/42" } + } = {}, + fn +) { + const previousFetch = globalThis.fetch; + const calls = []; + + globalThis.fetch = async (url, init) => { + const href = typeof url === "string" ? url : url.toString(); + calls.push({ url: href, init }); + + if (href.includes("/repository/files/")) { + return new Response(linksFileText, { status: readStatus }); + } + + if (href.includes("/trigger/pipeline")) { + return new Response(JSON.stringify(triggerResponse), { + status: triggerStatus, + headers: { "content-type": "application/json" } + }); + } + + return previousFetch(url, init); + }; + + try { + return await fn(calls); + } finally { + globalThis.fetch = previousFetch; + } +} + +await run("returns 404 for GET /add when remote submission is not configured", async () => { + await withSiteConfigAsset(undefined, async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + request("/add", { headers: { ...(await accessHeaders()) } }), + await accessEnv(), + ctx + ); + assert(response.status === 404, "status"); + }); +}); + +await run("returns a 404 JSON error for POST /add/submit when remote submission is not configured", async () => { + await withSiteConfigAsset(undefined, async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest("/add/submit", { url: "https://example.com/new" }, { headers: { ...(await accessHeaders()) } }), + await accessEnv(), + ctx + ); + assert(response.status === 404, "status"); + const body = await response.json(); + assert(body.error, "error message present"); + }); +}); + +await run("protects /add with a dedicated Cloudflare Access audience when configured", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const ctx = mockCtx(); + const generalAudienceResponse = await worker.fetch( + request("/add", { headers: { ...(await accessHeaders()) } }), + await accessEnv({ CF_ACCESS_SUBMIT_AUD: "submit-aud-xyz" }), + ctx + ); + assert(generalAudienceResponse.status === 403, "general-audience token is rejected"); + + const submitAudienceResponse = await worker.fetch( + request("/add", { headers: { ...(await accessHeaders({ aud: ["submit-aud-xyz"] })) } }), + await accessEnv({ CF_ACCESS_SUBMIT_AUD: "submit-aud-xyz" }), + mockCtx() + ); + assert(submitAudienceResponse.status === 200, "dedicated-audience token is accepted"); + }); +}); + +await run("falls back to the general Cloudflare Access audience when no dedicated one is configured", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const response = await worker.fetch( + request("/add", { headers: { ...(await accessHeaders()) } }), + await accessEnv(), + mockCtx() + ); + assert(response.status === 200, "status"); + }); +}); + +await run("never sandboxes the /add page, even when served as a custom HTML asset", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const response = await worker.fetch( + request("/add", { headers: { ...(await accessHeaders()) } }), + await accessEnv(), + mockCtx() + ); + assert(response.status === 200, "status"); + const csp = response.headers.get("content-security-policy") || ""; + assert(!csp.includes("sandbox"), "add page CSP is not sandboxed"); + // A sandboxed (opaque-origin) /add page would make its own fetch("/add/submit") cross-origin, + // whose CORS preflight Access always rejects (preflight never carries cookies), breaking + // submission entirely — see isAddPageAssetPath in worker.mjs. + }); +}); + +await run("creates a short link via /add/submit and triggers the GitLab pipeline", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + await withGitLabFetch( + { linksFileText: "a|https://example.com/existing|permanent||||felleg||\n" }, + async (calls) => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest("/add/submit", { url: "https://example.com/new" }, { headers: { ...(await accessHeaders()) } }), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + + assert(response.status === 200, "status"); + const body = await response.json(); + assert(body.slug === "b", "slug is the only unused code in the configured alphabet"); + assert(body.pipelineUrl === "https://gitlab.example/pipelines/42", "pipeline url returned"); + assert(body.etaMinutes === 2, "eta minutes returned"); + + const readCall = calls.find((call) => call.url.includes("/repository/files/")); + assert(readCall, "links file was read"); + assert(readCall.init.headers["PRIVATE-TOKEN"] === "read-token-value", "read token header sent"); + + const triggerCall = calls.find((call) => call.url.includes("/trigger/pipeline")); + assert(triggerCall, "pipeline trigger was called"); + const form = new URLSearchParams(triggerCall.init.body); + assert(form.get("token") === "trigger-token-value", "trigger token sent"); + assert(form.get("variables[SUBMIT_URL]") === "https://example.com/new", "submit url variable sent"); + assert(form.get("variables[SUBMIT_SLUG]") === "b", "submit slug variable sent"); + } + ); + }); +}); + +await run("fails clearly when every candidate short code is already taken", async () => { + const exhaustedSlugConfig = remoteSubmissionSiteConfig(); + exhaustedSlugConfig.links.random_slug_alphabet = "a"; + + await withSiteConfigAsset(exhaustedSlugConfig, async () => { + await withGitLabFetch({ linksFileText: "a|https://example.com/existing|permanent||||felleg||\n" }, async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest("/add/submit", { url: "https://example.com/new" }, { headers: { ...(await accessHeaders()) } }), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + + assert(response.status === 502, "status"); + const body = await response.json(); + assert(/unused short code/.test(body.error), "clear error message"); + }); + }); +}); + +await run("rejects a non-http(s) submitted URL", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest("/add/submit", { url: "javascript:alert(1)" }, { headers: { ...(await accessHeaders()) } }), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + assert(response.status === 400, "status"); + }); +}); + +await run("uses a requested custom slug instead of generating a random one", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + await withGitLabFetch( + { linksFileText: "existing|https://example.com/existing|permanent||||felleg||\n" }, + async (calls) => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest( + "/add/submit", + { url: "https://example.com/new", slug: "my-custom-link" }, + { headers: { ...(await accessHeaders()) } } + ), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + + assert(response.status === 200, "status"); + const body = await response.json(); + assert(body.slug === "my-custom-link", "requested slug is used as-is"); + + const triggerCall = calls.find((call) => call.url.includes("/trigger/pipeline")); + const form = new URLSearchParams(triggerCall.init.body); + assert(form.get("variables[SUBMIT_SLUG]") === "my-custom-link", "custom slug variable sent"); + } + ); + }); +}); + +await run("rejects a custom slug with invalid characters", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest( + "/add/submit", + { url: "https://example.com/new", slug: "bad slug?" }, + { headers: { ...(await accessHeaders()) } } + ), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + assert(response.status === 400, "status"); + const body = await response.json(); + assert(body.error, "error message present"); + }); +}); + +await run("rejects a custom slug that would shadow a reserved route", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest( + "/add/submit", + { url: "https://example.com/new", slug: "add" }, + { headers: { ...(await accessHeaders()) } } + ), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + assert(response.status === 400, "status"); + }); +}); + +await run("rejects a custom slug that is already taken", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + await withGitLabFetch({ linksFileText: "taken|https://example.com/existing|permanent||||felleg||\n" }, async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest( + "/add/submit", + { url: "https://example.com/new", slug: "taken" }, + { headers: { ...(await accessHeaders()) } } + ), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + assert(response.status === 409, "status"); + }); + }); +}); + +await run("returns a clear error when GitLab fails to serve the links file", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + await withGitLabFetch({ readStatus: 500 }, async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest("/add/submit", { url: "https://example.com/new" }, { headers: { ...(await accessHeaders()) } }), + await accessEnv({ GITLAB_READ_TOKEN: "read-token-value", GITLAB_TRIGGER_TOKEN: "trigger-token-value" }), + ctx + ); + assert(response.status === 502, "status"); + }); + }); +}); + +await run("returns a configuration error when GitLab credentials are missing", async () => { + await withSiteConfigAsset(remoteSubmissionSiteConfig(), async () => { + const ctx = mockCtx(); + const response = await worker.fetch( + jsonRequest("/add/submit", { url: "https://example.com/new" }, { headers: { ...(await accessHeaders()) } }), + await accessEnv(), + ctx + ); + assert(response.status === 503, "status"); + }); +}); + globalThis.fetch = originalFetch;