Skip to main content

Module address

Module address 

Source
Expand description

The address policy: which addresses the HTML backend writes into a page, and which it refuses.

An address here is a URL that the page references: what a link points at (href) or what an image is loaded from (src, srcset). Both come from the document or from the host that rendered it, and a browser can do more with an address than fetch it: when a link is followed whose address has the scheme javascript (the scheme is the part before the first colon, https in https://example.org), the browser runs the rest of the address as a program.

check_address therefore accepts an address only when it is one of these:

  • an address fragment, #section-2: a place in the same page;
  • a relative address, figures/plot.png, ../index.html, ?page=2: one with no scheme at all, read against the address of the page it is in;
  • an address whose scheme the render option HtmlAllowedUrlSchemes allows: by default, one of DEFAULT_ALLOWED_URL_SCHEMES;
  • for an image only, a data: address whose media type begins with image/ (DATA_SCHEME, IMAGE_MEDIA_PREFIX) — a picture written into the page itself.

Anything else is refused. A refused address loses the address and nothing else: the emitter writes the link without its href and the image without its src, so the link’s text and the image’s alternative text (the content) stay in the page, and it reports the refusal as flm.html.refused-address (AddressRefused) with the refused scheme.

§Who decides which schemes are allowed

The default list is the smallest that lets the standard features write the links and images they need, and it holds no scheme that a browser runs as a program. Under it, a page this library produced runs no program that a document asked for.

A host that needs more, such as a scheme of its own that a script of its page handles (app:open/42), sets the render option HtmlAllowedUrlSchemes: either the list of allowed schemes, which replaces the default list, or HtmlAllowedUrlSchemes::All, under which every address is written as given.

The option is set only by Rust code: on the render request (PlanRequest::render_options), or by a workflow defined in Rust (WorkflowDef::render_options). It is not one of the html feature’s settings keys (crate::core::settings), so no settings written as data (a document’s front matter, a command line, the settings of a package) can set it. Whether a page may reference an address that a browser runs is the decision of the program that serves the page, not of a document or of the configuration written beside it.

§Where the policy does not apply

The policy does not apply to a construct node, the node a lowering rule stages (builds into the lowered tree; see Lowerer) to write one HTML tag of its own with its own attributes: the rule that writes a tag is responsible for what it writes, and its author must check the addresses it writes as this module does, with check_address under the value of HtmlAllowedUrlSchemes that the rule declares a read of (LoweringDecl::option). Nor does it apply to the address of a style or script definition, which comes from a feature’s author or the host (super::page).

§How a browser reads the scheme of an address

A browser does not read the characters of an address exactly as written. Before it looks for the scheme it removes every tab, line feed and carriage return from the address, wherever they are, and ignores leading and trailing spaces and control characters. java\nscript:alert(1) and JaVaScRiPt:alert(1) are therefore both the javascript scheme. check_address does the same before it reads the scheme, and compares the scheme ignoring letter case.

It does not decode character references. javascript: is not a hidden scheme here: the sink escapes every & it writes into an attribute value as & (escape_attr_into), so the & of a reference never reaches the page as a &, and the address the browser reads is the one this module read. Decoding references before the check would therefore not make the check stricter; it would only make it disagree with what the page will hold.

Structs§

InvalidUrlScheme
The error UrlScheme::new returns for a name that is not a URL scheme, holding the name as given.
RefusedAddress
The error check_address returns for an address the policy refuses, holding the refused scheme.
UrlScheme
A URL scheme, such as https: the part of a URL before its first colon, without the colon, held in lower case.

Enums§

AddressUse
How an address is used: as the target of a link, or as the source of an image. check_address takes it.
HtmlAllowedUrlSchemes
The render option that decides which URL schemes the page may reference: the schemes allowed in the URLs that the HTML backend writes into a link’s href and an image’s src and srcset.

Constants§

DATA_SCHEME
The scheme of an address that carries its own content instead of pointing at something, data: allowed for an image whose media type begins with IMAGE_MEDIA_PREFIX, refused for everything else unless the render option HtmlAllowedUrlSchemes allows the scheme data itself.
DEFAULT_ALLOWED_URL_SCHEMES
The schemes an address may have by default, whether it is a link or an image: http, https, mailto, ftp, tel.
IMAGE_MEDIA_PREFIX
What the media type of a data: address must begin with for an image, image/, comparing without regard to letter case: data:image/png;base64,… is an image, data:text/html,… is a page and is refused.

Functions§

check_address
Checks whether address may be written for the use used_as under the value allowed of the render option HtmlAllowedUrlSchemes.