Documentation
¶
Overview ¶
Package safeurl decides whether a URL may be handed to a browser as a navigation target or written into a URL-bearing attribute.
It exists because escaping is the wrong tool for the question. HTML escaping makes a value safe to sit inside markup; it says nothing about what the browser does when the value is followed. "javascript:alert(1)" contains none of & < > " ', so it survives every escaper unchanged and then executes. The standard library draws this line for the same reason, which is why html/template rewrites such a URL to #ZgotmplZ rather than escaping it.
The rule here is an allowlist of schemes, not a denylist of dangerous ones. A denylist has to anticipate every scheme a browser will ever execute, and the set has grown before: javascript:, then data:, then vbscript:. An allowlist only has to name the ones a web application actually navigates to.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Navigable ¶
Navigable reports whether value is a URL this framework will hand to a browser.
A relative URL is always navigable: it cannot name a scheme, so it cannot name an executing one, and resolving it against the page can only produce the page's own scheme. That includes the protocol-relative form //host/path, which reaches another origin but still under the page's scheme. Whether reaching another origin is desirable is the caller's policy and not a property of the URL, so it is not decided here; what is decided here is that following the value cannot run script.
An empty value is not navigable. Navigating to "" reloads the current page, which is never what a caller meant to ask for and hides the bug that produced the empty string.
Types ¶
This section is empty.