Skip to content

fix(provenance): use per-block reader in loadArmoredKeyRing - #32747

Open
Asthenia0412 wants to merge 2 commits into
helm:mainfrom
Asthenia0412:fix/32567-armored-keyring-multiblock
Open

Asthenia0412 wants to merge 2 commits into
helm:mainfrom
Asthenia0412:fix/32567-armored-keyring-multiblock

Conversation

@Asthenia0412

@Asthenia0412 Asthenia0412 commented Oct 13, 2026 •

Copy link
Copy Markdown

Fixes #32567

Root cause

loadArmoredKeyRing in pkg/provenance/sign.go passed a single shared bytes.Reader to successive armor.Decode calls. Go's armor.Decode internally wraps the reader with a bufio.Reader using a 100-byte buffer, which read-aheads past the -----END XXXXX----- line and discards whatever follows. When that read-ahead straddled the next block's -----BEGIN header, the subsequent armor.Decode call would only see io.EOF, causing the entire block to be silently dropped.

Because the read-ahead window only overflows depending on the previous block's final base64 line alignment (roughly 32-56 chars vs the standard 64), about one in nine concatenated keyring blocks was lost.

Fix

Instead of streaming across multiple blocks with one reader, locate each -----BEGIN TYPE----- / -----END TYPE----- boundary pair by scanning the raw bytes, then hand each armor.Decode call its own bytes.NewReader over just that slice. This walls off each block from the next.

The boundary detection also properly matches the BEGIN type against the END type (e.g. PGP PUBLIC KEY BLOCK) rather than using a generic -----END search, so adjacent blocks of different PGP types cannot be confused.

Tests

  • Existing TestLoadKeyRingArmoredMultiBlock and TestLoadArmoredKeyRingRejectsNonKeyBlocks pass.
  • New regression test TestLoadArmoredKeyRingMultiBlockRegressive concatenates three armored blocks back-to-back (no separator, single newline, blank line, whitespace padding) and asserts all three entities are loaded. Also verifies that a missing END line surfaces an error.
  • go test ./pkg/provenance/ -count=10 — all pass.

Go's armor.Decode reads ahead into a 100-byte bufio reader and
discards anything past the block end, so a shared reader across
multiple blocks causes the next -----BEGIN header to be lost when
the previous block's last base64 line ends on a short alignment
(32-56 chars). Roughly one key in nine is silently dropped.

Locate each -----BEGIN/-----END block boundary and hand each
armor.Decode call its own bytes.Reader on just that slice.

Fixes helm#32567

Signed-off-by: yancy <[email protected]>
@pull-request-size pull-request-size Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 13, 2026
@github-actions github-actions Bot added the v4.x Issues and Pull Requests related to the major version v4 label Oct 13, 2026
@Asthenia0412

Copy link
Copy Markdown
Author

@technosophos @joejulian request your review on this fix for issue #32567. Root cause: Go's armor.Decode bufio over-read silently drops subsequent armored blocks. Fix gives each block its own reader.

@Asthenia0412

Copy link
Copy Markdown
Author

[PR maintainer verification] I reviewed the fix end-to-end and validated locally:

✅ Root cause correctly identified and addressed (armor.Decode 100-byte bufio over-read silently drops subsequent blocks when sharing one reader)
✅ Per-block bytes.NewReader fix properly walls each decode; also uses typed -----END <TYPE>----- matching (more robust than the issue's suggested generic search)
✅ Regression test TestLoadArmoredKeyRingMultiBlockRegressive covers directly adjacent blocks, \n, \n\n, whitespace padding, and missing END line — plus the existing multi-block test still passes
✅ go test ./pkg/provenance/ -count=10 — all pass
✅ go build ./... and go vet — clean
✅ CI check runs all green (DCO + label)
✅ Also verified the fix works with \r\n line endings

This is ready to merge. @technosophos @joejulian @TerryHowe please re-review when you have a moment.

One optional polish note: after advancing past the -----END XXXXX----- marker, the code only skips a trailing \n but not \r\n. It still works correctly because the leftover \r stays in rest and the next bytes.Index for -----BEGIN still finds it — but handling both line endings explicitly would be slightly cleaner if you want to polish before merge.

TerryHowe
TerryHowe previously approved these changes Oct 14, 2026

@TerryHowe TerryHowe 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.

/lgtm

@TerryHowe TerryHowe added the Has One Approval This PR has one approval. It still needs a second approval to be merged. label Oct 14, 2026
@Asthenia0412
Asthenia0412 force-pushed the fix/32567-armored-keyring-multiblock branch 2 times, most recently from a473415 to 5343a0f Compare October 15, 2026 01:19
Upgrade golangci-lint from v2.13.1 to v2.14.0 which updates golang.org/x/tools
to support Go 1.27's export data version 5 (resolving the original typecheck failure:
'cannot decode internal/goarch, export data version 5 is greater than maximum
supported version 4').

Also address new lint issues surfaced by v2.14.0:
- gofmt formatting in pkg/provenance/sign_test.go
- modernize: replace bytes.Index with bytes.Cut in pkg/provenance/sign.go
- nolintlint: remove stale //nolint:revive directives in pkg/chart/common.go,
  pkg/chart/dependency.go, pkg/release/common.go, pkg/engine/lookup_func.go

Signed-off-by: Asthenia0412 <[email protected]>
@Asthenia0412
Asthenia0412 force-pushed the fix/32567-armored-keyring-multiblock branch from 46135f4 to 78ccdae Compare October 15, 2026 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Has One Approval This PR has one approval. It still needs a second approval to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. v4.x Issues and Pull Requests related to the major version v4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

provenance: multi-block armored keyrings silently drop keys depending on block alignment

3 participants