Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apps/server/src/htmlRender/HtmlRender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,19 @@ describe("HtmlRender", () => {
"<style>.x\\'y{height:100vh;--label:'ok'}</style>",
`<main style="--a:&quot;'&quot;;height:100vh;--b:&quot;'&quot;">x</main>`,
`<script>document.write("<style>" + 'main{height:100vh}' + "</style>");</script>`,
'<textarea title="1 > 0" style="height: 100vh">Example</textarea>',
'<TITLE>Example</title ><div style="height: 100vh">Content</div>',
'<textarea-demo style="height: 100vh">Content</textarea-demo>',
"<svg><title><style>body{min-height:100vh}</style></title></svg>",
'<svg><title/></svg><div style="height: 100vh">Content</div>',
'<math><mtext></svg><textarea><div style="height:100vh">',
'<svg><![CDATA[</svg><textarea>]]></svg><main style="height:100vh">Content</main>',
'<svg><p><![CDATA[><div style="height:100vh">x</div>]]>',
"<!example <textarea>><style>main{height:100vh}</style>",
'<div></div data-example="<textarea>"><main style="height:100vh">Content</main>',
]) {
const error = yield* htmlRender.prepare(page).pipe(Effect.flip);
expect(error).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError);
expect(error, page).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError);
}
yield* htmlRender.prepare(
'<style>main{width:100vw;height:320px}</style><img src="data:image/png;base64,AB1vh+2vh/3vhC">',
Expand All @@ -179,6 +189,19 @@ describe("HtmlRender", () => {
);
yield* htmlRender.prepare(`<style>${"1".repeat(200_000)}vx</style>`);
yield* htmlRender.prepare(`<style>p::after{content:"\\"100vh\\""}</style><p>Unit: </p>`);
const example = '<div style="height: 100vh">Example</div>';
for (const tag of ["textarea", "title", "xmp", "noembed", "noframes", "noscript"]) {
const html = `<${tag}></${tag}-example>${example}<style>body{height:100vh}</style></${tag}>`;
expect(yield* htmlRender.prepare(html)).toContain(html);
}
for (const html of [
`<plaintext>${example}</plaintext>${example}`,
`<textarea/>${example}</textarea>`,
`<svg/><title>${example}</title>`,
`<svg><svg></svg></svg><title>${example}</title>`,
]) {
expect(yield* htmlRender.prepare(html)).toContain(html);
}
}).pipe(Effect.provide(layerTest)),
);

Expand Down
66 changes: 61 additions & 5 deletions apps/server/src/htmlRender/HtmlRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,25 @@ export class HtmlRenderViewportHeightError extends Schema.TaggedError<HtmlRender
}

// Check stylesheets, style attributes, and scripts (which may write either); skip comments whole.
// End tags are consumed whole so a `<` in their attributes does not open a tag.
// Consume unclosed comments, tags, or quotes through EOF to keep every pattern linear.
const HTML_CSS_SOURCE =
/<!--[\s\S]*?(?:-->|$)|<script(?=[\s/>])((?:=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)|[^>])*)(?:>|$)([\s\S]*?)(?:<\/script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:<\/style\s*>|$)|<[a-z]((?:=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)|[^>])*)/gi;
const HTML_CSS_ATTRIBUTE_VALUE = String.raw`=\s*(?:"[^"]*(?:"|$)|'[^']*(?:'|$)|[^\s>]*)`;
const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*`;
const HTML_TEXT_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|/(?!>)|[^>/])*`;
const HTML_CSS_SOURCE = new RegExp(
String.raw`<!--[\s\S]*?(?:-->|$)|<\?[^>]*(?:>|$)|<!\[(CDATA)\[|<![^>]*(?:>|$)` +
String.raw`|<script(?=[\s/>])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` +
String.raw`|</([a-z][^\t\n\f\r />]*)${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)` +
String.raw`|<([a-z][^\t\n\f\r />]*)(${HTML_TEXT_TAG_ATTRIBUTES})(\/?)(?:>|$)`,
Comment thread
saphid marked this conversation as resolved.
Resolved
"gi",
);
// Text-only elements whose body the browser never parses as markup in HTML content.
const HTML_TEXT_END_PATTERNS = new Map(
["textarea", "title", "xmp", "iframe", "noembed", "noframes", "noscript"].map((tag) => [
tag,
new RegExp(String.raw`</${tag}(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)`, "gi"),
]),
);
// Inside a tag, skip other attributes' quoted values, such as a title that mentions style=.
// A quote opens a value only right after `=`; an unquoted value such as `it's` or `a="b` runs to a space.
const TAG_STYLE =
Expand Down Expand Up @@ -147,16 +163,56 @@ const findViewportHeight = (source: string, pattern: RegExp) => {
return undefined;
};

