From 5dfaa393be19b3b19409cba23d9aa1754be7c260 Mon Sep 17 00:00:00 2001 From: Wolf Date: Tue, 29 Sep 2026 15:24:54 +0200 Subject: [PATCH] Guard every feed-, board-, discovery- or user-supplied link with safeUrl roles.js already checked a role's original_url is http(s) before putting it in an href, so a board that wrote something other than a URL could not make the reader navigate to it; sources.js (a search sample's row.url), companies.js (careers_url) and applications.js (application.url) put the same kind of outside data straight into a link unchecked. safeUrl moves to app.js, beside the app's other shared helpers, and is exported the same way; a link whose safeUrl comes back empty is now left out of the page (plain text) instead of getting an href of "". Co-Authored-By: Claude Sonnet --- app/static/app.js | 20 ++++++++++++++++---- app/static/applications.js | 7 ++++--- app/static/companies.js | 7 ++++--- app/static/roles.js | 4 +--- app/static/sources.js | 20 +++++++++++++------- docs/decisions.md | 2 ++ 6 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/static/app.js b/app/static/app.js index 2a96c17..f0eeb0f 100644 --- a/app/static/app.js +++ b/app/static/app.js @@ -14,9 +14,10 @@ * forgetting the header is the failure that looks like a server bug. * * Beside `api` on `window`, the few helpers more than one page script uses: - * `count`, `cell`, `debounce`, and the company list with its affinity control - * (`loadCompanies`, `companies`, `affinityControl`, `setAffinity`). One copy - * each, since two copies of a helper is one of them being fixed. + * `count`, `cell`, `debounce`, `safeUrl`, and the company list with its + * affinity control (`loadCompanies`, `companies`, `affinityControl`, + * `setAffinity`). One copy each, since two copies of a helper is one of them + * being fixed. * * Affinity is per search agent. The three company helpers take the agent's * id as an optional last argument and send it along; a caller that passes @@ -104,6 +105,17 @@ }; } + /** An `http(s)` URL, or "" for anything else — `javascript:`, a bare path, + empty. Every link whose address came from a feed, a board, discovery or + a person (a search result's `url`, a company's `careers_url`, a saved + application's `url`, a role's `original_url`) goes through this before + it lands in an `href`, so a board that wrote something other than a URL + cannot make this page navigate to it. Moved here from roles.js, which + first needed it for the reader's outbound links. */ + function safeUrl(u) { + return /^https?:\/\//i.test(u || "") ? u : ""; + } + // --------------------------------------------------------------- companies /** @type {Map} company_key -> row from /api/companies. One @@ -170,6 +182,6 @@ } Object.assign(window, { - api, count, cell, debounce, companies, loadCompanies, affinityControl, setAffinity, + api, count, cell, debounce, safeUrl, companies, loadCompanies, affinityControl, setAffinity, }); })(); diff --git a/app/static/applications.js b/app/static/applications.js index cb120ce..c6b8d6c 100644 --- a/app/static/applications.js +++ b/app/static/applications.js @@ -9,7 +9,7 @@ (function () { "use strict"; - const { api, count, cell, companies, loadCompanies } = window; + const { api, count, cell, safeUrl, companies, loadCompanies } = window; const $ = (sel, root = document) => root.querySelector(sel); // `ROLEDESK_STATUSES` is set by applications.html from the same `STATUSES` @@ -41,13 +41,14 @@ body.replaceChildren(); for (const application of applications) { const tr = document.createElement("tr"); - if (application.role_uid || application.url) { + const externalUrl = safeUrl(application.url); + if (application.role_uid || externalUrl) { const link = document.createElement("a"); link.textContent = application.role_title || "(untitled)"; if (application.role_uid) { link.href = `/roles/${encodeURIComponent(application.role_uid)}`; } else { - link.href = application.url; + link.href = externalUrl; link.target = "_blank"; link.rel = "noopener"; } diff --git a/app/static/companies.js b/app/static/companies.js index e7f2eb5..8848503 100644 --- a/app/static/companies.js +++ b/app/static/companies.js @@ -21,7 +21,7 @@ (function () { "use strict"; - const { api, count, debounce, companies, loadCompanies, affinityControl } = window; + const { api, count, debounce, safeUrl, companies, loadCompanies, affinityControl } = window; const $ = (sel, root = document) => root.querySelector(sel); /** @type {object[]} the search agents, by id */ @@ -76,9 +76,10 @@ // found where this company posts its roles. sub.append("board not found"); } - if (company.careers_url && !company.board) { + const careersUrl = safeUrl(company.careers_url); + if (careersUrl && !company.board) { const link = document.createElement("a"); - link.href = company.careers_url; + link.href = careersUrl; link.target = "_blank"; link.rel = "noopener"; link.textContent = "careers page"; diff --git a/app/static/roles.js b/app/static/roles.js index d6d9b53..ec30005 100644 --- a/app/static/roles.js +++ b/app/static/roles.js @@ -47,7 +47,7 @@ (function () { "use strict"; - const { api, esc, debounce, companies, loadCompanies, affinityControl, judgeButtons, + const { api, esc, debounce, safeUrl, companies, loadCompanies, affinityControl, judgeButtons, loadTracked } = window; const $ = (sel, root = document) => root.querySelector(sel); @@ -110,8 +110,6 @@ feedError: false, // the feed's own fetch failed; the empty state offers a retry }; - const safeUrl = (u) => (/^https?:\/\//i.test(u || "") ? u : ""); - function ago(iso) { const mins = (Date.now() - new Date(iso)) / 60000; if (!Number.isFinite(mins)) return ""; diff --git a/app/static/sources.js b/app/static/sources.js index 193318c..f6e15c9 100644 --- a/app/static/sources.js +++ b/app/static/sources.js @@ -14,7 +14,7 @@ (function () { "use strict"; - const { api, count, debounce } = window; + const { api, count, debounce, safeUrl } = window; const $ = (sel, root = document) => root.querySelector(sel); /** @type {{searches: object[], adzuna_key: boolean}} */ @@ -168,12 +168,18 @@ const list = $("#search-sample"); for (const row of result.sample) { const li = document.createElement("li"); - const link = document.createElement("a"); - link.href = row.url; - link.target = "_blank"; - link.rel = "noopener"; - link.textContent = row.title; - li.append(link, ` — ${[row.company, row.place].filter(Boolean).join(", ")}`); + const url = safeUrl(row.url); + if (url) { + const link = document.createElement("a"); + link.href = url; + link.target = "_blank"; + link.rel = "noopener"; + link.textContent = row.title; + li.appendChild(link); + } else { + li.append(row.title); + } + li.append(` — ${[row.company, row.place].filter(Boolean).join(", ")}`); list.appendChild(li); } } catch (err) { diff --git a/docs/decisions.md b/docs/decisions.md index ea1ed3c..0f4b001 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -154,6 +154,8 @@ What was decided for roledesk and the one reason for each. The longer reasoning **Roles are a feed with a reader, not links out.** `GET /roles/{uid}` renders the feed with its reader open on that role instead of sending the reader to the board, because 29,194 of 29,196 open board roles already have a `description` averaging 6,300 characters, and a search role's short teaser is worth reading in place before deciding whether to fetch the rest. +**Every link built from someone else's data goes through `safeUrl`.** A board, a search result, a discovered careers page and a saved application's `url` are all free text from outside the app; an `http(s)` check before it lands in an `href` is what keeps a stray `javascript:` link or a bare string typed into a field from being clickable. The reader needed this first, for `original_url`, so `safeUrl` moved to `app.js` beside the app's other shared helpers rather than staying roles.js's own; companies.js (`careers_url`), sources.js (a search sample's `url`) and applications.js (`application.url`) now use the same one. + **A vote moves the rank at read time and never hides.** `Store.roles` multiplies the served score by the vote (`0.5` passed, `1.2` liked, capped at 1) instead of triggering a rescore, so a vote's effect on Best match shows at once and needs no job. A vote never removes a role from the list either, because a stray tap must stay visible and a single further tap must be able to undo it. **Read state is a column on `roles`, and a vote stamps it too.** `read_at` sits beside `vote`, the same shape as newsdesk's `items.read_at`, so the feed filters and counts by it directly rather than joining a side table on every page load. `set_vote` stamps it only when it is still NULL, since voting is reading in newsdesk as well; clearing a vote leaves it alone, because taking back an opinion is not un-reading the posting.