Files
Tom Moor 2a9ac3b456 Tighten how editor node and mark attributes are rendered (#13486)
* fix: Sanitize the embed toolbar link and anchor embed url matchers

The "Open" link in an embed's bottom bar wrote the node's href attribute
into the DOM without passing it through sanitizeUrl, so a javascript: url
in that attribute ran on click.

Two embed matchers, and the regex built from a team's embed integration
settings, were not anchored, so a javascript: url that contained a
supported url further along still matched an embed and reached that link.

* fix: Only emit a list style the editor renders

The listStyle attribute is written into a list-style-type declaration.
Its validator returned a boolean rather than throwing, which Prosemirror
ignores, so any string reached the style attribute and a crafted value
appended further CSS declarations to the element.

* fix: Sanitize the image urls carried in a mention's unfurl attribute

The unfurl attribute holds a cached copy of the unfurl response on the
node, and is used whenever the store has no live entry for the url. Its
favicon and avatar urls were written to an img src unchanged, so a
crafted mention made every reader's browser request a chosen host.

* fix: Render a heading level and highlight color that cannot be used

Both attributes are used unchecked when the node is rendered. rgba()
throws for a color it cannot parse, and a level outside of 1-6 produces
an invalid tag name, so either one stopped the whole document rendering
in the browser and on the server. The heading level also reached a
repeat() in the Markdown serializer, unbounded.

Fall back when rendering rather than when reading, so that a value
written through collaborative editing, which does not run the attribute
validators, is covered too.
2026-08-19 08:06:48 -04:00

169 lines
4.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { parseToRgb, rgba } from "polished";
import type { MarkSpec, MarkType } from "prosemirror-model";
import { toggleMark } from "../commands/toggleMark";
import { markInputRuleForPattern } from "../lib/markInputRule";
import markRule from "../rules/mark";
import Mark from "./Mark";
import {
presetColors,
hexToRgba,
toHexColor,
validateColorHex,
} from "@shared/utils/color";
/**
* Whether an ancestor element already paints the same background, in which case
* the element is inheriting the shading of a container – such as a table cell –
* rather than highlighting text.
*/
function isInheritedBackground(dom: HTMLElement, color: string): boolean {
const hex = toHexColor(color);
if (!hex) {
return false;
}
let parent = dom.parentElement;
while (parent) {
// Ancestors that paint nothing, such as a transparent background, are
// skipped so that the closest painted background is the one compared.
const background = parent.style?.backgroundColor;
const parentHex = background ? toHexColor(background) : null;
if (parentHex) {
return parentHex === hex;
}
parent = parent.parentElement;
}
return false;
}
export default class Highlight extends Mark {
/** The default opacity of the highlight */
static opacity = 0.4;
/** Preset colors available for highlighting */
static presetColors = presetColors;
/**
* Checks if a color is one of the highlight preset colors.
*
* @param color - A hex color string to check.
* @returns true if the color matches a preset color's hex value.
*/
static isPresetColor(color: string): boolean {
return Highlight.presetColors.some((c) => c.hex === color);
}
/**
* Finds the preset color matching a CSS color value. Opacity is ignored, so a
* preset rendered translucently still matches.
*
* @param cssColor - A CSS color value (hex, rgb, rgba, etc.).
* @returns The matching preset color hex, or null if none match.
*/
static findMatchingPresetColor(cssColor: string): string | null {
try {
const { red, green, blue } = parseToRgb(cssColor);
for (const preset of Highlight.presetColors) {
const presetRgb = hexToRgba(preset.hex);
if (
red === presetRgb.red &&
green === presetRgb.green &&
blue === presetRgb.blue
) {
return preset.hex;
}
}
} catch {
// Failed to parse the color
}
return null;
}
get name() {
return "highlight";
}
get schema(): MarkSpec {
return {
attrs: {
color: {
default: null,
validate: "string|null",
},
},
parseDOM: [
{
tag: "mark",
getAttrs: (dom) => {
const color = dom.getAttribute("data-color") || "";
return {
color: validateColorHex(color) ? color : null,
};
},
},
{
tag: "span[style]",
getAttrs: (dom) => {
const style = dom.style.backgroundColor;
if (!style || isInheritedBackground(dom, style)) {
return false;
}
// Any background that is not one of the highlight colors is styling
// belonging to the source document, rather than a highlight.
const matchedColor = Highlight.findMatchingPresetColor(style);
return matchedColor ? { color: matchedColor } : false;
},
},
],
toDOM: (node) => {
// rgba() throws for a color it cannot parse, which would stop the
// document rendering.
const color = validateColorHex(node.attrs.color ?? "")
? node.attrs.color
: null;
return [
"mark",
{
"data-color": color,
style: `background-color: ${rgba(
color || Highlight.presetColors[0].hex,
Highlight.opacity
)}`,
},
];
},
};
}
inputRules({ type }: { type: MarkType }) {
return [markInputRuleForPattern("==", type)];
}
keys({ type }: { type: MarkType }) {
return {
"Mod-Shift-h": toggleMark(type),
};
}
get rulePlugins() {
return [markRule({ delim: "==", mark: "highlight" })];
}
toMarkdown() {
return {
open: "==",
close: "==",
mixable: true,
expelEnclosingWhitespace: true,
};
}
parseMarkdown() {
return { mark: "highlight" };
}
}