Macaw.Layers.Untrusted (macaw_layers v0.5.0)

Copy Markdown View Source

The checks for untrusted text (spec/01 §8): a string a document holds that is written into CSS, SVG or a page holds only what its context can carry. Each document type says which of its strings these are, and judges them with these checks, so every type refuses the same strings as {:unsafe, kind} at their path.

iex> Macaw.Layers.Untrusted.font_family?("Inter, 'Segoe UI', sans-serif")
true
iex> Macaw.Layers.Untrusted.font_family?("x; } body { color: red")
false
iex> Macaw.Layers.Untrusted.class?("critical wide")
true
iex> Macaw.Layers.Untrusted.identifier?("6x")
false

Ported from visualize's validator (its spec/14 §10.3).

Summary

Functions

The fault for a string at path that the check of kind refuses, or none: [{path, {:unsafe, kind}}] or []. kind is :font_family, :class or :identifier.

Whether a string is one or more CSS class names separated by spaces, each -?[_a-zA-Z][_a-zA-Z0-9-]*.

Whether a string is a font family list: no <, >, /, ;, {, }, \ or control character, and every quote closed. Commas, spaces and quotes stay, since a family list is Inter, 'Segoe UI', sans-serif; an unclosed quote would run past its declaration.

Whether a string is one CSS identifier, -?[_a-zA-Z][_a-zA-Z0-9-]*.

Functions

check(kind, value, path)

@spec check(:font_family | :class | :identifier, term(), Macaw.Layers.Fault.path()) ::
  [
    Macaw.Layers.Fault.t()
  ]

The fault for a string at path that the check of kind refuses, or none: [{path, {:unsafe, kind}}] or []. kind is :font_family, :class or :identifier.

iex> Macaw.Layers.Untrusted.check(:class, "a\" onload=\"x", [:styles, "s", :class])
[{[:styles, "s", :class], {:unsafe, :class}}]

class?(class)

@spec class?(term()) :: boolean()

Whether a string is one or more CSS class names separated by spaces, each -?[_a-zA-Z][_a-zA-Z0-9-]*.

font_family?(family)

@spec font_family?(term()) :: boolean()

Whether a string is a font family list: no <, >, /, ;, {, }, \ or control character, and every quote closed. Commas, spaces and quotes stay, since a family list is Inter, 'Segoe UI', sans-serif; an unclosed quote would run past its declaration.

identifier?(name)

@spec identifier?(term()) :: boolean()

Whether a string is one CSS identifier, -?[_a-zA-Z][_a-zA-Z0-9-]*.