日本語

Values the types do not check

Some values pass the type check while their content goes unchecked: the host of a URL, the value of an attribute or a CSS property, and the path of a file the page loads. For each, what is checked and what the browser does with a wrong value.

Each value on this page has a type, so it cannot be passed where another kind of value belongs. What the type does not check is the text inside: which host a URL names, whether an attribute value means anything to the browser, or whether the router serves a file. Each section names the check that runs, what is left, and what the browser does with a wrong value. What the compiler does not check lists the mistakes that make rendering panic.

El::href and El::src take url: impl IntoHref<S>: a link built from a Route, or a SafeUrl for an address the router does not serve, such as another site or a #fragment on the same page. El::poster takes the same, and El::srcset takes pairs of a SafeUrl and a width or a pixel density. A SafeUrl holds only a URL whose scheme cannot run a script: http:, https:, mailto:, or none at all, as in /logo.png, #top, or ?page=2.

FunctionSignatureOn a refused URL
SafeUrl::newconst fn new(url: &'static str) -> SafeUrlPanics, and in a const fails to compile
SafeUrl::parsefn parse(url: impl Into<String>) -> Result<SafeUrl, UnsafeUrl>Returns an UnsafeUrl, which names the rule the URL broke
Deserialize for SafeUrlA field of a page's signalsFails as SafeUrl::parse does, so a page's signals cannot hold a javascript: URL

SafeUrl::new takes a URL written in the program. SafeUrl::parse reads a URL from outside the program, such as one a visitor typed:

use hypersig::html::a;
use hypersig::{SafeUrl, Signals};

#[derive(Signals, Serialize, Deserialize)]
struct Profile {
    // Deserializing the signals checks this field as `SafeUrl::parse` does.
    website: SafeUrl,
}

// In a `const`, a URL whose scheme can run a script fails to compile.
const SOURCE: SafeUrl = SafeUrl::new("https://github.com/imaimai17468/hypersig");
const TOP: SafeUrl = SafeUrl::new("#top");

let source = a::<Profile>().href(SOURCE).text("Source");
let back = a::<Profile>().href(TOP).text("Back to top");

// A URL a visitor typed is checked when it arrives.
let typed = SafeUrl::parse("https://example.com/~ada").expect("an https URL");
assert!(SafeUrl::parse("javascript:alert(1)").is_err());
let theirs = a::<Profile>().href(Profile::website()).text("Website");

A plain string is not a URL href takes, so it is E0277. The note under the error names what it takes: a Link or a Url from a route, a SafeUrl, or a signal or expression of SafeUrl, and SafeUrl::parse for a String.

use hypersig::html::a;
use hypersig::Signals;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Profile {
    name: String,
}

// A plain string, where a link or a `SafeUrl` belongs.
let source = a::<Profile>().href("https://github.com/imaimai17468/hypersig").text("Source");

A javascript: URL in a const is E0080, with the message its scheme is not http, https, or mailto:

use hypersig::SafeUrl;

// The scheme `javascript:` runs a script, so the constant fails to compile.
const HELLO: SafeUrl = SafeUrl::new("javascript:alert(1)");

The host is not checked, so https://evil.example/a.js and //evil.example are SafeUrls too. A visitor's URL suits a link the visitor follows, and not the src of an <iframe>, which would show whatever page the host serves inside this one. So El::src panics on an <iframe>, an <embed>, or a <frame> when the URL reads a signal, and a fixed SafeUrl there comes from the program.

In the browser, a SafeUrl can also come from code outside hypersig, such as the detail of a custom event, which nothing checks on its way in. When a signal writes href, src, or poster, the browser checks the same rule and removes the attribute where the URL breaks it.

Attribute names and values

El::attr, fn attr(self, name: Attr, value: impl Into<String>) -> Self, sets a plain attribute. The name is an Attr: a constant of html::attr for each attribute of HTML, SVG, and ARIA, or attr! for any other name, such as data-track or an attribute of a custom element. attr! checks the name at compile time. It refuses the attributes hypersig writes itself (id, class, role, and those starting with data-hs-), the ones whose value names another element, such as for, and the ones whose value can run a script or load a URL: href, src, poster, srcset, every on* handler, srcdoc, action, formaction, style, and a few more. Each refused name has a method of its own, so attr!("href") is E0080, with a message naming El::href:

use hypersig::html::a;

// `El::href` sets `href`, so `attr!` refuses the name.
let link = a::<()>().attr(hypersig::attr!("href"), "javascript:alert(1)");

The value is any text, and nothing checks that it means anything for the attribute. The code below compiles and renders type="emial". The browser treats a type it does not recognize as text, so the field accepts any text and checks no address.

use hypersig::html::{attr, input};
use hypersig::Signals;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Signup {
    email: String,
}

// The name is a constant, and the value is any text: `emial` compiles.
let email = input::<Signup>()
    .attr(attr::TYPE, "emial")
    .attr(hypersig::attr!("data-track"), "signup-email")
    .bind(Signup::email());

bind_attr and attr_when take an Attr too, with a value that follows the signals. One attribute set twice on one element panics when the element is built, because the browser reads the first of two and ignores the second.

CSS values from signals

El::bind_style, fn bind_style(self, property: Property, value: impl IntoExpr<S, String>) -> Self, takes the property as a typed Property and the value as a string the browser parses. Nothing checks that the value is valid for the property. A value the browser rejects leaves the property as the element's classes and stylesheets set it. bind_token takes a value the same way. Where the value comes from a fixed set, El::css_match keeps it typed, as Styling shows.

Files a page loads

Asset::new, const fn new(href: &'static str) -> Asset, names a script, a stylesheet, an icon, or the runtime. It panics, and in a const fails to compile, on an empty URL, a scheme other than http or https, or a space or a control character. It takes only text written in the program, because a script's URL decides what code the page runs. Nothing checks that the router serves the file: a path that no service answers loads nothing, and the browser's console shows the 404. JavaScript libraries shows how to serve one.

Summary

ValueCheckedNot checkedWhen it is wrong
SafeUrlThe schemeThe host, and whether the address existsThe link leads wherever the host sends it
El::attr valueThe attribute's nameThe value's meaningThe browser ignores the value or falls back to the attribute's default
bind_style and bind_token valuesThe propertyThe value's syntaxThe browser keeps the property as the classes and stylesheets set it
Asset pathThe scheme and the charactersWhether the router serves the fileNothing loads, and the console shows the 404
CustomEvent name and detailHow the page uses the detailWhether the script dispatches the event, and the shape of its detailNothing runs, or a missing field reads as null

Next, Static pages serves a page without a server.