@webjsdev/server 0.8.74 → 0.8.75
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/package.json +1 -1
- package/src/auth.js +37 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webjsdev/server",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.75",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The server for WebJs, a full-stack JavaScript framework built on web components with server-side rendering and no build step. Provides the file-based router, SSR, server actions, route handlers, middleware, and live reload on Node 24+ or Bun.",
|
|
6
6
|
"main": "index.js",
|
package/src/auth.js
CHANGED
|
@@ -13,6 +13,8 @@ import { readTextBounded, readFormDataBounded, payloadTooLarge, DEFAULT_MAX_BODY
|
|
|
13
13
|
|
|
14
14
|
const enc = new TextEncoder();
|
|
15
15
|
const dec = new TextDecoder();
|
|
16
|
+
// Base names. In production each is sent with the `__Host-` prefix
|
|
17
|
+
// (cookieName below); development over http keeps the plain name.
|
|
16
18
|
const AUTH_COOKIE = 'webjs.auth';
|
|
17
19
|
const STATE_COOKIE = 'webjs.auth.state';
|
|
18
20
|
// Carries the post-sign-in target across the OAuth round trip (#1495). Signed
|
|
@@ -104,14 +106,33 @@ function parseCookies(header) {
|
|
|
104
106
|
return out;
|
|
105
107
|
}
|
|
106
108
|
|
|
109
|
+
/**
|
|
110
|
+
* The name a cookie is sent under. In production it carries the `__Host-`
|
|
111
|
+
* prefix (#1538): a browser stores a `__Host-` cookie only when it is
|
|
112
|
+
* `Secure`, has `Path=/` and has NO `Domain` attribute, so an app on a sibling
|
|
113
|
+
* subdomain (two apps on `a.example.app` and `b.example.app`, with
|
|
114
|
+
* `example.app` not on the Public Suffix List) can no longer plant its own
|
|
115
|
+
* `webjs.auth` with `Domain=.example.app` and sign a visitor in to the wrong
|
|
116
|
+
* account (cookie tossing, session fixation). Production never reads the
|
|
117
|
+
* unprefixed name: a fallback would reopen exactly that hole.
|
|
118
|
+
* Development over http keeps the plain name, since `__Host-` needs `Secure`.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} base @param {boolean} secure
|
|
121
|
+
*/
|
|
122
|
+
export function cookieName(base, secure) {
|
|
123
|
+
return secure ? `__Host-${base}` : base;
|
|
124
|
+
}
|
|
125
|
+
|
|
107
126
|
function setCookie(name, value, maxAge, secure) {
|
|
108
127
|
let s = `${name}=${encodeURIComponent(value)}; Max-Age=${Math.floor(maxAge / 1000)}; Path=/; HttpOnly; SameSite=Lax`;
|
|
109
128
|
if (secure) s += '; Secure';
|
|
110
129
|
return s;
|
|
111
130
|
}
|
|
112
131
|
|
|
113
|
-
|
|
114
|
-
|
|
132
|
+
// A `__Host-` cookie is cleared only by a Set-Cookie that also says Secure;
|
|
133
|
+
// a browser ignores one without it, and the cookie would stay.
|
|
134
|
+
function clearCookie(name, secure = false) {
|
|
135
|
+
return `${name}=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax${secure ? '; Secure' : ''}`;
|
|
115
136
|
}
|
|
116
137
|
|
|
117
138
|
// -- JWT --------------------------------------------------------------------
|
|
@@ -260,7 +281,7 @@ export function createAuth(config) {
|
|
|
260
281
|
// page cacheable so a logged-in body could be served to the next visitor.
|
|
261
282
|
markDynamicAccess();
|
|
262
283
|
const cookies = parseCookies(req.headers.get('cookie') || '');
|
|
263
|
-
const raw = cookies[AUTH_COOKIE];
|
|
284
|
+
const raw = cookies[cookieName(AUTH_COOKIE, secure())];
|
|
264
285
|
if (!raw) return null;
|
|
265
286
|
|
|
266
287
|
if (strategy === 'jwt') {
|
|
@@ -284,11 +305,11 @@ export function createAuth(config) {
|
|
|
284
305
|
if (strategy === 'jwt') {
|
|
285
306
|
let token = { sub: user.id, name: user.name, email: user.email, image: user.image, role: user.role, iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + maxAge };
|
|
286
307
|
if (cb.jwt) token = await cb.jwt({ token, user });
|
|
287
|
-
return setCookie(AUTH_COOKIE, await encodeJwt(token, secret), maxAgeMs, secure());
|
|
308
|
+
return setCookie(cookieName(AUTH_COOKIE, secure()), await encodeJwt(token, secret), maxAgeMs, secure());
|
|
288
309
|
}
|
|
289
310
|
const sid = randomId();
|
|
290
311
|
await dbStore.save(sid, user, maxAgeMs);
|
|
291
|
-
return setCookie(AUTH_COOKIE, await sign(sid, secret), maxAgeMs, secure());
|
|
312
|
+
return setCookie(cookieName(AUTH_COOKIE, secure()), await sign(sid, secret), maxAgeMs, secure());
|
|
292
313
|
}
|
|
293
314
|
|
|
294
315
|
// -- auth() ---------------------------------------------------------------
|
|
@@ -333,12 +354,12 @@ export function createAuth(config) {
|
|
|
333
354
|
async function signOutFn(opts = {}) {
|
|
334
355
|
const hdrs = new Headers();
|
|
335
356
|
hdrs.set('location', opts.redirectTo || pages.signOut || '/');
|
|
336
|
-
hdrs.append('set-cookie', clearCookie(AUTH_COOKIE));
|
|
357
|
+
hdrs.append('set-cookie', clearCookie(cookieName(AUTH_COOKIE, secure()), secure()));
|
|
337
358
|
|
|
338
359
|
if (strategy === 'database' && dbStore) {
|
|
339
360
|
const r = opts.req || getRequest();
|
|
340
361
|
if (r) {
|
|
341
|
-
const raw = parseCookies(r.headers.get('cookie') || '')[AUTH_COOKIE];
|
|
362
|
+
const raw = parseCookies(r.headers.get('cookie') || '')[cookieName(AUTH_COOKIE, secure())];
|
|
342
363
|
if (raw) { const sid = await unsign(raw, secret); if (sid) await dbStore.destroy(sid); }
|
|
343
364
|
}
|
|
344
365
|
}
|
|
@@ -361,14 +382,14 @@ export function createAuth(config) {
|
|
|
361
382
|
|
|
362
383
|
const hdrs = new Headers();
|
|
363
384
|
hdrs.set('location', url.toString());
|
|
364
|
-
hdrs.append('set-cookie', setCookie(STATE_COOKIE, await sign(state, secret), 600_000, secure()));
|
|
385
|
+
hdrs.append('set-cookie', setCookie(cookieName(STATE_COOKIE, secure()), await sign(state, secret), 600_000, secure()));
|
|
365
386
|
// The post-sign-in target rides in its own signed cookie (#1495). With no
|
|
366
387
|
// valid target, any leftover cookie from an abandoned earlier attempt is
|
|
367
388
|
// cleared so it cannot steer this sign-in somewhere stale.
|
|
368
389
|
const target = safeRedirectPath(opts.redirectTo);
|
|
369
390
|
hdrs.append('set-cookie', target
|
|
370
|
-
? setCookie(REDIRECT_COOKIE, await sign(target, secret), 600_000, secure())
|
|
371
|
-
: clearCookie(REDIRECT_COOKIE));
|
|
391
|
+
? setCookie(cookieName(REDIRECT_COOKIE, secure()), await sign(target, secret), 600_000, secure())
|
|
392
|
+
: clearCookie(cookieName(REDIRECT_COOKIE, secure()), secure()));
|
|
372
393
|
return new Response(null, { status: 302, headers: hdrs });
|
|
373
394
|
}
|
|
374
395
|
|
|
@@ -381,7 +402,7 @@ export function createAuth(config) {
|
|
|
381
402
|
* @returns {Promise<string | null>}
|
|
382
403
|
*/
|
|
383
404
|
async function readRedirectCookie(req) {
|
|
384
|
-
const raw = parseCookies(req.headers.get('cookie') || '')[REDIRECT_COOKIE];
|
|
405
|
+
const raw = parseCookies(req.headers.get('cookie') || '')[cookieName(REDIRECT_COOKIE, secure())];
|
|
385
406
|
if (!raw) return null;
|
|
386
407
|
return safeRedirectPath(await unsign(raw, secret));
|
|
387
408
|
}
|
|
@@ -392,7 +413,7 @@ export function createAuth(config) {
|
|
|
392
413
|
const state = url.searchParams.get('state');
|
|
393
414
|
if (!code || !state) return new Response('Missing code or state', { status: 400 });
|
|
394
415
|
|
|
395
|
-
const rawState = parseCookies(req.headers.get('cookie') || '')[STATE_COOKIE];
|
|
416
|
+
const rawState = parseCookies(req.headers.get('cookie') || '')[cookieName(STATE_COOKIE, secure())];
|
|
396
417
|
if (!rawState) return new Response('Missing state cookie', { status: 403 });
|
|
397
418
|
const verified = await unsign(rawState, secret);
|
|
398
419
|
if (!verified || verified !== state) return new Response('Invalid state', { status: 403 });
|
|
@@ -430,8 +451,8 @@ export function createAuth(config) {
|
|
|
430
451
|
if (ok === false) {
|
|
431
452
|
const denied = new Headers();
|
|
432
453
|
denied.set('location', pages.error || '/?error=AccessDenied');
|
|
433
|
-
denied.append('set-cookie', clearCookie(STATE_COOKIE));
|
|
434
|
-
denied.append('set-cookie', clearCookie(REDIRECT_COOKIE));
|
|
454
|
+
denied.append('set-cookie', clearCookie(cookieName(STATE_COOKIE, secure()), secure()));
|
|
455
|
+
denied.append('set-cookie', clearCookie(cookieName(REDIRECT_COOKIE, secure()), secure()));
|
|
435
456
|
return new Response(null, { status: 302, headers: denied });
|
|
436
457
|
}
|
|
437
458
|
}
|
|
@@ -439,8 +460,8 @@ export function createAuth(config) {
|
|
|
439
460
|
const hdrs = new Headers();
|
|
440
461
|
hdrs.set('location', (await readRedirectCookie(req)) || '/');
|
|
441
462
|
hdrs.append('set-cookie', await writeSession(user));
|
|
442
|
-
hdrs.append('set-cookie', clearCookie(STATE_COOKIE));
|
|
443
|
-
hdrs.append('set-cookie', clearCookie(REDIRECT_COOKIE));
|
|
463
|
+
hdrs.append('set-cookie', clearCookie(cookieName(STATE_COOKIE, secure()), secure()));
|
|
464
|
+
hdrs.append('set-cookie', clearCookie(cookieName(REDIRECT_COOKIE, secure()), secure()));
|
|
444
465
|
return new Response(null, { status: 302, headers: hdrs });
|
|
445
466
|
}
|
|
446
467
|
|