No file-size limit when reading and tokenizing scanned files enables memory exhaustion
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 62/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- rust
- Domain
- performance, security
Research direction
Start at read_file_with_binary_check, where file.read_to_end fills an unbounded buffer, then follow that buffer through encoding detection and tiktoken tokenization to see where the memory amplification happens. Check the file-walking and configuration code for an existing size or ignore mechanism that a cap could reuse, since the fix needs a defined behavior for oversized files (skip, truncate, or error). Reproduce with a 32 MiB text file as described and measure peak RSS; done means scanning such a file stays bounded and there is a test covering the oversized-file case.
Written by the indexing model from the issue text.
Description
Summary
Denial of service in code2prompt: read_file_with_binary_check uses file.read_to_end(&mut buffer) with no upper bound; the Vec::with_capacity(file_size.min(10MB)) is only an initial-allocation hint. The full buffer is then passed through encoding detection and tiktoken tokenization with no size limit.
To reproduce
Create a large text file in a scanned root and run 'code2prompt -q -O /dev/null '; observe peak RSS far exceeding the input size.
Expected
Attacker-influenced file sizes should be bounded so a scanned codebase cannot exhaust memory.
Actual
32 MiB input consumed ~1.7 GiB peak RSS
Independent CLI reproduction with a 32 MiB text file measured ~1.7 GiB peak RSS (32 MiB input -> ~54x memory amplification), confirming unbounded buffering and tokenization with no effective size cap in read_file_with_binary_check (file.read_to_end with no upper bound). A full OOM was not induced to avoid destabilizing the environment.
Observed side effect:
Command being timed: "/tmp/c2p-build/target/debug/code2prompt -q -O /dev/null /tmp/v4-big-1795224600"
User time (seconds): 134.91
System time (seconds): 1.34
Percent of CPU this job got: 100%
Elapsed (wall clock) time (h:mm:ss or m:ss): 2:16.02
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1724416
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 496785
Voluntary context switches: 1767
Involuntary context switches: 501
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Impact
Local denial of service via memory exhaustion when scanning an attacker-influenced large file.
Version
v4.2.0
- Dominant language
- Rust
- Stars
- 7.8k
- Forks
- 459
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mufeedvh/code2prompt
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
mufeedvh/code2prompt#355 ·
-
Symlink-to-file inside scanned root reads file content outside the root without --follow-symlinksOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mufeedvh/code2prompt#354 ·
-
No tag 4.3.0Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
mufeedvh/code2prompt#340 · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 66/100
mufeedvh/code2prompt#352 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
mufeedvh/code2prompt#345 ·
All issues in mufeedvh/code2prompt
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
topgrade-rs/topgrade#2540 ·
Maintainers usually reply within 1 day
-
Invalid JSON options for ngram and edge_ngram silently use defaultsPossibly taken @Tilendlesa claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
noemaforge/vimanam#153 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 3 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day