Repository navigation
fix(server): html_render no longer rejects viewport height examples inside textarea or title text #48327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(server): html_render no longer rejects viewport height examples inside textarea or title text #48327
Changes from all commits
5739e5c
9615073
ebc0ab7
100fa1d
813c72b
918a9dc
6eb431e
7157f70
edd3adc
1ea5892
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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})(\/?)(?:>|$)`, | ||
| "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 = | ||
|
|
@@ -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; | ||
| } | ||
|
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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium This scanner accepts a document with an active 🤖 Copy this AI Prompt to have your agent fix this: |
||
| 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; | ||
| }; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.