Skip to content

feat(fs): add bounded byte-range reads - #2792

Closed
bajrangCoder wants to merge 1 commit into
mainfrom
feat/fs-range-read
Closed

feat(fs): add bounded byte-range reads#2792
bajrangCoder wants to merge 1 commit into
mainfrom
feat/fs-range-read

Conversation

@bajrangCoder

Copy link
Copy Markdown
Member

No description provided.

@greptile-apps

greptile-apps Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds half-open, validated byte-range reads across local files, Android SAF, SFTP, and HTTP resources.

  • Introduces shared range validation and optional decoding.
  • Adds native Cordova bridge operations for SAF and SFTP bounded reads.
  • Adds local Blob slicing, HTTP Range requests, declarations, and unit/device tests.

Confidence Score: 2/5

The PR should not merge until HTTP responses are bounded during consumption and SAF JSON range decoding preserves the backend's BOM behavior.

The new HTTP method can allocate an arbitrarily large remote response despite its bounded-read contract, and the external-storage wrapper rejects BOM-prefixed JSON that its full-file counterpart accepts.

Files Needing Attention: src/fileSystem/index.js, src/fileSystem/externalFs.js

Security Review

The HTTP path buffers an untrusted partial response before enforcing its requested size, allowing an oversized 206 response to exhaust WebView memory. How this was verified: The optional header guards can be bypassed by omitted headers, after which response.arrayBuffer() consumes the complete body before checking its length.

Important Files Changed

Filename Overview
src/fileSystem/index.js Adds HTTP Range support, but buffers an untrusted response completely before enforcing the requested byte bound.
src/fileSystem/externalFs.js Exposes SAF range reads but does not preserve the backend's existing BOM normalization for JSON.
src/fileSystem/readRange.js Centralizes safe-integer and maximum-length validation plus optional decoding.
src/fileSystem/internalFs.js Uses Blob.slice and FileReader to avoid loading bytes outside the requested local-file range.
src/fileSystem/sftp.js Adds range reads through the existing connection lifecycle and native SFTP bridge.
src/plugins/sdcard/src/android/SDcard.java Implements validated stream skipping and capped native reads for SAF content.
src/plugins/sftp/src/com/foxdebug/sftp/Sftp.java Implements offset-based SFTP stream reads capped to the validated requested length.
tests/unit/readFileRange.test.js Covers validation and principal backend delegation, but not oversized headerless HTTP responses or BOM-prefixed JSON ranges.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    Caller[fsOperation URL readFileRange] --> Validate[validateReadRange]
    Validate --> Local[Local Blob.slice]
    Validate --> SAF[SDcard native stream]
    Validate --> SFTP[SFTP offset stream]
    Validate --> HTTP[HTTP Range fetch]
    Local --> Decode[decodeReadRange]
    SAF --> Decode
    SFTP --> Decode
    HTTP --> Decode
Loading

Reviews (1): Last reviewed commit: "feat(fs): add bounded byte-range reads" | Re-trigger Greptile

Comment thread src/fileSystem/index.js
Comment on lines +133 to +134
const data = await response.arrayBuffer();
if (data.byteLength > range.length) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security Unbounded HTTP response buffering

When a server returns 206 without a valid Content-Length or Content-Range and streams more bytes than requested, response.arrayBuffer() buffers the entire body before the length check, causing WebView memory exhaustion despite the bounded-read contract.

How this was verified: The optional header guards can be skipped, while the response-size check runs only after the complete body has been buffered.

Knowledge Base Used: File System

Comment on lines +222 to +225
async readFileRange(start, end, encoding) {
const { data } = await externalFs.readFileRange(url, start, end);
return decodeReadRange(data, encoding);
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Range reads retain JSON BOM

When a range contains an entire BOM-prefixed JSON document and uses the json encoding, this path passes the BOM directly to JSON decoding instead of applying the normalization used by readFile, causing the range read to reject for a document the full-file path accepts.

Knowledge Base Used: File System

@github-project-automation github-project-automation Bot moved this from Backlog to Done in The Code Board - Acode Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant