feat(reader-core): add parser timeouts for EPUB content parsing#842
Merged
Conversation
Implements TIER-1 requirement: parser timeouts to prevent infinite loops on malformed EPUB input. Adds withTimeout/checkDeadline/ createDeadline utilities in shared package, then integrates them across all EPUB parsing entry points: - Archive validation (10s) - DOMPurify sanitization (5s) - OPF/XML regex parsing (2s) - Accessibility metadata parsing (2s) - Reanchor text matching (15s) - Total load pipeline (30s) Also forwards AbortSignal through epub-loader for proper cancellation, and delegates worker-side validation to shared validateArchive (removing 78 lines of duplicated code). 26 new tests across 3 test files. All 1840 tests pass.
Deploying do-epub-studio with
|
| Latest commit: |
f6657ca
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://d4854f64.do-epub-studio.pages.dev |
| Branch Preview URL: | https://feat-parser-timeouts.do-epub-studio.pages.dev |
🚀 Performance Report⚡ Startup & Interaction
🛠️ CI & Workflow
|
Contributor
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 69 |
| Duplication | -14 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
withTimeout/checkDeadline/createDeadlineutilities in@do-epub-studio/sharedfor parser timeout enforcementAbortSignalthroughepub-loaderfor proper cancellationvalidateArchive(removes 78 lines of duplicated code)Motivation
Satisfies TIER-1 AGENTS.md requirement: "MUST apply parser timeouts to EPUB content parsing to prevent infinite loops on malformed input."
Default Timeout Budget
Files Changed
packages/shared/src/timeout.ts— new — core timeout utilitiespackages/shared/src/errors.ts— addedTimeoutErrorclasspackages/shared/src/index.ts— barrel exportpackages/shared/src/__tests__/timeout.test.ts— new — 10 testspackages/reader-core/src/archive-validator.ts— timeout wrapperpackages/reader-core/src/sanitizer.ts— deadline checks between DOMPurify passespackages/reader-core/src/fixed-layout.ts— deadline checks in OPF parsingpackages/reader-core/src/epub-accessibility.ts— deadline checks in accessibility parsingpackages/reader-core/src/epub-loader.ts— top-levelwithTimeout+ signal forwardingpackages/reader-core/src/reanchor.ts— deadline checks in reanchor loopspackages/reader-core/src/epub-parser.worker.ts— delegated to sharedvalidateArchivepackages/reader-core/src/epub-parser-worker.ts— timeout options for fallback pathpackages/reader-core/src/__tests__/archive-validator-timeout.test.ts— new — 6 testspackages/reader-core/src/__tests__/sanitizer-timeout.test.ts— new — 5 testsTest Results
All 1,840 tests pass across 7 packages. Quality gate (lint + typecheck) passes.
Design Decisions
checkDeadline()for sync code paths — DOMPurify and regex loops must remain synchronous for epubjs hook compatibility; usesperformance.now()deadline checks every N iterationswithTimeout()for async code paths — UsesAbortController/AbortSignalpattern for archive validation, reanchor, and top-level load pipeline