Skip to content

feat(client): add identity key resolvers and safefetch - #2242

Merged
ramizpolic merged 9 commits into
mainfrom
feat/identity-03-resolvers
Oct 1, 2026
Merged

ramizpolic merged 9 commits into
mainfrom
feat/identity-03-resolvers

Conversation

@ramizpolic

@ramizpolic ramizpolic commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

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:

type Resolver interface {
    Resolve(ctx context.Context, subject string, certificate []byte) ([]crypto.PublicKey, error)
}

keys, err := didresolver.New(fetch).Resolve(ctx, claim.GetSubject(), certificate)
ok, err := identity.Verify(claim, recordCID, expectedSubject, keys...)

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
wellknownresolver.New(fetch) https://acme.com[/...] 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.

Closes #2214

@github-actions github-actions Bot added the size/L Denotes a PR that changes 1000-1999 lines label Sep 30, 2026
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

DID URL handling, key filtering, and SSRF address classification contain correctness and security gaps.

Review effort: Balanced
Findings: 1 High severity · 4 Medium severity

Open (5)
What changed in this PR

Adds identity key resolvers and SSRF-safe document fetching for the identity claims workflow.

Changes:

  • Adds DNS, DID, well-known JWKS, and SPIFFE resolvers.
  • Adds reusable JWK/public-key conversion helpers.
  • Adds SSRF protections and resolver integration tests.
File Description
utils/​safefetch/​fetch.go Implements restricted HTTP fetching.
utils/​safefetch/​fetch_test.go Tests fetch limits and SSRF protections.
client/​utils/​jws/​keys.go Converts supported public keys and JWKs.
client/​utils/​jws/​keys_test.go Tests key conversion and filtering.
client/​utils/​identity/​resolvers/​resolvers.go Defines resolver contracts and errors.
client/​utils/​identity/​resolvers/​dns/​dns.go Resolves keys from DNS TXT records.
client/​utils/​identity/​resolvers/​dns/​dns_test.go Tests DNS resolution.
client/​utils/​identity/​resolvers/​did/​did.go Resolves did:web and did:key subjects.
client/​utils/​identity/​resolvers/​did/​did_test.go Tests DID resolution and validation.
client/​utils/​identity/​resolvers/​wellknown/​wellknown.go Resolves domain JWKS keys.
client/​utils/​identity/​resolvers/​wellknown/​wellknown_test.go Tests well-known resolution.
client/​utils/​identity/​resolvers/​spiffe/​spiffe.go Verifies SVIDs and extracts keys.
client/​utils/​identity/​resolvers/​spiffe/​spiffe_test.go Tests SPIFFE trust validation.
client/​utils/​identity/​resolvers/​internal/​testutil/​testutil.go Provides shared resolver test fixtures.
client/​utils/​identity/​resolvers/​e2e_test.go Tests sign-resolve-verify workflows.
client/​go.mod Promotes go-multibase to a direct dependency.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread utils/safefetch/fetch.go Resolved
Comment thread client/utils/jws/keys.go Resolved
Comment thread client/utils/jws/keys.go Resolved
@ramizpolic
ramizpolic force-pushed the feat/identity-03-resolvers branch 2 times, most recently from 5379d8c to 971c0bf Compare October 1, 2026 08:54
@github-actions github-actions Bot added size/XL Denotes a PR that changes 2000+ lines and removed size/L Denotes a PR that changes 1000-1999 lines labels Oct 1, 2026
@ramizpolic
ramizpolic force-pushed the feat/identity-03-resolvers branch from 600685b to 32175da Compare October 1, 2026 09:09
@ramizpolic
ramizpolic force-pushed the feat/identity-02-claim-crypto branch from 3e7cce7 to 56b1729 Compare October 1, 2026 09:19
@ramizpolic
ramizpolic force-pushed the feat/identity-03-resolvers branch from 32175da to 3cf4e84 Compare October 1, 2026 09:20
@ramizpolic ramizpolic linked an issue Oct 1, 2026 that may be closed by this pull request
@ramizpolic
ramizpolic marked this pull request as ready for review October 1, 2026 09:59
@ramizpolic
ramizpolic requested a review from a team as a code owner October 1, 2026 09:59
@ramizpolic
ramizpolic requested a balanced review from Copilot October 1, 2026 11:54
lgecse
lgecse previously approved these changes Oct 1, 2026

@lgecse lgecse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread client/utils/jws/jws.go Resolved
Comment thread utils/safefetch/fetch.go Resolved
Comment thread utils/safefetch/fetch.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The SSRF filter misses non-public IPv6 ranges, while malformed keys and nonconforming did:key encodings remain accepted.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (4)
Previously missed (3)

In code that hasn't changed since last review

Medium severity Require base58btc encoding for did:key identifiers

client/​utils/​identity/​resolvers/​did/​did.go:211

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.

Medium severity 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.

Medium severity 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.

Comment thread utils/safefetch/fetch.go Resolved
Base automatically changed from feat/identity-02-claim-crypto to main October 1, 2026 12:27
@ramizpolic
ramizpolic dismissed lgecse’s stale review October 1, 2026 12:27

The base branch was changed.

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]>
@ramizpolic
ramizpolic force-pushed the feat/identity-03-resolvers branch from cca056e to d4bce5a Compare October 1, 2026 12:33
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]>

@tkircsi tkircsi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ramizpolic
ramizpolic added this pull request to the merge queue Oct 1, 2026
Merged via the queue into main with commit 7877185 Oct 1, 2026
34 checks passed
@ramizpolic
ramizpolic deleted the feat/identity-03-resolvers branch October 1, 2026 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Add identity resolvers for dns, did, wellknown, and spiffe

5 participants