GitHub Star 1.6k

Security

Hologram protects your pages against cross-site scripting and request forgery by default, with nothing to configure. This page lists what it does for you and where to read more.

What Hologram Does for You

Escaping makes a value safe where it stands as text. It can't help when a value ends up somewhere that is itself code or a URL:

CSRF Protection

A cross-site request forgery makes a logged-in user's browser send a request your app trusts, from a page on another site. Hologram guards commands and data layer writes against it. The page a visit starts on carries a token tied to the user's session, the runtime sends it with every command and every batch of writes, and the server answers a request whose token doesn't match with a 403. The token is masked anew on every page load, so it never repeats in the markup.

A page load carries no token, whether it's the first one of a visit or a navigation. Another site can link to any of your pages, and the browser loads it as the logged-in user. So don't let middleware or init/3 act on the user's behalf, such as by deleting a post or sending an email. Do that in a command, which the token protects. A link whose URL carries its own secret, like an email confirmation link, is fine, since another site can't know it.

The realtime stream needs no token either. The server refuses its handshake without a session, and accepts only the subscriptions it signed for that tab and user. In embedded mode, a native form that posts to a Phoenix route gets Phoenix's own CSRF protection.

Content Security Policy

Hologram sends a Content Security Policy with every page a visit starts on. The policy tells the browser which scripts may run, so the browser refuses a script an attacker gets into a page, through an escaping bug for example. It needs no configuration, and your templates need no changes.

The Default Policy

base-uri 'none'; form-action 'self'; frame-ancestors 'self'; object-src 'none'; script-src 'nonce-R4nd0m...' 'strict-dynamic' 'unsafe-inline' https:

What each directive does:

Nothing restricts images, fonts, styles, media, the iframes you embed, or the servers the page talks to, since none of them run code. Google Fonts, a CDN stylesheet, an analytics pixel, a YouTube embed and inline style attributes all work with no configuration.

This is the strict policy Google and OWASP recommend. It passes Lighthouse, Google's CSP Evaluator and Mozilla's HTTP Observatory. Their one note is that it names no endpoint for violation reports, which only your app can provide. Configuring the Policy below shows how.

Nonces

Hologram puts the page's nonce on every <script> element it renders. That covers the ones in your layouts and templates, its own runtime and bundles, and the scripts the client creates when it navigates, so inline and third-party scripts in your templates work as written:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
</script>

Hologram removes a nonce you write yourself on a <script>, <style> or <link>. The policy lists only the page's own nonce, so yours would never match.

The default policy doesn't restrict styles, so Hologram adds no nonce to <style> and <link> elements. If you allow only styles that carry the page's nonce, Hologram adds it to every <style> and <link> it renders:

config :hologram, content_security_policy: [style_src: [:self, :nonce]]

The same goes for :nonce in style_src_elem, or in default_src if you set neither style_src_elem nor style_src. The browser checks styles against the first of those three that your policy has, and so does Hologram.

What the policy blocks, by design, is code that doesn't come through a nonced script: inline event handler attributes like onclick="...", javascript: URLs, and scripts a third-party script writes into the page with document.write. Use Hologram's event bindings, such as $click, instead of handler attributes.

JavaScript interop works under the policy without 'unsafe-eval'. JS.eval/1, JS.exec/1 and the ~JS sigil don't use eval() or new Function(). Hologram runs their code as a script carrying the page's nonce, so the policy trusts it like your own scripts. The data Hologram sends between the server and the client travels as JSON.

Configuring the Policy

config :hologram, content_security_policy: [
  form_action: [:self, "https://checkout.stripe.com"],
  img_src: [:self, "https://images.example.com", "data:"]
]

How the config maps to the header:

The policy is read for every page, so unlike the compiler settings it also works from config/runtime.exs. A malformed config fails your app's start with an error naming the entry. So does one under which no Hologram page could work, such as a script_src that allows neither the page's nonce nor inline scripts and your own origin, a default_src: [:none] that blocks the requests Hologram makes to your server, or a sandbox without "allow-scripts" and "allow-same-origin".

Common additions:

config :hologram, content_security_policy: [
  # The servers your client code talks to: your own, and an analytics service
  connect_src: [:self, "https://analytics.example.com"],
  # Your pages embedded on a partner's site
  frame_ancestors: [:self, "https://partner.example.com"],
  # Violation reports sent to an endpoint you run or a reporting service
  report_uri: ["https://csp-reports.example.com"],
  # Stylesheets and style elements limited to your own site and the page's nonce,
  # with inline style attributes still allowed, which style_src alone would block
  style_src: [:self, :nonce],
  style_src_attr: [:unsafe_inline]
]

