Skip to content

feat(authn): harden OIDC HTTPS and validate Kubernetes tokens - #2218

Merged
jhrozek merged 1 commit into
mainfrom
ext-broker-02-harden-oidc
Oct 9, 2026
Merged

jhrozek merged 1 commit into
mainfrom
ext-broker-02-harden-oidc

Conversation

@jhrozek

@jhrozek jhrozek commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

The broker authenticates workloads with Kubernetes service-account tokens.
The API server's JWKS endpoint requires a bearer token, and authn/oidc had no
way to send one. Its private-HTTPS mode also approved more network
destinations than it used.

NewKubernetesValidator (new)

  • Reads the "iss" claim from the broker's projected token without verifying
    it, and accepts it only if the configured discovery URL returns the same
    issuer. Tokens are then verified with keys from the configured JWKS URL.
    Neither the token nor the discovery response chooses where requests go;
    the discovery document is read only for its issuer.
  • Discovery and JWKS URLs must be HTTPS on the same host and port, are reached
    over private addresses, and are verified only against the CA file
    (TrustedCAFile). There is no per-host address pin; TLS verification against
    the cluster CA is what authenticates the API server.
  • One client built with networking.HttpClientBuilder (private addresses, the
    CA, the token file, a 15s timeout, redirects refused) serves the discovery
    request, toolhive-core's key fetches, and Ready. The builder re-reads the
    token file on every request, so rotation works; this relies on toolhive-core
    v0.0.51, where v0.0.50 read the file once. TokenSource supplies the token's
    contents once, for the issuer claim, because authn/oidc may not read files.
  • A dedicated audience is required. An empty audience or AllowAnyAudience is
    rejected, because every pod in the cluster can mint tokens from this issuer.
    cfg.Issuer and cfg.JWKSURI must be empty (both come from the bootstrap), and
    a caller-supplied HTTPClient or another private-address mode is rejected.

Private-HTTPS hardening

  • With JWKSURI set, only the JWKS host is approved; the issuer host no longer
    is. toolhive-core never fetches the issuer when a JWKS URL is pinned, so
    nothing used that destination. A request to the issuer now fails with
    "not an approved endpoint" instead of being allowed.
  • scopedhttps caps every response body at 4 MiB using http.MaxBytesReader. A
    body of exactly 4 MiB succeeds; one more byte is an error. This applies to
    every NewSingleIssuerClient caller (cliconfig native endpoint, clientauth
    issuer client).
  • scopedhttps rejects two different CA bundles for the same host:port instead
    of merging them into one pool.

Validator.Ready (new)

  • Fetches the JWKS URL and requires at least one key that parses as an RSA or
    EC public key, the key types toolhive-core accepts (it excludes EdDSA). The
    set is parsed with jwx v4's jwk package, non-strictly as core does, so one
    malformed key does not hide its siblings; a kid is optional. It returns nil
    without checking anything when no JWKSURI is configured (discovery mode).
  • authn/oidc takes jwx/v4 as a direct dependency (already in the module
    graph through toolhive-core), and depguard allows its jwk package.
  • Ready probes through the validator's own client: the one this package
    built for private-HTTPS mode, or the Kubernetes client above, so a probe
    uses the same transport, CA, token and timeout as the key fetches.
    Otherwise Ready applies the policy toolhive-core uses for its JWKS fetches:
    a caller-supplied client is copied and gets redirect refusal and a 15s
    timeout if it set none, and with no client one is built with
    networking.HttpClientBuilder.
  • The key check approximates toolhive-core's policy (it does not enforce the
    RSA size floor, use, key_ops, or algorithm match), as noted on usableJWKS.
  • depguard: authn-oidc may now import toolhive-core/networking.

Tests

  • Kubernetes: the issuer is derived from the token, which reaches both
    endpoints, and tokens from another issuer are rejected; Ready sends a
    rotated token; a discovery issuer that differs from the token's, unusable
    tokens (none exposed in an error), bad discovery responses, a different
    origin, a JWKS URL that redirects to a valid key set, and 14 invalid
    configurations are all refused.
  • Ready: a table that makes every case reach Ready (usable keys, 500,
    redirect, not JSON, empty set, key without material), and a table for
    usableJWKS built from generated RSA, EC and Ed25519 keys, including fields
    that are present but not a key.
  • scopedhttps: exact-limit and over-limit bodies through the client,
    conflicting CA bundles.

Checked with gofmt, go vet, golangci-lint and go test -race in authn/oidc, plus
the tests of its direct consumers (internal/cliconfig, clientauth,
oidcclient). The full lint and race gates are left to CI.

Co-Authored-By: mecatl [email protected]

Summary

Issue relationship

Relates to #

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

