mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Store: CSS filter bypasses via comments in strings and vendor prefixes
normalizeCss stripped /* */ without knowing about strings or escapes, so content:"/*" ... "*/" hid a url() between them from the checks, and -webkit-image-set( slipped past the function-name boundary. The checks now run with and without comment stripping and allow a vendor prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
512366bc94
commit
deff678cfa
2 files changed
+32
-7
No files matched your search
+11
-7
@@ -75,9 +75,10 @@ interface StoreTheme {
|
||||
|
||||
// Undo what a plain pattern check would miss: comments splitting a keyword
|
||||
// (u/**/rl), CSS escapes (\75 rl( is url(), and case.
|
||||
function normalizeCss(css: string): string {
|
||||
return css
|
||||
.replace(/\/\*[\s\S]*?\*\//g, "")
|
||||
// Comment stripping isn't string-aware ("/*" inside content:"..."), so the
|
||||
// checks run on both variants: with and without comments removed.
|
||||
function normalizeCss(css: string, stripComments: boolean): string {
|
||||
return (stripComments ? css.replace(/\/\*[\s\S]*?\*\//g, "") : css)
|
||||
.replace(/\\([0-9a-f]{1,6})\s?/gi, (_, hex: string) => {
|
||||
const cp = parseInt(hex, 16);
|
||||
return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : "\ufffd";
|
||||
@@ -94,7 +95,7 @@ const FORBIDDEN_CSS = [
|
||||
/@import/,
|
||||
/@font-face/,
|
||||
/@namespace/,
|
||||
/(?:^|[^a-z-])(?:image-set|image|cross-fade|element|src|expression)\s*\(/,
|
||||
/(?:^|[^a-z])(?:-[a-z]+-)?(?:image-set|image|cross-fade|element|src|expression)\s*\(/,
|
||||
/-moz-binding/,
|
||||
/behavior\s*:/,
|
||||
/javascript:/,
|
||||
@@ -102,7 +103,10 @@ const FORBIDDEN_CSS = [
|
||||
];
|
||||
|
||||
export function isSafeThemeCss(css: string): boolean {
|
||||
const normalized = normalizeCss(css);
|
||||
return isSafeNormalized(normalizeCss(css, true)) && isSafeNormalized(normalizeCss(css, false));
|
||||
}
|
||||
|
||||
function isSafeNormalized(normalized: string): boolean {
|
||||
if (FORBIDDEN_CSS.some((re) => re.test(normalized))) return false;
|
||||
for (const match of normalized.matchAll(/url\s*\(\s*(["']?)\s*([^"')\s]*)/g)) {
|
||||
if (!/^data:image\/(?:png|jpeg|gif|webp|svg\+xml)[;,]/.test(match[2])) return false;
|
||||
@@ -372,8 +376,8 @@ async function handleStoreSubmit(req: IncomingMessage, res: ServerResponse) {
|
||||
|
||||
const themes = await loadStoreThemes();
|
||||
// Re-uploading an installed store theme unchanged would just duplicate it.
|
||||
const cssKey = normalizeCss(css).replace(/\s+/g, "");
|
||||
if (themes.some((t) => normalizeCss(t.css).replace(/\s+/g, "") === cssKey)) {
|
||||
const cssKey = normalizeCss(css, true).replace(/\s+/g, "");
|
||||
if (themes.some((t) => normalizeCss(t.css, true).replace(/\s+/g, "") === cssKey)) {
|
||||
res.writeHead(409, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ error: "duplicate" }));
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Theme CSS from the Design Store must never load anything external.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { isSafeThemeCss } from "../dist/store.js";
|
||||
|
||||
test("theme CSS filter", () => {
|
||||
const evil = "url(https://attacker.example/t.png)";
|
||||
for (const css of [
|
||||
`body{background:${evil}}`,
|
||||
`body{background:\\75rl(https://attacker.example/t.png)}`,
|
||||
`body{background:image-set("https://attacker.example/t.png" 1x)}`,
|
||||
`a{content:"/*"}body{background:${evil}}b{content:"*/"}`,
|
||||
`a{--x:\\/*}body{background:${evil}}b{--y:*/}`,
|
||||
`body{background:-webkit-image-set("https://attacker.example/t.png" 1x)}`,
|
||||
]) assert.equal(isSafeThemeCss(css), false, css);
|
||||
for (const css of [
|
||||
":root{--bg:#123}body{color:red}a:hover{opacity:.5}",
|
||||
"body{background:url(data:image/png;base64,AAAA)}",
|
||||
"/* a comment */ body{color:red}",
|
||||
]) assert.equal(isSafeThemeCss(css), true, css);
|
||||
});
|
||||
Reference in new issue
Block a user