@assistant-ui/better-auth-dbsc 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ import { defineErrorCodes } from "better-auth";
2
+ // src/error-codes.ts
3
+ /** Error codes the plugin answers with. Also on `$ERROR_CODES` for the client. */
4
+ const DBSC_ERROR_CODES = defineErrorCodes({
5
+ DBSC_DISABLED: "DBSC is turned off",
6
+ DBSC_SESSION_REQUIRED: "DBSC registration needs a signed-in session",
7
+ DBSC_PROOF_REQUIRED: "DBSC registration needs a `Secure-Session-Response` header",
8
+ DBSC_INVALID_CHALLENGE: "The DBSC challenge is missing, expired, or for another session",
9
+ DBSC_INVALID_PROOF: "The DBSC proof is invalid",
10
+ DBSC_BOUND_TO_OTHER_KEY: "This session is bound to another device key",
11
+ DBSC_SESSION_ID_REQUIRED: "A DBSC refresh needs a `Sec-Secure-Session-Id` header",
12
+ DBSC_CHALLENGE_REQUIRED: "Sign the new DBSC challenge and retry",
13
+ DBSC_CREDENTIAL_REQUIRED: "This request carries a device-bound session without its device credential",
14
+ DBSC_FRESH_PROOF_REQUIRED: "Confirm this device and try again",
15
+ DBSC_BINDING_REQUIRED: "This action needs a device-bound session"
16
+ });
17
+ // package.json
18
+ var version = "0.1.0";
19
+ export { DBSC_ERROR_CODES as n, version as t };
@@ -0,0 +1,45 @@
1
+ # Threat model
2
+
3
+ STRIDE, following the [dbsc-toolkit threat model](https://github.com/SulimanAbdulrazzaq/dbsc-toolkit/blob/main/docs/security/threat-model.md) so the two can be compared line by line. Scope: the `dbsc()` Better Auth plugin and `@assistant-ui/better-auth-dbsc/core`.
4
+
5
+ Every mitigation below names the test that proves it. The tests run against Better Auth on SQLite with keys generated by `jose`, not against Chrome.
6
+
7
+ ## Assets
8
+
9
+ - **The binding**: a `dbscSession` row tying a Better Auth session to the device's public key and its thumbprint.
10
+ - **Challenges**: `<issued ms>.<nonce>.<HMAC>` under the Better Auth secret, bound to a purpose (`register` or `refresh`) and to the session token. They are never stored.
11
+ - **The bound cookie**: `<expires>.<issued>.<nonce>.<HMAC>` over the session token. It lives 5 minutes, and only a proof from the device key renews it.
12
+ - **The store**: `dbscSession` by default, or anything implementing `DbscStore`.
13
+
14
+ ## Threats
15
+
16
+ | ID | Threat | Here | How | Test |
17
+ |---|---|---|---|---|
18
+ | S1 | Stolen cookies replayed on another device | Mitigated | A bound session without a live bound cookie is treated as signed out: the session cookie is dropped from the request before any endpoint reads it. The bound cookie expires within `boundCookieMaxAge`, and only the device key renews it. | `treats a bound session without its bound cookie as signed out`, `refuses an expired bound cookie until the browser refreshes`, `refuses a bound cookie minted for another session` |
19
+ | S1 | Stolen cookies used inside the bound-cookie window | Mitigated on `freshProof` paths | Sensitive paths need a bound cookie that a device-signed refresh minted while the browser held that action's challenge: the cookie carries the challenge nonce under its MAC. A copied cookie, however fresh, never answers one, and no clocks are compared, so skew between instances can't make an old cookie look newer. A path in both `freshProof.paths` and `enforcement.exclude` is refused at startup. Other paths keep up to `boundCookieMaxAge` of exposure. | `makes a bound session prove itself again…`, `is not answered by a bound cookie from before the challenge`, `is not answered by a copied cookie minted on a server whose clock runs ahead`, `refuses a path that needs a fresh proof but is excluded from enforcement` |
20
+ | S1 | Captured registration proof replayed to renew a stolen session | Mitigated | A repeated registration from the same key gets the same answer, so a lost response can be fetched again, but its bound cookie expires `boundCookieMaxAge` after the challenge was issued, not after the replay. | `does not let a replayed registration outlive the first bound cookie` |
21
+ | S1 | Stolen session token sent as `Authorization: Bearer`, alone, next to the copied cookie, or next to the thief's own session cookie | Mitigated | The gate reads every session token in the request, with the bearer token read the way `bearer()` reads it. A request with a bearer token that carries a bound session is refused with 401 unless the bound cookie proves it and the cookie and bearer token name that same session. It refuses rather than dropping the cookie, because `bearer()` may rewrite the cookie header after the gate. Tested with `dbsc()` before and after `bearer()`. | `refuses a bound session sent as a bearer token`, `refuses a stolen cookie sent again as a bearer token`, `refuses a stolen bearer token next to the thief's own session cookie`, `refuses a stolen bearer token alone`, `refuses a bound browser that sends another session as a bearer token` |
22
+ | S1 | Copied `multiSession()` cookie of a bound session | Mitigated | Each `_multi-` cookie naming a bound session without a live bound cookie is dropped (or, with a bearer token, the request is refused), so it can't list, switch to, or revoke the owner's session. | `does not leak a bound session through a copied multiSession() cookie`, `refuses a copied multiSession() cookie sent with a bearer token` |
23
+ | S1 | Copied `admin_session` cookie of a bound admin, next to a copy of the impersonation session | Mitigated | `admin_session` naming a bound session without a live bound cookie is dropped, so `/admin/stop-impersonating` can't return or restore the admin's session. The impersonation session itself is a separate session and is bound only if the browser registers it. | `does not leak a bound admin session through a copied admin_session cookie`, `lets the bound admin stop impersonating` |
24
+ | S1 | Copied cookies read by `getCookieCache()` or `getSessionCookie()` in middleware | Not mitigated (documented) | These helpers decode cookies without calling Better Auth, so no plugin hook runs. The README says to use them for redirects only and to authorize with `auth.api.getSession`, which is gated, cookie cache included. | `does not answer from session.cookieCache for a copied cookie` |
25
+ | S2 | Attacker's key substituted at registration | Mitigated | The registration JWT must be self-signed by the key it carries. The registration challenge appears only in the sign-in response, is bound to that session token, and expires. A session binds once; another key is refused. | `refuses a challenge this server did not issue for this session`, `answers a repeated registration from the same key, and refuses another key` |
26
+ | T1 | Refresh proof replayed | Mitigated | `store.spend` is a guarded `incrementOne`: a challenge is spent once, and older ones are refused once a newer one is spent. Atomic across instances. | `spends a proof once even when requests race`, `refuses an older challenge once a newer one is spent` |
27
+ | T1 | A refresh challenge reused for registration, or the reverse | Mitigated | The purpose is inside the challenge MAC. | `does not take a refresh challenge for a registration` |
28
+ | T1b, T3, T4 | Per-request proof replay, DPoP | Not applicable | This plugin has no per-request proof header and no DPoP. Those belong to the toolkit's JS polyfill tier. | — |
29
+ | T2 | Stored keys changed by someone with database access | Out of scope | Access control on the database. Thumbprints make a swapped key visible. | — |
30
+ | R1 | Theft goes unnoticed | Mitigated | `onEvent` reports `proof_rejected` (a refresh signed by the wrong key, the strongest theft signal) and `credential_missing` (a bound session arriving without its bound cookie, by cookie, bearer token, `multiSession()` cookie, or `admin_session`). | `reports the lifecycle and theft signals without key material` |
31
+ | I1 | Keys or challenges leak into logs | Mitigated | Events carry session and user ids and reason codes only. The plugin logs no keys, challenges, cookies, or tokens. | `reports the lifecycle and theft signals without key material` |
32
+ | I2 | Key copied from the browser profile by malware | Mitigated for DBSC browsers | Native DBSC keeps the key in the TPM or Secure Enclave. There is no software-key tier here. For routes that must resist this, `freshProof` with `unbound: "deny"` refuses sessions that aren't bound. | `denies plain sessions on paths that need a device-bound session` |
33
+ | D1 | Challenge flood exhausts storage | Mitigated | Challenges are stateless: issuing one writes nothing. Register and refresh are also rate limited through Better Auth's limiter. That limiter is on in production by default and off in development. | `writes nothing when it hands out a challenge`, `limits register and refresh through Better Auth's limiter` |
34
+ | D1b | Bindings outlive their sessions in a custom store | Mitigated | `DbscStore.delete` runs from Better Auth's session delete hook on every way a session ends. Sessions that expire unseen are pruned with the sessions. | `releases a binding in a custom store when its session ends` |
35
+ | D2 | Replays or garbage lock the owner out | Mitigated | A challenge is spent only after the signature verifies, so garbage spends nothing. A copied cookie can't reach any endpoint that changes the session, sign-out included. | `does not let garbage proofs spend a challenge`, `never lets a copied cookie sign the owner out` |
36
+ | E1 | Algorithm confusion (`none`, HS256 with the public key) | Mitigated | Only ES256 and RS256 are accepted, checked before any key is imported. The key type must match the algorithm, private members are refused, and RSA keys must be at least 2048 bits. | `rejects alg: none`, `rejects HS256 signed with the public key as the secret`, `rejects an RSA key presented for ES256`, `rejects a private JWK`, `rejects RSA keys under 2048 bits` |
37
+ | E2 | A plain session treated as device-bound | Mitigated | `getDbscStatus` answers `bound` only for a session bound to a device key, proven by a live bound cookie. `unbound` sessions are labeled as such. | `binds the session and returns the session config`, `leaves sessions alone in browsers without DBSC` |
38
+
39
+ ## Not covered
40
+
41
+ - **Malware running inside the browser** can ask the TPM to sign, so it can refresh like the real browser. DBSC stops cookies from being used elsewhere, not code running on the device.
42
+ - **Browsers without DBSC** get no binding. There is no JavaScript polyfill tier. Use `freshProof` with `unbound: "deny"` or `"fresh-session"` where that matters.
43
+ - **Unguarded paths inside the cookie window.** A thief who copies both cookies keeps access to paths outside `freshProof` until the bound cookie expires (5 minutes by default).
44
+ - **The origin check is skipped on `/dbsc/register` and `/dbsc/refresh`.** The browser sends those requests itself, and nothing promises they carry `Origin`. A page on another site can't set `Secure-Session-Response` or `Sec-Secure-Session-Id`, has no challenge to sign, and has no key. Refresh refuses any request without `Sec-Secure-Session-Id`.
45
+ - **Not tested against real Chrome.** The tests play the browser with `jose`.
@@ -0,0 +1,4 @@
1
+ -- Generated by scripts/generate-migrations.ts. Do not edit by hand.
2
+ create table "dbscSession" ("id" text not null primary key, "sessionId" text not null unique references "session" ("id") on delete cascade, "userId" text not null references "user" ("id") on delete cascade, "publicKey" text not null, "thumbprint" text not null, "lastRefreshAt" date not null, "lastFreshProofAt" date not null, "proofCount" integer not null, "createdAt" date not null);
3
+
4
+ create index "dbscSession_userId_idx" on "dbscSession" ("userId");
package/package.json CHANGED
@@ -1,6 +1,80 @@
1
1
  {
2
2
  "name": "@assistant-ui/better-auth-dbsc",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Device Bound Session Credentials (DBSC) for Better Auth.",
5
+ "keywords": [
6
+ "better-auth",
7
+ "dbsc",
8
+ "device-bound-session-credentials",
9
+ "session",
10
+ "security"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "KinfeMichael Tariku <kinfishtech@gmail.com> (https://github.com/Kinfe123)",
14
+ "homepage": "https://github.com/assistant-ui/better-auth-dbsc#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/assistant-ui/better-auth-dbsc.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/assistant-ui/better-auth-dbsc/issues"
21
+ },
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "engines": {
25
+ "node": ">=22"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "packageManager": "pnpm@10.9.0",
31
+ "files": [
32
+ "dist",
33
+ "docs",
34
+ "migrations"
35
+ ],
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/index.d.ts",
39
+ "default": "./dist/index.js"
40
+ },
41
+ "./client": {
42
+ "types": "./dist/client.d.ts",
43
+ "default": "./dist/client.js"
44
+ },
45
+ "./core": {
46
+ "types": "./dist/core.d.ts",
47
+ "default": "./dist/core.js"
48
+ },
49
+ "./package.json": "./package.json"
50
+ },
51
+ "scripts": {
52
+ "build": "tsdown",
53
+ "typecheck": "tsc --noEmit",
54
+ "test": "vitest run",
55
+ "lint": "oxlint src test example scripts bump.config.ts",
56
+ "format": "oxfmt src test example scripts bump.config.ts",
57
+ "format:check": "oxfmt --check src test example scripts bump.config.ts",
58
+ "generate:migrations": "node scripts/generate-migrations.ts",
59
+ "example": "node example/migrate.ts && node example/server.ts",
60
+ "check:package": "node scripts/check-package.ts",
61
+ "prepublishOnly": "pnpm build && pnpm check:package",
62
+ "release": "bumpp"
63
+ },
64
+ "dependencies": {
65
+ "jose": "^6.2.18"
66
+ },
67
+ "peerDependencies": {
68
+ "better-auth": "^1.7.0"
69
+ },
70
+ "devDependencies": {
71
+ "@types/node": "^26.6.6",
72
+ "better-auth": "1.7.27",
73
+ "bumpp": "12.3.0",
74
+ "oxfmt": "^0.80.0",
75
+ "oxlint": "^1.95.0",
76
+ "tsdown": "^0.23.7",
77
+ "typescript": "^7.0.2",
78
+ "vitest": "^5.1.1"
79
+ }
80
+ }