Arezgitfield notes / engineering
SecurityUPDATED NOV 23, 2026

Keep Untrusted Text and URLs in Separate DOM Boundaries

Separate DOM text insertion from URL authorization, test an HTTPS origin policy in Node.js, and trace denied updates without leaving a stale link active.

Arezgit Editorial4 min read
AREZGIT / FIELD NOTESECURITY
A label rendered as plain text can still sit inside a dangerous link. Text insertion and navigation reach different browser mechanisms, so give each input its own contract. A documentation l
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A label rendered as plain text can still sit inside a dangerous link. Text insertion and navigation reach different browser mechanisms, so give each input its own contract. A documentation-link component should preserve the label as text and independently decide whether the parsed destination belongs to the allowed site.

The synthetic design accepts only HTTPS links to docs.example.com. Its executable portion is a URL policy tested in Node.js 24; the DOM lifecycle is an illustrative, source-checked sequence rather than a browser integration. The protected assets are the page's execution context and the user's navigation decision; the attacker controls both submitted strings.

Identify the interpreter reached by each value

The OWASP DOM XSS guidance recommends text insertion methods such as textContent when input should remain displayable text. Assigning that property on an ordinary anchor does not interpret a label as HTML. That property does not decide whether the anchor's destination is appropriate.

A URL needs parsing and a destination policy. The WHATWG URL standard defines distinct protocol, origin, host, and user-information components. Comparing the parsed origin avoids treating a familiar-looking prefix as proof of destination identity. A permitted origin also says nothing about which server-side resource the current user may access.

Define the base URL explicitly. Resolving against the current document implicitly can make a component behave differently after a route or page-base change. Here, relative paths resolve against the approved documentation root. External origins, non-HTTPS schemes, and embedded user information are rejected.

Validate the destination before creating nodes

Save this original JavaScript policy in a Node.js module. Returning null is the explicit denied result; a UI caller must display its normal unavailable-link state rather than inventing a fallback destination:

function documentationTarget(rawUrl) {
  if (typeof rawUrl !== "string" || rawUrl.length === 0 || rawUrl.length > 2048) return null;
  let target;
  try {
    target = new URL(rawUrl, "https://docs.example.com/");
  } catch {
    return null;
  }
  if (target.protocol !== "https:" || target.origin !== "https://docs.example.com") return null;
  if (target.username !== "" || target.password !== "") return null;
  return target;
}

The helper returns the parsed, accepted URL and performs no DOM mutation. In the illustrative browser sequence, the caller first validates a nonempty label within its product limit, then obtains the accepted destination, creates a detached ordinary anchor, assigns the label through textContent, and assigns the accepted URL to href. It inserts that node only after every decision succeeds.

The URL length bound is an example product limit measured in JavaScript string code units. It is not a network request-body limit. Enforce transport limits separately when these values arrive from a server request. A local check can protect this component's work without replacing validation at the source of stored data.

Test parsing and define the DOM transition

Append these assertions and execute the module with Node.js. They exercise the actual URL parser without depending on an emulated DOM:

import assert from "node:assert/strict";

assert.equal(documentationTarget("/releases")?.href, "https://docs.example.com/releases");
const denied = [
  "javascript:void(0)",
  "http://docs.example.com/",
  "https://docs.example.com.evil.invalid/",
  "https://reader@docs.example.com/",
  "https://other.example.com/"
];
for (const value of denied) {
  assert.equal(documentationTarget(value), null);
}
assert.equal(documentationTarget(""), null);
assert.equal(documentationTarget(null), null);
assert.equal(documentationTarget("x".repeat(2049)), null);

Each denied destination must return null. For browser integration, use the harmless label <b>Release notes</b> and assert that it remains literal text with no child elements. That is a separate DOM assertion: the URL-only Node test does not establish how a UI wrapper inserts or updates nodes.

For an existing UI, trace an accepted link followed by a denied update. Require the wrapper to remove or disable the previous active link according to the component contract. A pure URL policy can reject the new value correctly while a stateful wrapper accidentally leaves an older destination clickable. Test the wrapper transition, not only the helper's return value.

Keep rich content and downstream authority separate

If the product later requires formatted descriptions, keep that feature separate from this plain-label contract. Use a maintained sanitizer with a deliberately restricted HTML policy and test the actual insertion boundary. Do not turn the label into HTML because one caller wants emphasis; that changes which interpreter receives attacker-controlled data.

The allowed origin can still host an open redirect, compromised page, or path with its own access requirements. If only selected documentation routes should be linked, add a documented route policy after parsing. Do not describe an origin allowlist as a guarantee about everything the destination may do.

A restrictive Content Security Policy can reduce some consequences of unsafe insertion, but it does not make a dangerous sink appropriate. Correct the insertion operation first, then verify the policy as an additional control.

If a deployed component used untrusted HTML or destinations, stop rendering the affected data through that path, assess whether execution or unintended navigation occurred, and invalidate exposed authority where necessary. For the repaired component, the useful acceptance evidence is concrete: labels stay text, denied values create no active link, and stale UI state cannot preserve a destination that the latest input no longer permits.

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit