A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
403 lines
9.8 KiB
Text
403 lines
9.8 KiB
Text
// The HTML sanitiser: an allowlist over a tokenizer. What comes out is well formed and holds only allowed tags and
|
|
// attributes, every text escaped, so nothing a browser parses differently can bring a removed element back.
|
|
import type { Policy } from "../ast"
|
|
import { escapeHtml, isAlpha, scanEntity } from "./chars"
|
|
|
|
let DEFAULT_TAGS = [
|
|
"a",
|
|
"abbr",
|
|
"b",
|
|
"blockquote",
|
|
"br",
|
|
"code",
|
|
"dd",
|
|
"del",
|
|
"details",
|
|
"div",
|
|
"dl",
|
|
"dt",
|
|
"em",
|
|
"figcaption",
|
|
"figure",
|
|
"h1",
|
|
"h2",
|
|
"h3",
|
|
"h4",
|
|
"h5",
|
|
"h6",
|
|
"hr",
|
|
"i",
|
|
"img",
|
|
"input",
|
|
"ins",
|
|
"kbd",
|
|
"li",
|
|
"mark",
|
|
"ol",
|
|
"p",
|
|
"pre",
|
|
"q",
|
|
"s",
|
|
"samp",
|
|
"small",
|
|
"span",
|
|
"strong",
|
|
"sub",
|
|
"summary",
|
|
"sup",
|
|
"table",
|
|
"tbody",
|
|
"td",
|
|
"tfoot",
|
|
"th",
|
|
"thead",
|
|
"tr",
|
|
"u",
|
|
"ul",
|
|
"var",
|
|
]
|
|
|
|
let DEFAULT_ATTRIBUTES = Map<string, string[]>(
|
|
[
|
|
["a", ["href", "title", "target"]],
|
|
["img", ["src", "alt", "title", "width", "height"]],
|
|
["ol", ["start"]],
|
|
["code", ["class"]],
|
|
["td", ["align", "colspan", "rowspan"]],
|
|
["th", ["align", "colspan", "rowspan", "scope"]],
|
|
["input", ["type", "checked", "disabled"]],
|
|
["details", ["open"]],
|
|
],
|
|
)
|
|
|
|
/// URL schemes allowed unless the options say otherwise.
|
|
export let DEFAULT_SCHEMES = ["http", "https", "mailto", "tel"]
|
|
|
|
let VOID_TAGS = Set(
|
|
[
|
|
"br",
|
|
"hr",
|
|
"img",
|
|
"input",
|
|
"wbr",
|
|
"area",
|
|
"base",
|
|
"col",
|
|
"embed",
|
|
"link",
|
|
"meta",
|
|
"param",
|
|
"source",
|
|
"track",
|
|
],
|
|
)
|
|
|
|
/// Elements removed with everything inside them.
|
|
let DROPPED = Set(
|
|
[
|
|
"script",
|
|
"style",
|
|
"template",
|
|
"iframe",
|
|
"object",
|
|
"embed",
|
|
"applet",
|
|
"noscript",
|
|
"noembed",
|
|
"noframes",
|
|
"title",
|
|
"textarea",
|
|
"xmp",
|
|
"plaintext",
|
|
"svg",
|
|
"math",
|
|
"select",
|
|
"frameset",
|
|
"head",
|
|
],
|
|
)
|
|
|
|
fn isDataImage(url: string): boolean {
|
|
let lower = url.slice(0, 16).toLowerCase()
|
|
return lower.startsWith("data:image/png") ||
|
|
lower.startsWith("data:image/gif") ||
|
|
lower.startsWith("data:image/jpeg") ||
|
|
lower.startsWith("data:image/jpg") ||
|
|
lower.startsWith("data:image/webp")
|
|
}
|
|
|
|
/// Whether a URL is relative or uses one of `schemes`. Browsers ignore tabs, line breaks and leading control characters
|
|
/// in a scheme, so they are ignored here too.
|
|
export fn isSafeUrl(url: string, schemes: string[], image: boolean): boolean {
|
|
let mut compact: string[] = []
|
|
for ch of url {
|
|
let c = ch.codeUnitAt(0) ?? 0
|
|
if c > 32 && c != 127 {
|
|
compact.push(ch)
|
|
}
|
|
}
|
|
let text = compact.join("")
|
|
let mut colon = -1
|
|
for i of 0..text.length {
|
|
let c = text.codeUnitAt(i) ?? 0
|
|
if c == 58 {
|
|
colon = i
|
|
break
|
|
}
|
|
if c == 47 || c == 63 || c == 35 {
|
|
return true
|
|
}
|
|
}
|
|
if colon < 0 {
|
|
return true
|
|
}
|
|
if image && isDataImage(text) {
|
|
return true
|
|
}
|
|
return schemes.includes(text.slice(0, colon).toLowerCase())
|
|
}
|
|
|
|
/// Text decoded from an attribute value: its character references replaced.
|
|
fn decodeAttribute(value: string): string {
|
|
if !value.includes("&") {
|
|
return value
|
|
}
|
|
let mut parts: string[] = []
|
|
let mut from = 0
|
|
let mut i = value.indexOf("&")
|
|
while i >= 0 {
|
|
let found = scanEntity(value, i)
|
|
if found != null {
|
|
parts.push(value.slice(from, i))
|
|
parts.push(found[0])
|
|
from = found[1]
|
|
i = value.indexOf("&", from)
|
|
} else {
|
|
i = value.indexOf("&", i + 1)
|
|
}
|
|
}
|
|
parts.push(value.slice(from))
|
|
return parts.join("")
|
|
}
|
|
|
|
/// Text as it may appear between tags: a character reference stays, anything else that means something is escaped.
|
|
fn cleanText(text: string): string {
|
|
if !text.includes("&") && !text.includes("<") && !text.includes(">") {
|
|
return text
|
|
}
|
|
let mut parts: string[] = []
|
|
let mut from = 0
|
|
let mut i = 0
|
|
while i < text.length {
|
|
let c = text.codeUnitAt(i) ?? 0
|
|
if c == 38 {
|
|
let found = scanEntity(text, i)
|
|
if found != null {
|
|
i = found[1]
|
|
} else {
|
|
parts.push(text.slice(from, i))
|
|
parts.push("&")
|
|
i += 1
|
|
from = i
|
|
}
|
|
} else if c == 60 || c == 62 {
|
|
parts.push(text.slice(from, i))
|
|
parts.push(c == 60 ? "<" : ">")
|
|
i += 1
|
|
from = i
|
|
} else {
|
|
i += 1
|
|
}
|
|
}
|
|
parts.push(text.slice(from))
|
|
return parts.join("")
|
|
}
|
|
|
|
type Attr = { name: string, value: string }
|
|
|
|
type Tag = { name: string, attrs: Attr[], end: int }
|
|
|
|
fn isSpace(c: int): boolean {
|
|
return c == 32 || c == 9 || c == 10 || c == 12 || c == 13
|
|
}
|
|
|
|
/// Reads the tag starting at `<` at `pos` (its name begins at `start`).
|
|
fn readTag(html: string, start: int): Tag {
|
|
let mut i = start
|
|
while i < html.length {
|
|
let c = html.codeUnitAt(i) ?? 32
|
|
if isSpace(c) || c == 62 || c == 47 {
|
|
break
|
|
}
|
|
i += 1
|
|
}
|
|
let name = html.slice(start, i).toLowerCase()
|
|
let mut attrs: Attr[] = []
|
|
while i < html.length {
|
|
let c = html.codeUnitAt(i) ?? 32
|
|
if c == 62 {
|
|
return { name, attrs, end: i + 1 }
|
|
}
|
|
if isSpace(c) || c == 47 {
|
|
i += 1
|
|
continue
|
|
}
|
|
let nameStart = i
|
|
i += 1
|
|
while i < html.length {
|
|
let u = html.codeUnitAt(i) ?? 32
|
|
if isSpace(u) || u == 61 || u == 62 || u == 47 {
|
|
break
|
|
}
|
|
i += 1
|
|
}
|
|
let attr = html.slice(nameStart, i).toLowerCase()
|
|
while isSpace(html.codeUnitAt(i) ?? 0) {
|
|
i += 1
|
|
}
|
|
let mut value = ""
|
|
if html.codeUnitAt(i) == 61 {
|
|
i += 1
|
|
while isSpace(html.codeUnitAt(i) ?? 0) {
|
|
i += 1
|
|
}
|
|
let q = html.codeUnitAt(i) ?? -1
|
|
if q == 34 || q == 39 {
|
|
let close = html.indexOf(String.fromCharCode(q), i + 1)
|
|
let stop = close < 0 ? html.length : close
|
|
value = html.slice(i + 1, stop)
|
|
i = close < 0 ? html.length : close + 1
|
|
} else {
|
|
let from = i
|
|
while i < html.length {
|
|
let u = html.codeUnitAt(i) ?? 32
|
|
if isSpace(u) || u == 62 {
|
|
break
|
|
}
|
|
i += 1
|
|
}
|
|
value = html.slice(from, i)
|
|
}
|
|
}
|
|
attrs.push({ name: attr, value: decodeAttribute(value) })
|
|
}
|
|
return { name, attrs, end: html.length }
|
|
}
|
|
|
|
/// Where the content of the dropped element `name` ends: after its closing tag, or at the end of the text.
|
|
fn skipElement(html: string, name: string, from: int): int {
|
|
let mut i = html.indexOf("</", from)
|
|
while i >= 0 {
|
|
if html.slice(i + 2, i + 2 + name.length).toLowerCase() == name {
|
|
let after = html.codeUnitAt(i + 2 + name.length) ?? 62
|
|
if isSpace(after) || after == 62 || after == 47 {
|
|
let close = html.indexOf(">", i)
|
|
return close < 0 ? html.length : close + 1
|
|
}
|
|
}
|
|
i = html.indexOf("</", i + 2)
|
|
}
|
|
return html.length
|
|
}
|
|
|
|
fn keepAttribute(tag: string, a: Attr, schemes: string[]): boolean {
|
|
if a.name == "href" || a.name == "src" {
|
|
return isSafeUrl(a.value, schemes, tag == "img")
|
|
}
|
|
if a.name == "target" {
|
|
return a.value == "_blank"
|
|
}
|
|
if a.name == "type" && tag == "input" {
|
|
return a.value.toLowerCase() == "checkbox"
|
|
}
|
|
if a.name == "class" {
|
|
return a.value.startsWith("language-") && !a.value.includes(" ")
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// The only input kept is a task list's checkbox, always disabled: a text field in rendered content can phish.
|
|
fn isCheckbox(tag: Tag): boolean {
|
|
return tag.attrs.some(a => a.name == "type" && a.value.toLowerCase() == "checkbox")
|
|
}
|
|
|
|
fn openTag(tag: Tag, allowed: string[], schemes: string[]): string {
|
|
let mut out: string[] = ["<", tag.name]
|
|
let mut blank = false
|
|
let mut seen = Set<string>()
|
|
for a of tag.attrs {
|
|
if allowed.includes(a.name) && !seen.has(a.name) && keepAttribute(tag.name, a, schemes) {
|
|
seen.add(a.name)
|
|
out.push(` ${a.name}="${escapeHtml(a.value)}"`)
|
|
blank = blank || (a.name == "target" && a.value == "_blank")
|
|
}
|
|
}
|
|
if blank {
|
|
out.push(" rel=\"noopener noreferrer\"")
|
|
}
|
|
if tag.name == "input" && !seen.has("disabled") {
|
|
out.push(" disabled=\"\"")
|
|
}
|
|
out.push(VOID_TAGS.has(tag.name) ? " />" : ">")
|
|
return out.join("")
|
|
}
|
|
|
|
/// Removes everything `policy` doesn't allow from `html`: scripts, styles, event handlers and unsafe URLs by default.
|
|
/// The result is well formed: stray closing tags are dropped and open ones closed.
|
|
///
|
|
/// ```weft
|
|
/// sanitize("<p onclick=\"x()\">hi<script>bad()</script></p>") // "<p>hi</p>"
|
|
/// ```
|
|
export fn sanitize(html: string, policy?: Policy): string {
|
|
let tags = Set(policy?.tags ?? DEFAULT_TAGS)
|
|
let attributes = policy?.attributes ?? DEFAULT_ATTRIBUTES
|
|
let schemes = policy?.schemes ?? DEFAULT_SCHEMES
|
|
let mut out: string[] = []
|
|
let mut open: string[] = []
|
|
let mut i = 0
|
|
while i < html.length {
|
|
let lt = html.indexOf("<", i)
|
|
if lt < 0 {
|
|
out.push(cleanText(html.slice(i)))
|
|
break
|
|
}
|
|
if lt > i {
|
|
out.push(cleanText(html.slice(i, lt)))
|
|
}
|
|
let next = html.codeUnitAt(lt + 1) ?? -1
|
|
if html.startsWith("<!--", lt) {
|
|
let end = html.indexOf("-->", lt + 4)
|
|
i = end < 0 ? html.length : end + 3
|
|
} else if next == 33 || next == 63 {
|
|
let end = html.indexOf(">", lt + 2)
|
|
i = end < 0 ? html.length : end + 1
|
|
} else if next == 47 && isAlpha(html.codeUnitAt(lt + 2) ?? -1) {
|
|
let tag = readTag(html, lt + 2)
|
|
i = tag.end
|
|
let at = open.lastIndexOf(tag.name)
|
|
if at >= 0 {
|
|
while open.length > at {
|
|
out.push(`</${open.pop() ?? ""}>`)
|
|
}
|
|
}
|
|
} else if isAlpha(next) {
|
|
let tag = readTag(html, lt + 1)
|
|
i = tag.end
|
|
if DROPPED.has(tag.name) {
|
|
i = VOID_TAGS.has(tag.name) ? i : skipElement(html, tag.name, i)
|
|
} else if tags.has(tag.name) && (tag.name != "input" || isCheckbox(tag)) {
|
|
out.push(openTag(tag, attributes.get(tag.name) ?? [], schemes))
|
|
if !VOID_TAGS.has(tag.name) {
|
|
open.push(tag.name)
|
|
}
|
|
}
|
|
} else {
|
|
out.push("<")
|
|
i = lt + 1
|
|
}
|
|
}
|
|
while open.length > 0 {
|
|
out.push(`</${open.pop() ?? ""}>`)
|
|
}
|
|
return out.join("")
|
|
}
|