Repository navigation
refactor: decompose monolithic modules + sliding-window crawler - #2
Conversation
Summary of ChangesHello @teng-lin, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on significant refactoring efforts to improve the modularity, maintainability, and performance of the content extraction and HTTP fetching mechanisms. By decomposing large files into smaller, focused units and upgrading the crawler's concurrency model, the codebase becomes more organized and efficient. These changes aim to streamline development and enhance the system's ability to handle diverse web content more effectively. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request significantly refactors the project by decomposing monolithic content-extractors.ts and http-fetch.ts modules into smaller, more maintainable sub-modules, and optimizes crawling throughput with a new sliding-window implementation using Promise.race. However, a critical Cross-Site Scripting (XSS) vulnerability was identified in the new json-ld-extractor.ts due to a lack of sanitization and improper handling of HTML tags in the textContent field; this strategy needs to be updated to match existing robust extraction methods. Furthermore, a high-severity suggestion has been made to enhance the new crawler logic with robust error handling, including logging non-critical failures at a debug level for traceability, to prevent potential crashes.
| (item.articleBody as string) ?? (item.text as string) ?? (item.description as string); | ||
| if (!content) return null; | ||
|
|
||
| return { | ||
| title: (item.headline as string) ?? (item.name as string) ?? null, | ||
| byline: extractAuthorFromJsonLd(item.author), | ||
| content, | ||
| textContent: content, | ||
| excerpt: (item.description as string) ?? null, | ||
| }; | ||
| } |
There was a problem hiding this comment.
The json-ld extraction strategy extracts article content from application/ld+json script tags but fails to sanitize the extracted HTML or properly extract plain text for the textContent field.
While other strategies like next-data explicitly check for HTML and apply sanitizeHtml and extractTextFromHtml, the json-ld strategy returns the raw string from the JSON-LD articleBody, text, or description fields for both content and textContent.
Furthermore, json-ld is included in SKIP_CLEANUP_METHODS in src/extract/content-extractors.ts, meaning it bypasses the cleanExtractedHtml step. If an attacker-controlled page provides malicious HTML within a JSON-LD script, this tool will return that unsanitized HTML in the content field. If a consumer of this library renders this content as HTML, it leads to a Cross-Site Scripting (XSS) vulnerability. Additionally, the textContent field will contain raw HTML tags instead of plain text, which may break downstream logic expecting plain text.
Recommended remediation: Check if the extracted content contains HTML, and if so, apply sanitizeHtml to the content field and use htmlToText (or extractTextFromHtml) for the textContent field.
References
- Sanitize HTML content constructed from external API responses to prevent XSS by removing dangerous tags such as
<script>,<style>, and<iframe>.
Re: JSON-LD XSS concern (gemini-code-assist)This is pre-existing behavior on That said, the observation is valid as a pre-existing concern. JSON-LD |
|
Thanks for the clarification, @teng-lin! I understand that the JSON-LD XSS concern is pre-existing and not introduced by this refactor. It's great to hear that you're tracking it as a follow-up for sanitization. Appreciate the diligence! |
- Decompose content-extractors.ts (1281 → ~400 lines) into 7 focused sub-modules: metadata, readability, selector, json-ld, next-data, text-density, and RSC extractors. Barrel re-exports preserve all existing import paths. - Decompose http-fetch.ts (1074 → ~700 lines) by extracting WP REST API logic into wp-rest-api.ts and Next.js data route logic into next-data-route.ts. - Replace crawler batch-wait (Promise.all) with sliding-window concurrency (Promise.race + Map) for better throughput with non-uniform fetch times. - Promote RequestContext interface to shared fetch/types.ts. - Eliminate duplicate htmlToText by importing shared utility. - Extract attachRawHtml helper to reduce repeated keepRawHtml patterns. All 683 tests pass unchanged — no test modifications needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5c8de48 to
49214a1
Compare
JSON-LD articleBody/text fields can contain raw HTML with script tags, event handlers, and dangerous URI schemes. Apply sanitizeHtml to strip dangerous elements and htmlToText to produce clean textContent, matching the pattern used by next-data-extractor. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
content-extractors.ts(1281 → ~400 lines) into 7 focused sub-modules: metadata, readability, selector, json-ld, next-data, text-density, and RSC extractors. Barrel re-exports preserve all existing import paths — zero test changes needed.http-fetch.ts(1074 → ~700 lines) by extracting WP REST API logic intowp-rest-api.tsand Next.js data route logic intonext-data-route.ts.Promise.all) with sliding-window concurrency (Promise.race+Map<number, Promise>) for better throughput with non-uniform fetch times.RequestContextinterface to sharedfetch/types.ts.htmlToTextby importing shared utility with|| nullsemantic equivalence.attachRawHtmlhelper to reduce 7 repeatedkeepRawHtmlpatterns.New files (9)
src/extract/metadata-extractors.tssrc/extract/readability-extractor.tssrc/extract/selector-extractor.tssrc/extract/json-ld-extractor.tssrc/extract/next-data-extractor.ts__NEXT_DATA__extractionsrc/extract/text-density-extractor.tssrc/extract/rsc-extractor.tssrc/fetch/wp-rest-api.tssrc/fetch/next-data-route.ts/_next/data/route fetchTest plan
npm run lint— 0 errorsnpm run format:check— all files passnpm run build— TypeScript strict mode compiles🤖 Generated with Claude Code