@jskit-ai/auth-core 0.2.22 → 0.2.24
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 +2 -0
- package/package.json +2 -2
- package/src/server/authPolicy.js +9 -5
- package/src/server/utils.js +13 -0
- package/test/providerRuntime.test.js +25 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `AUTH_COOKIE_SAME_SITE` sets the CSRF cookie's SameSite: `lax` (the default, unset or blank) or `strict`, as local auth's cookies take it; any other value stops the auth policy's startup with `AUTH_COOKIE_SAME_SITE must be lax or strict, not "<value>".` `./server/utils` exports `resolveAuthCookieSameSite(env)`, which reads it. Applications that leave it unset are unchanged.
|
|
6
|
+
|
|
5
7
|
- **Second factor.** A route asks for one with the kernel's `secondFactor` option (`true`, or `{ maxAgeSeconds }` for a dangerous action), and the auth policy enforces it: a signed-in session that has not proved its second factor, or proved it longer ago than `maxAgeSeconds`, is refused with 403 `auth.secondFactor.required` (params `{ maxAgeSeconds }` when the route names one), checked after the CSRF token, the sign-in and the route's permission. The policy sets `request.secondFactorAt`, when the session last proved it (`""` when it has not), from the provider's `authenticate` result. `./server/secondFactor` gives `requireSecondFactor(request, requirement)`, the same refusal for an action whose requirement depends on more than its route (the actor's role, say; a call without a request, as from the server's own code, is refused too), with `isSecondFactorMet()` and `secondFactorRequiredParams()`; `./shared/authSecondFactor` gives `SECOND_FACTOR_REQUIRED` and `isSecondFactorRequired(error)` for clients.
|
|
6
8
|
- The second factor's actions and commands, each one call to the installed `auth.service` for the request's own session (channels `api` and `internal`, never automation): `auth.secondFactor.read`, `auth.secondFactor.setup.start`, `auth.secondFactor.setup.confirm` (body `{ code }`), `auth.secondFactor.verify`, `auth.secondFactor.recoveryCodes.replace` and `auth.secondFactor.remove` (body `{ code }` or `{ recoveryCode }`), exported from `./server/actions/secondFactorActions` and `./shared/commands`, at `AUTH_PATHS.SECOND_FACTOR`, `SECOND_FACTOR_SETUP`, `SECOND_FACTOR_SETUP_CONFIRM`, `SECOND_FACTOR_VERIFY` and `SECOND_FACTOR_RECOVERY_CODES`. `AuthService` documents the provider operations behind them, plus `listSecondFactors({ userIds })` and `resetSecondFactor({ userId, byUserId })` for an application's own screens (an owner resetting staff who lost their authenticator); a provider without a second factor answers 501 `AUTH_OPERATION_UNSUPPORTED`. The capability `features.secondFactor` says the provider has one, and the security status's `mfa` says whether the account has one set up. New codes: `auth.secondFactor.required`, `auth.validation.secondFactorCodeRequired`; new message keys `jskit.auth.secondFactor.required` and `jskit.auth.validation.secondFactorCodeRequired` (an app with other locales adds them with `npx jskit i18n export <locale>`).
|
|
7
9
|
- A route's `permission` now matches the actor's grants as action permissions do: `*` grants every route, and a grant ending in `.*`, such as `crud.labels.*`, grants every permission under that prefix. It used to need the exact name, so a workspace owner holding `*` was refused a route requiring `crud.labels.update`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -147,6 +147,6 @@
|
|
|
147
147
|
}
|
|
148
148
|
},
|
|
149
149
|
"peerDependencies": {
|
|
150
|
-
"@jskit-ai/kernel": "0.2.
|
|
150
|
+
"@jskit-ai/kernel": "0.2.23"
|
|
151
151
|
}
|
|
152
152
|
}
|
package/src/server/authPolicy.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { hasPermission } from "@jskit-ai/kernel/shared/support/permissions";
|
|
2
2
|
import { parseBooleanFlag } from "./booleanFlag.js";
|
|
3
|
+
import { resolveAuthCookieSameSite } from "./utils.js";
|
|
3
4
|
|
|
4
5
|
function parseList(value) {
|
|
5
6
|
return String(value || "").split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
@@ -27,6 +28,7 @@ const everyoneSignedOut = Object.freeze({
|
|
|
27
28
|
*/
|
|
28
29
|
function createAuthPolicy({ authService, extensions, env }) {
|
|
29
30
|
const unsafeMethods = parseList(env.AUTH_CSRF_UNSAFE_METHODS);
|
|
31
|
+
const csrfCookieSameSite = resolveAuthCookieSameSite(env);
|
|
30
32
|
|
|
31
33
|
function resolveActor(request) {
|
|
32
34
|
return authService.authenticateRequest(request);
|
|
@@ -46,12 +48,14 @@ function createAuthPolicy({ authService, extensions, env }) {
|
|
|
46
48
|
apiPrefix: env.AUTH_API_PREFIX,
|
|
47
49
|
// Unset AUTH_CSRF_UNSAFE_METHODS keeps the plugin default (POST, PUT, PATCH, DELETE).
|
|
48
50
|
unsafeMethods: unsafeMethods.length > 0 ? Object.freeze(unsafeMethods) : undefined,
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
// The CSRF cookie's SameSite is the sign-in cookies' (AUTH_COOKIE_SAME_SITE). Unset AUTH_CSRF_COOKIE_SECURE
|
|
52
|
+
// follows NODE_ENV (Secure in production).
|
|
53
|
+
csrfCookieOpts: Object.freeze({
|
|
54
|
+
sameSite: csrfCookieSameSite,
|
|
55
|
+
...(String(env.AUTH_CSRF_COOKIE_SECURE ?? "").trim()
|
|
52
56
|
? { secure: parseBooleanFlag(env.AUTH_CSRF_COOKIE_SECURE, false) }
|
|
53
|
-
: {}
|
|
54
|
-
)
|
|
57
|
+
: {})
|
|
58
|
+
})
|
|
55
59
|
})
|
|
56
60
|
});
|
|
57
61
|
}
|
package/src/server/utils.js
CHANGED
|
@@ -12,6 +12,19 @@ export function hostOnlyCookieName(name, secure) {
|
|
|
12
12
|
return secure && !cookieName.startsWith(HOST_ONLY_COOKIE_PREFIX) ? `${HOST_ONLY_COOKIE_PREFIX}${cookieName}` : cookieName;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* AUTH_COOKIE_SAME_SITE: the SameSite attribute of the sign-in cookies, `lax`
|
|
17
|
+
* (when unset or blank) or `strict`. Anything else stops startup.
|
|
18
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
19
|
+
* @returns {"lax" | "strict"}
|
|
20
|
+
*/
|
|
21
|
+
export function resolveAuthCookieSameSite(env) {
|
|
22
|
+
const value = String(env.AUTH_COOKIE_SAME_SITE ?? "").trim().toLowerCase();
|
|
23
|
+
if (!value) return "lax";
|
|
24
|
+
if (value === "lax" || value === "strict") return value;
|
|
25
|
+
throw new Error(`AUTH_COOKIE_SAME_SITE must be lax or strict, not "${env.AUTH_COOKIE_SAME_SITE}".`);
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
export function normalizeEmail(value) {
|
|
16
29
|
return String(value || "")
|
|
17
30
|
.trim()
|
|
@@ -151,3 +151,28 @@ test("AuthPolicyProvider checks CSRF on unsafe methods when AUTH_CSRF_UNSAFE_MET
|
|
|
151
151
|
const missingToken = await app.inject({ method: "POST", url: "/api/write" });
|
|
152
152
|
assert.equal(missingToken.statusCode, 403);
|
|
153
153
|
});
|
|
154
|
+
|
|
155
|
+
/** The Set-Cookie header of the CSRF secret an application started with these env values sends. */
|
|
156
|
+
async function csrfSecretCookie(t, env) {
|
|
157
|
+
const app = Fastify();
|
|
158
|
+
t.after(() => app.close());
|
|
159
|
+
const runtime = createCapabilityRuntime({
|
|
160
|
+
inputs: { ...runtimeInputs(app), "runtime.env": { NODE_ENV: "test", ...env } },
|
|
161
|
+
providers: [EventProvider, ActionProvider, HttpProvider, AuthExtensionsProvider, AuthFeature, AuthPolicyProvider]
|
|
162
|
+
});
|
|
163
|
+
await runtime.start();
|
|
164
|
+
app.get("/api/csrf", { config: { authPolicy: "public" } }, (_request, reply) => ({ token: reply.generateCsrf() }));
|
|
165
|
+
const response = await app.inject({ method: "GET", url: "/api/csrf" });
|
|
166
|
+
return [].concat(response.headers["set-cookie"]).find((cookie) => cookie.startsWith("_csrf="));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
test("the CSRF cookie is SameSite=Lax unless AUTH_COOKIE_SAME_SITE is strict, and another value stops startup", async (t) => {
|
|
170
|
+
assert.match(await csrfSecretCookie(t, {}), /; SameSite=Lax(;|$)/u);
|
|
171
|
+
assert.match(await csrfSecretCookie(t, { AUTH_COOKIE_SAME_SITE: "" }), /; SameSite=Lax(;|$)/u);
|
|
172
|
+
assert.match(await csrfSecretCookie(t, { AUTH_COOKIE_SAME_SITE: "strict" }), /; SameSite=Strict(;|$)/u);
|
|
173
|
+
for (const value of ["none", "Strict;"]) {
|
|
174
|
+
await assert.rejects(csrfSecretCookie(t, { AUTH_COOKIE_SAME_SITE: value }), {
|
|
175
|
+
message: `AUTH_COOKIE_SAME_SITE must be lax or strict, not "${value}".`
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
});
|