fix: strip trailing whitespace from attached signature files/stdin - #5058
Open
pujitha24 wants to merge 1 commit into
Open
fix: strip trailing whitespace from attached signature files/stdin#5058pujitha24 wants to merge 1 commit into
pujitha24 wants to merge 1 commit into
Conversation
Motivation: The linked report below describes a failure after detaching a signature with `cosign download signature ... | jq -r '.Base64Signature' > signature.sig` and later re-attaching it on another registry with `cosign attach signature --signature signature.sig ...`. The reporter's own --verbose debug output shows the OCI manifest annotation dev.cosignproject.cosign/signature on the re-attached image literally contains the base64 signature value with a trailing newline embedded in it, unlike the original signing machine's copy. That newline comes from shell redirection after `jq -r`; cosign's attach signature command stores whatever bytes it reads from the --signature file or stdin verbatim, with no trimming. Storing extraneous whitespace inside a signature annotation is a real, demonstrated defect regardless of whether it fully explains the "invalid signature when validating ASN.1 encoded signature" error the reporter hit: Go's own base64 decoder tolerates embedded \r/\n and decodes to the same bytes, so this alone would not necessarily reproduce that exact failure, and the report's repro also mixed up `cosign download signature`'s JSON envelope for --payload, a separate issue. This change fixes the annotation-pollution defect on its own merits; it is not a claim that it resolves the full reported scenario end-to-end. Approach: Trim leading and trailing whitespace from bytes read from a signature file or stdin in signatureBytes() (cmd/cosign/cli/attach/sig.go) before they are stored as the signature annotation. Base64 has no whitespace in its alphabet, so trimming cannot remove meaningful signature bytes. The literal-argument path is untouched. Validation: - go test ./cmd/cosign/cli/attach/... — added TestSignatureBytesTrimsTrailingWhitespace, which fails before this change (returned bytes include the trailing "\n") and passes after; verified this directly by stashing the fix and re-running the test. Also added TestSignatureBytesFileWithoutTrailingWhitespaceUnaffected confirming clean input is unaffected. - go build ./... and go vet ./cmd/cosign/cli/attach/... pass. - go test ./cmd/cosign/... passes (all subpackages). - golangci-lint run ./cmd/cosign/cli/attach/... (v2.12.2, matching this repo's Dockerfile.golangci-lint) reports 0 issues. Report: sigstore#4207 Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5058 +/- ##
==========================================
- Coverage 40.10% 39.53% -0.57%
==========================================
Files 155 207 +52
Lines 10044 13037 +2993
==========================================
+ Hits 4028 5154 +1126
- Misses 5530 7150 +1620
- Partials 486 733 +247 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation:
The linked report below describes a failure after detaching a
signature with
cosign download signature ... | jq -r '.Base64Signature' > signature.sigand later re-attaching it onanother registry with
cosign attach signature --signature signature.sig .... The reporter's own --verbose debug output showsthe OCI manifest annotation dev.cosignproject.cosign/signature on the
re-attached image literally contains the base64 signature value with
a trailing newline embedded in it, unlike the original signing
machine's copy. That newline comes from shell redirection after
jq -r; cosign's attach signature command stores whatever bytes itreads from the --signature file or stdin verbatim, with no trimming.
Storing extraneous whitespace inside a signature annotation is a
real, demonstrated defect regardless of whether it fully explains the
"invalid signature when validating ASN.1 encoded signature" error the
reporter hit: Go's own base64 decoder tolerates embedded \r/\n and
decodes to the same bytes, so this alone would not necessarily
reproduce that exact failure, and the report's repro also mixed up
cosign download signature's JSON envelope for --payload, a separateissue. This change fixes the annotation-pollution defect on its own
merits; it is not a claim that it resolves the full reported scenario
end-to-end.
Approach:
Trim leading and trailing whitespace from bytes read from a signature
file or stdin in signatureBytes() (cmd/cosign/cli/attach/sig.go)
before they are stored as the signature annotation. Base64 has no
whitespace in its alphabet, so trimming cannot remove meaningful
signature bytes. The literal-argument path is untouched.
Validation:
TestSignatureBytesTrimsTrailingWhitespace, which fails before this
change (returned bytes include the trailing "\n") and passes after;
verified this directly by stashing the fix and re-running the test.
Also added TestSignatureBytesFileWithoutTrailingWhitespaceUnaffected
confirming clean input is unaffected.
this repo's Dockerfile.golangci-lint) reports 0 issues.
Report: #4207
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com
Fixes #4207