Skip to content

builtin_scan() reads a file's full contents into memory before checking the 5MB size cap #16

Description

@kimdzhekhon

Summary

In src/scanners.rs::builtin_scan():

let Ok(bytes) = std::fs::read(entry.path()) else {
    continue;
};
if bytes.len() > 5_000_000 || is_probably_binary(&bytes) {
    continue;
}

The 5MB size cap is meant to bound memory/CPU use per file, but std::fs::read unconditionally reads the entire file into memory first, and only checks the length (and binary-ness) afterward. A large file anywhere under the scan target (a stray VM image, database dump, log file, or an adversarially-placed huge file) is read fully into memory regardless of the cap, defeating its purpose. Scanning a directory containing a multi-GB file can cause excessive memory use / slowdown, i.e. a resource-exhaustion / DoS vector for a tool whose whole job is to be run against arbitrary repos before a push.

Suggested fix

Check the file size via entry.metadata() (or std::fs::metadata) before calling std::fs::read, and skip files over the cap without reading them at all:

let Ok(meta) = entry.metadata() else { continue; };
if meta.len() > 5_000_000 {
    continue;
}
let Ok(bytes) = std::fs::read(entry.path()) else { continue; };
if is_probably_binary(&bytes) { continue; }

Filing before fixing per repo convention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions