@bymax-one/nest-auth 1.0.4 → 1.0.5

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/CHANGELOG.md CHANGED
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.0.5] - 2026-05-25
11
+
12
+ ### Changed
13
+
14
+ - **Default `cookies.sameSite` lowered from `'strict'` to `'lax'`** ([`src/server/config/default-options.ts`](src/server/config/default-options.ts), [`src/server/services/token-delivery.service.ts`](src/server/services/token-delivery.service.ts)). The previous `'strict'` default broke the OAuth return-trip: Chromium does not include `SameSite=Strict` cookies on the very first request after a cross-site-initiated navigation (e.g. `accounts.google.com → /auth/oauth/google/callback → /dashboard`), so the freshly-issued auth cookies never reached the destination route. The `'lax'` default mirrors what Chromium applies to cookies that omit the attribute entirely and is the industry posture for browser auth cookies (Passport, Auth0, NextAuth all default to or expect Lax). The CSRF margin loss is negligible — POST endpoints are still cross-site safe under Lax, and the lib already short-circuits forgery vectors with origin checks at the controller boundary.
15
+
16
+ This is a **behavior change** for consumers that relied on the `'strict'` posture; they can restore it explicitly with `cookies.sameSite: 'strict'`. SemVer-wise this is a patch because the previous behavior broke a documented feature (OAuth callbacks).
17
+
18
+ ### Added
19
+
20
+ - **`cookies.sameSite?: 'lax' | 'strict' | 'none'` configuration option** ([`src/server/interfaces/auth-module-options.interface.ts`](src/server/interfaces/auth-module-options.interface.ts)). Lets consumers explicitly pick the SameSite posture for every cookie the module issues (access token, refresh token, session signal). Defaults to `'lax'` (see Changed above).
21
+
22
+ Validated at startup by [`src/server/config/resolved-options.ts`](src/server/config/resolved-options.ts): `cookies.sameSite: 'none'` requires `secureCookies: true` (browser-spec rule — `SameSite=None` cookies without `Secure` are silently dropped, which would surface as "auth doesn't work" instead of a configuration error).
23
+
24
+ Covered by 3 new unit tests in [`src/server/services/token-delivery.service.spec.ts`](src/server/services/token-delivery.service.spec.ts) (default propagates, explicit `'strict'` overrides, explicit `'none'` propagates) and 5 new validator tests in [`src/server/config/resolved-options.spec.ts`](src/server/config/resolved-options.spec.ts) (default to `'lax'`, explicit override, `'none' + secureCookies: true` accepted, `'none' + secureCookies: false` rejected, `'none'` accepted in production via `NODE_ENV`-driven default). One existing e2e cookie-header assertion in [`test/e2e/auth-flow.e2e-spec.ts`](test/e2e/auth-flow.e2e-spec.ts) updated to match the new default; the rest of the suite continues to pass unmodified.
25
+
10
26
  ## [1.0.4] - 2026-05-25
11
27
 
12
28
  ### Added
@@ -45,7 +45,13 @@ var DEFAULT_OPTIONS = {
45
45
  accessTokenName: "access_token",
46
46
  refreshTokenName: "refresh_token",
47
47
  sessionSignalName: "has_session",
48
- refreshCookiePath: "/auth"
48
+ refreshCookiePath: "/auth",
49
+ // 'lax' matches Chromium's default for cookies that omit SameSite. It
50
+ // delivers auth cookies on top-level cross-site GET navigations — the
51
+ // case that makes OAuth redirects (Google → callback → dashboard) work
52
+ // without dropping the freshly-issued session on the first hop. Consumers
53
+ // that want the stricter posture can override via `cookies.sameSite`.
54
+ sameSite: "lax"
49
55
  },
