Skip to content

Commit e83ed76

Browse files
committed
fix(studio-server): a CR or form feed in a quoted font name cannot end the @font-face rule
1 parent efc5ede commit e83ed76

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎packages/studio-server/src/helpers/studioFontFace.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ describe("isStudioFontFaceCss", () => {
4545
expect(isStudioFontFaceCss('@font-face { src: url("assets/Brand{1}.ttf"); }')).toBe(true);
4646
expect(isStudioFontFaceCss('@font-face { font-family: "x</style><script>"; }')).toBe(false);
4747
expect(isStudioFontFaceCss('@font-face { font-family: "x; } body{display:none}')).toBe(false);
48-
expect(isStudioFontFaceCss('@font-face { font-family: "x\\"; } body{display:none}')).toBe(false);
49-
expect(isStudioFontFaceCss('@font-face { font-family: "x\\""; } body{display:none}')).toBe(false);
48+
expect(isStudioFontFaceCss('@font-face { font-family: "x\\"; } body{}')).toBe(false);
49+
expect(isStudioFontFaceCss('@font-face { font-family: "x\\""; } body{}')).toBe(false);
50+
expect(isStudioFontFaceCss('@font-face { font-family: "x\r} body{} "; }')).toBe(false);
51+
expect(isStudioFontFaceCss("@font-face { font-family: 'x\\\f} body{} '; }")).toBe(false);
5052
expect(isStudioFontFaceCss('@font-face { src: url("x") } </style><script>')).toBe(false);
5153
expect(isStudioFontFaceCss(42)).toBe(false);
5254
});

‎packages/studio-server/src/helpers/studioFontFace.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const FONT_STYLE_RE = /<style\b[^>]*data-hf-studio-fonts=(["'])true\1[^>]*>([\s\S]*?)<\/style>/i;
22

3-
// Braces only inside quoted strings; `<` nowhere, since `</style` ends the block whatever the quoting.
3+
// Braces only inside quoted strings; `<` nowhere: `</style` ends the block whatever the quoting.
44
const ONE_FONT_FACE_RULE =
5-
/^@font-face \{(?:[^{}<"'\\]|"(?:[^"\\<\n]|\\[^<\n])*"|'(?:[^'\\<\n]|\\[^<\n])*')*\}$/;
5+
/^@font-face \{(?:[^{}<"'\\]|"(?:[^"\\<\n\r\f]|\\[^<\n\r\f])*"|'(?:[^'\\<\n\r\f]|\\[^<\n\r\f])*')*\}$/;
66

77
export function isStudioFontFaceCss(css: unknown): css is string {
88
return typeof css === "string" && ONE_FONT_FACE_RULE.test(css);

‎packages/studio-server/src/routes/files.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ describe("registerFileRoutes", () => {
666666
expect((await patch({ fontFaceCss: `${css} body{display:none}` })).status).toBe(400);
667667
const quotedBreakout = '@font-face { font-family: "x</style><script>"; }';
668668
expect((await patch({ fontFaceCss: quotedBreakout })).status).toBe(400);
669+
for (const newline of ["\r", "\f"]) {
670+
const smuggled = `@font-face { font-family: "x${newline}} body{background:red} "; }`;
671+
expect((await patch({ fontFaceCss: smuggled })).status).toBe(400);
672+
}
669673
expect((await patch({ fontFaceCss: css }, { id: "missing" })).status).toBe(200);
670674
expect(readFileSync(join(projectDir, "index.html"), "utf-8")).toBe(original);
671675

0 commit comments

Comments
 (0)