@axiumine/koa-utils 5.8.0 → 6.0.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/README.md +19 -3
- package/dist/email/SocketLabsLib.d.mts +20 -1
- package/dist/email/SocketLabsLib.mjs +23 -2
- package/dist/graphQL/schema/mutations/resetPwd.d.mts +10 -0
- package/dist/graphQL/schema/mutations/resetPwd.mjs +3 -3
- package/dist/lib/access/createResetPwdFlow.d.mts +10 -1
- package/dist/lib/access/createResetPwdFlow.mjs +3 -2
- package/dist/lib/access/resetPwdMailer.d.mts +44 -0
- package/dist/lib/access/resetPwdMailer.mjs +31 -0
- package/dist/lib/isIntrospectionBypassAllowed.d.mts +19 -0
- package/dist/lib/isIntrospectionBypassAllowed.mjs +23 -0
- package/dist/private/lib/verifyIntrospectionCode.d.mts +13 -4
- package/dist/private/lib/verifyIntrospectionCode.mjs +17 -4
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ The remaining twelve are declared **optional** in `peerDependenciesMeta`. There
|
|
|
28
28
|
|
|
29
29
|
| Optional peer | Required by |
|
|
30
30
|
|---|---|
|
|
31
|
-
| `@socketlabs/email` | `email/SocketLabsLib`, `lib/access/createResetPwdFlow`, `lib/access/createVerifyEmailFlow`, `lib/access/verifyEmailMailer`, `koa/router/verifyEmail`, and the `signUp` / `login*` / `resetPwd` / `updatePassword` / `emailChangeHashVerify` mutations |
|
|
31
|
+
| `@socketlabs/email` | `email/SocketLabsLib`, `lib/access/createResetPwdFlow`, `lib/access/createVerifyEmailFlow`, `lib/access/resetPwdMailer`, `lib/access/verifyEmailMailer`, `koa/router/verifyEmail`, and the `signUp` / `login*` / `resetPwd` / `updatePassword` / `emailChangeHashVerify` mutations |
|
|
32
32
|
| `clamscan` | `files/scanVirus`, `files/uploadTempImage`, `files/uploadTempPdf` |
|
|
33
33
|
| `file-type` | `files/validateMimeType`, `files/validateMimeTypeImages`, `files/validateJpgPngMimeType`, `files/uploadTempImage`, `files/uploadTempPdf` — loaded lazily, at first call |
|
|
34
34
|
| `fs-extra` | the `files/move*` and `files/validate*` helpers, `files/uploadTempImage`, `files/uploadTempPdf` |
|
|
@@ -200,14 +200,30 @@ import { StringLib } from '@axiumine/koa-utils/lib/StringLib'
|
|
|
200
200
|
| `POSTGRESQL_USER`, `_PWD`, `_HOST`, `_PORT`, `_DBNAME`, `_POOL_MAX`, `_IDLE_TIMEOUT` | PostgreSQL |
|
|
201
201
|
| `REDIS_URL` or `REDIS_IS_CLUSTER` + `REDIS_DB{1,2,3}_HOST/PORT`, `REDIS_USERNAME`, `REDIS_PASSWORD` | Redis |
|
|
202
202
|
| `REDIS_KEY` | Prefix for all keys (e.g. `myapp:`) |
|
|
203
|
-
| `INTROSPECTION_CODE` | Bypass header `x-introspectioncode` for schema introspection |
|
|
203
|
+
| `INTROSPECTION_CODE` | Bypass header `x-introspectioncode` for schema introspection — **only honoured when `NODE_ENV` is `development` or `test`** (see below) |
|
|
204
204
|
| `SOCKETLABS_SERVER_ID`, `SOCKETLABS_SERVER_APIKEY` | SocketLabs auth |
|
|
205
205
|
| `PLATFORM_NAME`, `APP_DOMAIN`, `EMAIL_FROM`, `DEV_TEAM_EMAIL` | Email templating |
|
|
206
206
|
| `STATIC_FOLDER` | Destination for `moveFileStaticDomain` |
|
|
207
|
-
| `NODE_ENV` | `development` enables extra Sentry capture in the error handler |
|
|
207
|
+
| `NODE_ENV` | `development` enables extra Sentry capture in the error handler. Also gates the introspection bypass: `development` and `test` are the only values that let `x-introspectioncode` skip authentication |
|
|
208
208
|
|
|
209
209
|
Each module calls `dotenv.config()` at import time.
|
|
210
210
|
|
|
211
|
+
### Introspection bypass and `NODE_ENV` (6.0.0)
|
|
212
|
+
|
|
213
|
+
`x-introspectioncode` skips authentication in `authenticatedResourceHandler`, `authenticatedAuthorizationHandler` and `authenticatedLogoutHandler`. Up to 5.9.0 it did so under **every** `NODE_ENV`. From 6.0.0 the bypass is refused unless `process.env.NODE_ENV` is exactly `development` or `test` — the check runs before `INTROSPECTION_CODE` is read, so a correct secret and a correct header still fail everywhere else.
|
|
214
|
+
|
|
215
|
+
The gate is an allowlist, not `NODE_ENV !== 'production'`, so unset, empty, `Production`, `prod` and `staging` all refuse. A mislabelled environment loses a development convenience rather than opening authentication.
|
|
216
|
+
|
|
217
|
+
The predicate ships as a public export so consumers can drop their own copy:
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
import { isIntrospectionBypassAllowed } from '@axiumine/koa-utils/lib/isIntrospectionBypassAllowed'
|
|
221
|
+
|
|
222
|
+
if (isIntrospectionBypassAllowed()) {
|
|
223
|
+
// mount the introspection-friendly route
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
211
227
|
## TLS / cookies
|
|
212
228
|
|
|
213
229
|
`tokenOptions` sets `secure: false` by design. Flip it at the reverse proxy (Nginx / Caddy / CloudFront), not in source.
|
|
@@ -100,7 +100,26 @@ SendResponse {
|
|
|
100
100
|
sendSubscriptionEmail(emailTo: string, otp: string): Promise<boolean>;
|
|
101
101
|
sendResetPwdConfirmation(emailTo: string, name?: string): Promise<boolean>;
|
|
102
102
|
sendResetPwdConfirmationHash(email: string, name: string, hash: string): Promise<boolean>;
|
|
103
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Send the password-reset link.
|
|
105
|
+
*
|
|
106
|
+
* `linkBase` and `linkPath` are here for the same reason they are on {@link sendEmailVerify}, and the
|
|
107
|
+
* pair has to move together: an account that confirmed its address on one front end and then resets its
|
|
108
|
+
* password on another has been sent to two different sites for two halves of the same login. Both
|
|
109
|
+
* default to the previous behaviour — `APP_DOMAIN` and `/x/reset` — so no existing caller changes.
|
|
110
|
+
*
|
|
111
|
+
* ⚠️ Unlike the verification link, this one is consumed by a **front-end route**, not by a backend
|
|
112
|
+
* router: the page reads the email and the hash out of its own path and calls `updatePwd`. `linkPath`
|
|
113
|
+
* therefore names a route in whatever app `linkBase` serves, and the two have to agree with that app's
|
|
114
|
+
* router rather than with a service mount point.
|
|
115
|
+
*
|
|
116
|
+
* @param emailTo recipient, also embedded in the link (URI-encoded)
|
|
117
|
+
* @param hash the reset hash stored on the account
|
|
118
|
+
* @param name optional greeting name
|
|
119
|
+
* @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
|
|
120
|
+
* @param linkPath front-end route prefix that renders the new-password form; defaults to `/x/reset`
|
|
121
|
+
*/
|
|
122
|
+
sendEmailReset(emailTo: string, hash: string, name?: string, linkBase?: string, linkPath?: string): Promise<boolean>;
|
|
104
123
|
/**
|
|
105
124
|
* fallback footer
|
|
106
125
|
*/
|
|
@@ -256,10 +256,31 @@ SendResponse {
|
|
|
256
256
|
return false;
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Send the password-reset link.
|
|
261
|
+
*
|
|
262
|
+
* `linkBase` and `linkPath` are here for the same reason they are on {@link sendEmailVerify}, and the
|
|
263
|
+
* pair has to move together: an account that confirmed its address on one front end and then resets its
|
|
264
|
+
* password on another has been sent to two different sites for two halves of the same login. Both
|
|
265
|
+
* default to the previous behaviour — `APP_DOMAIN` and `/x/reset` — so no existing caller changes.
|
|
266
|
+
*
|
|
267
|
+
* ⚠️ Unlike the verification link, this one is consumed by a **front-end route**, not by a backend
|
|
268
|
+
* router: the page reads the email and the hash out of its own path and calls `updatePwd`. `linkPath`
|
|
269
|
+
* therefore names a route in whatever app `linkBase` serves, and the two have to agree with that app's
|
|
270
|
+
* router rather than with a service mount point.
|
|
271
|
+
*
|
|
272
|
+
* @param emailTo recipient, also embedded in the link (URI-encoded)
|
|
273
|
+
* @param hash the reset hash stored on the account
|
|
274
|
+
* @param name optional greeting name
|
|
275
|
+
* @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
|
|
276
|
+
* @param linkPath front-end route prefix that renders the new-password form; defaults to `/x/reset`
|
|
277
|
+
*/
|
|
278
|
+
async sendEmailReset(emailTo, hash, name = '', linkBase = this.linkBase, linkPath = '/x/reset') {
|
|
260
279
|
const subject = `Password reset for ${this.platformName}`;
|
|
261
280
|
const encodedEmail = encodeURI(emailTo);
|
|
262
|
-
const
|
|
281
|
+
const base = linkBase.replace(/\/+$/, '');
|
|
282
|
+
const path = `/${linkPath.replace(/^\/+/, '')}`;
|
|
283
|
+
const link = `${base}${path}/${encodedEmail}/${hash}`;
|
|
263
284
|
const linkHtml = this.StringObj.makeLink(link);
|
|
264
285
|
const nameFixed = this.fixName(name);
|
|
265
286
|
const textBody = `Hi${nameFixed}, you or someone else has requested a password reset. You can change your login password
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IResetPwdMailer } from "../../../lib/access/resetPwdMailer.mjs";
|
|
1
2
|
import { TGetResetPwd } from "../../../private/lib/access/db/getResetPwd.mjs";
|
|
2
3
|
import { TSaveResetReq } from "../../../private/lib/access/db/saveResetReq.mjs";
|
|
3
4
|
import { GraphQLNonNull } from 'graphql';
|
|
@@ -9,6 +10,15 @@ export interface IResetPwdArgs {
|
|
|
9
10
|
export interface IResetPwdDeps {
|
|
10
11
|
getResetPwd: TGetResetPwd;
|
|
11
12
|
saveResetReq: TSaveResetReq;
|
|
13
|
+
/**
|
|
14
|
+
* Where the link in the mail points. Defaults to SocketLabs on `APP_DOMAIN`, which is what every
|
|
15
|
+
* caller got before the option existed.
|
|
16
|
+
*
|
|
17
|
+
* Supply one when the process serves more than one front end: the link host is the only part of this
|
|
18
|
+
* flow that differs per audience, and it is not derivable from the account — two collections behind
|
|
19
|
+
* one service look identical by the time the resolver has an email and a hash.
|
|
20
|
+
*/
|
|
21
|
+
mailer?: IResetPwdMailer;
|
|
12
22
|
}
|
|
13
23
|
/**
|
|
14
24
|
* Take the user email and send an email with a link for change the password.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { socketLabsResetPwdMailer } from "../../../lib/access/resetPwdMailer.mjs";
|
|
2
2
|
import { checkEmailLen } from "../../../lib/checkEmailLen.mjs";
|
|
3
3
|
import { EMAIL_HASH_LEN } from "../../../lib/Constants.mjs";
|
|
4
4
|
import { DateLib } from "../../../lib/DateLib.mjs";
|
|
@@ -93,8 +93,8 @@ export const createResetPwdMutation = (deps) => ({
|
|
|
93
93
|
const mail = pendingMail;
|
|
94
94
|
if (mail !== null) {
|
|
95
95
|
try {
|
|
96
|
-
const
|
|
97
|
-
void
|
|
96
|
+
const mailer = deps.mailer ?? socketLabsResetPwdMailer;
|
|
97
|
+
void mailer.sendEmailReset(uEmail, mail.hash, mail.name).catch((e) => {
|
|
98
98
|
Sentry.captureException(e);
|
|
99
99
|
});
|
|
100
100
|
}
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { TResetPwdMutation } from '../../graphQL/schema/mutations/resetPwd.mjs';
|
|
2
2
|
import { TUpdatePasswordMutation } from '../../graphQL/schema/mutations/updatePassword.mjs';
|
|
3
3
|
import { IResetPwdPaths, TAccessModel } from './accessPaths.mjs';
|
|
4
|
+
import { IResetPwdMailer } from './resetPwdMailer.mjs';
|
|
4
5
|
/** What the factory needs: the account model, plus any path that differs from the default layout. */
|
|
5
6
|
export interface ICreateResetPwdFlowArgs {
|
|
6
7
|
model: TAccessModel;
|
|
7
8
|
paths?: Partial<IResetPwdPaths>;
|
|
9
|
+
/**
|
|
10
|
+
* Where the reset link points. Omitted means SocketLabs on `APP_DOMAIN`, the pre-existing behaviour.
|
|
11
|
+
*
|
|
12
|
+
* Pass `createResetPwdMailer(base, path)` when this process mails more than one audience. The flow is
|
|
13
|
+
* already per-model, so per-flow is the right granularity: the collection and the front end that
|
|
14
|
+
* serves it are the same choice made twice.
|
|
15
|
+
*/
|
|
16
|
+
mailer?: IResetPwdMailer;
|
|
8
17
|
}
|
|
9
18
|
/** The two mutations the flow exposes, bound to the model and paths passed in. */
|
|
10
19
|
export interface IResetPwdFlow {
|
|
@@ -30,4 +39,4 @@ export interface IResetPwdFlow {
|
|
|
30
39
|
* })
|
|
31
40
|
* ```
|
|
32
41
|
*/
|
|
33
|
-
export declare const createResetPwdFlow: ({ model, paths }: ICreateResetPwdFlowArgs) => IResetPwdFlow;
|
|
42
|
+
export declare const createResetPwdFlow: ({ model, paths, mailer }: ICreateResetPwdFlowArgs) => IResetPwdFlow;
|
|
@@ -24,13 +24,14 @@ import { resolveResetPwdPaths } from './accessPaths.mjs';
|
|
|
24
24
|
* })
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
export const createResetPwdFlow = ({ model, paths }) => {
|
|
27
|
+
export const createResetPwdFlow = ({ model, paths, mailer }) => {
|
|
28
28
|
const resolved = resolveResetPwdPaths(paths);
|
|
29
29
|
const getResetPwd = createGetResetPwd(model, resolved);
|
|
30
30
|
return {
|
|
31
31
|
resetPwd: createResetPwdMutation({
|
|
32
32
|
getResetPwd,
|
|
33
|
-
saveResetReq: createSaveResetReq(model, resolved)
|
|
33
|
+
saveResetReq: createSaveResetReq(model, resolved),
|
|
34
|
+
mailer
|
|
34
35
|
}),
|
|
35
36
|
updatePassword: createUpdatePasswordMutation({
|
|
36
37
|
getResetPwd,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one message the password-reset flow sends.
|
|
3
|
+
*
|
|
4
|
+
* A single-method interface looks like ceremony next to `IVerifyEmailMailer`'s six, and it is not: the
|
|
5
|
+
* link in this mail carries the host the account will finish the reset on, and the resolver that sends it
|
|
6
|
+
* has no way to know which of a deployment's front ends the account belongs to. Before this seam existed
|
|
7
|
+
* the host was `APP_DOMAIN`, read once in `SocketLabsLib`'s constructor — correct for a deployment with
|
|
8
|
+
* one front end, and wrong the moment a second one authenticates against the same backend, because every
|
|
9
|
+
* customer's reset link then points at the operator panel.
|
|
10
|
+
*
|
|
11
|
+
* Structural, so `SocketLabsLib` itself satisfies it and a test double is an object literal.
|
|
12
|
+
*
|
|
13
|
+
* The resolver never awaits the returned promise (see `createResetPwdMutation` for the three reasons), so
|
|
14
|
+
* an implementation must not rely on its rejection being observed by the caller — the flow attaches its
|
|
15
|
+
* own Sentry handler and nothing else looks at it.
|
|
16
|
+
*/
|
|
17
|
+
export interface IResetPwdMailer {
|
|
18
|
+
sendEmailReset(email: string, hash: string, name: string): Promise<unknown>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* SocketLabs on the default link base, constructed per call.
|
|
22
|
+
*
|
|
23
|
+
* Per call rather than once at module load for the reason `socketLabsVerifyEmailMailer` gives: the
|
|
24
|
+
* constructor reads `process.env`, and hoisting it to import time pins whatever the environment held when
|
|
25
|
+
* the module was first resolved.
|
|
26
|
+
*/
|
|
27
|
+
export declare const socketLabsResetPwdMailer: IResetPwdMailer;
|
|
28
|
+
/**
|
|
29
|
+
* A mailer that points the reset link at a specific host and route.
|
|
30
|
+
*
|
|
31
|
+
* The intended use is one call per audience: a deployment serving a customer site next to an operator
|
|
32
|
+
* panel builds one of these per front end and passes it to that tier's `createResetPwdFlow`. Both
|
|
33
|
+
* arguments are configuration rather than a pre-joined URL — `sendEmailReset` normalises a trailing slash
|
|
34
|
+
* on the base and a missing leading slash on the path.
|
|
35
|
+
*
|
|
36
|
+
* Both are optional so a caller can pass `process.env.SOMETHING` straight through: an unset variable
|
|
37
|
+
* arrives as `undefined` and the default takes over — `APP_DOMAIN` for the base, `/x/reset` for the path.
|
|
38
|
+
* Normalising that to `''` at the call site instead would produce a link with no host at all.
|
|
39
|
+
*
|
|
40
|
+
* ⚠️ `linkPath` names a **front-end route**, not a backend mount. It has to match the route that renders
|
|
41
|
+
* the new-password form in the app `linkBase` serves; there is nothing on the server side that would fail
|
|
42
|
+
* loudly if it does not, because the link is only ever followed by a person.
|
|
43
|
+
*/
|
|
44
|
+
export declare const createResetPwdMailer: (linkBase?: string, linkPath?: string) => IResetPwdMailer;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SocketLabsLib } from "../../email/SocketLabsLib.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* SocketLabs on the default link base, constructed per call.
|
|
4
|
+
*
|
|
5
|
+
* Per call rather than once at module load for the reason `socketLabsVerifyEmailMailer` gives: the
|
|
6
|
+
* constructor reads `process.env`, and hoisting it to import time pins whatever the environment held when
|
|
7
|
+
* the module was first resolved.
|
|
8
|
+
*/
|
|
9
|
+
export const socketLabsResetPwdMailer = {
|
|
10
|
+
sendEmailReset: (email, hash, name) => new SocketLabsLib().sendEmailReset(email, hash, name)
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A mailer that points the reset link at a specific host and route.
|
|
14
|
+
*
|
|
15
|
+
* The intended use is one call per audience: a deployment serving a customer site next to an operator
|
|
16
|
+
* panel builds one of these per front end and passes it to that tier's `createResetPwdFlow`. Both
|
|
17
|
+
* arguments are configuration rather than a pre-joined URL — `sendEmailReset` normalises a trailing slash
|
|
18
|
+
* on the base and a missing leading slash on the path.
|
|
19
|
+
*
|
|
20
|
+
* Both are optional so a caller can pass `process.env.SOMETHING` straight through: an unset variable
|
|
21
|
+
* arrives as `undefined` and the default takes over — `APP_DOMAIN` for the base, `/x/reset` for the path.
|
|
22
|
+
* Normalising that to `''` at the call site instead would produce a link with no host at all.
|
|
23
|
+
*
|
|
24
|
+
* ⚠️ `linkPath` names a **front-end route**, not a backend mount. It has to match the route that renders
|
|
25
|
+
* the new-password form in the app `linkBase` serves; there is nothing on the server side that would fail
|
|
26
|
+
* loudly if it does not, because the link is only ever followed by a person.
|
|
27
|
+
*/
|
|
28
|
+
export const createResetPwdMailer = (linkBase, linkPath) => ({
|
|
29
|
+
sendEmailReset: (email, hash, name) => new SocketLabsLib().sendEmailReset(email, hash, name, linkBase, linkPath)
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=resetPwdMailer.mjs.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* May the `x-introspectioncode` authentication bypass run in this process at all?
|
|
3
|
+
*
|
|
4
|
+
* True only when `process.env.NODE_ENV` is exactly `'development'` or `'test'`. Every other value
|
|
5
|
+
* — including unset, empty, `'Production'`, `'prod'` and `'staging'` — returns `false`.
|
|
6
|
+
*
|
|
7
|
+
* ⚠️ This is an allowlist, and it must stay one. Do not "simplify" it to
|
|
8
|
+
* `process.env.NODE_ENV !== 'production'`. The two forms differ only on unrecognised values, and
|
|
9
|
+
* that difference is the entire point: the negated form fails *open* on exactly the input most
|
|
10
|
+
* likely to be wrong. An unset or empty `NODE_ENV` is the ordinary failure of a container runtime,
|
|
11
|
+
* and `'Production'` / `'prod'` / `'staging'` are the ordinary failures of a deploy script — under
|
|
12
|
+
* the negated form every one of them enables an authentication bypass in production, silently.
|
|
13
|
+
* Under the allowlist an unrecognised value refuses, so a mislabelled environment loses a
|
|
14
|
+
* development convenience instead of opening authentication.
|
|
15
|
+
*
|
|
16
|
+
* Exported because consumers already maintain their own copy of this predicate: the bypass is only
|
|
17
|
+
* coherent while the library and the app agree on which environments are permitted to use it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isIntrospectionBypassAllowed(): boolean;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* May the `x-introspectioncode` authentication bypass run in this process at all?
|
|
3
|
+
*
|
|
4
|
+
* True only when `process.env.NODE_ENV` is exactly `'development'` or `'test'`. Every other value
|
|
5
|
+
* — including unset, empty, `'Production'`, `'prod'` and `'staging'` — returns `false`.
|
|
6
|
+
*
|
|
7
|
+
* ⚠️ This is an allowlist, and it must stay one. Do not "simplify" it to
|
|
8
|
+
* `process.env.NODE_ENV !== 'production'`. The two forms differ only on unrecognised values, and
|
|
9
|
+
* that difference is the entire point: the negated form fails *open* on exactly the input most
|
|
10
|
+
* likely to be wrong. An unset or empty `NODE_ENV` is the ordinary failure of a container runtime,
|
|
11
|
+
* and `'Production'` / `'prod'` / `'staging'` are the ordinary failures of a deploy script — under
|
|
12
|
+
* the negated form every one of them enables an authentication bypass in production, silently.
|
|
13
|
+
* Under the allowlist an unrecognised value refuses, so a mislabelled environment loses a
|
|
14
|
+
* development convenience instead of opening authentication.
|
|
15
|
+
*
|
|
16
|
+
* Exported because consumers already maintain their own copy of this predicate: the bypass is only
|
|
17
|
+
* coherent while the library and the app agree on which environments are permitted to use it.
|
|
18
|
+
*/
|
|
19
|
+
export function isIntrospectionBypassAllowed() {
|
|
20
|
+
const nodeEnv = process.env.NODE_ENV;
|
|
21
|
+
return nodeEnv === 'development' || nodeEnv === 'test';
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=isIntrospectionBypassAllowed.mjs.map
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Constant-time check of the `x-introspectioncode` header against INTROSPECTION_CODE.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Environment-gated first: unless `isIntrospectionBypassAllowed()` says this process runs under
|
|
5
|
+
* `NODE_ENV` `'development'` or `'test'`, this returns `false` before INTROSPECTION_CODE is read
|
|
6
|
+
* at all. A correctly configured secret plus a correct header still fails in production. That
|
|
7
|
+
* gate is an allowlist on purpose — see `isIntrospectionBypassAllowed` for why the negated
|
|
8
|
+
* `NODE_ENV !== 'production'` form is unsafe and must not be substituted here.
|
|
9
|
+
*
|
|
10
|
+
* The gate lives in this function rather than in the three middlewares that call it, so a direct
|
|
11
|
+
* caller of the primitive cannot reach an ungated comparison.
|
|
12
|
+
*
|
|
13
|
+
* Fails closed on the secret too: an unset or empty INTROSPECTION_CODE never matches, whatever the
|
|
14
|
+
* caller sends. The call sites previously compared against `${process.env.INTROSPECTION_CODE}`,
|
|
15
|
+
* which coerces an unset variable to the literal string 'undefined' — a client sending that exact
|
|
16
|
+
* header value satisfied the check with no secret at all.
|
|
8
17
|
*/
|
|
9
18
|
export declare const verifyIntrospectionCode: (headerValue: string | undefined) => boolean;
|
|
@@ -1,14 +1,27 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import { timingSafeEqual } from 'node:crypto';
|
|
3
|
+
import { isIntrospectionBypassAllowed } from "../../lib/isIntrospectionBypassAllowed.mjs";
|
|
3
4
|
/**
|
|
4
5
|
* Constant-time check of the `x-introspectioncode` header against INTROSPECTION_CODE.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* Environment-gated first: unless `isIntrospectionBypassAllowed()` says this process runs under
|
|
8
|
+
* `NODE_ENV` `'development'` or `'test'`, this returns `false` before INTROSPECTION_CODE is read
|
|
9
|
+
* at all. A correctly configured secret plus a correct header still fails in production. That
|
|
10
|
+
* gate is an allowlist on purpose — see `isIntrospectionBypassAllowed` for why the negated
|
|
11
|
+
* `NODE_ENV !== 'production'` form is unsafe and must not be substituted here.
|
|
12
|
+
*
|
|
13
|
+
* The gate lives in this function rather than in the three middlewares that call it, so a direct
|
|
14
|
+
* caller of the primitive cannot reach an ungated comparison.
|
|
15
|
+
*
|
|
16
|
+
* Fails closed on the secret too: an unset or empty INTROSPECTION_CODE never matches, whatever the
|
|
17
|
+
* caller sends. The call sites previously compared against `${process.env.INTROSPECTION_CODE}`,
|
|
18
|
+
* which coerces an unset variable to the literal string 'undefined' — a client sending that exact
|
|
19
|
+
* header value satisfied the check with no secret at all.
|
|
10
20
|
*/
|
|
11
21
|
export const verifyIntrospectionCode = (headerValue) => {
|
|
22
|
+
if (!isIntrospectionBypassAllowed()) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
12
25
|
const code = process.env.INTROSPECTION_CODE;
|
|
13
26
|
if (typeof code !== 'string' || code.length === 0) {
|
|
14
27
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axiumine/koa-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Koa Utils",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -72,6 +72,10 @@
|
|
|
72
72
|
"import": "./dist/lib/access/verifyEmailMailer.mjs",
|
|
73
73
|
"types": "./dist/lib/access/verifyEmailMailer.d.mts"
|
|
74
74
|
},
|
|
75
|
+
"./lib/access/resetPwdMailer": {
|
|
76
|
+
"import": "./dist/lib/access/resetPwdMailer.mjs",
|
|
77
|
+
"types": "./dist/lib/access/resetPwdMailer.d.mts"
|
|
78
|
+
},
|
|
75
79
|
"./lib/MongoDB/IOnboarding": {
|
|
76
80
|
"import": "./dist/lib/MongoDB/IOnboarding.mjs",
|
|
77
81
|
"types": "./dist/lib/MongoDB/IOnboarding.d.mts"
|
|
@@ -608,6 +612,10 @@
|
|
|
608
612
|
"import": "./dist/lib/ICookies.mjs",
|
|
609
613
|
"types": "./dist/lib/ICookies.d.mts"
|
|
610
614
|
},
|
|
615
|
+
"./lib/isIntrospectionBypassAllowed": {
|
|
616
|
+
"import": "./dist/lib/isIntrospectionBypassAllowed.mjs",
|
|
617
|
+
"types": "./dist/lib/isIntrospectionBypassAllowed.d.mts"
|
|
618
|
+
},
|
|
611
619
|
"./lib/isSessionBlocked": {
|
|
612
620
|
"import": "./dist/lib/isSessionBlocked.mjs",
|
|
613
621
|
"types": "./dist/lib/isSessionBlocked.d.mts"
|