50
56
  mfa: {
51
57
  recoveryCodeCount: 8,
@@ -144,6 +150,7 @@ function resolveOptions(userOptions) {
144
150
  validateOAuthProviders(userOptions.oauth);
145
151
  validateOAuthSuccessRedirectUrl(userOptions);
146
152
  validateRefreshCookiePath(userOptions.routePrefix, userOptions.cookies);
153
+ validateSameSiteNoneRequiresSecure(userOptions);
147
154
  validateRefreshGraceWindow(userOptions.jwt);
148
155
  const { mfa: _mfa, ...userOptionsWithoutMfa } = userOptions;
149
156
  const resolved = {
@@ -344,6 +351,15 @@ function validateRefreshCookiePath(routePrefix, cookies) {
344
351
  }
345
352
  }
346
353
  __name(validateRefreshCookiePath, "validateRefreshCookiePath");
354
+ function validateSameSiteNoneRequiresSecure(userOptions) {
355
+ const sameSite = userOptions.cookies?.sameSite;
356
+ if (sameSite !== "none") return;
357
+ const secureCookies = userOptions.secureCookies ?? process.env["NODE_ENV"] === "production";
358
+ if (!secureCookies) {
359
+ throw new Error(`[BymaxAuthModule] cookies.sameSite is 'none' but secureCookies is false. Browsers reject SameSite=None cookies without the Secure attribute, so the auth cookies would never be stored. Set secureCookies: true (and serve over HTTPS) or use cookies.sameSite: 'lax' / 'strict'.`);
360
+ }
361
+ }
362
+ __name(validateSameSiteNoneRequiresSecure, "validateSameSiteNoneRequiresSecure");
347
363
  function validateRefreshGraceWindow(jwt) {
348
364
  const graceSeconds = jwt.refreshGraceWindowSeconds ?? DEFAULT_OPTIONS.jwt.refreshGraceWindowSeconds;
349
365
  const refreshExpiresInDays = jwt.refreshExpiresInDays ?? DEFAULT_OPTIONS.jwt.refreshExpiresInDays;
@@ -1458,9 +1474,16 @@ var TokenDeliveryService = class {
1458
1474
  maxAge: refreshMaxAge
1459
1475
  });
1460
1476
  }
1477
+ /**
1478
+ * Returns the shared cookie attributes applied to every cookie issued by
1479
+ * this service. `sameSite` comes from the resolved options (defaults to
1480
+ * `'lax'`); the `domain` is folded in only when the consumer supplies a
1481
+ * resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
1482
+ * `maxAge`) are spread on top of this base by the callers.
1483
+ */
1461
1484
  baseCookieOptions(domain) {
1462
1485
  return {
1463
- sameSite: "strict",
1486
+ sameSite: this.options.cookies.sameSite,
1464
1487
  ...domain ? {
1465
1488
  domain
1466
1489
  } : {}
@@ -422,6 +422,26 @@ interface BymaxAuthModuleOptions {
422
422
  * @returns Array of domain strings where cookies should be set (e.g. `['.example.com']`).
423
423
  */
424
424
  resolveDomains?: (requestDomain: string) => string[];
425
+ /**
426
+ * `SameSite` attribute applied to every cookie issued by the module
427
+ * (access token, refresh token, session signal). Default: `'lax'`.
428
+ *
429
+ * Trade-offs:
430
+ * - `'lax'` (default): cookies travel on top-level cross-site GET navigations.
431
+ * This is the standard posture for browser auth — OAuth redirects from a
432
+ * provider (Google, GitHub, …) back to the app deliver the auth cookies on
433
+ * the first navigation, which is exactly what users expect after sign-in.
434
+ * Aligns with Chromium's behavior for cookies that omit `SameSite`.
435
+ * - `'strict'`: cookies are withheld on every cross-site request, including
436
+ * the OAuth return-trip. Choose this only when the app does not use
437
+ * third-party identity providers and the extra CSRF margin justifies the
438
+ * broken OAuth flow.
439
+ * - `'none'`: cookies are sent on every cross-site request. The browser
440
+ * requires `Secure` for `SameSite=None`, so the resolver throws at startup
441
+ * when this combination would be unset (`secureCookies` must be `true`).
442
+ * Pick this for embedded scenarios (iframes, third-party widgets).
443
+ */
444
+ sameSite?: 'lax' | 'strict' | 'none';
425
445
  };
426
446
  /**
427
447
  * Multi-factor authentication (MFA/TOTP) configuration.
@@ -2881,6 +2901,13 @@ declare class TokenDeliveryService {
2881
2901
  */
2882
2902
  extractDomain(req: Request): string | undefined;
2883
2903
  private setAuthCookies;
2904
+ /**
2905
+ * Returns the shared cookie attributes applied to every cookie issued by
2906
+ * this service. `sameSite` comes from the resolved options (defaults to
2907
+ * `'lax'`); the `domain` is folded in only when the consumer supplies a
2908
+ * resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
2909
+ * `maxAge`) are spread on top of this base by the callers.
2910
+ */
2884
2911
  private baseCookieOptions;
2885
2912
  private readCookie;
2886
2913
  private readBearerHeader;
@@ -422,6 +422,26 @@ interface BymaxAuthModuleOptions {
422
422
  * @returns Array of domain strings where cookies should be set (e.g. `['.example.com']`).
423
423
  */
424
424
  resolveDomains?: (requestDomain: string) => string[];
425
+ /**
426
+ * `SameSite` attribute applied to every cookie issued by the module
427
+ * (access token, refresh token, session signal). Default: `'lax'`.
428
+ *
429
+ * Trade-offs:
430
+ * - `'lax'` (default): cookies travel on top-level cross-site GET navigations.
431
+ * This is the standard posture for browser auth — OAuth redirects from a
432
+ * provider (Google, GitHub, …) back to the app deliver the auth cookies on
433
+ * the first navigation, which is exactly what users expect after sign-in.
434
+ * Aligns with Chromium's behavior for cookies that omit `SameSite`.
435
+ * - `'strict'`: cookies are withheld on every cross-site request, including
436
+ * the OAuth return-trip. Choose this only when the app does not use
437
+ * third-party identity providers and the extra CSRF margin justifies the
438
+ * broken OAuth flow.
439
+ * - `'none'`: cookies are sent on every cross-site request. The browser
440
+ * requires `Secure` for `SameSite=None`, so the resolver throws at startup
441
+ * when this combination would be unset (`secureCookies` must be `true`).
442
+ * Pick this for embedded scenarios (iframes, third-party widgets).
443
+ */
444
+ sameSite?: 'lax' | 'strict' | 'none';
425
445
  };
426
446
  /**
427
447
  * Multi-factor authentication (MFA/TOTP) configuration.
@@ -2881,6 +2901,13 @@ declare class TokenDeliveryService {
2881
2901
  */
2882
2902
  extractDomain(req: Request): string | undefined;
2883
2903
  private setAuthCookies;
2904
+ /**
2905
+ * Returns the shared cookie attributes applied to every cookie issued by
2906
+ * this service. `sameSite` comes from the resolved options (defaults to
2907
+ * `'lax'`); the `domain` is folded in only when the consumer supplies a
2908
+ * resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
2909
+ * `maxAge`) are spread on top of this base by the callers.
2910
+ */
2884
2911
  private baseCookieOptions;
2885
2912
  private readCookie;
2886
2913
  private readBearerHeader;
@@ -43,7 +43,13 @@ var DEFAULT_OPTIONS = {
43
43
  accessTokenName: "access_token",
44
44
  refreshTokenName: "refresh_token",
45
45
  sessionSignalName: "has_session",
46
- refreshCookiePath: "/auth"
46
+ refreshCookiePath: "/auth",
47
+ // 'lax' matches Chromium's default for cookies that omit SameSite. It
48
+ // delivers auth cookies on top-level cross-site GET navigations — the
49
+ // case that makes OAuth redirects (Google → callback → dashboard) work
50
+ // without dropping the freshly-issued session on the first hop. Consumers
51
+ // that want the stricter posture can override via `cookies.sameSite`.
52
+ sameSite: "lax"
47
53
  },
48
54
  mfa: {
49
55
  recoveryCodeCount: 8,
@@ -142,6 +148,7 @@ function resolveOptions(userOptions) {
142
148
  validateOAuthProviders(userOptions.oauth);
143
149
  validateOAuthSuccessRedirectUrl(userOptions);
144
150
  validateRefreshCookiePath(userOptions.routePrefix, userOptions.cookies);
151
+ validateSameSiteNoneRequiresSecure(userOptions);
145
152
  validateRefreshGraceWindow(userOptions.jwt);
146
153
  const { mfa: _mfa, ...userOptionsWithoutMfa } = userOptions;
147
154
  const resolved = {
@@ -342,6 +349,15 @@ function validateRefreshCookiePath(routePrefix, cookies) {
342
349
  }
343
350
  }
344
351
  __name(validateRefreshCookiePath, "validateRefreshCookiePath");
352
+ function validateSameSiteNoneRequiresSecure(userOptions) {
353
+ const sameSite = userOptions.cookies?.sameSite;
354
+ if (sameSite !== "none") return;
355
+ const secureCookies = userOptions.secureCookies ?? process.env["NODE_ENV"] === "production";
356
+ if (!secureCookies) {
357
+ throw new Error(`[BymaxAuthModule] cookies.sameSite is 'none' but secureCookies is false. Browsers reject SameSite=None cookies without the Secure attribute, so the auth cookies would never be stored. Set secureCookies: true (and serve over HTTPS) or use cookies.sameSite: 'lax' / 'strict'.`);
358
+ }
359
+ }
360
+ __name(validateSameSiteNoneRequiresSecure, "validateSameSiteNoneRequiresSecure");
345
361
  function validateRefreshGraceWindow(jwt) {
346
362
  const graceSeconds = jwt.refreshGraceWindowSeconds ?? DEFAULT_OPTIONS.jwt.refreshGraceWindowSeconds;
347
363
  const refreshExpiresInDays = jwt.refreshExpiresInDays ?? DEFAULT_OPTIONS.jwt.refreshExpiresInDays;
@@ -1456,9 +1472,16 @@ var TokenDeliveryService = class {
1456
1472
  maxAge: refreshMaxAge
1457
1473
  });
1458
1474
  }
1475
+ /**
1476
+ * Returns the shared cookie attributes applied to every cookie issued by
1477
+ * this service. `sameSite` comes from the resolved options (defaults to
1478
+ * `'lax'`); the `domain` is folded in only when the consumer supplies a
1479
+ * resolver. Other per-cookie attributes (`httpOnly`, `secure`, `path`,
1480
+ * `maxAge`) are spread on top of this base by the callers.
1481
+ */
1459
1482
  baseCookieOptions(domain) {
1460
1483
  return {
1461
- sameSite: "strict",
1484
+ sameSite: this.options.cookies.sameSite,
1462
1485
  ...domain ? {
1463
1486
  domain
1464
1487
  } : {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bymax-one/nest-auth",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Full-stack authentication and authorization package for NestJS, React and Next.js — JWT, MFA, OAuth, sessions, multi-tenant SaaS ready",
5
5
  "author": "Bymax One <support@bymax.one>",
6
6
  "license": "MIT",