Skip to content

Build your personal knowledge base with Trilium Notes

fix(icon packs): resolve CSS escape sequences in manifest glyphs

Elian Doranfc1d39128994
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html
index 56e2800e6c..315ecc805c 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html
@@ -98,7 +98,9 @@
<aside class="admonition note">
<ul>
<li>You can supply glyph as the escaped <code spellcheck="false">\uXXXX</code> sequence
- or as the actual UTF‑8 character.</li>
+ or as the actual UTF‑8 character. The CSS form (<code spellcheck="false">\ea3f</code>,
+ as written in a stylesheet's <code spellcheck="false">content</code>) is accepted
+ too, so glyphs copied out of the font's own CSS work as they stand.</li>
<li>It is also possible to use the unescaped glyph inside the JSON. It will
appear strange (e.g. ), but it will be rendered properly regardless.</li>
<li>The manifest keys (e.g. <code spellcheck="false">ph-acorn</code>) should
@@ -184,7 +186,10 @@ processIconPack("light");</code></pre>
</p>
<p>The Unicode escape syntax is different in CSS (<code spellcheck="false">"\ea3f"</code>)
when compared to JSON (<code spellcheck="false">"\uea3f"</code>). Notice
- how the JSON escape is <code spellcheck="false">\u</code> and not <code spellcheck="false">\</code>.</p>
+ how the JSON escape is <code spellcheck="false">\u</code> and not <code spellcheck="false">\</code>.
+ Both forms are accepted: keep the CSS escape by writing the backslash itself
+ as <code spellcheck="false">"\\ea3f"</code> in the JSON, or convert it to the
+ JSON escape <code spellcheck="false">"\uea3f"</code>.</p>
<p>As a more compact alternative, provide the un-escaped character directly,
as UTF-8 is supported.</p>
</aside>
diff --git a/docs/User Guide/User Guide/Theme development/Creating an icon pack.md b/docs/User Guide/User Guide/Theme development/Creating an icon pack.md
index 6c9c4deee7..7652458953 100644
--- a/docs/User Guide/User Guide/Theme development/Creating an icon pack.md
+++ b/docs/User Guide/User Guide/Theme development/Creating an icon pack.md
@@ -61,7 +61,7 @@ Example minimal manifest:
```
> [!NOTE]
-> * You can supply glyph as the escaped `\uXXXX` sequence or as the actual UTF‑8 character.
+> * You can supply glyph as the escaped `\uXXXX` sequence or as the actual UTF‑8 character. The CSS form (`\ea3f`, as written in a stylesheet's `content`) is accepted too, so glyphs copied out of the font's own CSS work as they stand.
> * It is also possible to use the unescaped glyph inside the JSON. It will appear strange (e.g. ), but it will be rendered properly regardless.
> * The manifest keys (e.g. `ph-acorn`) should match the class names used by the font (prefix + name is a common pattern).
> * Like the prefix, a manifest key becomes a CSS class name, so it can contain only letters, digits, hyphens, underscores and non-ASCII characters. Punctuation and whitespace (e.g. `ph.acorn` or `ph acorn`) are not usable as class names; icons with such a key are skipped and an error is logged in <a class="reference-link" href="../Troubleshooting/Error%20logs/Backend%20(server)%20logs.md">Backend (server) logs</a>, while the rest of the pack still loads.
@@ -142,7 +142,7 @@ What to do with the script:
> [!TIP]
> **Mind the escape format when processing CSS**
>
-> The Unicode escape syntax is different in CSS (`"\ea3f"`) when compared to JSON (`"\uea3f"`). Notice how the JSON escape is `\u` and not `\`.
+> The Unicode escape syntax is different in CSS (`"\ea3f"`) when compared to JSON (`"\uea3f"`). Notice how the JSON escape is `\u` and not `\`. Both forms are accepted: keep the CSS escape by writing the backslash itself as `"\\ea3f"` in the JSON, or convert it to the JSON escape `"\uea3f"`.
>
> As a more compact alternative, provide the un-escaped character directly, as UTF-8 is supported.
diff --git a/packages/trilium-core/src/services/icon_packs.spec.ts b/packages/trilium-core/src/services/icon_packs.spec.ts
index dfa4979db9..f8c06820b2 100644
--- a/packages/trilium-core/src/services/icon_packs.spec.ts
+++ b/packages/trilium-core/src/services/icon_packs.spec.ts
@@ -211,6 +211,16 @@ describe("Generating CSS for untrusted manifests", () => {
expect(css).toContain(`.un.utf-\u{1F600}::before { content: "\u{1F600}"; }`);
});
+ it("resolves CSS escape sequences in glyphs, which packs copied from a stylesheet carry", () => {
+ const css = generateCssFor({
+ "fa-0": { glyph: "\\30 ", terms: [ "0" ] },
+ "fa-house": { glyph: "\\f015", terms: [ "house" ] }
+ });
+
+ expect(css).toContain(`.un.fa-0::before { content: "0"; }`);
+ expect(css).toContain(`.un.fa-house::before { content: "\uf015"; }`);
+ });
+
it("escapes a glyph so it cannot end the style element it is served in", () => {
const css = generateCssFor({
"un-evil": { glyph: `</style><script>alert(1)</script>`, terms: [ "evil" ] }
diff --git a/packages/trilium-core/src/services/icon_packs.ts b/packages/trilium-core/src/services/icon_packs.ts
index 9d1c49f8d0..48487e11d3 100644
--- a/packages/trilium-core/src/services/icon_packs.ts
+++ b/packages/trilium-core/src/services/icon_packs.ts
@@ -5,7 +5,7 @@ import type BNote from "../becca/entities/bnote";
import boxiconsManifest from "./icon_pack_boxicons-v2.json" with { type: "json" };
import { getLog } from "./log";
import search from "./search/services/search";
-import { escapeCssString, safeExtractMessageAndStackFromError } from "./utils/index";
+import { decodeCssEscapes, escapeCssString, safeExtractMessageAndStackFromError } from "./utils/index";
const PREFERRED_MIME_TYPE = [
"font/woff2",
@@ -169,7 +169,7 @@ export function generateCss({ manifest, fontMime, builtin, fontAttachmentId, pre
continue;
}
- const glyph = escapeCssString(String(mapping.glyph ?? ""));
+ const glyph = escapeCssString(decodeCssEscapes(String(mapping.glyph ?? "")));
iconDeclarations.push(`.${prefix}.${key}::before { content: "${glyph}"; }`);
}
diff --git a/packages/trilium-core/src/services/task_states.ts b/packages/trilium-core/src/services/task_states.ts
index f5a4d781c6..2a992d9968 100644
--- a/packages/trilium-core/src/services/task_states.ts
+++ b/packages/trilium-core/src/services/task_states.ts
@@ -7,7 +7,7 @@ import BAttribute from "../becca/entities/battribute.js";
import type BNote from "../becca/entities/bnote.js";
import { getIconPacks } from "./icon_packs.js";
import noteService from "./notes.js";
-import { escapeCssString } from "./utils/index.js";
+import { decodeCssEscapes, escapeCssString } from "./utils/index.js";
/**
* Returns the task states from the `_taskStates` hidden subtree, in note order.
@@ -186,7 +186,7 @@ export function generateTaskStateCss(): string {
const hue = color ? computeHue(color) : undefined;
rules.push(`[data-trilium-task-state="${name}"], .tn-task-checkbox[data-trilium-task-state="${name}"] {
- --task-state-glyph: "${escapeCssString(resolved.glyph)}";
+ --task-state-glyph: "${escapeCssString(decodeCssEscapes(resolved.glyph))}";
--task-state-glyph-font-family: "${escapeCssString(resolved.fontFamily)}";
--task-state-color: ${color || "inherit"};
--task-state-hue: ${hue ?? "unset"};
diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.ts
index 2b1b8ff1bb..f288663cfd 100644
--- a/packages/trilium-core/src/services/utils/index.spec.ts
+++ b/packages/trilium-core/src/services/utils/index.spec.ts
@@ -1091,6 +1091,38 @@ describe("#escapeCssString", () => {
});
});
+describe("#decodeCssEscapes", () => {
+ it("decodes a hex escape with or without its terminating space", () => {
+ expect(utils.decodeCssEscapes("\\30 ")).toBe("0");
+ expect(utils.decodeCssEscapes("\\f015")).toBe("\uf015");
+ expect(utils.decodeCssEscapes("\\1F600")).toBe("\u{1F600}");
+ expect(utils.decodeCssEscapes("\\61\\62")).toBe("ab");
+ });
+
+ it("resolves the code points CSS maps to the replacement character", () => {
+ expect(utils.decodeCssEscapes("\\0")).toBe("\uFFFD");
+ expect(utils.decodeCssEscapes("\\d800 ")).toBe("\uFFFD");
+ expect(utils.decodeCssEscapes("\\ffffff")).toBe("\uFFFD");
+ });
+
+ it("leaves text carrying no hex escape untouched", () => {
+ expect(utils.decodeCssEscapes("")).toBe("");
+ expect(utils.decodeCssEscapes("\uf015")).toBe("\uf015");
+ expect(utils.decodeCssEscapes("</style>")).toBe("</style>");
+ expect(utils.decodeCssEscapes("\\")).toBe("\\");
+ });
+
+ it("cannot widen what the generated stylesheet emits, since escaping follows it", () => {
+ // A glyph spelling `</style>` as escapes decodes to the markup, then escapes back to it.
+ const smuggled = "\\3c /style\\3e ";
+ expect(utils.decodeCssEscapes(smuggled)).toBe("</style>");
+ expect(utils.escapeCssString(utils.decodeCssEscapes(smuggled))).toBe("\\3c /style\\3e ");
+
+ // A backslash reaching the output would let the glyph end its own CSS string.
+ expect(utils.escapeCssString(utils.decodeCssEscapes("\\5c "))).toBe("\\5c ");
+ });
+});
+
describe("#escapeInlineStylesheet", () => {
it("escapes every closing style tag, whatever its casing", () => {
const escaped = utils.escapeInlineStylesheet(`.a::before { content: "</style><SCRIPT>x</STYLE>"; }`);
diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.ts
index ec395fe565..8547db50dc 100644
--- a/packages/trilium-core/src/services/utils/index.ts
+++ b/packages/trilium-core/src/services/utils/index.ts
@@ -234,6 +234,22 @@ export function escapeCssString(value: string): string {
return value.replace(/["'\\<>&\u0000-\u001F\u007F]/g, (char) => `\\${char.charCodeAt(0).toString(16)} `);
}
+/**
+ * Decodes the CSS escape sequences (`\30 `, `\f015`) an icon pack manifest carries when its
+ * glyphs were copied out of a stylesheet instead of written as characters. Callers pass the
+ * result to `escapeCssString()`, which re-escapes a decoded `<` or `"` rather than emitting
+ * it raw, so decoding widens the accepted input without widening the output. A backslash
+ * that starts no hex sequence stays literal text. Code points CSS resolves to U+FFFD — zero,
+ * surrogates, and anything past the Unicode range — resolve to it here too.
+ */
+export function decodeCssEscapes(value: string): string {
+ return value.replace(/\\([0-9a-fA-F]{1,6})(?:\r\n|[ \t\n\f\r])?/g, (_, hex: string) => {
+ const codePoint = parseInt(hex, 16);
+ const isSurrogate = codePoint >= 0xD800 && codePoint <= 0xDFFF;
+ return (codePoint === 0 || codePoint > 0x10FFFF || isSurrogate) ? "\uFFFD" : String.fromCodePoint(codePoint);
+ });
+}
+
/**
* Escapes the `</style` sequences in `stylesheet` so it can be embedded in an inline
* `<style>` element without ending it early. `\3c ` is the CSS escape for `<`, so a