Skip to content

Commit ab920ac

Browse files
t3dotggclaude
andcommitted
fix(server): skip comments and read unquoted style values in the vh check
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent aeff09c commit ab920ac

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎apps/server/src/htmlRender/HtmlRender.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,17 @@ describe("HtmlRender", () => {
128128
"<style>main{min-height:100vh}</style>",
129129
"<style media=screen>main{height: calc(100dvh - 2rem)}",
130130
"<div style='top:.5svh'></div>",
131+
"<div style=height:100vh></div>",
131132
]) {
132133
const error = yield* htmlRender.prepare(page).pipe(Effect.flip);
133134
expect(error).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError);
134135
}
135136
yield* htmlRender.prepare(
136137
'<style>main{width:100vw;height:320px}</style><img src="data:image/png;base64,AB1vh+2vh/3vhC">',
137138
);
138-
yield* htmlRender.prepare("<p>Avoid 100vh here.</p><script>const unit = '100vh';</script>");
139+
yield* htmlRender.prepare(
140+
"<p>Avoid 100vh here.</p><!-- <div style=\"height:100vh\"></div> --><script>const unit = '100vh';</script>",
141+
);
139142
yield* htmlRender.prepare(`<style>${"1".repeat(200_000)}vx</style>`);
140143
}).pipe(Effect.provide(layerTest)),
141144
);

‎apps/server/src/htmlRender/HtmlRender.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,19 @@ export class HtmlRenderViewportHeightError extends Schema.TaggedError<HtmlRender
7777
}
7878

7979
// Only stylesheets and style attributes are checked, so page text that mentions
80-
// a unit passes. An unclosed tag or quote runs to the end of the page, which
81-
// keeps both patterns linear on hostile input.
80+
// a unit passes; comments match first so their contents are skipped. Scripts
81+
// are still checked, since markup they insert applies. An unclosed comment,
82+
// tag, or quote runs to the end of the page, which keeps both patterns linear
83+
// on hostile input.
8284
const CSS_SOURCE =
83-
/<style\b[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$))/gi;
85+
/<!--[\s\S]*?(?:-->|$)|<style\b[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|\sstyle\s*=\s*(?:"([^"]*)(?:"|$)|'([^']*)(?:'|$)|([^\s>]+))/gi;
8486
// The lookbehind skips base64 runs, such as an inlined image that happens to contain "1vh".
8587
const VIEWPORT_HEIGHT_UNIT = /(?<![\w.+/])(?:\d+(?:\.\d+)?|\.\d+)[sld]?vh(?![\w-])/i;
8688

8789
const rejectViewportHeight = (html: string) => {
88-
for (const [, sheet, doubleQuoted, singleQuoted] of html.matchAll(CSS_SOURCE)) {
89-
const found = VIEWPORT_HEIGHT_UNIT.exec(sheet ?? doubleQuoted ?? singleQuoted ?? "")?.[0];
90+
for (const [, sheet, doubleQuoted, singleQuoted, unquoted] of html.matchAll(CSS_SOURCE)) {
91+
const css = sheet ?? doubleQuoted ?? singleQuoted ?? unquoted ?? "";
92+
const found = VIEWPORT_HEIGHT_UNIT.exec(css)?.[0];
9093
if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found }));
9194
}
9295
return Effect.void;

0 commit comments

Comments
 (0)