Repository navigation
fix(apk): stop repository key IDs from escaping key paths and URLs - #2698
Merged
konradzapalowicz merged 2 commits intoNov 25, 2026
Merged
Conversation
A repository chooses each JWKS "kid", and DiscoverKeys turns it into the key file name. A kid containing a path separator or ".." let install-keys write outside /etc/apk/keys. DiscoverKeys now rejects the whole key set before building a name from such a kid, which protects every caller.
discoverKeysForLock built each key URL as repository + "/" + key ID. A repository-chosen kid containing "?", "#" or "%" changed which resource the URL named. The key ID is now escaped as a single path segment, which leaves ordinary key IDs unchanged.
tomgeorge
approved these changes
Nov 25, 2026
tomgeorge
left a comment
Member
There was a problem hiding this comment.
LGTM. The kid check closes the path escape for every caller, and the lockfile URL now treats the key ID as one path segment.
What I verified
DiscoverKeysrejects the whole set before building a file name when anykidcontains/,\or..(pkg/apk/apk/implementation.go:1533). With those excluded,kid + ".rsa.pub"is a single plain file name, sofilepath.Join("/etc/apk/keys", key.ID)in internal/cli/install-keys.go:44 can't leave the directory.- All three discovery callers go through that check: install-keys,
InitRuntimeOnlyKeys(implementation.go:460, whichWriteRuntimeKeysguards a second time at :492), andfetchChainguardKeysvia(*APK).DiscoverKeys. The singleflight cache in front of it caches the result, not the raw JWKS, so a cached result was validated too. url.PathEscape(key.ID)in internal/cli/lock.go:383 leaves IDs likersa-test.rsa.pubunchanged, so existing lockfiles don't churn.url.Parseof the escaped URL gives back the literal ID as the last path segment, which is the file name discovery would have used.TestDiscoverKeysForLockEscapesKeyIDInURLcovers both points.TestDiscoverKeysRejectsUnsafeKeyIDscovers a badkidboth before and after a good one and checks that no keys come back. It also checks that ordinary names, including one with a single dot, still pass.- At 050cf6d, CodeQL and the other checks pass.
Security
- untrusted-input: cleared. A repository-chosen
kidis validated at implementation.go:1533 before it reaches a file path or URL. - crypto (trusted key installation): cleared. A malformed
kidnow fails discovery instead of planting a key outside the keyring directory. Not checked by a second agent.
brian-mitchell-sec
approved these changes
Nov 25, 2026
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.
A repository that supports key discovery serves its signing keys as a JWKS. Each key's
kidis chosen by the repository, andDiscoverKeysturns it into the key file name (<kid>.rsa.pub). Until now thekidwas not checked, so akidcontaining a path separator or..letapko install-keyswrite a key outside/etc/apk/keys. The samekidalso went unescaped into the key URL thatapko lockrecords, so characters such as?,#or%changed which resource that URL named.DiscoverKeysnow rejects the whole key set when anykidcontains/,\or.., before a file name is built from it. Because the check sits in discovery, every caller is protected. The lockfile key URL now path-escapes the key ID as a single segment. Keys with ordinary identifiers are discovered, installed and locked exactly as before.What changed
pkg/apk/apk/implementation.gokidcheck inDiscoverKeyspkg/apk/apk/implementation_test.goTestDiscoverKeysRejectsUnsafeKeyIDs(6 reject rows, 1 accept row)internal/cli/lock.gonet/urlimport,url.PathEscape, commentinternal/cli/lock_security_test.goTestDiscoverKeysForLockEscapesKeyIDInURLTesting
go build ./...go test ./pkg/apk/apk/ ./internal/cli/golangci-lint run ./pkg/apk/apk/... ./internal/cli/...(0 issues)🤖 Generated with Claude Code