Skip to content

Add local image secret scanning with cog scan - #3228

Open
markphelps wants to merge 7 commits into
mainfrom
feat/cog-scan-secrets
Open

markphelps wants to merge 7 commits into
mainfrom
feat/cog-scan-secrets

Conversation

@markphelps

@markphelps markphelps commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Add cog scan and cog scan . to build the current project and scan its immutable image ID. cog scan IMAGE scans an existing local image without building or pulling. Neither mode pushes.
  • Scan every layer, including files hidden by later layers, plus image metadata. Return redacted findings and support --json.
  • Add deterministic non-live OpenAI-, Anthropic-, Google-, and GitHub-shaped key tests, and update CLI and architecture documentation.

Review follow-up

  • Read actual Docker-save manifests to resolve content-addressed config/layer blobs, including blobs before the manifest.
  • Scan large files in bounded windows and scan archive entries with up to eight workers. Supply file paths to filename-dependent Gitleaks rules. Paths remain hashed by default; --show-paths explicitly reveals local paths.
  • Send results to stdout; exit 0 for clean, 2 for findings, and 1 for scan errors.

Validation

  • mise run test:go
  • go test -race ./pkg/scan -count=1
  • mise run lint:go
  • mise run docs:cli:check
  • go test ./pkg/scan -run TestScanDockerSaveWhenAvailable -count=1 -v
  • go run ./cmd/cog scan alpine:3.20 --json returned {"version":1,"findings":[]} against local Docker.

Notes

  • This does not change cog push or enforce a registry policy.
  • Separate 8 GiB archive and decompressed-layer budgets and a maximum line length of 8 MiB still cause an explicit incomplete-scan error; performance on large model images needs measurement.
  • Archive contents are temporarily spooled to a private local file for manifest-based classification; the file is removed on exit.

CI and review follow-up

  • Reject trailing data after the outer image tar and inside both compressed and uncompressed layer tars. Keep the underlying Docker error when an image cannot be inspected or exported.
  • Rust advisory CI found new unmaintained-crate notices for chrono and pin-utils, neither with a safe upgrade. crates/deny.toml acknowledges only RUSTSEC-2026-0543 and RUSTSEC-2026-0441. This does not remove those dependencies; migration needs separate follow-up.

Build-first scanning

  • Reuse normal build options, including --secret, --no-cache, and --file/--config. Build errors stop the scan.
  • Use the context-aware Docker client for both building and scanning. Build diagnostics stay on stderr and scan results on stdout.
  • Unit tests cover no-argument/dot/image dispatch, immutable ID selection, JSON output, build flags, and build-error short-circuiting. A real project build-and-scan was not run.

@ask-bonk ask-bonk Bot left a comment

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.

Review summary

cog scan scans every layer of a local Docker image with Gitleaks, fails closed on anything it cannot scan, and has solid unit tests (all pass, including -race).

Findings:

  • warning (pkg/scan/scan.go:136): data after the outer tar's end-of-archive marker is silently discarded and reported as clean.
  • suggestion (pkg/cli/scan.go:43): Docker client errors are replaced with static messages, losing the underlying error.

Note (already disclosed in the PR description): the 8 MiB per-file / 2 GiB per-layer / 8 GiB total limits mean most real ML model images (weight files are typically far larger than 8 MiB) will report incomplete scan rather than useful results. The fail-closed direction is the right call; the limits themselves need validation before this can scan real images.

Comment thread pkg/scan/scan.go Resolved
Comment thread pkg/cli/scan.go Resolved
@ask-bonk

ask-bonk Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

I'm Bonk, and I've done a quick review of your PR.

Summary: Adds cog scan IMAGE to scan every layer (including deleted files) plus image metadata of an existing local Docker image with Gitleaks, failing closed on any skipped, malformed, or oversized content, with redacted findings, --json output, and docs. All unit tests pass (verified with -race).

Issues (ranked):

  1. warning — pkg/scan/scan.go:136: data after the outer tar's end-of-archive blocks is consumed by io.Copy(io.Discard, outer) and discarded without ever being scanned; as long as it fits the byte budget, Archive returns a clean result. I verified this by appending a secret after the end blocks of a valid image archive — it returned zero findings and no error. This contradicts the documented contract ("An incomplete scan fails instead of reporting clean") and is inconsistent with scanLayer, which rejects trailing data. Fix: fail when the trailing copy consumes any bytes.
  2. suggestion — pkg/cli/scan.go:43 (and 37, 48): Docker client errors are replaced with static messages, so a user can't tell whether the image is missing, the daemon is down, or the call was denied; wrap the real error with %w.
  3. info — the 8 MiB/file, 2 GiB/layer, 8 GiB/total limits mean real ML model images (weight files are typically far larger than 8 MiB) will report incomplete scan rather than useful results. Already disclosed in the PR description; the fail-closed direction is correct, but the limits need validation before this can scan actual model images.

Both inline findings were posted as a single COMMENT review (2 comments).

github run

Comment thread pkg/scan/scan.go Resolved
@ask-bonk

ask-bonk Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Review: 1 finding.

One inline finding posted: scanLayer silently discards trailing decompressed bytes after a layer tar's end-of-archive marker (gzip and uncompressed alike), so a secret hidden there is missed and the scan reports clean — verified empirically against the current code. This is distinct from the outer-archive trailing-data issue already raised in the earlier review, which remains open, as does the Docker error-message suggestion.

github run

@ask-bonk

ask-bonk Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Since last review: 0 resolved, 3 still open, 0 new.

