Skip to content

fix: reject stream peeks beyond known EOF before allocating lookahead - #1345

Merged
Borewit merged 1 commit into
masterfrom
fix/known-size-stream-peek
Oct 25, 2026
Merged

Borewit merged 1 commit into
masterfrom
fix/known-size-stream-peek

Conversation

@Borewit

@Borewit Borewit commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Resolves the issue where a nonempty stream peek at or beyond the known input size allocates lookahead from an untrusted offset before discovering EOF.

Return zero for mayBeLess peeks or reject with EndOfStreamError before allocation or source I/O, including through tokenizer views. Preserve zero-length peeks and partial EOF behavior for requests starting inside the advertised input.

Summary by CodeRabbit

  • Bug Fixes
    • Peeking at or beyond the known end of a stream now consistently returns zero when shorter reads are allowed, or reports an end-of-stream error otherwise. These peeks leave the input position unchanged.
  • Documentation
    • Clarified how peeking behaves at the end of streams with a known size, including through nested views.

@Borewit Borewit added bug Existing functionality behaves incorrectly or produces unexpected results. security Changes that address security concerns, including dependency security updates. labels Oct 25, 2026
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 7a338e4 and d9e92b3.

Files selected for processing: 3
  • README.md
  • lib/ReadStreamTokenizer.ts
  • test/test-truncation.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

For streams with a known size, nonempty peeks at or beyond EOF return zero when mayBeLess is true or throw EndOfStreamError otherwise. The check occurs before lookahead allocation or source I/O. Tests cover direct and nested tokenizer views.

Changes: Known-size peek behavior

Layer / File(s) Summary
EOF preflight and validation
lib/ReadStreamTokenizer.ts, README.md, test/test-truncation.ts
peekBuffer checks the known size before lookahead allocation or source I/O. The README documents the behavior, and tests cover both mayBeLess settings, view depths, unchanged buffers and positions, and subsequent reads.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • Borewit/strtok3#1344: Adds a similar known-size EOF preflight check to ignore(), while this PR applies it to peekBuffer().
Merge Risk: Minimal · up to d9e92

The known-size EOF behavior appears aligned across the change and its tests; no merge-blocking issue is identified.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: rejecting stream peeks beyond known EOF before allocating lookahead.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.81%. Comparing base (7a338e4) to head (d9e92b3).

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Borewit
Borewit merged commit 8420352 into master Oct 25, 2026
31 checks passed
@Borewit
Borewit deleted the fix/known-size-stream-peek branch October 25, 2026 16:33
@Borewit Borewit removed the bug Existing functionality behaves incorrectly or produces unexpected results. label Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Changes that address security concerns, including dependency security updates.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant