@softure-ai/auth 0.1.10 → 0.1.11

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
@@ -4,6 +4,13 @@ Newest first. Each version lists what changed for an app that uses `@softure-ai/
4
4
  production, the version gets a line `verified in: <app>@<commit>` ([docs/05](../../docs/05-adoption-playbook.md),
5
5
  "Definition of done"). Versions before the first one below are described in their GitHub Releases (`auth@x.y.z`).
6
6
 
7
+ ## 0.1.11
8
+
9
+ - The guard's login redirect uses core's `resolveAppOrigin` (#311): an origin listed in the config's
10
+ `origins.trustedOrigins` is trusted like one in the guard's `trustedOrigins` option, and `origins.trustRequestHost`
11
+ is honoured. A non-http(s) `X-Forwarded-Proto` value now falls back to the URL's scheme instead of making the
12
+ origin unreadable. Needs `@softure-ai/core` 0.1.8.
13
+
7
14
  ## 0.1.10
8
15
 
9
16
  - `isPasswordHash(value)` is exact: the salt and key must be unpadded base64url of exactly 16 and 64 bytes (22 and 86
package/README.md CHANGED
@@ -226,16 +226,19 @@ A protected prefix matches whole path segments (`/account` covers `/account/pass
226
226
  because behind a reverse proxy the request URL carries the server's internal host.
227
227
 
228
228
  An app served on several hosts from one build (the product on `app.example.com`, pages on
229
- `example.com`) lists the others in `trustedOrigins`, so a visitor is sent to the login page on the
230
- host they used:
229
+ `example.com`) lists the others in the config's `origins.trustedOrigins` (read by every module), so a
230
+ visitor is sent to the login page on the host they used. The guard's own `trustedOrigins` option adds
231
+ origins for the guard alone:
231
232
 
232
233
  ```ts
233
- const guard = createAuthGuard(softureConfig, { protect: ["/account"], trustedOrigins: ["https://example.com"] });
234
+ defineSoftureConfig({ appOrigin: "https://app.example.com", origins: { trustedOrigins: ["https://example.com"] }, ... });
235
+ const guard = createAuthGuard(softureConfig, { protect: ["/account"] });
234
236
  ```
235
237
 
236
- The guard reads the request's public origin from `X-Forwarded-Proto` and `X-Forwarded-Host` (first
237
- values), else from `Host` and the URL's scheme, and uses it only when it equals a listed origin
238
- (scheme, host and port); any other request goes to `appOrigin`. Headers alone can therefore never
238
+ The guard builds the redirect on core's `resolveAppOrigin` (core README, "Request origins"): the
239
+ request's public origin from `X-Forwarded-Proto` and `X-Forwarded-Host` (first values), else from
240
+ `Host` and the URL's scheme, used only when it equals a listed origin (scheme, host and port); any
241
+ other request goes to `appOrigin` (or, with `origins.trustRequestHost`, to the `Host` origin). Headers alone can therefore never
239
242
  send a visitor to a host the app did not list. An entry that is not an `http(s)` origin throws when
240
243
  the guard is created. A session cookie shared by the hosts needs `cookie.domain`.
241
244
 
package/dist/index.js CHANGED
@@ -36,7 +36,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
36
36
  export const auth = defineModule({
37
37
  manifest: {
38
38
  id: MODULE_ID,
39
- version: "0.1.10",
39
+ version: "0.1.11",
40
40
  dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
41
41
  dbSchema: "auth",
42
42
  tables: ["users", "sessions", "user_roles", "password_resets"],
@@ -1,4 +1,4 @@
1
- import type { SoftureConfig } from "@softure-ai/core";
1
+ import { type SoftureConfig } from "@softure-ai/core";
2
2
  export interface AuthGuardOptions {
3
3
  /** Path prefixes that need a session; each matches whole segments (`/account` → `/account/…`). */
4
4
  readonly protect: readonly string[];
@@ -8,9 +8,10 @@ export interface AuthGuardOptions {
8
8
  */
9
9
  readonly exclude?: readonly string[];
10
10
  /**
11
- * Origins besides `appOrigin` that the login redirect may stay on (`https://example.com`), for an app
12
- * served on several hosts. The request's public origin (`X-Forwarded-Proto` / `X-Forwarded-Host`,
13
- * else `Host` and the URL's scheme) is used only when it is listed; any other goes to `appOrigin`.
11
+ * Origins besides `appOrigin` and the config's `origins.trustedOrigins` that the login redirect may stay on
12
+ * (`https://example.com`). Prefer the config's block, which every module reads. The request's public origin
13
+ * (core `resolveAppOrigin`: `X-Forwarded-Host`, else `Host`) is used only when it is listed; any other goes to
14
+ * `appOrigin`.
14
15
  */
15
16
  readonly trustedOrigins?: readonly string[];
16
17
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAItD,MAAM,WAAW,gBAAgB;IAC/B,kGAAkG;IAClG,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC;;;;OAIG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC7C;AAKD,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,GAAG,IAAI,CAAC;AAE9D,6GAA6G;AAC7G,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAiC3F"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAiC,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAIrF,MAAM,WAAW,gBAAgB;IAC/B,kGAAkG;IAClG,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC;;;;;OAKG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC7C;AAKD,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,GAAG,IAAI,CAAC;AAE9D,6GAA6G;AAC7G,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAgC3F"}
@@ -1,3 +1,13 @@
1
+ // The route guard for the app's `proxy.ts` (roadmap ID-3, unknown 1). It uses only Web `Request`
2
+ // and `Response`, imports nothing from Next and no React, so it chains with other proxy pieces:
3
+ // `return channels.carry(request, guard(request)) ?? channels.tag(request) ?? NextResponse.next()`
4
+ // (the channel piece of @softure-ai/analytics/proxy).
5
+ //
6
+ // It checks that the session cookie is present, nothing more: an optimistic check that keeps
7
+ // anonymous visitors off private pages without a database round trip in the proxy. Pages and
8
+ // actions still call `requireUser`, which reads the session row. An app that is private by default
9
+ // protects "/" and lists its public paths in `exclude`; auth's own public pages are never guarded.
10
+ import { parseOrigin, resolveAppOrigin } from "@softure-ai/core";
1
11
  import { getAuthOptions, getAuthRoutes } from "../server/options.js";
2
12
  import { getSessionCookie, readSessionToken } from "../session-cookie.js";
3
13
  /** Mounted at a fixed path by the module (manifest `mount`); it answers `{ user: null }` without a session. */
@@ -11,7 +21,7 @@ export function createAuthGuard(config, options) {
11
21
  // Auth's own public pages: guarding them would send a visitor from the login page to itself.
12
22
  const publicPaths = [routes.login, routes.register, routes.forgotPassword, routes.resetPassword, SESSION_ROUTE].map((path) => normalizePrefix(path, "excluded"));
13
23
  const excluded = (options.exclude ?? []).map((prefix) => normalizePrefix(prefix, "excluded"));
14
- const trustedOrigins = new Set((options.trustedOrigins ?? []).map(normalizeOrigin));
24
+ const trustedOrigins = (options.trustedOrigins ?? []).map(normalizeOrigin);
15
25
  const isGuarded = (path) => {
16
26
  if (isUnder(path, changePassword))
17
27
  return true;
@@ -31,8 +41,7 @@ export function createAuthGuard(config, options) {
31
41
  return null;
32
42
  // Built on appOrigin, or on the request's public origin when the app trusts it: behind a proxy
33
43
  // the request URL carries an internal host, and request headers alone are never trusted.
34
- const origin = findPublicOrigin(request, url);
35
- const login = new URL(routes.login, origin !== null && trustedOrigins.has(origin) ? origin : config.appOrigin);
44
+ const login = new URL(routes.login, resolveAppOrigin(config, request, { trustedOrigins }));
36
45
  login.searchParams.set("next", `${url.pathname}${url.search}`);
37
46
  return Response.redirect(login, 307);
38
47
  };
@@ -46,34 +55,12 @@ function normalizePrefix(prefix, kind) {
46
55
  }
47
56
  /** `entry` as an origin (`https://example.com`, a trailing `/` dropped); anything else is a config error. */
48
57
  function normalizeOrigin(entry) {
49
- const parsed = parseOrigin(entry.endsWith("/") ? entry.slice(0, -1) : entry);
58
+ const parsed = parseOrigin(entry);
50
59
  if (parsed === null) {
51
60
  throw new Error(`createAuthGuard: trusted origin "${entry}" must be an http(s) origin such as https://example.com`);
52
61
  }
53
62
  return parsed;
54
63
  }
55
- /** The origin the browser used: the front proxy's forwarded scheme and host, else `Host` and the URL's scheme. */
56
- function findPublicOrigin(request, url) {
57
- const host = firstValue(request.headers.get("x-forwarded-host")) ?? firstValue(request.headers.get("host")) ?? url.host;
58
- const scheme = firstValue(request.headers.get("x-forwarded-proto")) ?? url.protocol.slice(0, -1);
59
- return parseOrigin(`${scheme}://${host}`);
60
- }
61
- function firstValue(header) {
62
- const value = header?.split(",")[0]?.trim();
63
- return value === undefined || value === "" ? null : value;
64
- }
65
- /** The normalized origin of `candidate` when it is exactly `http(s)://host[:port]`, else null. */
66
- function parseOrigin(candidate) {
67
- // Checked on the raw text, so a path, query, fragment or credentials make it no origin.
68
- if (!/^https?:\/\/[^/\\?#@\s]+$/i.test(candidate))
69
- return null;
70
- try {
71
- return new URL(candidate).origin;
72
- }
73
- catch {
74
- return null;
75
- }
76
- }
77
64
  function decodePath(pathname) {
78
65
  try {
79
66
  return decodeURIComponent(pathname).toLowerCase();
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAkB1E,+GAA+G;AAC/G,MAAM,aAAa,GAAG,mBAAmB,CAAC;AAI1C,6GAA6G;AAC7G,MAAM,UAAU,eAAe,CAAC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AC,WAAW,EAAE,CAAC;IACpD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,6FAA6F;AAC7F,SAAS,YAAY,CAAC,QAAgB,EAAE,MAAc;IACpD,OAAO,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,OAAO,CAAC,QAAgB,EAAE,MAAc;IAC/C,OAAO,MAAM,KAAK,GAAG,IAAI,QAAQ,KAAK,MAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;AACpF,CAAC"}
package/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "auth",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "dependsOn": { "security": "^0.1.0", "mailing": "^0.1.0?", "ops": "^0.1.0?" },
5
5
  "dbSchema": "auth",
6
6
  "tables": ["users", "sessions", "user_roles", "password_resets"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softure-ai/auth",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "SOFTURE AI auth module: users, scrypt passwords, database sessions, register with consent, login, logout, password change, roles, a proxy route guard, pages and forms for Next.js, and a password reset sender over @softure-ai/mailing.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -72,7 +72,7 @@
72
72
  "build": "tsc -p tsconfig.build.json"
73
73
  },
74
74
  "dependencies": {
75
- "@softure-ai/core": "^0.1.0",
75
+ "@softure-ai/core": "^0.1.8",
76
76
  "@softure-ai/db": "^0.1.0",
77
77
  "@softure-ai/security": "^0.1.0",
78
78
  "zod": "^4.6.5"
package/src/index.ts CHANGED
@@ -41,7 +41,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
41
41
  export const auth = defineModule({
42
42
  manifest: {
43
43
  id: MODULE_ID,
44
- version: "0.1.10",
44
+ version: "0.1.11",
45
45
  dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
46
46
  dbSchema: "auth",
47
47
  tables: ["users", "sessions", "user_roles", "password_resets"],
@@ -7,7 +7,7 @@
7
7
  // anonymous visitors off private pages without a database round trip in the proxy. Pages and
8
8
  // actions still call `requireUser`, which reads the session row. An app that is private by default
9
9
  // protects "/" and lists its public paths in `exclude`; auth's own public pages are never guarded.
10
- import type { SoftureConfig } from "@softure-ai/core";
10
+ import { parseOrigin, resolveAppOrigin, type SoftureConfig } from "@softure-ai/core";
11
11
  import { getAuthOptions, getAuthRoutes } from "../server/options.js";
12
12
  import { getSessionCookie, readSessionToken } from "../session-cookie.js";
13
13
 
@@ -20,9 +20,10 @@ export interface AuthGuardOptions {
20
20
  */
21
21
  readonly exclude?: readonly string[];
22
22
  /**
23
- * Origins besides `appOrigin` that the login redirect may stay on (`https://example.com`), for an app
24
- * served on several hosts. The request's public origin (`X-Forwarded-Proto` / `X-Forwarded-Host`,
25
- * else `Host` and the URL's scheme) is used only when it is listed; any other goes to `appOrigin`.
23
+ * Origins besides `appOrigin` and the config's `origins.trustedOrigins` that the login redirect may stay on
24
+ * (`https://example.com`). Prefer the config's block, which every module reads. The request's public origin
25
+ * (core `resolveAppOrigin`: `X-Forwarded-Host`, else `Host`) is used only when it is listed; any other goes to
26
+ * `appOrigin`.
26
27
  */
27
28
  readonly trustedOrigins?: readonly string[];
28
29
  }
@@ -43,7 +44,7 @@ export function createAuthGuard(config: SoftureConfig, options: AuthGuardOptions
43
44
  normalizePrefix(path, "excluded"),
44
45
  );
45
46
  const excluded = (options.exclude ?? []).map((prefix) => normalizePrefix(prefix, "excluded"));
46
- const trustedOrigins = new Set((options.trustedOrigins ?? []).map(normalizeOrigin));
47
+ const trustedOrigins = (options.trustedOrigins ?? []).map(normalizeOrigin);
47
48
 
48
49
  const isGuarded = (path: string): boolean => {
49
50
  if (isUnder(path, changePassword)) return true;
@@ -61,8 +62,7 @@ export function createAuthGuard(config: SoftureConfig, options: AuthGuardOptions
61
62
  if (cookieNames.some((name) => readSessionToken(cookieHeader, name) !== null)) return null;
62
63
  // Built on appOrigin, or on the request's public origin when the app trusts it: behind a proxy
63
64
  // the request URL carries an internal host, and request headers alone are never trusted.
64
- const origin = findPublicOrigin(request, url);
65
- const login = new URL(routes.login, origin !== null && trustedOrigins.has(origin) ? origin : config.appOrigin);
65
+ const login = new URL(routes.login, resolveAppOrigin(config, request, { trustedOrigins }));
66
66
  login.searchParams.set("next", `${url.pathname}${url.search}`);
67
67
  return Response.redirect(login, 307);
68
68
  };
@@ -78,36 +78,13 @@ function normalizePrefix(prefix: string, kind: "protected" | "excluded"): string
78
78
 
79
79
  /** `entry` as an origin (`https://example.com`, a trailing `/` dropped); anything else is a config error. */
80
80
  function normalizeOrigin(entry: string): string {
81
- const parsed = parseOrigin(entry.endsWith("/") ? entry.slice(0, -1) : entry);
81
+ const parsed = parseOrigin(entry);
82
82
  if (parsed === null) {
83
83
  throw new Error(`createAuthGuard: trusted origin "${entry}" must be an http(s) origin such as https://example.com`);
84
84
  }
85
85
  return parsed;
86
86
  }
87
87
 
88
- /** The origin the browser used: the front proxy's forwarded scheme and host, else `Host` and the URL's scheme. */
89
- function findPublicOrigin(request: Request, url: URL): string | null {
90
- const host = firstValue(request.headers.get("x-forwarded-host")) ?? firstValue(request.headers.get("host")) ?? url.host;
91
- const scheme = firstValue(request.headers.get("x-forwarded-proto")) ?? url.protocol.slice(0, -1);
92
- return parseOrigin(`${scheme}://${host}`);
93
- }
94
-
95
- function firstValue(header: string | null): string | null {
96
- const value = header?.split(",")[0]?.trim();
97
- return value === undefined || value === "" ? null : value;
98
- }
99
-
100
- /** The normalized origin of `candidate` when it is exactly `http(s)://host[:port]`, else null. */
101
- function parseOrigin(candidate: string): string | null {
102
- // Checked on the raw text, so a path, query, fragment or credentials make it no origin.
103
- if (!/^https?:\/\/[^/\\?#@\s]+$/i.test(candidate)) return null;
104
- try {
105
- return new URL(candidate).origin;
106
- } catch {
107
- return null;
108
- }
109
- }
110
-
111
88
  function decodePath(pathname: string): string | null {
112
89
  try {
113
90
  return decodeURIComponent(pathname).toLowerCase();