Check the hosts a third-party service needs in its own docs before limiting a directive. An analytics service often sends to several domains.

Most apps never change script_src. Every script Hologram renders carries the nonce, and with 'strict-dynamic' the scripts those load are trusted too. When you do change it, restate the parts of the default you want to keep, since a directive you give replaces the default: script_src: [:nonce, :strict_dynamic, :unsafe_inline, "https:", ...].

Hologram's own forms submit through $submit and never post natively, so form-action 'self' doesn't affect them. A native <form> that posts to another site needs that site in form_action. So does a native form whose route answers with a redirect to another site, since browsers check the redirect too. Stripe Checkout's quickstart and some OAuth sign-in buttons work that way. Hologram doesn't need the form for either. For a checkout, a command creates the session and puts an action with its URL, and the action sends the browser there with JS.set(:window, :location, url). For a sign-in, a plain link to the route works.

Trusted Types

Trusted Types extends the protection to the browser APIs that turn strings into code or markup, like innerHTML and script.src. When it's enforced, the browser refuses a plain string there and takes only a value that a named policy created. Turn it on with:

config :hologram, content_security_policy: [require_trusted_types_for: [:script]]

Hologram's runtime writes its scripts, and HTML marked with trust_html/1, through a Trusted Types policy named hologram, so it keeps working under enforcement. A third-party script that writes raw HTML needs a default policy of your own. Without a trusted_types directive any policy name is allowed. When you give one to limit the names, Hologram adds hologram to it:

config :hologram, content_security_policy: [
  require_trusted_types_for: [:script],
  trusted_types: ["default"]
]

Under enforcement the browser also refuses a few attributes your templates can write, on elements Hologram creates on the client: srcdoc on an <iframe>, on* event handler attributes, which the policy blocks anyway, and href on an SVG <script>.

Who Sends the Policy

Data Layer Access

This section applies if your app uses Hologram's data layer. With another data source, you check access in your own code.

Access Rules

Each entity type says who may read and write its entities, with allow lines. Nobody may do what no line allows, so an entity type with no lines is closed to every user. The server checks every write an action makes, whatever the browser did with it, and it never sends a browser an entity its user may not read. The rules also apply to reads and writes in init/3, middleware, commands and the jobs a user creates. See Access Control.

The rules are part of the code the browser gets, along with the names of your entity types, attributes and roles. Anyone who can open your app can read them, signed in or not, so don't write a secret into one. This doesn't weaken the rules, since the server checks every read and write with its own copy.

Going Past the Rules

Two things on the server go past the access rules, so check access yourself wherever you use them for a user:

Server-Only Attributes

A user who may read an entity gets all of its attributes, except the ones marked with server_only: true. Mark attributes like password hashes and API tokens:

attribute :api_token, :string, optional: true, server_only: true

Hologram removes these values from every entity it sends to the browser, also when the entity is in the state or in an action's params. It can't do that for a value you copy out of an entity. put_state(component, :token, user.api_token) sends the token to the browser. See Server-Only Attributes in the Entities documentation.

What the Client Can Fake

Everything a request carries comes from the client and can be faked: a page's params, the module, name, target and params of a command, the request headers, the cookies, and the host. Users can read and change client code too, since it runs in their browser, so authorize on the server, never in an action alone. Decide who may see a page in its middleware or init/3, and who may run a command in middleware or in the command itself. For the entities an action writes through the data layer, your access rules decide, and the server checks them.

A command can be sent to any component, not only through the buttons a page shows, and only the middleware of the component it names runs. A gate on a page doesn't cover its components' commands, so attach it to each component that needs it. The Middleware documentation shows how.

The host is client-supplied even with no proxy in front. Validate it against an allowlist before using it for a security decision, such as picking a tenant, and never build a security-sensitive URL from it, such as a password reset link in an email.

Reporting Vulnerabilities

To report a security vulnerability in Hologram, see the Reporting Vulnerabilities page.

Sponsored by
Curiosum
Main sponsor
Erlang Ecosystem Foundation
Milestone sponsor
Evo Store
Performance sponsor
Sruplex
Innovation partner