Safety
Data can't become markup
Cross-site scripting is data (a name, a comment, a URL) landing in a page as markup or script. In rocx a value is always text, so a page's only markup is what its .rocx files contain: there is no escaping to remember.
Html is opaque#
Markup is the platform's Html type; outside its module, no string can become markup. Every value goes through one of these doors:
Text and attributes
Escaped: < > & " ' come out as entities, in text and in attribute values.
URLs
href, src, action, formaction, poster and cite take only safe URLs.
Styles
A style:color or style:--size value is dropped if it could end the declaration or calls a function that loads something (url(…)).
Tags and script
Tags, attribute names and the text of <script> and <style> come only from the file, never from a value.
Names in hand-written Roc#
Plain Roc can build markup with Html.element, Html.void and Html.attr (and Html.optional, Html.flag and their computed forms), where a tag or attribute name is a Str that could come from data. Text and values are escaped as in a file, and every renderer (the server's, the browser's, Html.to_str) leaves out a name that isn't plain:
- A tag is an ASCII letter, then letters, digits and
-(p,h2,linearGradient,acme-card).img src=x onerror=…isn't, so that element and its contents are left out. - An attribute is an ASCII letter, then letters, digits,
-,_,.and:(data-id,aria-label,xml:lang,viewBox), never starting withon: an inline handler (onclick) runs script, while Roc's ison:click(below). The element is written without a bad one. - A
<script>is written only as a.rocxfile has it, with its text as written there (Html.source_text, whichrocx lintrefuses in hand-written Roc). One built from text, a hole, or only asrcis left out.
A bad name never fails a page; it is left out, and reported. The server logs rocx server: markup named what a page can't have, which wasn't written with the page's path and the names, once per render; the browser warns on the console, once per name. .rocx files are unaffected: their names come from the file, and the build refuses inline handlers there.
Try to break it#
This component renders <a href={url} title={text}>{text}</a> with whatever you type, twice: as Html.to_str writes it, as the server would (compiled to WebAssembly here), and live, where the runtime sets holes as text and attributes, never markup.
<a href={url} title={text}>{text}</a> renders as
<a href="about:invalid#rocx-unsafe-url" title="<script>alert(1)</script>"><script>alert(1)</script></a> and here it is, live:
URLs#
A URL attribute takes a relative URL or an http, https, mailto or tel one; anything else becomes about:invalid#rocx-unsafe-url. Tabs, line breaks and leading spaces in a scheme are dropped first, since browsers ignore them (java\tscript: runs). In a picture's URL (<img src>, <link href>, <source src>, <video poster>), data:image/… passes too, SVG included: a picture runs no script.
The check runs in the platform's Html module, in Roc, whenever a value becomes an attribute, so the server's markup and the browser's patches agree. What this page rendered for these values in an href:
| The value | Renders | |
|---|---|---|
/syntax#holes | <a href="/syntax#holes">x</a> | kept |
https://roc-lang.org | <a href="https://roc-lang.org">x</a> | kept |
mailto:ada@example.com | <a href="mailto:ada@example.com">x</a> | kept |
javascript:alert(1) | <a href="about:invalid#rocx-unsafe-url">x</a> | replaced |
JavaScript:alert(1) | <a href="about:invalid#rocx-unsafe-url">x</a> | replaced |
java\tscript:alert(1) | <a href="about:invalid#rocx-unsafe-url">x</a> | replaced |
data:text/html,<b>hi</b> | <a href="about:invalid#rocx-unsafe-url">x</a> | replaced |
What the build refuses#
Some markup is refused before the compiler sees it, at its line and column. rocx's messages, from this page's build:
| You write | rocx says |
|---|---|
<a onclick="go()">Go</a> | inline event handlers (onclick) aren't allowed: in a component, write on:click={…} |
<b on:click="go()">Go</b> | on:click needs {a handler}, not a string: a handler is Roc, never script |
<p style={styles}>Hi</p> | style takes a static value: change classes, not styles, with data |
<a title=hi>Hi</a> | title= needs "a value" or {an expression}: unquoted values aren't allowed |
<br> | <br> has no children or closing tag: write <br /> |
<ul><li>One</li></ol> | <ul> is closed by </ol> |
<p>1 < 2</p> | a < in text starts a tag: write < or {"<"} |
<p>&bogus;</p> | unknown entity &bogus;; write the character itself |
<DIV>Hi</DIV> | write HTML tags in lower case: <div> |
<input checked?="yes" /> | checked?= needs {a Bool}, not a string |
<p style:font_size={c}>Hi</p> | style:font_size: a style property's name is letters and dashes (style:color={…}), or a custom property's (style:--size={…}) |
<p on:click.files={f}>Hi</p> | on:click.files: .files goes with on:drop or on:paste, or a file input's on:change, which carry it |
Handlers aren't script#
onclick="…" is refused: it is script in an attribute. A component's on:click={…} is a Roc function, which the server renders nothing for and the browser never sees as text. The runtime builds elements with createElement and sets text and attributes directly, never parsing markup, so a value can't become a tag. A handler inside a <NoHydration>, which is never hydrated, is refused at its line.
Requests from other sites#
Another site's page can make a visitor's browser send your app a request with its cookies (a form that posts, a script that fetches). So the platform answers 403 to a POST, PUT, PATCH or DELETE (to an endpoint, say) whose Origin isn't your server. Clients that aren't pages (curl, another server) send no Origin and are answered; a GET changes nothing and is answered whoever asks.
The check runs on every request, before the app's middleware, unless App.program says cross_site: Allowed (an API other sites' pages call, with CORS); then Middleware.same_origin({}) in a directory's +middleware.roc puts it back where needed. Cookies from Server.with_cookie are HttpOnly (no script reads them) and SameSite=Lax (another site's form doesn't send them). When the app's public URL is HTTPS (--public-url, PUBLIC_URL), the server makes every cookie it sets Secure too, so it never goes over plain HTTP; on http://localhost they work as they are.
Headers that tell the browser to be careful are middleware too. Guard.security_headers(Security.defaults), from packages/guard, adds a Content-Security-Policy that runs the site's own scripts and nothing else, HSTS, nosniff and the rest. The app rocx new makes has it in routes/+middleware.roc.
rocx lint#
Html.source_text is how rocx writes a <script>'s or <style>'s text from the file. In hand-written Roc it would put any string in a page as markup, so rocx lint refuses it there. Run it in CI beside rocx check (tooling).
$ rocx lint app
✗ app/Sneaky.roc:1: Html.source_text puts text in a page as markup; only rocx may write it