mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 13:51:36 +00:00
854135dfb7
RefreshKeys), hand-rolled group-claim resolver, 21+ negative-test
matrix, token-leak hygiene, IdP downgrade-attack defense
Phase 3 of the bundle ships the business logic that turns the Phase 2
storage primitives into a working OpenID Connect 1.0 + RFC 7636 PKCE
authorization-code flow against any enterprise IdP (Okta / Azure AD /
Google Workspace / Keycloak / Authentik / Auth0).
Service surface:
- Service.HandleAuthRequest(providerID) -> authURL, cookie, preLoginID
Builds the IdP redirect with PKCE-S256 (mandatory; RFC 9700 §2.1.1),
server-generated 32-byte state + nonce, persisted to the pre-login
row keyed by the cookie value.
- Service.HandleCallback(cookie, code, state, ip, ua) -> *CallbackResult
11-step validation: pre-login lookup-and-consume (single-use),
constant-time state compare, code-for-token exchange with PKCE
verifier, ID-token verify (alg pin via go-oidc/v3), service-layer
re-checks of iss / aud / azp (multi-aud requires it; mismatch
rejected) / at_hash (REQUIRED when access_token returned —
Phase 3 lifts the OIDC core "MAY" to a service-level "MUST") /
exp / iat-window / nonce, group-claim resolution with userinfo
fallback, group->role mapping (fail-closed on no match),
user upsert, session mint via SessionMinter port.
- Service.RefreshKeys(providerID) — explicit cache eviction +
re-load. Re-runs the IdP downgrade-attack defense so a provider
that later rotates to advertising HS* / none is caught BEFORE the
next user login attempt.
Security posture (every fail-closed branch is a sentinel error +
test):
- Algorithm pinning: allow-list {RS256, RS512, ES256, ES384, EdDSA};
deny-list {HS256, HS384, HS512, none}. Belt-and-braces re-check
via isDisallowedAlg after go-oidc.Verify.
- PKCE-S256 mandatory (oauth2.GenerateVerifier + S256ChallengeOption);
`plain` rejection sentinel exists for defense-in-depth.
- State + nonce: 32-byte crypto/rand, base64url-no-pad,
constant-time compare, single-use.
- IdP downgrade-attack defense: at provider creation / RefreshKeys,
reject any IdP whose discovery doc advertises HS* / none in
id_token_signing_alg_values_supported.
- JWKS fail-closed: in-flight login fails 503; existing sessions
untouched. isJWKSFetchError detects the gooidc verify-error
shape; ErrJWKSUnreachable is the wire mapping.
- Token-leak hygiene: ID tokens, access tokens, refresh tokens,
authorization codes, PKCE verifiers, state, nonce, signing key
bytes — NEVER logged at any level. logging_test.go pins the
invariant via a slog buffer + grep-assert across HandleAuthRequest,
HandleCallback, alg rejection, and provider-load paths.
Group-claim resolver (internal/auth/oidc/groupclaim/):
- Hand-rolled per Decision 10 (no JSON-path lib; ~150 LOC).
- URL-shape paths (https:// / http://) treated as a single
literal key — Auth0 namespaced claims like
https://your-namespace/groups work without splitting on the
dots in the URL.
- Dot-separated paths walked through nested map[string]interface{}.
- []interface{} / []string / single-string normalized to []string;
bool / number / object / nil → fail closed.
- 18 unit tests + sentinels (ErrPathEmpty, ErrSegmentMissing,
ErrSegmentNotObject, ErrInvalidValueType).
Test surface:
- service_test.go: 57 test functions including all 21 prompt-mandated
negative cases (wrong aud / wrong iss / expired / unknown alg /
alg=none / HMAC alg / azp missing on multi-aud / azp mismatched /
at_hash missing / at_hash mismatched / iat in future / iat too old /
nonce mismatched / state mismatched / state replayed / PKCE plain
sentinel / pre-login replay / forged cookie / IdP downgrade /
group-claim missing / group-claim unmapped) plus the userinfo
fallback matrix (happy path + endpoint-missing + endpoint-failing +
userinfo-also-empty), HandleAuthRequest entry point + RNG-failure
paths, upsertUser update + create + display-name fallback +
Validate-error paths, decryptClientSecret real-encrypt round-trip
+ bad-passphrase, alg-parser malformed-header matrix.
- logging_test.go: 4 hygiene tests pinning no token / code / verifier /
state / cookie / client_secret / alg name appears in any captured
log line.
- groupclaim/resolver_test.go: 18 cases covering Okta string-array,
Keycloak realm_access.roles, Auth0 namespaced URL claim,
single-string normalization, deeply-nested 3-segment walks, and
every fail-closed branch.
Coverage:
internal/auth/oidc 92.2% (floor: 90)
internal/auth/oidc/groupclaim 100.0% (floor: 95)
internal/auth/oidc/domain 96.2% (floor: 90)
Coverage gates added at .github/coverage-thresholds.yml so a future
regression in any fail-closed branch fails CI before the commit lands.
Phase 3 of cowork/auth-bundle-2-prompt.md is closed. Next up: Phase 4
(Session service: cookies, revocation, sliding-vs-absolute expiry).
184 lines
6.2 KiB
Go
184 lines
6.2 KiB
Go
package oidc
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// =============================================================================
|
|
// Token-leak hygiene: no secret value (ID token, access token, refresh
|
|
// token, authorization code, PKCE verifier, state, nonce, signing key
|
|
// material) appears in any log line at any level.
|
|
//
|
|
// Methodology mirrors Bundle 1's
|
|
// internal/auth/bootstrap/service_test.go::TestService_TokenLeakHygiene:
|
|
// redirect slog.Default to a buffer, run the OIDC service paths,
|
|
// grep-assert the secret string never appears in any captured line.
|
|
//
|
|
// This is the load-bearing invariant for Phase 3's "tokens never
|
|
// logged" contract. Every secret-bearing path that enters the
|
|
// service.go code MUST flow through write-once-to-response patterns;
|
|
// adding a `slog.Info("got token", "value", token)` somewhere would
|
|
// fail this test immediately.
|
|
// =============================================================================
|
|
|
|
// captureLogger swaps the slog.Default with one that writes to the
|
|
// returned buffer. The returned restore func re-installs the original
|
|
// logger; callers must defer it.
|
|
func captureLogger(t *testing.T) (*bytes.Buffer, func()) {
|
|
t.Helper()
|
|
buf := &bytes.Buffer{}
|
|
original := slog.Default()
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(io.Writer(buf), &slog.HandlerOptions{
|
|
Level: slog.LevelDebug,
|
|
})))
|
|
return buf, func() { slog.SetDefault(original) }
|
|
}
|
|
|
|
// TestLoggingHygiene_HandleAuthRequest_LeaksNothing exercises the full
|
|
// HandleAuthRequest path against a mock IdP and asserts that the
|
|
// generated state, nonce, PKCE verifier, and pre-login cookie never
|
|
// appear in any captured log line.
|
|
func TestLoggingHygiene_HandleAuthRequest_LeaksNothing(t *testing.T) {
|
|
idp := newMockIdP(t)
|
|
svc, _ := newServiceWithProviderAndPL(t, idp.URL(), "op-leak-1")
|
|
|
|
buf, restore := captureLogger(t)
|
|
defer restore()
|
|
|
|
authURL, cookieValue, _, err := svc.HandleAuthRequest(context.Background(), "op-leak-1")
|
|
if err != nil {
|
|
t.Fatalf("HandleAuthRequest: %v", err)
|
|
}
|
|
|
|
// Extract state from the authURL query so we can grep-assert.
|
|
parts := strings.Split(authURL, "state=")
|
|
if len(parts) < 2 {
|
|
t.Fatalf("authURL missing state param: %q", authURL)
|
|
}
|
|
stateValue := strings.SplitN(parts[1], "&", 2)[0]
|
|
|
|
captured := buf.String()
|
|
for _, secret := range []string{stateValue, cookieValue} {
|
|
if secret == "" {
|
|
continue
|
|
}
|
|
if strings.Contains(captured, secret) {
|
|
t.Errorf("secret value %q appeared in log output:\n%s", secret, captured)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLoggingHygiene_HandleCallback_LeaksNothing runs the full callback
|
|
// flow (against the mock IdP) and grep-asserts the captured log buffer
|
|
// has no occurrence of the access token, the ID token, the
|
|
// authorization code, or the PKCE verifier.
|
|
func TestLoggingHygiene_HandleCallback_LeaksNothing(t *testing.T) {
|
|
idp := newMockIdP(t)
|
|
svc, pl := newServiceWithProviderAndPL(t, idp.URL(), "op-leak-2")
|
|
|
|
// Pre-login row with a known verifier we can grep for after.
|
|
verifier := "test-verifier-do-not-leak-aaaaaaaaaaaaa"
|
|
cookie, _, err := pl.CreatePreLogin(context.Background(), "op-leak-2", "the-state", "test-nonce-fixed", verifier)
|
|
if err != nil {
|
|
t.Fatalf("CreatePreLogin: %v", err)
|
|
}
|
|
|
|
buf, restore := captureLogger(t)
|
|
defer restore()
|
|
|
|
authCode := "secret-auth-code-do-not-leak"
|
|
res, err := svc.HandleCallback(context.Background(), cookie, authCode, "the-state", "10.0.0.1", "Mozilla")
|
|
if err != nil {
|
|
t.Fatalf("HandleCallback: %v", err)
|
|
}
|
|
|
|
captured := buf.String()
|
|
|
|
// Direct secrets that flow through HandleCallback's parameter list.
|
|
for _, secret := range []string{
|
|
authCode,
|
|
verifier,
|
|
"test-access-token",
|
|
idp.receivedCode,
|
|
idp.receivedVerifier,
|
|
} {
|
|
if secret == "" {
|
|
continue
|
|
}
|
|
if strings.Contains(captured, secret) {
|
|
t.Errorf("secret value %q appeared in log output:\n%s", secret, captured)
|
|
}
|
|
}
|
|
|
|
// The session cookie + CSRF token are returned by the mint stub;
|
|
// in production they're set on the response, not logged. Pin that
|
|
// we never logged them.
|
|
for _, secret := range []string{res.CookieValue, res.CSRFToken} {
|
|
if secret == "" {
|
|
continue
|
|
}
|
|
if strings.Contains(captured, secret) {
|
|
t.Errorf("session secret %q appeared in log output:\n%s", secret, captured)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLoggingHygiene_AlgPinningDoesNotLogAlg is a defense-in-depth pin:
|
|
// when isDisallowedAlg rejects a token, the alg name might land in an
|
|
// error returned to the handler — but the service.go MUST NOT log the
|
|
// alg value itself (an attacker could probe to discover allow-list
|
|
// composition). The handler maps to a uniform 400; alg detail lives
|
|
// only in audit rows the operator owns.
|
|
func TestLoggingHygiene_AlgRejectionDoesNotLogAlg(t *testing.T) {
|
|
buf, restore := captureLogger(t)
|
|
defer restore()
|
|
|
|
// Direct call to the helper; this exercises the deny-list match.
|
|
_, _ = isDisallowedAlg("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.body.sig")
|
|
|
|
captured := buf.String()
|
|
if strings.Contains(captured, "HS256") {
|
|
t.Errorf("alg value HS256 appeared in log output (defense-in-depth violation):\n%s", captured)
|
|
}
|
|
}
|
|
|
|
// TestLoggingHygiene_ProviderLoadDoesNotLogClientSecret pins that
|
|
// even on getOrLoad failures, the decrypted client_secret bytes never
|
|
// land in a log line. Decryption happens before verifier construction;
|
|
// any error path that flows through must not surface the plaintext.
|
|
func TestLoggingHygiene_ProviderLoadDoesNotLogClientSecret(t *testing.T) {
|
|
idp := newMockIdP(t)
|
|
|
|
// Use a provider with a recognizable plaintext "secret" (no encryption
|
|
// key set, so decryptClientSecret returns the bytes as-is).
|
|
prov := makeProvider(idp.URL(), "op-leak-secret")
|
|
prov.ClientSecretEncrypted = []byte("client-secret-plaintext-do-not-leak-xxxxx")
|
|
|
|
pl := newStubPreLogin()
|
|
svc := NewService(
|
|
&stubProviderLookup{provider: prov},
|
|
&stubMappings{roleIDs: []string{"r-operator"}},
|
|
newStubUsers(),
|
|
&stubSessions{},
|
|
pl,
|
|
"",
|
|
)
|
|
|
|
buf, restore := captureLogger(t)
|
|
defer restore()
|
|
|
|
if _, err := svc.getOrLoad(context.Background(), "op-leak-secret"); err != nil {
|
|
t.Fatalf("getOrLoad: %v", err)
|
|
}
|
|
|
|
captured := buf.String()
|
|
if strings.Contains(captured, "client-secret-plaintext-do-not-leak") {
|
|
t.Errorf("client secret plaintext appeared in log output:\n%s", captured)
|
|
}
|
|
}
|