-
Notifications
You must be signed in to change notification settings - Fork 1
plugin/objectverifier: add gpg and ssh verifiers #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pjbgf
wants to merge
1
commit into
main
Choose a base branch
from
verifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| //go:build linux | ||
|
|
||
| package gpg_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/go-git/go-git/v6" | ||
| "github.com/go-git/go-git/v6/plumbing/object" | ||
| ) | ||
|
|
||
| func TestCommitVerifyAlignment(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if testing.Short() { | ||
| t.Skip("skipping commit verify") | ||
| } | ||
|
|
||
| if gnupg == nil { | ||
| t.Error("gpg not available") | ||
| t.FailNow() | ||
| } | ||
|
|
||
| cases := []struct { | ||
| build func() []byte | ||
| postSign func(signed []byte, sig string) []byte | ||
| name string | ||
| }{ | ||
| { | ||
| name: "valid", | ||
| build: func() []byte { | ||
| h, m := canonicalCommit() | ||
|
|
||
| return assembleCommit(h, m) | ||
| }, | ||
| }, | ||
| { | ||
| name: "duplicate-tree", | ||
| build: func() []byte { | ||
| h, m := canonicalCommit() | ||
| dup := append([]string{ | ||
| h[0], | ||
| "tree 5555555555555555555555555555555555555555", | ||
| }, h[1:]...) | ||
|
|
||
| return assembleCommit(dup, m) | ||
| }, | ||
| }, | ||
| { | ||
| name: "duplicate-author", | ||
| build: func() []byte { | ||
| h, m := canonicalCommit() | ||
| dup := []string{ | ||
| h[0], | ||
| h[1], | ||
| h[2], | ||
| "author Override Author <override@example.local> 1700000001 +0000", | ||
| h[3], | ||
| } | ||
|
|
||
| return assembleCommit(dup, m) | ||
| }, | ||
| }, | ||
| { | ||
| name: "duplicate-committer", | ||
| build: func() []byte { | ||
| h, m := canonicalCommit() | ||
| dup := []string{ | ||
| h[0], h[1], h[2], h[3], | ||
| "committer Override Committer <override@example.local> 1700000001 +0000", | ||
| } | ||
|
|
||
| return assembleCommit(dup, m) | ||
| }, | ||
| }, | ||
| { | ||
| name: "misplaced-parent-after-committer", | ||
| build: func() []byte { | ||
| h, m := canonicalCommit() | ||
| dup := []string{ | ||
| h[0], h[1], h[2], h[3], | ||
| "parent 2222222222222222222222222222222222222222", | ||
| } | ||
|
|
||
| return assembleCommit(dup, m) | ||
| }, | ||
| }, | ||
| { | ||
| // Inject the same signature again as a second gpgsig | ||
| // header. Both occurrences sit before the blank line; both | ||
| // signers strip them when computing the payload. | ||
| name: "double-signature", | ||
| build: func() []byte { | ||
| h, m := canonicalCommit() | ||
|
|
||
| return assembleCommit(h, m) | ||
| }, | ||
| postSign: injectGpgSig, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| if tc.name == "double-signature" && os.Getenv("GIT_VERSION") == "v2.11.0" { | ||
| t.Skip("multi-signature rejection not yet established in Git 2.11.0") | ||
| } | ||
|
|
||
| repo := initRepo(t) | ||
| unsigned := tc.build() | ||
| sig := gpgSign(t, unsigned) | ||
|
|
||
| signed := injectGpgSig(unsigned, sig) | ||
| if tc.postSign != nil { | ||
| signed = tc.postSign(signed, sig) | ||
| } | ||
|
|
||
| hash := writeLooseObject(t, repo, "commit", signed) | ||
| gitErr := gitVerifyCommit(t, repo, hash) | ||
|
|
||
| r, err := git.PlainOpen(repo) | ||
| require.NoError(t, err) | ||
|
|
||
| defer func() { _ = r.Close() }() | ||
|
|
||
| commit, err := r.CommitObject(hash) | ||
|
|
||
| ggErr := err | ||
| if ggErr == nil { | ||
| _, ggErr = commit.Verify(context.Background(), object.WithVerifier(newVerifier(t))) | ||
| } | ||
|
|
||
| assertSameVerdict(t, "git verify-commit", gitErr, "go-git Verify", ggErr) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // canonicalCommit returns the byte-exact unsigned commit body produced by | ||
| // upstream `git commit-tree` for a single-parent commit. Used as the | ||
| // baseline; individual scenarios mutate the header lines around it. | ||
| func canonicalCommit() (headers []string, message string) { | ||
| return []string{ | ||
| "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904", | ||
| "parent 1111111111111111111111111111111111111111", | ||
| "author Test Author <author@example.local> 1700000000 +0000", | ||
| "committer Test Committer <committer@example.local> 1700000000 +0000", | ||
| }, | ||
| "signed commit message\n" | ||
| } | ||
|
|
||
| func assembleCommit(headers []string, message string) []byte { | ||
| var buf bytes.Buffer | ||
| for _, h := range headers { | ||
| buf.WriteString(h) | ||
| buf.WriteByte('\n') | ||
| } | ||
|
|
||
| buf.WriteByte('\n') | ||
| buf.WriteString(message) | ||
|
|
||
| return buf.Bytes() | ||
| } | ||
|
|
||
| // assertSameVerdict fails the test unless both verifiers agree on | ||
| // success-or-failure. It does not compare error messages, only verdicts. | ||
| func assertSameVerdict(t *testing.T, leftLabel string, leftErr error, rightLabel string, rightErr error) { | ||
| t.Helper() | ||
|
|
||
| leftOK := leftErr == nil | ||
|
|
||
| rightOK := rightErr == nil | ||
| if leftOK == rightOK { | ||
| if !leftOK { | ||
| t.Logf("both verifiers rejected (expected for malformed input)\n %s: %v\n %s: %v", | ||
| leftLabel, leftErr, rightLabel, rightErr) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| assert.Failf(t, "verifier verdicts diverge", | ||
| "%s succeeded=%v err=%v\n%s succeeded=%v err=%v", | ||
| leftLabel, leftOK, leftErr, rightLabel, rightOK, rightErr) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.