// Inside inline SVG or MathML a textarea or title body is markup, so it is checked like any other.
// Leaving foreign content only on its own end tag errs towards checking more.
const rejectViewportHeight = (html: string) => {
for (const [, scriptTag, script, sheet, tag] of html.matchAll(HTML_CSS_SOURCE)) {
const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags);
const foreign: Array<string> = [];
// HTML content nested in SVG ends a `<![CDATA[` at the first `>`, so its body is still checked;
// it only stops tags from changing the scanner's state.
let cdataEnd = -1;
for (let match = pattern.exec(html); match !== null; match = pattern.exec(html)) {
const [, cdata, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] =
match;
const inCdata = match.index < cdataEnd;
if (cdata !== undefined) {
if (inCdata) continue;
if (cdata === "CDATA" && foreign.length > 0) {
const endIndex = html.indexOf("]]>", pattern.lastIndex);
cdataEnd = endIndex === -1 ? html.length : endIndex;
continue;
}
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
const endIndex = html.indexOf(">", pattern.lastIndex);
if (endIndex === -1) break;
pattern.lastIndex = endIndex + 1;
continue;
}
if (closeTag !== undefined) {
const index = foreign.lastIndexOf(closeTag.toLowerCase());
if (index !== -1 && !inCdata) foreign.length = index;
continue;
}
const found =
script !== undefined
? (findViewportHeight(scriptTag ?? "", TAG_STYLE) ??
findViewportHeight(script, SCRIPT_STYLE))
: tag !== undefined
? findViewportHeight(tag, TAG_STYLE)
: tagAttributes !== undefined
? findViewportHeight(tagAttributes, TAG_STYLE)
: findInCss(sheet ?? "");
if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found }));
const tag = tagName?.toLowerCase();
if (tag === undefined || inCdata) continue;
if (tag === "svg" || tag === "math") {
if (!selfClosing) foreign.push(tag);
continue;
}
if (foreign.length > 0) continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium htmlRender/HtmlRender.ts:208

This scanner accepts a document with an active height:100vh style after <main> because the literal </svg> and <title> inside a <textarea> nested in <foreignObject> clear foreign and make the scanner stop at EOF. Track HTML integration points such as foreignObject and apply RCDATA handling to textarea so its text cannot change the foreign-content state or hide later styles.

🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/htmlRender/HtmlRender.ts around line 208:

This scanner accepts a document with an active `height:100vh` style after `<main>` because the literal `</svg>` and `<title>` inside a `<textarea>` nested in `<foreignObject>` clear `foreign` and make the scanner stop at EOF. Track HTML integration points such as `foreignObject` and apply RCDATA handling to `textarea` so its text cannot change the foreign-content state or hide later styles.

if (tag === "plaintext") break;
const endPattern = HTML_TEXT_END_PATTERNS.get(tag);
if (endPattern === undefined) continue;
endPattern.lastIndex = pattern.lastIndex;
if (endPattern.exec(html) === null) break;
pattern.lastIndex = endPattern.lastIndex;
}
return Effect.void;
};
Expand Down
Loading