Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(web-crawler): prevent happy-dom CSS parsing crash in htmlToMarkdown (#13652)

Arvin Xua6e330cfa938
diff --git a/packages/web-crawler/src/utils/htmlToMarkdown.test.ts b/packages/web-crawler/src/utils/htmlToMarkdown.test.ts
index 611d44743b5..2d021aeb8ee 100644
--- a/packages/web-crawler/src/utils/htmlToMarkdown.test.ts
+++ b/packages/web-crawler/src/utils/htmlToMarkdown.test.ts
@@ -50,6 +50,39 @@ describe('htmlToMarkdown', () => {
expect(result.content.length).toBeLessThan(html.length);
}, 20000);
+ it('should not crash on HTML with invalid CSS selectors (LOBE-6869)', () => {
+ // Regression: happy-dom throws TypeError on pages with CSS selectors it cannot parse.
+ // htmlToMarkdown must not propagate this β€” it should fall back to raw HTML conversion.
+ const html = `
+ <html><head>
+ <style>:is(.foo, :has(> .bar)) { color: red }</style>
+ </head><body>
+ <script type="application/ld+json">{"@type":"Article","name":"Test"}</script>
+ <p>Valid content here</p>
+ </body></html>`;
+
+ const result = htmlToMarkdown(html, { url: 'https://example.com', filterOptions: {} });
+
+ expect(result).toBeDefined();
+ expect(result.content).toContain('Valid content');
+ });
+
+ it('should not crash on HTML with external stylesheet links (LOBE-6869)', () => {
+ // Regression: happy-dom's HTMLLinkElement.#loadStyleSheet can crash on CSS parsing.
+ // disableCSSFileLoading should prevent this path entirely.
+ const html = `
+ <html><head>
+ <link rel="stylesheet" href="https://example.com/styles.css">
+ </head><body>
+ <p>Content with external CSS</p>
+ </body></html>`;
+
+ const result = htmlToMarkdown(html, { url: 'https://example.com', filterOptions: {} });
+
+ expect(result).toBeDefined();
+ expect(result.content).toContain('Content with external CSS');
+ });
+
it('should not truncate HTML under 1 MB', () => {
const html = '<html><body><p>Small content</p></body></html>';
diff --git a/packages/web-crawler/src/utils/htmlToMarkdown.ts b/packages/web-crawler/src/utils/htmlToMarkdown.ts
index c964b352d42..d4a55edcd4a 100644
--- a/packages/web-crawler/src/utils/htmlToMarkdown.ts
+++ b/packages/web-crawler/src/utils/htmlToMarkdown.ts
@@ -31,13 +31,21 @@ export const htmlToMarkdown = (
{ url, filterOptions }: { filterOptions: FilterOptions; url: string },
): HtmlToMarkdownOutput => {
const html = rawHtml.length > MAX_HTML_SIZE ? rawHtml.slice(0, MAX_HTML_SIZE) : rawHtml;
- const window = new Window({ url });
+ const window = new Window({
+ settings: { disableCSSFileLoading: true, disableJavaScriptEvaluation: true },
+ url,
+ });
const document = window.document;
document.body.innerHTML = html;
- // @ts-expect-error reason: Readability expects a Document type
- const parsedContent = new Readability(document).parse();
+ let parsedContent: ReturnType<Readability<string>['parse']> = null;
+ try {
+ // @ts-expect-error reason: Readability expects a Document type
+ parsedContent = new Readability(document).parse();
+ } catch {
+ // happy-dom may throw on pages with invalid CSS selectors β€” fall back to raw HTML
+ }
const useReadability = filterOptions.enableReadability ?? true;