← Back to Pull requests

[AUTH-12] Session device metadata (privacy-conscious) #676

Merged
leo-aa88 merged 8 files (650 lines) on Nov 20, 2026 19:01 UTC
View on GitHub
leo-aa88 leo-aa88 commented on Nov 20, 2026 02:20 UTC
Author

Summary

Each session now records the device it signed in from, so a user can recognize it in GET {prefix}/auth/sessions without the framework storing more than that needs.

  • New fields: AuthSession gains user_agent, client_address and location, always present and empty when unknown. They come from the sign-in request, and refreshes keep them: the login writes them to new refresh_tokens columns (user_agent, client_address, location), and every rotation copies them to its successor.

  • User-Agent: stored as given, cut to 255 bytes on a character boundary, kept to one line of valid text: invalid UTF-8 replaced, and control characters (C0, DEL and C1), line and paragraph separators and bidi formatting characters dropped. PostgreSQL refuses NUL and MySQL refuses invalid UTF-8, so a hostile header can't break the login, and a header can't put line breaks, escapes or reversed text into the list. A locator's location gets the same treatment, and the cut sizes are held to the column tags by a test. The server doesn't parse it: a client that wants "Firefox on Linux" builds the label with a parser of its choice, which keeps UA sniffing out of the framework.

  • Client address: taken from gin's ClientIP, so GOMBIT_HTTP_TRUSTED_PROXIES still applies. New config GOMBIT_AUTH_SESSION_CLIENT_ADDRESS (Config.Auth.SessionClientAddress):

  • - coarse (the default; also what an empty value means) keeps the network, an IPv4 /24 or IPv6 /48;
    - full keeps the whole address;
    - none keeps nothing;
    - any other value is refused.

    IPv4-mapped IPv6 is shown as IPv4, and zones are dropped.

  • Location: the framework does no geolocation. An app passes an auth.SessionLocator (auth.WithSessionLocator, or framework.WithAuthSessionLocator). It is called once per sign-in, before the user lock, with the full address whatever mode is set. Its answer is stored cut to 128 bytes.

  • last_used_at: not added. last_refreshed_at already records use at refresh time, which the issue allows, without a write on every request.
  • Closes #662

    Acceptance criteria

  • GET /auth/sessions shows each session's device description in both auth modes. TestSessionDeviceHTTP/bearer and /cookie log in with a User-Agent from 203.0.113.7, refresh from another device, then list the session.

  • By default, no full client address is stored or returned. The default is coarse, and an empty value also means coarse. Covered by TestSessionClientAddressConfig and the client address table in runSessionDevice.

  • The metadata survives refreshes. Covered by recorded at sign-in, kept by refreshes (two rotations from another address and UA) and by the HTTP tests in both modes.

  • Docs state exactly what is stored. docs/auth.md#sessions lists the three values, the columns, how each is derived, the none/full switch, the locator contract, the retention (as long as the rows; nothing prunes them until #578), and the separate audit trail, which keeps the full address.
  • Scope notes

  • Schema: a framework-owned schema change, registered as in #661: manifest session-device-schema (manual, breaking, refresh_tokens altered). The same migration as session-id-schema adds the columns. Existing rows keep NULLs and list as empty, with no backfill. Also session-device-metadata (informational, config: GOMBIT_AUTH_SESSION_CLIENT_ADDRESS added). docs/upgrade-notes.md is regenerated.

  • Stale doc fixed: docs/auth.md still said that revoking one session answers 404 when it rotated first. That stopped being true with #661, so I corrected it here.

  • Retention: the metadata lives on the rows, so #578's pruning will bound it. Nothing is added for it here.

  • AUTH-13 (#663): the sessions screen in the generated frontend, which would show these fields, is left to that issue.
  • Validation

  • go test ./...: all pass except the known local-only TestDecimalDefaultIsAString, which needs the adminui node_modules.

  • go test -tags integration ./auth/ against PostgreSQL and MySQL (runSessionDevice was added to both runs, including the NUL and invalid-UTF-8 User-Agent case): pass

  • go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2 run: 0 issues

  • CGO_ENABLED=0 go build ./...

  • Mutation checks. Each was caught by the new tests:

  • - rotation not copying the device;
    - the coarse mode returning the full address.
    - the external exchange route without request-info capture (the session-start device assertions now cover password login, MFA login in both modes, the cookie-mode external callback and the Bearer exchange).
  • Project code-review skill run against this diff
  • Working agreement checklist

  • New behavior has tests; DB-touching changes pass the SQLite + PostgreSQL + MySQL matrix

  • Stable features ship docs and appear in an example app (examples/auth/README.md sessions section, docs/auth.md, docs/config.md, scaffold .env.example)

  • Extraction preserves contracts: N/A, no extraction

  • Generators are idempotent/additive, AST-safe, and never overwrite user-owned files: N/A. Only the scaffold templates' .env.example and the placeholder schema.ts changed, and the goldens are regenerated.

  • API changes regenerate the OpenAPI doc + TS client in this PR. The AuthSession schema gains three fields. The scaffold's placeholder schema.ts and its goldens carry them, and no other committed spec or client does.

  • No secrets in generated frontend source; VITE_* is treated as public

  • Scope stays inside this issue's milestone — no "battery" creep

  • This PR links its issue and states which acceptance criteria it satisfies

  • A CHANGELOG entry for a change an app can notice is classified in upgrade/manifest.yaml, and docs/upgrade-notes.md is regenerated
  • 🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Brunotlps Review duration: 1000m
    All Vouch review confidence checks passed
    Residual risk 0.40 = Change Risk (0.75) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #676"
    general weight: 0.35
    Reviewers
    Assignees
    leo-aa88 leo-aa88
    Labels
    risk: medium confidence: 46.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 40.1% · Moderate risk
    40.1%