Baseline checks

  • Linting (task lint)
  • Fast offline test suite (task test)
  • Pre-review race suite (task test:race)
  • Offline demo (go run ./cmd/mecademo)
  • Markdown changed: docs generation/link checks (task docs)
  • User docs/user-facing behavior changed: site build (task site:build)
  • Guarded engine API affected: compatibility check (task api:check)
  • Intentional engine API change: task api:update + engine/CHANGELOG.md

Changes

File Change

User-facing change

Special notes for reviewers

The broker authenticates workloads with Kubernetes service-account tokens.
The API server's JWKS endpoint requires a bearer token, and authn/oidc had no
way to send one. Its private-HTTPS mode also approved more network
destinations than it used.

NewKubernetesValidator (new)

- Reads the "iss" claim from the broker's projected token without verifying
  it, and accepts it only if the configured discovery URL returns the same
  issuer. Tokens are then verified with keys from the configured JWKS URL.
  Neither the token nor the discovery response chooses where requests go;
  the discovery document is read only for its issuer.
- Discovery and JWKS URLs must be HTTPS on the same host and port, are reached
  over private addresses, and are verified only against the CA file
  (TrustedCAFile). There is no per-host address pin; TLS verification against
  the cluster CA is what authenticates the API server.
- One client built with networking.HttpClientBuilder (private addresses, the
  CA, the token file, a 15s timeout, redirects refused) serves the discovery
  request, toolhive-core's key fetches, and Ready. The builder re-reads the
  token file on every request, so rotation works; this relies on toolhive-core
  v0.0.51, where v0.0.50 read the file once. TokenSource supplies the token's
  contents once, for the issuer claim, because authn/oidc may not read files.
- A dedicated audience is required. An empty audience or AllowAnyAudience is
  rejected, because every pod in the cluster can mint tokens from this issuer.
  cfg.Issuer and cfg.JWKSURI must be empty (both come from the bootstrap), and
  a caller-supplied HTTPClient or another private-address mode is rejected.

Private-HTTPS hardening

- With JWKSURI set, only the JWKS host is approved; the issuer host no longer
  is. toolhive-core never fetches the issuer when a JWKS URL is pinned, so
  nothing used that destination. A request to the issuer now fails with
  "not an approved endpoint" instead of being allowed.
- scopedhttps caps every response body at 4 MiB using http.MaxBytesReader. A
  body of exactly 4 MiB succeeds; one more byte is an error. This applies to
  every NewSingleIssuerClient caller (cliconfig native endpoint, clientauth
  issuer client).
- scopedhttps rejects two different CA bundles for the same host:port instead
  of merging them into one pool.

Validator.Ready (new)

- Fetches the JWKS URL and requires at least one key that parses as an RSA or
  EC public key, the key types toolhive-core accepts (it excludes EdDSA). The
  set is parsed with jwx v4's jwk package, non-strictly as core does, so one
  malformed key does not hide its siblings; a kid is optional. It returns nil
  without checking anything when no JWKSURI is configured (discovery mode).
- authn/oidc takes jwx/v4 as a direct dependency (already in the module
  graph through toolhive-core), and depguard allows its jwk package.
- Ready probes through the validator's own client: the one this package
  built for private-HTTPS mode, or the Kubernetes client above, so a probe
  uses the same transport, CA, token and timeout as the key fetches.
  Otherwise Ready applies the policy toolhive-core uses for its JWKS fetches:
  a caller-supplied client is copied and gets redirect refusal and a 15s
  timeout if it set none, and with no client one is built with
  networking.HttpClientBuilder.
- The key check approximates toolhive-core's policy (it does not enforce the
  RSA size floor, use, key_ops, or algorithm match), as noted on usableJWKS.
- depguard: authn-oidc may now import toolhive-core/networking.

Tests

- Kubernetes: the issuer is derived from the token, which reaches both
  endpoints, and tokens from another issuer are rejected; Ready sends a
  rotated token; a discovery issuer that differs from the token's, unusable
  tokens (none exposed in an error), bad discovery responses, a different
  origin, a JWKS URL that redirects to a valid key set, and 14 invalid
  configurations are all refused.
- Ready: a table that makes every case reach Ready (usable keys, 500,
  redirect, not JSON, empty set, key without material), and a table for
  usableJWKS built from generated RSA, EC and Ed25519 keys, including fields
  that are present but not a key.
- scopedhttps: exact-limit and over-limit bodies through the client,
  conflicting CA bundles.

Checked with gofmt, go vet, golangci-lint and go test -race in authn/oidc, plus
the tests of its direct consumers (internal/cliconfig, clientauth,
oidcclient). The full lint and race gates are left to CI.

Co-Authored-By: mecatl <[email protected]>
@jhrozek
jhrozek merged commit a4771a7 into main Oct 9, 2026
37 checks passed
@jhrozek
jhrozek deleted the ext-broker-02-harden-oidc branch October 9, 2026 06:54
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 13, 2026
2 tasks
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