You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(client): add identity key resolvers and safefetch - #2242
Third PR in the identity/ownership claims epic (#2212), following #2221 (claim types) and #2223 (claim signing and verification), both merged.
Adds one resolver per key-discovery scheme under client/utils/identity/resolvers, each in its own package, plus the SSRF-guarded HTTP client two of them use.
A resolver only finds keys; it never checks a signature. Its result goes to identity.Verify (from #2223), which does all of the cryptography and accepts the claim if any one key validates:
This differs from the sketch in #2214, where each scheme was a Verifier that did its own crypto under server/identity/*. Keeping resolvers key-only puts all signature logic in one place (jws) and lets a resolver hand over a whole key set, so it lives next to jws/identity in client/utils where local verification already happens.
Layout
client/utils/identity/resolvers/ Resolver, Fetcher, ErrNoKeys: the public contract
├── dns/ package dnsresolver
├── did/ package didresolver
├── wellknown/ package wellknownresolver
└── spiffe/ package spifferesolver
client/utils/internal/testutil/ test fixtures shared by jws, identity and the resolvers
Each scheme is its own package with an explicit constructor, so a caller imports only what it uses and the call site says what it is. There is no registry or self-registration; the caller picks one by subject scheme.
Constructor
Subject
Where the key comes from
dnsresolver.New(opts...)
dns:acme.com, acme.com
TXT records at _agntcy-key.DOMAIN, formatted v=akv1;key=BASE64_SPKI
https://HOST/.well-known/jwks.json; skips keys not usable for verification
didresolver.New(fetch)
did:web:...
the DID document over HTTPS; its id must equal the subject; only keys in its assertionMethod
did:key:...
decoded locally, no network (ed25519, P-256, P-384, RSA)
spifferesolver.New(bundles)
spiffe://TRUST_DOMAIN/...
the claim's X.509-SVID, see below
A resolver returns every key it finds, or resolvers.ErrNoKeys if there are none, so an empty result never comes back with a nil error. dnsresolver takes a WithLookupTXT option to replace the system lookup in tests.
Key handling lives in jws.jws.ToPublicKey and jws.PublicKeyFromJWK decide which keys are usable, reusing the same check jws.Verify applies, so a resolver only returns keys jws can verify with: no unsupported curves, no RSA under 2048 bits (also for did:key), no malformed keys (typed-nil or incomplete ECDSA/RSA, an Ed25519 key of the wrong length: these are refused, not a panic), and no JWK marked for encryption (use: enc) or whose key_ops omits verify.
SPIFFE is the one scheme where the key isn't published anywhere. It comes from the claim's certificate (embedded at signing time with identity.WithCertificate), and is trusted only after x509svid.ParseAndVerify validates it against the subject's trust-domain bundle (validity window, leaf rules) and its SPIFFE ID equals the subject exactly. A trust domain with no bundle fails closed with an error. spifferesolver.LoadBundles reads one PEM bundle per trust domain.
Hardening worth a reviewer's eye
Subjects that become URLs are the ones that need host validation. wellknown parses the subject as a URL and refuses userinfo (in https://[email protected] the host is evil.com, not acme.com). did:web builds its document URL from percent-encoded segments: the host must survive a reparse unchanged, and each path segment must decode to a single plain segment, so an escaped /, \, . or .. can't steer the request to another path or host. The path is built from the decoded segments, so did:web:acme.com:alice%20smith fetches /alice%20smith/did.json. dns builds a TXT name rather than a URL, so it only rejects an empty subject.
did:web requires the document's id to match the requested DID, otherwise one domain could publish keys on behalf of another identity. Only keys the document lists under assertionMethod (as a reference, absolute or relative, or embedded) are returned: a key that appears only under verificationMethod, or under another relationship such as capabilityInvocation or authentication, is not authorized to make assertions. A document that only lists verificationMethod therefore resolves to no keys.
safefetch: https only; non-public addresses are refused at dial time, after DNS resolution, and the connection goes to a validated IP so DNS rebinding can't slip through. Every resolved address is checked, and the validated addresses are dialed in order so one down endpoint of a multi-address host doesn't make the host unreachable. Covers loopback, RFC 1918, link-local (cloud metadata), shared address space (100.64.0.0/10), IPv4-mapped IPv6, deprecated site-local (fec0::/10), the IPv6 transition ranges that can translate to a blocked IPv4 target (NAT64 64:ff9b::/96 and 64:ff9b:1::/48, 6to4, Teredo, the deprecated IPv4-compatible range), and the IPv4 documentation and relay ranges. Redirects are bounded and can't downgrade to http. Responses are size-capped, which also bounds how many keys a document can carry.
Contains
client/utils/identity/resolvers/: the Resolver and Fetcher interfaces and ErrNoKeys
resolvers/{dns,did,wellknown,spiffe}/: one package per scheme (+ tests, each with its own end-to-end test)
client/utils/internal/testutil/: the test fixtures shared by the jws, identity and resolver tests (key generators, PEM and certificate helpers, a fake fetcher, and the sign → resolve → verify claim flow the end-to-end tests use), importable only from within client/utils. Because it imports jws and identity, their tests are external packages (jws_test, identity_test). The jws and identity tests from feat(client): add identity.v1 claim signing and verification #2223 now use it, which removes their duplicated key and certificate helpers.
client/utils/jws/keys.go: ToPublicKey, PublicKeyFromJWK (+ tests), and a malformed-key check in jws.go
utils/safefetch/ (+ tests)
client/go.mod: go-multibase becomes a direct dependency. It was already in the module graph, so there is no new module and no go.sum change.
Nothing outside these packages calls any of this yet. Wiring into the reconciler is a later PR.
Verification
gofmt, go build, go vet, go test, and golangci-lint (CI-pinned 2.13.2, over the full client/utils tree and utils/safefetch) are clean for the client and utils modules, and go mod tidy shows no drift in client.
Tests use no live network: fake TXT lookups, fake fetchers, and generated CA/SVID certificates.
Each resolver has passing and failing cases, plus malformed-subject cases that assert no fetch is made.
Each scheme's package has a TestEndToEnd* that runs identity.Sign → resolver → identity.Verify, so the resolvers are proven against the real signing code and not only against themselves. That includes a forged claim failing against a published key. A TestResolver_SatisfiesContract per package asserts that the resolver satisfies resolvers.Resolver.
SPIFFE fails closed for: no bundle, empty bundle set, untrusted CA, wrong SPIFFE ID, other trust domain, expired, not-yet-valid, no SPIFFE ID, several IDs, a CA cert used as a leaf, no certificate, and garbage.
The review fixes each have regression cases, and each case fails when its fix is reverted: NAT64/6to4/Teredo/site-local/documentation addresses and the multi-address dial fallback in safefetch, an escaped did:web path segment, did:web keys outside assertionMethod (other relationships, unreferenced, dangling reference, relative and embedded methods), a sub-2048-bit did:key RSA key, typed-nil, incomplete and wrong-length keys (which panicked before), and key_ops without verify.
I mutation-tested the safeguards: breaking each guard in turn (the SPIFFE ID check, the DID id check and host/path-segment checks, the wellknown userinfo and scheme checks, the JWKS use filter, the jws key-type and RSA-size checks, the identity.Sign certificate key-match and subject checks, DNS record version, and the safefetch private-range, redirect and IPv4-mapped checks) makes a test fail. One mutation initially survived (IPv4-mapped forms of the ranges netip doesn't cover); I added tests for it. The resolver, jws and safefetch mutations were last run before the identity.WithCertificate change in feat(client): add identity.v1 claim signing and verification #2223 and the fixture move; the identity.Sign certificate guards and the review-fix reverts were checked after it.
multibase.Decode accepts encodings other than base58btc, but the did:key method requires the method-specific identifier to use base58btc (z...). Ignoring the returned encoding makes invalid, non-interoperable DID identifiers resolve successfully. Capture the encoding and reject values other than multibase.Base58BTC.
Reject incomplete or off-curve ECDSA public keys
client/utils/jws/jws.go:35
This guard still accepts incomplete or off-curve ECDSA keys such as &ecdsa.PublicKey{Curve: elliptic.P256()}. ToPublicKey consequently reports them as usable, and downstream verification can fail unsafely instead of honoring the new malformed-key guarantee. Validate both coordinates and the encoded point before selecting an algorithm.
Validate RSA modulus and public exponent
client/utils/jws/jws.go:48
Checking only N leaves other malformed RSA keys accepted: for example, a 2048-bit modulus with the zero-value exponent passes ToPublicKey, although crypto/rsa rejects it as an invalid public key. Validate the modulus sign and the public-exponent bounds before returning RS256.
Adds client/utils/identity/resolvers: one resolver per key-discovery
scheme, each returning the candidate public keys for a claim's subject.
A resolver only finds keys; it never checks a signature. Pass the result
to identity.Verify, which does all of the cryptography and accepts the
claim if any one key validates.
Resolve(ctx, subject, certificate) ([]crypto.PublicKey, error)
- DNS: "_agntcy-key.<domain>" TXT records, "v=akv1;key=<base64 SPKI>".
- WellKnown: https subjects, the host's /.well-known/jwks.json. Skips
encryption-only and symmetric keys.
- DID: did:web (document over HTTPS, whose id must equal the subject) and
did:key (self-certifying, no network) for ed25519, P-256, P-384, RSA.
did:web segments are validated so an escaped "/" or ".." can't steer
the request to another path or host.
- SPIFFE: the key comes from the claim's X.509-SVID, trusted only after
x509svid.ParseAndVerify validates it against the subject's trust-domain
bundle and its SPIFFE ID equals the subject. A trust domain with no
bundle fails closed.
Each resolver is built with an explicit constructor; there is no registry
or self-registration. Network access goes through a small Fetcher
interface, so resolvers are tested against fakes with no live network.
Every resolver caps the number of keys it will return.
utils/safefetch is the SSRF-guarded HTTPS client did and wellknown use by
default: https only, non-public addresses refused at dial time after DNS
resolution (loopback, RFC 1918, link-local, shared address space, IPv4-
mapped IPv6), redirects bounded and unable to downgrade to http, and a
response size cap.
Stacked on #2223; nothing outside these packages calls any of this yet.
Closes#2214
Signed-off-by: Ramiz Polic <[email protected]>
Each resolver now lives in its own package under resolvers/, with a
package name that reads at the call site:
didresolver.New(fetch) resolvers/did
dnsresolver.New(opts...) resolvers/dns
wellknownresolver.New(fetch) resolvers/wellknown
spifferesolver.New(bundles) resolvers/spiffe
The type in each is Resolver, and spifferesolver.LoadBundles replaces
LoadSPIFFEBundles. The parent resolvers package keeps only the public
contract: the Resolver and Fetcher interfaces and ErrNoKeys.
Code the schemes share moves to resolvers/internal/keyutil (key
normalization, host parsing, the key-count cap, JWK conversion), and the
test fixtures to resolvers/internal/testutil, so neither is importable
from outside resolvers/. dnsresolver gains a WithLookupTXT option, since
the end-to-end test now lives outside the package.
Each scheme wraps the shared key-count error with its own name, so a
failure reads "dns: no usable public keys found at ...".
No behavior change: the same 21 test functions and 39 subtests pass.
Signed-off-by: Ramiz Polic <[email protected]>
- ToPublicKey and PublicKeyFromJWK move to client/utils/jws, where the set
of usable key types is already decided. ToPublicKey now reuses
jwsAlgorithmFor, so it accepts exactly what jws can verify with (and
refuses unsupported curves and RSA keys under 2048 bits up front)
instead of keeping a second, separate type switch.
- Finish and the per-subject key cap are removed: a resolver returns every
key it finds. Each resolver still returns ErrNoKeys rather than an
empty result with a nil error.
- ParseHost is removed. It only mattered where a URL is built from the
subject: wellknown now parses the subject as a URL and refuses
userinfo (in "https://[email protected]" the host is evil.com), and
did:web validates its host inline. dns builds a TXT name, not a URL,
so it only rejects an empty subject.
- internal/keyutil is deleted.
Signed-off-by: Ramiz Polic <[email protected]>
The jws and identity tests each carried a byte-identical generateKey, and
PKCS#8 encoding, key generation and self-signed certificates were written
out in four places. The resolvers' testutil added a third copy.
- client/utils/internal/testutil is now the one shared package, importable
by jws, identity and the resolvers and by nothing outside client/utils:
key generators (including a NewKey(kind) that replaces both generateKey
copies), PKCS#8 and certificate PEM helpers, a self-signed certificate,
JWKS, a fake fetcher, and RequireSameKeys. It imports none of this
repo's packages, so it cannot form an import cycle with their tests.
- Fixtures only one scheme uses move into that scheme's tests: the DID
document and did:key builders, the DNS TXT key encoding, a JWK with a
"use", and the test CA, SVID issuer and trust bundle.
- The parent-package end-to-end test splits into each scheme's package, so
a scheme's sign -> resolve -> verify test uses only its own fixtures.
The shared flow lives in resolvers/internal/claimtest, kept apart from
testutil because it imports identity and jws. The compile-time contract
assertions become a TestResolver_SatisfiesContract per package.
- The two 1024-bit RSA key generations in the jws tests share one helper.
No production code changes. Every previous test still runs: the five
end-to-end tests are now TestEndToEnd* in their own packages, and the
security mutations (SPIFFE ID, DID id and host, userinfo, key/cert match,
RSA floor and others) are all still caught.
Signed-off-by: Ramiz Polic <[email protected]>
- safefetch: also refuse the IPv6 transition ranges (NAT64, 6to4, Teredo,
discard, documentation, deprecated IPv4-compatible) and the IPv4
documentation and relay ranges, which can reach a blocked IPv4 target
- did:web: build the document path from the decoded segments so an escaped
segment is not escaped a second time
- did:key: apply the shared jws key filter, so an RSA key below the minimum
size is no longer returned as a usable key
- jws: refuse typed-nil and incomplete ECDSA/RSA keys instead of panicking
- jws: honor key_ops on a JWK and require verify when it is present
Signed-off-by: Ramiz Polic <[email protected]>
Move the sign-resolve-verify claim helpers into client/utils/internal/testutil
so the shared test code is one package. testutil now imports jws and
identity, so their tests become external (package jws_test, identity_test).
Signed-off-by: Ramiz Polic <[email protected]>
- did:web: only accept keys authorized by assertionMethod (by reference or
embedded) instead of every verificationMethod
- jws: refuse Ed25519 keys of the wrong length instead of panicking
- safefetch: also refuse deprecated site-local fec0::/10
- safefetch: try every validated address in order rather than only the first
Signed-off-by: Ramiz Polic <[email protected]>
CodeQL flags rsa.GenerateKey with 1024 bits. The key is deliberately weak, to
test the 2048-bit floor, so assemble it from two random primes instead.
Signed-off-by: Ramiz Polic <[email protected]>
The 1024-bit key is deliberately weak, to test the 2048-bit floor. The CodeQL
finding is a false positive and will be dismissed rather than worked around.
This reverts commit dad0d7b.
Signed-off-by: Ramiz Polic <[email protected]>
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
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.
Summary
Third PR in the identity/ownership claims epic (#2212), following #2221 (claim types) and #2223 (claim signing and verification), both merged.
Adds one resolver per key-discovery scheme under
client/utils/identity/resolvers, each in its own package, plus the SSRF-guarded HTTP client two of them use.A resolver only finds keys; it never checks a signature. Its result goes to
identity.Verify(from #2223), which does all of the cryptography and accepts the claim if any one key validates:This differs from the sketch in #2214, where each scheme was a
Verifierthat did its own crypto underserver/identity/*. Keeping resolvers key-only puts all signature logic in one place (jws) and lets a resolver hand over a whole key set, so it lives next tojws/identityinclient/utilswhere local verification already happens.Layout
Each scheme is its own package with an explicit constructor, so a caller imports only what it uses and the call site says what it is. There is no registry or self-registration; the caller picks one by subject scheme.
dnsresolver.New(opts...)dns:acme.com,acme.com_agntcy-key.DOMAIN, formattedv=akv1;key=BASE64_SPKIwellknownresolver.New(fetch)https://acme.com[/...]https://HOST/.well-known/jwks.json; skips keys not usable for verificationdidresolver.New(fetch)did:web:...idmust equal the subject; only keys in itsassertionMethoddid:key:...spifferesolver.New(bundles)spiffe://TRUST_DOMAIN/...A resolver returns every key it finds, or
resolvers.ErrNoKeysif there are none, so an empty result never comes back with a nil error.dnsresolvertakes aWithLookupTXToption to replace the system lookup in tests.Key handling lives in
jws.jws.ToPublicKeyandjws.PublicKeyFromJWKdecide which keys are usable, reusing the same checkjws.Verifyapplies, so a resolver only returns keysjwscan verify with: no unsupported curves, no RSA under 2048 bits (also fordid:key), no malformed keys (typed-nil or incomplete ECDSA/RSA, an Ed25519 key of the wrong length: these are refused, not a panic), and no JWK marked for encryption (use: enc) or whosekey_opsomitsverify.SPIFFE is the one scheme where the key isn't published anywhere. It comes from the claim's certificate (embedded at signing time with
identity.WithCertificate), and is trusted only afterx509svid.ParseAndVerifyvalidates it against the subject's trust-domain bundle (validity window, leaf rules) and its SPIFFE ID equals the subject exactly. A trust domain with no bundle fails closed with an error.spifferesolver.LoadBundlesreads one PEM bundle per trust domain.Hardening worth a reviewer's eye
wellknownparses the subject as a URL and refuses userinfo (inhttps://[email protected]the host isevil.com, notacme.com).did:webbuilds its document URL from percent-encoded segments: the host must survive a reparse unchanged, and each path segment must decode to a single plain segment, so an escaped/,\,.or..can't steer the request to another path or host. The path is built from the decoded segments, sodid:web:acme.com:alice%20smithfetches/alice%20smith/did.json.dnsbuilds a TXT name rather than a URL, so it only rejects an empty subject.did:webrequires the document'sidto match the requested DID, otherwise one domain could publish keys on behalf of another identity. Only keys the document lists underassertionMethod(as a reference, absolute or relative, or embedded) are returned: a key that appears only underverificationMethod, or under another relationship such ascapabilityInvocationorauthentication, is not authorized to make assertions. A document that only listsverificationMethodtherefore resolves to no keys.safefetch: https only; non-public addresses are refused at dial time, after DNS resolution, and the connection goes to a validated IP so DNS rebinding can't slip through. Every resolved address is checked, and the validated addresses are dialed in order so one down endpoint of a multi-address host doesn't make the host unreachable. Covers loopback, RFC 1918, link-local (cloud metadata), shared address space (100.64.0.0/10), IPv4-mapped IPv6, deprecated site-local (fec0::/10), the IPv6 transition ranges that can translate to a blocked IPv4 target (NAT6464:ff9b::/96and64:ff9b:1::/48, 6to4, Teredo, the deprecated IPv4-compatible range), and the IPv4 documentation and relay ranges. Redirects are bounded and can't downgrade tohttp. Responses are size-capped, which also bounds how many keys a document can carry.Contains
client/utils/identity/resolvers/: theResolverandFetcherinterfaces andErrNoKeysresolvers/{dns,did,wellknown,spiffe}/: one package per scheme (+ tests, each with its own end-to-end test)client/utils/internal/testutil/: the test fixtures shared by thejws,identityand resolver tests (key generators, PEM and certificate helpers, a fake fetcher, and the sign → resolve → verify claim flow the end-to-end tests use), importable only from withinclient/utils. Because it importsjwsandidentity, their tests are external packages (jws_test,identity_test). Thejwsandidentitytests from feat(client): add identity.v1 claim signing and verification #2223 now use it, which removes their duplicated key and certificate helpers.client/utils/jws/keys.go:ToPublicKey,PublicKeyFromJWK(+ tests), and a malformed-key check injws.goutils/safefetch/(+ tests)client/go.mod:go-multibasebecomes a direct dependency. It was already in the module graph, so there is no new module and nogo.sumchange.Nothing outside these packages calls any of this yet. Wiring into the reconciler is a later PR.
Verification
gofmt,go build,go vet,go test, and golangci-lint (CI-pinned 2.13.2, over the fullclient/utilstree andutils/safefetch) are clean for theclientandutilsmodules, andgo mod tidyshows no drift inclient.Tests use no live network: fake TXT lookups, fake fetchers, and generated CA/SVID certificates.
TestEndToEnd*that runsidentity.Sign→ resolver →identity.Verify, so the resolvers are proven against the real signing code and not only against themselves. That includes a forged claim failing against a published key. ATestResolver_SatisfiesContractper package asserts that the resolver satisfiesresolvers.Resolver.safefetch, an escapeddid:webpath segment,did:webkeys outsideassertionMethod(other relationships, unreferenced, dangling reference, relative and embedded methods), a sub-2048-bitdid:keyRSA key, typed-nil, incomplete and wrong-length keys (which panicked before), andkey_opswithoutverify.idcheck and host/path-segment checks, thewellknownuserinfo and scheme checks, the JWKSusefilter, thejwskey-type and RSA-size checks, theidentity.Signcertificate key-match and subject checks, DNS record version, and the safefetch private-range, redirect and IPv4-mapped checks) makes a test fail. One mutation initially survived (IPv4-mapped forms of the rangesnetipdoesn't cover); I added tests for it. The resolver,jwsand safefetch mutations were last run before theidentity.WithCertificatechange in feat(client): add identity.v1 claim signing and verification #2223 and the fixture move; theidentity.Signcertificate guards and the review-fix reverts were checked after it.Closes #2214