Skip to content

fix(scanner): prevent oom crashes by capping file read size#184

Merged
san-zrl merged 1 commit intocbomkit:mainfrom
Xavelyn:fix/unbound_memory_allocation
Apr 13, 2026
Merged

fix(scanner): prevent oom crashes by capping file read size#184
san-zrl merged 1 commit intocbomkit:mainfrom
Xavelyn:fix/unbound_memory_allocation

Conversation

@Xavelyn
Copy link
Copy Markdown
Contributor

@Xavelyn Xavelyn commented Apr 10, 2026

Summary

This PR should fix a Out-Of-Memory problem in the scanner plugins by enforcing a strict memory cap during file reads, preventing unbounded memory allocations when analyzing massive files.

Motivation

Previously, the certificates and secrets plugins utilized unbounded read operations to load files entirely into RAM before parsing. When the scanner encountered large files (e.g. multi-gigabyte Database files) this caused big memory spikes. In our deployment, scanner was running in K8s environment with limited memory usage to scan other container images, this resulted in abrupt crashes of the entire container the scanner was running in.

This was tested with the following image: https://hub.docker.com/r/tenable/securitycenter-install. While the previous code spikes on memory usage near 6 GiB while after the fix it spikes at around 500 MiB.

Features

  • Bounded Memory Allocation: Caps all file read operations to a maximum threshold using io.LimitReader.

  • Keeping the configurable Limits and Default Limits

Files Modified

provider/plugins/certificates/
└── certificates.go # Replaced ReadAllAndClose with LimitReader + defer
provider/plugins/secrets/
└── secrets.go # Replaced unbounded ReadAll with LimitReader + defer

Testing

$ go test ./scanner/plugins/certificates/... -v
=== RUN   TestIssue140_CombinedPEMFile
=== RUN   TestIssue140_CombinedPEMFile/parsePEMCertificatesFromPath_finds_cert_in_combined_file
=== RUN   TestIssue140_CombinedPEMFile/parsePEMCertificatesFromPath_returns_nil_for_non-PEM_file
=== RUN   TestIssue140_CombinedPEMFile/parsePEMCertificatesFromPath_returns_nil_for_PEM_with_only_keys
--- PASS: TestIssue140_CombinedPEMFile (0.00s)
    --- PASS: TestIssue140_CombinedPEMFile/parsePEMCertificatesFromPath_finds_cert_in_combined_file (0.00s)
    --- PASS: TestIssue140_CombinedPEMFile/parsePEMCertificatesFromPath_returns_nil_for_non-PEM_file (0.00s)
    --- PASS: TestIssue140_CombinedPEMFile/parsePEMCertificatesFromPath_returns_nil_for_PEM_with_only_keys (0.00s)
=== RUN   TestIssue56
=== RUN   TestIssue56/Issue_56
_redacted_
--- PASS: TestIssue56 (0.00s)
    --- PASS: TestIssue56/Issue_56 (0.00s)
PASS
ok      github.com/cbomkit/cbomkit-theia/scanner/plugins/certificates   0.007s

$ go test ./scanner/plugins/secrets/... -v
=== RUN   TestPrivateKey
_redacted_
--- PASS: TestPrivateKey (0.04s)
PASS
ok      github.com/cbomkit/cbomkit-theia/scanner/plugins/secrets        0.049s

Breaking Changes

None.

@Xavelyn Xavelyn requested a review from a team as a code owner April 10, 2026 20:11
@Xavelyn Xavelyn force-pushed the fix/unbound_memory_allocation branch from c57b0ab to 93b7188 Compare April 10, 2026 20:30
Signed-off-by: Joshua Kristof <joshua.kristof@digits.schwarz>
@Xavelyn Xavelyn force-pushed the fix/unbound_memory_allocation branch from 93b7188 to c13749f Compare April 10, 2026 20:33
Copy link
Copy Markdown
Contributor

@san-zrl san-zrl left a comment

Choose a reason for hiding this comment

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

Hi @Xavelyn - Thank you very much for your interest in cbomkit-theia and your contributions. LGTM - this makes a lot of sense!

@san-zrl san-zrl merged commit 7d9c8b9 into cbomkit:main Apr 13, 2026
1 of 2 checks passed
@Xavelyn Xavelyn deleted the fix/unbound_memory_allocation branch April 21, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants