cursedauth 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.
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/dist/account/AccountMenu.d.ts +153 -0
- package/dist/account/AccountMenu.d.ts.map +1 -0
- package/dist/account/AccountMenu.js +388 -0
- package/dist/account/AccountMenu.js.map +1 -0
- package/dist/account/AccountNotice.d.ts +35 -0
- package/dist/account/AccountNotice.d.ts.map +1 -0
- package/dist/account/AccountNotice.js +133 -0
- package/dist/account/AccountNotice.js.map +1 -0
- package/dist/account/accountNotice.state.d.ts +20 -0
- package/dist/account/accountNotice.state.d.ts.map +1 -0
- package/dist/account/accountNotice.state.js +55 -0
- package/dist/account/accountNotice.state.js.map +1 -0
- package/dist/account/avatar.d.ts +37 -0
- package/dist/account/avatar.d.ts.map +1 -0
- package/dist/account/avatar.js +47 -0
- package/dist/account/avatar.js.map +1 -0
- package/dist/accountSlot.client.d.ts +113 -0
- package/dist/accountSlot.client.d.ts.map +1 -0
- package/dist/accountSlot.client.js +330 -0
- package/dist/accountSlot.client.js.map +1 -0
- package/dist/accountSlots.d.ts +192 -0
- package/dist/accountSlots.d.ts.map +1 -0
- package/dist/accountSlots.js +274 -0
- package/dist/accountSlots.js.map +1 -0
- package/dist/auth.d.ts +167 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +310 -0
- package/dist/auth.js.map +1 -0
- package/dist/auth.server.d.ts +35 -0
- package/dist/auth.server.d.ts.map +1 -0
- package/dist/auth.server.js +67 -0
- package/dist/auth.server.js.map +1 -0
- package/dist/clientDescription.d.ts +186 -0
- package/dist/clientDescription.d.ts.map +1 -0
- package/dist/clientDescription.js +278 -0
- package/dist/clientDescription.js.map +1 -0
- package/dist/constantTime.d.ts +35 -0
- package/dist/constantTime.d.ts.map +1 -0
- package/dist/constantTime.js +46 -0
- package/dist/constantTime.js.map +1 -0
- package/dist/contentCensus.d.ts +59 -0
- package/dist/contentCensus.d.ts.map +1 -0
- package/dist/contentCensus.js +55 -0
- package/dist/contentCensus.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/mountGatedApp.d.ts +128 -0
- package/dist/mountGatedApp.d.ts.map +1 -0
- package/dist/mountGatedApp.js +107 -0
- package/dist/mountGatedApp.js.map +1 -0
- package/dist/runtimeMode.d.ts +28 -0
- package/dist/runtimeMode.d.ts.map +1 -0
- package/dist/runtimeMode.js +37 -0
- package/dist/runtimeMode.js.map +1 -0
- package/dist/secureRequest.d.ts +22 -0
- package/dist/secureRequest.d.ts.map +1 -0
- package/dist/secureRequest.js +27 -0
- package/dist/secureRequest.js.map +1 -0
- package/dist/sso.d.ts +373 -0
- package/dist/sso.d.ts.map +1 -0
- package/dist/sso.js +420 -0
- package/dist/sso.js.map +1 -0
- package/dist/ssoAutoSignIn.d.ts +272 -0
- package/dist/ssoAutoSignIn.d.ts.map +1 -0
- package/dist/ssoAutoSignIn.js +431 -0
- package/dist/ssoAutoSignIn.js.map +1 -0
- package/dist/ssoConsumer.server.d.ts +295 -0
- package/dist/ssoConsumer.server.d.ts.map +1 -0
- package/dist/ssoConsumer.server.js +972 -0
- package/dist/ssoConsumer.server.js.map +1 -0
- package/dist/stageIdentity.d.ts +72 -0
- package/dist/stageIdentity.d.ts.map +1 -0
- package/dist/stageIdentity.js +74 -0
- package/dist/stageIdentity.js.map +1 -0
- package/dist/ui/popover.d.ts +190 -0
- package/dist/ui/popover.d.ts.map +1 -0
- package/dist/ui/popover.js +351 -0
- package/dist/ui/popover.js.map +1 -0
- package/dist/userSessions.d.ts +205 -0
- package/dist/userSessions.d.ts.map +1 -0
- package/dist/userSessions.js +419 -0
- package/dist/userSessions.js.map +1 -0
- package/package.json +115 -0
- package/src/account/AccountMenu.tsx +729 -0
- package/src/account/AccountNotice.tsx +223 -0
- package/src/account/accountNotice.state.ts +66 -0
- package/src/account/avatar.ts +50 -0
- package/src/accountSlot.client.ts +362 -0
- package/src/accountSlots.ts +305 -0
- package/src/auth.server.ts +89 -0
- package/src/auth.ts +515 -0
- package/src/clientDescription.ts +366 -0
- package/src/constantTime.ts +45 -0
- package/src/contentCensus.ts +106 -0
- package/src/index.ts +58 -0
- package/src/mountGatedApp.ts +240 -0
- package/src/runtimeMode.ts +37 -0
- package/src/secureRequest.ts +25 -0
- package/src/sso.ts +767 -0
- package/src/ssoAutoSignIn.ts +544 -0
- package/src/ssoConsumer.server.ts +1260 -0
- package/src/stageIdentity.ts +76 -0
- package/src/ui/popover.ts +390 -0
- package/src/userSessions.ts +678 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mount order every private satellite depends on, as a STRUCTURE rather than as a
|
|
3
|
+
* comment.
|
|
4
|
+
*
|
|
5
|
+
* ── The rule ────────────────────────────────────────────────────────────────────────
|
|
6
|
+
* A private satellite assembles the same five steps in its `mount:` block, and the ORDER
|
|
7
|
+
* of them is a security property, not a style:
|
|
8
|
+
*
|
|
9
|
+
* 1. `open` — the SSO start/callback/session/logout routes. They must sit OUTSIDE
|
|
10
|
+
* the gate, or there is no way to obtain the session the gate demands.
|
|
11
|
+
* 2. `beforeGate` — surfaces that authenticate with their OWN bearer or device token
|
|
12
|
+
* (feedback sync, a drop-folder ingest, a sync bridge, a capacity
|
|
13
|
+
* probe). A cookie gate in front of them refuses a perfectly good
|
|
14
|
+
* token, and the route becomes one only a browser can reach.
|
|
15
|
+
* 3. `gate` — the blanket `app.use("/api/*", requireUser)`.
|
|
16
|
+
* 4. `routes` — the app's own API. The gate has run, so `userOf` answers.
|
|
17
|
+
* 5. `afterGate` — the few routes that need the gate to have RESOLVED a user before
|
|
18
|
+
* they are even registered (feedback's reporter routes).
|
|
19
|
+
*
|
|
20
|
+
* Two of the fleet's apps needed a step the five above cannot describe, and each held a
|
|
21
|
+
* WAIVER against this helper's gate saying so until 2026-08-14:
|
|
22
|
+
*
|
|
23
|
+
* 0. `wall` — middleware wider than the gate, registered before any route
|
|
24
|
+
* exists. vault's CSP/security-header wall.
|
|
25
|
+
* 3b. `innerGates` — narrower gates after the blanket one, each raising the bar on a
|
|
26
|
+
* subset of what it already covers. auth's owner-only 403 in front
|
|
27
|
+
* of `/api/admin/*`, behind the 401 in front of `/api/*`.
|
|
28
|
+
*
|
|
29
|
+
* Both were added rather than waived around, because a waiver's whole value is that it is
|
|
30
|
+
* short and argued — and "the plan cannot say this" is a reason to teach the plan to say
|
|
31
|
+
* it, not a reason for an app to keep hand-assembling the four steps it CAN express.
|
|
32
|
+
*
|
|
33
|
+
* Getting step 2 wrong is not hypothetical: `apps/notes` shipped its device-token sync
|
|
34
|
+
* routes AFTER the gate, and `/api/sync/info` answered 200 with a token while
|
|
35
|
+
* `/api/ask/pending` answered 401 with the SAME one. It cost a day to find, because
|
|
36
|
+
* nothing about the code looked wrong — the routes were all there, in a plausible order.
|
|
37
|
+
*
|
|
38
|
+
* ── Why a helper and not a longer comment ───────────────────────────────────────────
|
|
39
|
+
* Seven apps hand-assembled this sequence, each re-explaining the reason in its own
|
|
40
|
+
* prose, and `feedback-kit/server/host.ts` documented the rule while nothing enforced it.
|
|
41
|
+
* Prose cannot fail a gate. Here the phases are NAMED FIELDS, so the order is not
|
|
42
|
+
* something a `mount:` block can express wrongly — the only way to move a token surface
|
|
43
|
+
* behind the gate is to move it into `routes`, which is at least an honest statement of
|
|
44
|
+
* what you are doing.
|
|
45
|
+
*
|
|
46
|
+
* On top of that this refuses, at boot, two mistakes the field ordering cannot prevent:
|
|
47
|
+
* a pre-gate mounter that installs the blanket gate itself (which would put everything
|
|
48
|
+
* after it behind a cookie), and a plan whose gate never reaches the router at all.
|
|
49
|
+
*
|
|
50
|
+
* DOM-free, dependency-free beyond Hono's types.
|
|
51
|
+
*/
|
|
52
|
+
import type { Hono, MiddlewareHandler } from "hono";
|
|
53
|
+
|
|
54
|
+
/** Anything that registers routes on the app. The apps' own `mountX(app, deps)` functions
|
|
55
|
+
* become `(app) => mountX(app, deps)` at the call site, which keeps their signatures. */
|
|
56
|
+
export type Mounter = (app: Hono) => void;
|
|
57
|
+
|
|
58
|
+
export interface SessionGate {
|
|
59
|
+
/**
|
|
60
|
+
* The path pattern(s) the gate covers. `"/api/*"` for every satellite but `roms`,
|
|
61
|
+
* which also gates its own `/play/*` base — hence the array form.
|
|
62
|
+
*/
|
|
63
|
+
path?: string | readonly string[];
|
|
64
|
+
handler: MiddlewareHandler;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface GatedMountPlan {
|
|
68
|
+
/**
|
|
69
|
+
* Step 0 — middleware over EVERYTHING, registered before any route exists.
|
|
70
|
+
*
|
|
71
|
+
* `apps/vault`'s CSP/security-header wall is the case: `app.use("*", …)` in front
|
|
72
|
+
* of the SSO routes, in front of the gate, and in front of the SPA static handler
|
|
73
|
+
* `createSatelliteApp` registers after `mount` returns. It is genuinely wider than
|
|
74
|
+
* the gate, so it is neither an `open` route (it is not how you obtain a session)
|
|
75
|
+
* nor a `beforeGate` surface (it carries no auth of its own) — calling it either
|
|
76
|
+
* would say something false about it, which is why vault held a waiver against
|
|
77
|
+
* this helper until this phase existed.
|
|
78
|
+
*
|
|
79
|
+
* 🔴 For MIDDLEWARE, never for routes. A route mounted here sits outside the gate
|
|
80
|
+
* with no phase saying so; that is what `open` and `beforeGate` are for, and they
|
|
81
|
+
* are the ones a reader checks.
|
|
82
|
+
*/
|
|
83
|
+
wall?: Mounter | readonly Mounter[];
|
|
84
|
+
/** Step 1 — reachable with NO session, because they are how you get one. */
|
|
85
|
+
open?: Mounter | readonly Mounter[];
|
|
86
|
+
/** Step 2 — authenticated by their own bearer/device token, never by the cookie. */
|
|
87
|
+
beforeGate?: Mounter | readonly Mounter[];
|
|
88
|
+
/** Step 3 — the blanket session gate. A bare handler means the default `/api/*`. */
|
|
89
|
+
gate: MiddlewareHandler | SessionGate;
|
|
90
|
+
/**
|
|
91
|
+
* Step 3b — NARROWER gates, registered after the blanket one and before any
|
|
92
|
+
* route, each raising the bar on a subset of what `gate` already covers.
|
|
93
|
+
*
|
|
94
|
+
* `apps/auth` is the case: a 401 in front of `/api/*` and then an owner-only 403
|
|
95
|
+
* in front of `/api/admin/*`. Two gates in sequence, and the sequence matters —
|
|
96
|
+
* every `/api/admin` route sits behind BOTH. Modelling the second as an ordinary
|
|
97
|
+
* `routes` mounter would have been true about registration order and false about
|
|
98
|
+
* meaning, which is the waiver `auth` held until this field existed.
|
|
99
|
+
*
|
|
100
|
+
* 🔴 A path here must be strictly INSIDE the blanket gate's coverage; a gate on a
|
|
101
|
+
* path the blanket one does not reach is a first gate wearing a second one's
|
|
102
|
+
* name, and is refused at boot.
|
|
103
|
+
*/
|
|
104
|
+
innerGates?: readonly SessionGate[];
|
|
105
|
+
/** Step 4 — the app's own API, behind the gate. */
|
|
106
|
+
routes: Mounter | readonly Mounter[];
|
|
107
|
+
/** Step 5 — registered after the API, for handlers that read the resolved user. */
|
|
108
|
+
afterGate?: Mounter | readonly Mounter[];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** What the gate covers when a plan gives a bare middleware handler. */
|
|
112
|
+
export const DEFAULT_GATE_PATH = "/api/*";
|
|
113
|
+
|
|
114
|
+
const asMounters = (value: Mounter | readonly Mounter[] | undefined): readonly Mounter[] =>
|
|
115
|
+
value === undefined ? [] : typeof value === "function" ? [value] : value;
|
|
116
|
+
|
|
117
|
+
const normalizeGate = (
|
|
118
|
+
gate: GatedMountPlan["gate"],
|
|
119
|
+
): { paths: string[]; handler: MiddlewareHandler } => {
|
|
120
|
+
if (typeof gate === "function") return { paths: [DEFAULT_GATE_PATH], handler: gate };
|
|
121
|
+
const path = gate.path ?? DEFAULT_GATE_PATH;
|
|
122
|
+
return { paths: typeof path === "string" ? [path] : [...path], handler: gate.handler };
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A route Hono has registered, in registration order. Narrower than Hono's own
|
|
127
|
+
* `RouterRoute` on purpose: this reads three fields and must not care what else is there.
|
|
128
|
+
*/
|
|
129
|
+
interface RegisteredRoute {
|
|
130
|
+
path: string;
|
|
131
|
+
method: string;
|
|
132
|
+
handler: unknown;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const routesOf = (app: Hono): RegisteredRoute[] => app.routes as unknown as RegisteredRoute[];
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether a gate on `outer` also runs for every request an inner gate on `inner`
|
|
139
|
+
* would see. Only the trailing-`*` form Hono's `app.use` takes needs answering here
|
|
140
|
+
* (`/api/*` covers `/api/admin/*`), plus the identical case.
|
|
141
|
+
*/
|
|
142
|
+
const covers = (outer: string, inner: string): boolean => {
|
|
143
|
+
if (outer === inner) return true;
|
|
144
|
+
if (!outer.endsWith("*")) return false;
|
|
145
|
+
return inner.startsWith(outer.slice(0, -1));
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Mount an app's routes in the one order that is correct, and refuse the orderings that
|
|
150
|
+
* are not. See the file header for what each phase is and why.
|
|
151
|
+
*/
|
|
152
|
+
export function mountGatedApp(app: Hono, plan: GatedMountPlan): void {
|
|
153
|
+
const gate = normalizeGate(plan.gate);
|
|
154
|
+
|
|
155
|
+
// Step 0, before anything can be registered — see `wall`.
|
|
156
|
+
for (const mount of asMounters(plan.wall)) mount(app);
|
|
157
|
+
|
|
158
|
+
const preGateStart = routesOf(app).length;
|
|
159
|
+
for (const mount of asMounters(plan.open)) mount(app);
|
|
160
|
+
for (const mount of asMounters(plan.beforeGate)) mount(app);
|
|
161
|
+
const preGate = routesOf(app).slice(preGateStart);
|
|
162
|
+
|
|
163
|
+
/*
|
|
164
|
+
* 🔴 A pre-gate mounter that installs the blanket gate ITSELF is the failure this
|
|
165
|
+
* helper exists to make impossible, wearing its other face: the phases would be in the
|
|
166
|
+
* right order and every open route would still end up behind a cookie, because the
|
|
167
|
+
* gate ran earlier than the plan says it does. `notes` and `roms` both used to hide
|
|
168
|
+
* their gate inside a `mountX(app)` for exactly this reason, so this is the check that
|
|
169
|
+
* lets them adopt the helper without the gate quietly moving.
|
|
170
|
+
*/
|
|
171
|
+
const early = preGate.find((route) => route.method === "ALL" && gate.paths.includes(route.path));
|
|
172
|
+
if (early) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`mountGatedApp: a route mounted before the gate already installs a blanket ` +
|
|
175
|
+
`middleware on ${early.path}. The pre-gate phases are for surfaces that carry ` +
|
|
176
|
+
`their own auth; whatever mounts that middleware belongs in the \`gate\` field.`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
for (const path of gate.paths) app.use(path, gate.handler);
|
|
181
|
+
|
|
182
|
+
/*
|
|
183
|
+
* 🔴 An inner gate that reaches somewhere the blanket gate does not is the mistake
|
|
184
|
+
* this check exists for: it would LOOK like a second, stricter wall and actually be
|
|
185
|
+
* the only wall on that path — so a typo in `/api/admin/*` turns the owner-only 403
|
|
186
|
+
* into the sole protection on a path with no 401 in front of it at all.
|
|
187
|
+
*/
|
|
188
|
+
for (const inner of plan.innerGates ?? []) {
|
|
189
|
+
const paths = typeof inner.path === "string" ? [inner.path] : [...(inner.path ?? [])];
|
|
190
|
+
const stray = paths.find((path) => !gate.paths.some((covered) => covers(covered, path)));
|
|
191
|
+
if (stray !== undefined || paths.length === 0) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`mountGatedApp: the inner gate on ${stray ?? "(no path)"} is not inside the ` +
|
|
194
|
+
`blanket gate (${gate.paths.join(", ")}). An inner gate raises the bar on a ` +
|
|
195
|
+
`subset of what the gate already covers; a path outside it needs its own \`gate\`.`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
for (const path of paths) app.use(path, inner.handler);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
for (const mount of asMounters(plan.routes)) mount(app);
|
|
202
|
+
for (const mount of asMounters(plan.afterGate)) mount(app);
|
|
203
|
+
|
|
204
|
+
/*
|
|
205
|
+
* The gate reached the router exactly once per path it claims. A zero here means an
|
|
206
|
+
* app whose whole API is public and whose `mount:` block says otherwise — the one
|
|
207
|
+
* failure mode worth paying a boot-time scan for.
|
|
208
|
+
*/
|
|
209
|
+
const registered = routesOf(app).filter((route) => route.handler === gate.handler);
|
|
210
|
+
if (registered.length !== gate.paths.length) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`mountGatedApp: expected the session gate on ${gate.paths.length} path(s) ` +
|
|
213
|
+
`(${gate.paths.join(", ")}), found ${registered.length} registration(s).`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* What actually got mounted, in registration order, split at the gate — the shape an
|
|
220
|
+
* app's own test asserts on.
|
|
221
|
+
*
|
|
222
|
+
* This is the readable half of the security property: *"`/api/sync/ops` is registered
|
|
223
|
+
* before the gate"* is a sentence a test can hold, and it stays true through a refactor
|
|
224
|
+
* that moves the route to a different file. `gateIndex` is -1 when the handler never
|
|
225
|
+
* reached the router, which `mountGatedApp` refuses but a hand-assembled app can still do.
|
|
226
|
+
*/
|
|
227
|
+
export function gatedMountReport(
|
|
228
|
+
app: Hono,
|
|
229
|
+
gate: MiddlewareHandler,
|
|
230
|
+
): { gateIndex: number; beforeGate: string[]; afterGate: string[] } {
|
|
231
|
+
const routes = routesOf(app);
|
|
232
|
+
const gateIndex = routes.findIndex((route) => route.handler === gate);
|
|
233
|
+
const pathsOf = (subset: RegisteredRoute[]) => [...new Set(subset.map((route) => route.path))];
|
|
234
|
+
if (gateIndex === -1) return { gateIndex, beforeGate: pathsOf(routes), afterGate: [] };
|
|
235
|
+
return {
|
|
236
|
+
gateIndex,
|
|
237
|
+
beforeGate: pathsOf(routes.slice(0, gateIndex)),
|
|
238
|
+
afterGate: pathsOf(routes.slice(gateIndex + 1)),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Am I running a production build?" — the single answer both halves of the auth
|
|
3
|
+
* boundary refuse on.
|
|
4
|
+
*
|
|
5
|
+
* Two signals, in priority order:
|
|
6
|
+
*
|
|
7
|
+
* 1. **Vite's compile-time flag.** In a browser bundle `import.meta.env.DEV` is
|
|
8
|
+
* substituted literally by Vite: `true` in `vite dev`, `false` in `vite build`. That
|
|
9
|
+
* substitution is what makes the mock adapter's refusal un-bypassable in a shipped
|
|
10
|
+
* bundle — there is no env var to flip on the client, the `throw` is simply compiled
|
|
11
|
+
* in. It is read defensively (`?.`) because Bun's `import.meta.env` is a plain
|
|
12
|
+
* process-env object with no DEV/PROD keys, so under `bun test` this signal is absent
|
|
13
|
+
* rather than false.
|
|
14
|
+
* 2. **`NODE_ENV`.** The server-side signal, and the fallback whenever (1) is absent.
|
|
15
|
+
*
|
|
16
|
+
* Anything that is not positively development is treated as production — the safe
|
|
17
|
+
* direction for a guard whose failure mode is "an open door on a public subdomain".
|
|
18
|
+
*/
|
|
19
|
+
export function isDevRuntime(): boolean {
|
|
20
|
+
const viteDev = (import.meta as { env?: Record<string, unknown> }).env?.DEV;
|
|
21
|
+
if (typeof viteDev === "boolean") return viteDev;
|
|
22
|
+
return process.env.NODE_ENV !== "production";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Inverse of {@link isDevRuntime}, for guards that read better in the positive. */
|
|
26
|
+
export function isProductionRuntime(): boolean {
|
|
27
|
+
return !isDevRuntime();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The git short-SHA stamped into the bundle at build time by `buildSatelliteArtifact`
|
|
32
|
+
* (`bun build --define process.env.SATELLITE_COMMIT=…`). Unstamped runs — dev servers,
|
|
33
|
+
* tests — report "dev".
|
|
34
|
+
*/
|
|
35
|
+
export function satelliteCommit(): string {
|
|
36
|
+
return process.env.SATELLITE_COMMIT || "dev";
|
|
37
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Is this request on HTTPS — the one question the `Secure` cookie attribute turns on.
|
|
3
|
+
*
|
|
4
|
+
* 🔴 Extracted 2026-09-14 from the previous generation's `ownerAuth.server.ts`, which
|
|
5
|
+
* is NOT part of this package. That module is single-principal password auth: an
|
|
6
|
+
* argon2 hash, one owner, a revocation store imported from cursedbelt. Everything
|
|
7
|
+
* here signs in through the accounts service instead, so the whole file was dead
|
|
8
|
+
* weight — and carrying it would have made `cursedbelt` a runtime dependency of an
|
|
9
|
+
* auth library for the sake of ONE re-exported type. This function was the only
|
|
10
|
+
* thing the SSO path actually called.
|
|
11
|
+
*
|
|
12
|
+
* Trusting a client-supplied header is normally wrong, and here it is only ever
|
|
13
|
+
* used to make the cookie MORE restrictive: a forged `x-forwarded-proto: https`
|
|
14
|
+
* on a plain connection makes the browser refuse to send the cookie back, which
|
|
15
|
+
* costs the attacker their own session and nothing else.
|
|
16
|
+
*
|
|
17
|
+
* Behind the tunnel every request arrives as plain HTTP with the real scheme in
|
|
18
|
+
* `x-forwarded-proto`, so reading `req.url` alone would drop `Secure` from every
|
|
19
|
+
* production cookie — silently, and only in production.
|
|
20
|
+
*/
|
|
21
|
+
export function isSecureRequest(req: Request): boolean {
|
|
22
|
+
const forwarded = req.headers.get("x-forwarded-proto");
|
|
23
|
+
if (forwarded) return forwarded.split(",")[0]?.trim() === "https";
|
|
24
|
+
return new URL(req.url).protocol === "https:";
|
|
25
|
+
}
|