@axiumine/koa-utils 5.9.0 → 7.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 CHANGED
@@ -148,11 +148,11 @@ All `throw*` helpers wrap `throwGraphQLError(status, title, description)` and yi
148
148
 
149
149
  ```ts
150
150
  import { initClamScan } from '@axiumine/koa-utils/files/scanVirus'
151
- import { uploadTemp } from '@axiumine/koa-utils/files/uploadTempImage'
151
+ import { uploadTempImage } from '@axiumine/koa-utils/files/uploadTempImage'
152
152
  import { uploadTempPdf } from '@axiumine/koa-utils/files/uploadTempPdf'
153
153
 
154
154
  await initClamScan() // once at boot
155
- const { tempFile, ext } = await uploadTemp(filePromise) // jpg/png → webp
155
+ const { tempFile, ext } = await uploadTempImage(filePromise) // jpg/png → webp
156
156
  ```
157
157
 
158
158
  Pipeline: stream to `/tmp` (5 MB cap) → extension + magic-number MIME check → ClamAV → sharp re-encode (strips EXIF). PDF path scans only (no re-encode).
@@ -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.
@@ -5,4 +5,4 @@ import { IUploadTemp } from './IUploadTemp.mjs';
5
5
  * @todo log uploaded file
6
6
  * @param img
7
7
  */
8
- export declare function uploadTemp(img: Promise<IFileUpload>): Promise<IUploadTemp>;
8
+ export declare function uploadTempImage(img: Promise<IFileUpload>): Promise<IUploadTemp>;
@@ -8,7 +8,7 @@ import { validateJpgPngMimeType } from './validateMimeTypeImages.mjs';
8
8
  * @todo log uploaded file
9
9
  * @param img
10
10
  */
11
- export async function uploadTemp(img) {
11
+ export async function uploadTempImage(img) {
12
12
  let storedTempFile = '';
13
13
  try {
14
14
  // Step 1 - Readme.md
@@ -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
- * Fails closed: an unset or empty INTROSPECTION_CODE never matches, whatever the caller sends.
5
- * The call sites previously compared against `${process.env.INTROSPECTION_CODE}`, which coerces
6
- * an unset variable to the literal string 'undefined' — a client sending that exact header value
7
- * satisfied the check with no secret at all.
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
- * Fails closed: an unset or empty INTROSPECTION_CODE never matches, whatever the caller sends.
7
- * The call sites previously compared against `${process.env.INTROSPECTION_CODE}`, which coerces
8
- * an unset variable to the literal string 'undefined' — a client sending that exact header value
9
- * satisfied the check with no secret at all.
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": "5.9.0",
3
+ "version": "7.0.0",
4
4
  "description": "Koa Utils",
5
5
  "type": "module",
6
6
  "repository": {
@@ -612,6 +612,10 @@
612
612
  "import": "./dist/lib/ICookies.mjs",
613
613
  "types": "./dist/lib/ICookies.d.mts"
614
614
  },
615
+ "./lib/isIntrospectionBypassAllowed": {
616
+ "import": "./dist/lib/isIntrospectionBypassAllowed.mjs",
617
+ "types": "./dist/lib/isIntrospectionBypassAllowed.d.mts"
618
+ },
615
619
  "./lib/isSessionBlocked": {
616
620
  "import": "./dist/lib/isSessionBlocked.mjs",
617
621
  "types": "./dist/lib/isSessionBlocked.d.mts"