The three findings from the previous reviews are all still present at the current head (7b80f19), and commit 7b80f19 (parallel workers) does not touch any of these paths:

  1. pkg/scan/scan.go:229 (warning) — trailing data after the outer tar's end-of-archive blocks is still consumed by io.Copy(io.Discard, outer) and never scanned. Verified against the current code: appending TOKEN=github_pat_... after the end-of-archive blocks returns 0 findings and no error.
  2. pkg/scan/scan.go:428 (warning) — trailing data after the layer tar (decompressed side, gzip and uncompressed) is still drained un-scanned. Verified. I also confirmed the previously suggested fix (check n > 0 after the drain) catches every trailing-data variant, including a trailing concatenated gzip stream appended after a gzip layer's stream.
  3. pkg/cli/scan.go:43 (suggestion) — Docker client errors still replaced with static messages, losing the underlying error.

I reviewed the new scanJobs/budget/countReader concurrency code and the windowed scanFile logic in 7b80f19 and found no new actionable issues; the added tests cover the concurrency paths and pass (verified with go test ./pkg/scan/ -count=1).

github run

@ask-bonk

ask-bonk Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Since last review: 3 resolved, 0 still open, 0 new.
LGTM!

github run

@ask-bonk

ask-bonk Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

APIError: Gone: AiError: Model has been deprecated: @cf/deepseek-ai/deepseek-v4-flash-0731 was deprecated on 2026-11-20. See the model catalog for alternatives: https://developers.cloudflare.com/workers-ai/models/ (4a88061f-254a-43ea-b4ac-10104c763fde)

github run

@ask-bonk

ask-bonk Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

APIError: Gone: AiError: Model has been deprecated: @cf/deepseek-ai/deepseek-v4-flash-0731 was deprecated on 2026-11-20. See the model catalog for alternatives: https://developers.cloudflare.com/workers-ai/models/ (099fb3c8-46e6-4b95-a187-1d1fbaddd3df)

github run

@ask-bonk

ask-bonk Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

APIError: Gone: AiError: Model has been deprecated: @cf/deepseek-ai/deepseek-v4-flash-0731 was deprecated on 2026-11-20. See the model catalog for alternatives: https://developers.cloudflare.com/workers-ai/models/ (07e74c75-c178-4fcb-941f-1fc85a8cdb0f)

github run

@ask-bonk

ask-bonk Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

@markphelps Bonk workflow failed. Check the logs for details.

View workflow run · To retry, trigger Bonk again.

@markphelps
markphelps marked this pull request as ready for review November 20, 2026 17:05
@markphelps
markphelps requested a review from a team as a code owner November 20, 2026 17:05
@ask-bonk

ask-bonk Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

Comment thread pkg/scan/scan.go
Comment on lines +432 to +439
// Consume and validate the complete compressed stream (including gzip checksum).
trailing, err := io.Copy(io.Discard, counted)
if err != nil {
return errors.New("incomplete scan: truncated or oversized layer")
}
if trailing != 0 {
return errors.New("incomplete scan: trailing layer data")
}

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.

After tar.Reader returns EOF it has consumed exactly the two zero end-of-archive blocks, but valid tar archives may carry additional zero-block padding (GNU tar writes a full 10 KiB record, and layers imported via docker import can retain it). Those zero bytes cannot contain a secret, yet this check counts them and returns incomplete scan: trailing layer data, so cog scan fails on otherwise valid images. I reproduced this by appending 18 zero blocks to a layer and got incomplete scan: trailing layer data.

Drain the stream and reject only non-zero trailing bytes:

Suggested change
// Consume and validate the complete compressed stream (including gzip checksum).
trailing, err := io.Copy(io.Discard, counted)
if err != nil {
return errors.New("incomplete scan: truncated or oversized layer")
}
if trailing != 0 {
return errors.New("incomplete scan: trailing layer data")
}
// Consume and validate the complete compressed stream (including gzip checksum).
// tar.Reader stops after the two end-of-archive blocks, but valid archives may
// carry additional zero-block padding (GNU tar, and layers produced via
// docker import). Zero bytes cannot contain a secret, so allow zero padding
// while still rejecting any non-zero trailing data.
trailing := make([]byte, 32<<10)
for {
n, err := counted.Read(trailing)
if err != nil && err != io.EOF {
return errors.New("incomplete scan: truncated or oversized layer")
}
for _, b := range trailing[:n] {
if b != 0 {
return errors.New("incomplete scan: trailing layer data")
}
}
if err == io.EOF {
break
}
}

A regression test with extra zero blocks would lock this in.

@ask-bonk

ask-bonk Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

I'm Bonk, and I've done a quick review of your PR.

Summary: Adds cog scan (build-and-scan or scan an existing local image) backed by a fail-closed layer/metadata scanner using Gitleaks, plus CLI wiring, docs, and tests.

Issues (ranked):

  1. warning — pkg/scan/scan.go:437: the trailing-data check rejects valid zero-block padding (GNU tar / docker import layers), so cog scan can fail with incomplete scan on otherwise valid images. Posted inline with a zero-aware drain suggestion and a note to add a regression test.

The dispatch/build-first flow, Docker error wrapping, spool/manifest handling, worker concurrency, JSON/exit-code paths, and the manifest-filename constant extraction all looked correct. go test ./pkg/scan ./pkg/cli ./cmd/cog and go test -race ./pkg/scan pass.

github run

This branch has not been deployed

No deployments
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.

1 participant