Small SEO Tools

Why HTML Encoding Does Not Validate a Link URL

A link can have perfectly escaped quotation marks and still point to a destination your application should reject. If a form accepts a help-page URL, running that value through an HTML encoder does not decide which kinds of links belong in the field. Treat URL acceptance and HTML output as separate steps.

This walkthrough uses the HTML Encoder to inspect that boundary, then builds a small, deliberately restricted help-link function. All sample addresses are fictional. The demonstration inspects a detached link without clicking it or executing its URL.

1. Check what encoding actually changes

Open the encoder, select Encode special characters only, and paste this harmless test string:

javascript:void("demo")

Press Convert. The current tool produces:

javascript:void("demo")

The quotation marks changed into character references. The javascript: prefix did not change. The tool converts text; it does not approve the resulting address for use in a link.

Now consider this HTML source, shown here only as text:

<a href="javascript:void(&quot;demo&quot;)">Help</a>

In a local browser parsing check, the detached anchor had these values:

getAttribute("href"): javascript:void("demo")
protocol: javascript:

The HTML parser recovered the quotes inside the attribute value. Encoding kept them from ending the surrounding quoted attribute; it did not turn the address into an ordinary web link. MDN explains the javascript scheme: navigation to such a URL can interpret its contents as JavaScript. No navigation is needed to demonstrate that the scheme survived.

2. Write the acceptance rule for this particular field

Suppose a fictional application has a help-link field that should point only to its documentation site. Write the rule before choosing an encoder: accept an absolute HTTPS URL at https://docs.example.com, with no embedded username or password. Reject relative references, other schemes, other origins, and non-default ports.

This is an example product policy, not a universal list of acceptable links. A public directory or email-link field would need different rules. Merely allowing HTTPS does not establish that a destination is trustworthy.

OWASP distinguishes URL validation from output encoding. For a URL placed in HTML source, validate the destination and use the appropriate encoding for its quoted attribute context. Prefer the framework protections already designed for that context rather than assembling an escape routine from replacements.

3. Parse the original URL, then create the link

The function below implements the narrow documentation-site rule. Pass the original URL string, not the HTML-encoded version copied from the tool:

function makeHelpLink(rawUrl, label) {
  let url;
  try {
    url = new URL(rawUrl);
  } catch {
    return null;
  }

  if (url.protocol !== "https:" ||
      url.origin !== "https://docs.example.com" ||
      url.username !== "" || url.password !== "") {
    return null;
  }

  const link = document.createElement("a");
  link.href = url.href;
  link.textContent = label;
  return link;
}

The URL constructor parses the address and, without a base URL, rejects a relative reference. The separate protocol check excludes schemes with different behavior. Comparing the parsed origin checks the HTTPS scheme, host, and effective port together; a hostname that merely contains the expected text will not pass.

The final two assignments use DOM properties. They do not require pre-encoding the runtime strings as HTML. In particular, textContent puts the label into an ordinary anchor as text. A label such as Setup <draft> remains visible text instead of creating a draft element.

4. Test rejection as well as a successful link

These local test cases exercise the example policy without visiting any destination:

Input Expected result
https://docs.example.com/start Accept
https://docs.example.com:443/start Accept: default HTTPS port
javascript:void("demo") Reject: wrong scheme
http://docs.example.com/start Reject: requires HTTPS
https://docs.example.com.other.example/start Reject: different origin
https://docs.example.com:8443/start Reject: different port
https://user@docs.example.com/start Reject: embedded username
/start Reject: no absolute URL

For a rejected value, show a validation message and do not create a clickable link. Repeat the rule wherever the application accepts or renders this data, including the server when applicable. This small function checks the submitted address against one policy; it does not inspect remote page content or control later redirects.

Use the encoder to understand the HTML layer, and test the actual rendering path separately. For the related problem of showing markup literally in a tutorial, follow the guide to displaying HTML code as text.