Skip to content

fix: reject malformed prepend signatures and non-HSM keys, write -out atomically - #9

Merged
deansheather merged 2 commits into
mainfrom
code-review-xfqs
Nov 17, 2026
Merged

deansheather merged 2 commits into
mainfrom
code-review-xfqs

Conversation

@deansheather

Copy link
Copy Markdown
Contributor

Fixes the findings from a code quality and security review of the module, and adds AGENTS.md.

What changed and why

Problem Fix
ValidateSignatures used packet.Reader.Next, which silently skips unknown, unsupported and marker packets. They were copied into the release signature, and gpg --verify then failed (exit 2). Text and certification signatures were also accepted: gpg reports BAD for a certification signature, and with a text signature first it checks only that one, so the KMS signature goes unchecked. Read packets with packet.Read and accept only binary-document signatures (type 0x00). A real gpg --detach-sign signature (gpg 2.4.8) still passes.
A sub-second Lifetime was truncated to 0, meaning "never expires". A Created before 1970 or after 2106 silently wrapped around in the uint32 key field. Identity.validate requires a lifetime in whole seconds and a creation time that fits the field.
NewKMSSigner accepted SOFTWARE and external keys, though the README says the key never leaves the HSM. Require protection level HSM or HSM_SINGLE_TENANT.
KMSSigner.Sign returned a PKCS#1 v1.5 signature when the caller asked for RSA-PSS. Reject *rsa.PSSOptions.
KMS calls had no timeout, and the CLI ignored SIGINT and SIGTERM. Each KMS request is limited to one minute, and the CLI cancels on SIGINT and SIGTERM.
-out was written with os.WriteFile: it followed symlinks, wasn't atomic, and could overwrite the file being signed. Write a temp file in the same directory, chmod 0644, rename. Refuse an -out that is the file to sign, or a symlink to it.
The docs said -fingerprint pins "the identity". README, package doc and flag help now say it covers only -key and -created, not the user ID or -lifetime.

AGENTS.md is a contribution guide for AI agents: commands, layout, patterns, and the rule never to change the exported key bytes.

Behaviour changes for consumers

  • Release jobs fail if the KMS key isn't HSM-protected. Before bumping the pin, please confirm gpg-release and gpg-terraform-provider have protection level HSM.
  • -out needs a writable directory, because the temp file is created next to it. -out /dev/stdout no longer works. Both consumers write a normal file.
  • -prepend rejects files with non-signature packets or non-binary signatures. These used to produce a signature file that gpg rejected.
  • -lifetime must be whole seconds. 35040h is unaffected.

Does the exported public key or fingerprint change?

No. Golden and compat tests pass unchanged, and go test -run TestGoldenPublicKey -update . leaves testdata/ clean.

Tests

  • kms_test.go (new): a fake Cloud KMS server (httptest + option.WithEndpoint), so kms.go is tested for the first time. Covers a good signature with both HSM levels; and rejects a wrong algorithm, software, external or missing protection level, bad PEM or DER, a non-RSA key, PSS, SHA-512 or short digests, a corrupted KMS signature, and a hung request.
  • gpgkms_test.go: lifetime and creation-time limits (including the largest valid values), a non-RSA signer, and a ValidateSignatures table (unknown and marker packets, public-key packet, text and certification signatures, empty and truncated input).
  • cmd/gpgkms/main_test.go: -prepend with default armored output (old and new key both verify), public-key -armor=false, refusing -out equal to the input or a symlink to it, replacing a symlinked -out instead of following it (mode 0644, no temp files left), and an expected error per TestRequiredFlags row. The "no out" row used to pass only because its input file did not exist.

Every new test fails with the old gpgkms.go, kms.go and cmd/gpgkms/main.go.

Validation

  • gofmt -l . (empty), go vet ./...
  • go test -race -shuffle=on -count=1 ./...
  • go mod tidy -diff (root and tools/), go mod verify
  • go tool -modfile=tools/go.mod golangci-lint run ./...: 0 issues
  • go tool -modfile=tools/go.mod govulncheck ./...: 0 vulnerabilities reached
  • GOOS=windows go vet ./...

Not done here: checking that a prepended signature covers the signed file (it would need the old public key as a new input), and making -fingerprint required for sign.


Opened by Xum on behalf of @deansheather.

… atomically

ValidateSignatures now reads packets with packet.Read and only accepts binary-document signatures. Identity rejects sub-second lifetimes and creation times outside OpenPGP's uint32 range. NewKMSSigner requires an HSM protection level; KMSSigner rejects PSS and bounds each request with a timeout. The CLI writes -out via temp file and rename, refuses to overwrite the signed file, and cancels on SIGINT/SIGTERM. Adds fake-KMS, validation and CLI tests.
@deansheather
deansheather requested a review from a team as a code owner November 17, 2026 07:48
@deansheather

Copy link
Copy Markdown
Contributor Author

@codex review

@deansheather

Copy link
Copy Markdown
Contributor Author

@codex security-review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T07:50:39.687455Z 34dbcbf Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@deansheather

Copy link
Copy Markdown
Contributor Author

@codex security-review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 34dbcbf6d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Summary

  • Security review identified a lossy timestamp-validation issue: OpenPGP v4 stores creation times in whole seconds, but fractional Identity.Created values were silently truncated. Such values are now rejected so distinct inputs cannot unexpectedly derive the same key and fingerprint. gpgkms.goL47-L54
  • Added regression coverage ensuring fractional creation timestamps fail validation. gpgkms_test.goL120-L139
  • Committed the changes as 3167f87 (fix: reject fractional key creation times) and prepared a follow-up pull request with the same title.
  • Existing whole-second timestamps are unaffected, so exported public-key bytes and fingerprints do not change.

Testing

  • ✅ gofmt -l .
  • ✅ go vet ./...
  • ✅ go test -race -shuffle=on -count=1 ./...
  • ✅ go test -run TestGoldenPublicKey -update . (passed and left testdata/ unchanged)
  • ✅ git diff --check
  • ✅ go tool -modfile=tools/go.mod golangci-lint run ./... (0 issues)
  • ⚠️ go mod tidy -diff && (cd tools && go mod tidy -diff) && go mod verify (environment returned HTTP 403 while downloading transitive test modules)
  • ⚠️ go tool -modfile=tools/go.mod govulncheck ./... (environment returned HTTP 403 while refreshing the vulnerability database)
  • ✅ git status --short --branch (clean working tree)

View task →

@deansheather
deansheather merged commit 5417e87 into main Nov 17, 2026
8 checks passed
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