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 <noreply@anthropic.com>
This commit is contained in:
1 parent
4fb9e801f5
commit
5dfaa393be
6 files changed
+40
-20
No files matched your search
+16
-4
@@ -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<string, object>} 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,
|
||||
});
|
||||
})();
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
+1
-3
@@ -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 "";
|
||||
|
||||
+13
-7
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user