@axiumine/koa-utils 7.0.0 → 7.1.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 +24 -0
- package/dist/dataSources/Redis.mjs +13 -5
- package/dist/private/dataSources/isRedisTlsRequired.d.mts +19 -0
- package/dist/private/dataSources/isRedisTlsRequired.mjs +20 -0
- package/dist/private/dataSources/redisNodeUrl.d.mts +14 -0
- package/dist/private/dataSources/redisNodeUrl.mjs +16 -0
- package/dist/private/dataSources/resolveRedisUrl.d.mts +17 -0
- package/dist/private/dataSources/resolveRedisUrl.mjs +37 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -200,6 +200,7 @@ 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
|
+
| `REDIS_TLS` | Set to exactly `true` to require TLS: cluster nodes are built as `rediss://` and a non-`rediss://` `REDIS_URL` refuses to boot (see below). Any other value, unset included, keeps the previous plaintext behaviour |
|
|
203
204
|
| `INTROSPECTION_CODE` | Bypass header `x-introspectioncode` for schema introspection — **only honoured when `NODE_ENV` is `development` or `test`** (see below) |
|
|
204
205
|
| `SOCKETLABS_SERVER_ID`, `SOCKETLABS_SERVER_APIKEY` | SocketLabs auth |
|
|
205
206
|
| `PLATFORM_NAME`, `APP_DOMAIN`, `EMAIL_FROM`, `DEV_TEAM_EMAIL` | Email templating |
|
|
@@ -224,6 +225,29 @@ if (isIntrospectionBypassAllowed()) {
|
|
|
224
225
|
}
|
|
225
226
|
```
|
|
226
227
|
|
|
228
|
+
### Redis TLS — the `REDIS_TLS` flag
|
|
229
|
+
|
|
230
|
+
`REDIS_TLS` is opt-in and off by default: with the variable unset, `redisClient` connects exactly as it did before the flag existed, so upgrading changes nothing for an existing deployment.
|
|
231
|
+
|
|
232
|
+
Set it to the **exact** string `true` to require TLS. The match is deliberately exact — `TRUE`, `1`, `yes` and `on` are all read as *off*, because the alternatives (`Boolean(process.env.REDIS_TLS)`, `!== 'false'`) would force TLS on operators who wrote `false` or wrote nothing at all.
|
|
233
|
+
|
|
234
|
+
| | `REDIS_TLS` unset / any other value | `REDIS_TLS=true` |
|
|
235
|
+
| --- | --- | --- |
|
|
236
|
+
| Single client (`REDIS_URL`) | `REDIS_URL` used as-is, whatever its scheme | `REDIS_URL` **must** use the `rediss://` scheme, or the module throws at import time |
|
|
237
|
+
| Cluster (`REDIS_IS_CLUSTER=1`) | root nodes built as `redis://host:port`, `defaults.socket.tls` is `false` | root nodes built as `rediss://host:port`, `defaults.socket.tls` is `true` |
|
|
238
|
+
|
|
239
|
+
The single-client path fails closed rather than rewriting the scheme for you. A silent upgrade from `redis://` to `rediss://` would hide the misconfiguration until the TLS handshake failed, against a host that may not be the one you meant; refusing at startup turns it into one unmissable error instead. The three messages it can throw are:
|
|
240
|
+
|
|
241
|
+
```
|
|
242
|
+
[Redis] REDIS_TLS is enabled but REDIS_URL is not set — a rediss:// URL is required.
|
|
243
|
+
[Redis] REDIS_TLS is enabled but REDIS_URL is not a valid URL — a rediss:// URL is required.
|
|
244
|
+
[Redis] REDIS_TLS is enabled but REDIS_URL uses scheme "redis:" — rediss:// required.
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
The scheme is compared through `new URL().protocol`, the same way node-redis compares it, so an upper-case `REDISS://cache:6379` is accepted.
|
|
248
|
+
|
|
249
|
+
For the cluster path the `rediss://` scheme on `rootNodes` is not enough on its own: node-redis uses `rootNodes` only to discover the topology and does not inherit their TLS setting for the connections it then opens to the discovered nodes. `defaults.socket.tls` is what actually carries TLS across the cluster, and it is set from the same flag.
|
|
250
|
+
|
|
227
251
|
## TLS / cookies
|
|
228
252
|
|
|
229
253
|
`tokenOptions` sets `secure: false` by design. Flip it at the reverse proxy (Nginx / Caddy / CloudFront), not in source.
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { isRedisTlsRequired } from "../private/dataSources/isRedisTlsRequired.mjs";
|
|
2
|
+
import { redisNodeUrl } from "../private/dataSources/redisNodeUrl.mjs";
|
|
3
|
+
import { resolveRedisUrl } from "../private/dataSources/resolveRedisUrl.mjs";
|
|
1
4
|
import * as Sentry from '@sentry/node';
|
|
2
5
|
import * as dotenv from 'dotenv';
|
|
3
6
|
import { createClient, createCluster } from 'redis';
|
|
@@ -6,17 +9,22 @@ export const redisClient = process.env.REDIS_IS_CLUSTER === '1'
|
|
|
6
9
|
? /* c8 ignore start -- cluster branch: requires REDIS_IS_CLUSTER=1 + 3 redis nodes, not in unit env */
|
|
7
10
|
createCluster({
|
|
8
11
|
rootNodes: [
|
|
9
|
-
{ url:
|
|
10
|
-
{ url:
|
|
11
|
-
{ url:
|
|
12
|
+
{ url: redisNodeUrl(process.env.REDIS_DB1_HOST, process.env.REDIS_DB1_PORT) },
|
|
13
|
+
{ url: redisNodeUrl(process.env.REDIS_DB2_HOST, process.env.REDIS_DB2_PORT) },
|
|
14
|
+
{ url: redisNodeUrl(process.env.REDIS_DB3_HOST, process.env.REDIS_DB3_PORT) }
|
|
12
15
|
],
|
|
13
16
|
defaults: {
|
|
14
17
|
username: process.env.REDIS_USERNAME,
|
|
15
|
-
password: process.env.REDIS_PASSWORD
|
|
18
|
+
password: process.env.REDIS_PASSWORD,
|
|
19
|
+
// rootNodes only carry the topology-discovery connections; node-redis does not inherit their
|
|
20
|
+
// TLS setting for the connections it then opens to the discovered nodes. Without this the
|
|
21
|
+
// rediss:// scheme above would be a no-op and every real cluster connection would stay
|
|
22
|
+
// plaintext. See the rootNodes JSDoc in @redis/client's cluster/index.d.ts.
|
|
23
|
+
socket: isRedisTlsRequired() ? { tls: true } : { tls: false }
|
|
16
24
|
}
|
|
17
25
|
})
|
|
18
26
|
: /* c8 ignore stop */
|
|
19
|
-
createClient({ url: process.env.REDIS_URL });
|
|
27
|
+
createClient({ url: resolveRedisUrl(process.env.REDIS_URL) });
|
|
20
28
|
export const RedisConnect = async () => {
|
|
21
29
|
console.info('[Redis] Try connect to database... ');
|
|
22
30
|
redisClient.on('error', (e) => {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Does this process require a TLS Redis connection?
|
|
3
|
+
*
|
|
4
|
+
* True only when `process.env.REDIS_TLS` is exactly the string `'true'`. Every other value — unset,
|
|
5
|
+
* empty, `'1'`, `'TRUE'`, `'yes'` — returns `false` and leaves the plaintext `redis://` scheme in
|
|
6
|
+
* place, byte-for-byte the behaviour every deployment had before this flag existed.
|
|
7
|
+
*
|
|
8
|
+
* ⚠️ The match is exact, and the trade-off it makes is deliberate rather than an oversight. A
|
|
9
|
+
* misspelled value (`REDIS_TLS=TRUE`, `REDIS_TLS=1`) silently leaves the connection on plaintext
|
|
10
|
+
* instead of failing, which is the price of keeping the flag strictly opt-in: the accepted spelling
|
|
11
|
+
* is documented in `README.md`, and the gate is loud in the other direction — once the flag is on,
|
|
12
|
+
* a non-`rediss://` `REDIS_URL` refuses to boot at all. Do not widen this to a truthiness test: `Boolean(process.env.REDIS_TLS)` reads the literal string
|
|
13
|
+
* `'false'` as true, and `process.env.REDIS_TLS !== 'false'` enables TLS for an unset variable —
|
|
14
|
+
* both change the scheme of connections whose operator never asked for it.
|
|
15
|
+
*
|
|
16
|
+
* Read at call time, never captured in a module-level constant: the ESM module cache would freeze a
|
|
17
|
+
* constant at first import, and both callers here run at module load of `dataSources/Redis.mts`.
|
|
18
|
+
*/
|
|
19
|
+
export declare const isRedisTlsRequired: () => boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Does this process require a TLS Redis connection?
|
|
3
|
+
*
|
|
4
|
+
* True only when `process.env.REDIS_TLS` is exactly the string `'true'`. Every other value — unset,
|
|
5
|
+
* empty, `'1'`, `'TRUE'`, `'yes'` — returns `false` and leaves the plaintext `redis://` scheme in
|
|
6
|
+
* place, byte-for-byte the behaviour every deployment had before this flag existed.
|
|
7
|
+
*
|
|
8
|
+
* ⚠️ The match is exact, and the trade-off it makes is deliberate rather than an oversight. A
|
|
9
|
+
* misspelled value (`REDIS_TLS=TRUE`, `REDIS_TLS=1`) silently leaves the connection on plaintext
|
|
10
|
+
* instead of failing, which is the price of keeping the flag strictly opt-in: the accepted spelling
|
|
11
|
+
* is documented in `README.md`, and the gate is loud in the other direction — once the flag is on,
|
|
12
|
+
* a non-`rediss://` `REDIS_URL` refuses to boot at all. Do not widen this to a truthiness test: `Boolean(process.env.REDIS_TLS)` reads the literal string
|
|
13
|
+
* `'false'` as true, and `process.env.REDIS_TLS !== 'false'` enables TLS for an unset variable —
|
|
14
|
+
* both change the scheme of connections whose operator never asked for it.
|
|
15
|
+
*
|
|
16
|
+
* Read at call time, never captured in a module-level constant: the ESM module cache would freeze a
|
|
17
|
+
* constant at first import, and both callers here run at module load of `dataSources/Redis.mts`.
|
|
18
|
+
*/
|
|
19
|
+
export const isRedisTlsRequired = () => process.env.REDIS_TLS === 'true';
|
|
20
|
+
//# sourceMappingURL=isRedisTlsRequired.mjs.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds one cluster root-node URL — `redis://host:port`, or `rediss://host:port` when
|
|
3
|
+
* `isRedisTlsRequired()`.
|
|
4
|
+
*
|
|
5
|
+
* This lives in its own module so the scheme choice stays unit-tested. Its only call site is the
|
|
6
|
+
* `createCluster` arm of `dataSources/Redis.mts`, which is `c8 ignore`d wholesale because it needs
|
|
7
|
+
* `REDIS_IS_CLUSTER=1` plus three live nodes; interpolating the scheme inline there would park the
|
|
8
|
+
* one security-relevant decision of that arm outside coverage.
|
|
9
|
+
*
|
|
10
|
+
* `host` and `port` are interpolated exactly as the previous inline template did, `undefined`
|
|
11
|
+
* included — an unset `REDIS_DB1_HOST` still yields the literal `'undefined'` in the URL. Validating
|
|
12
|
+
* them is out of scope here and intentionally unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare const redisNodeUrl: (host: string | undefined, port: string | undefined) => string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isRedisTlsRequired } from './isRedisTlsRequired.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Builds one cluster root-node URL — `redis://host:port`, or `rediss://host:port` when
|
|
4
|
+
* `isRedisTlsRequired()`.
|
|
5
|
+
*
|
|
6
|
+
* This lives in its own module so the scheme choice stays unit-tested. Its only call site is the
|
|
7
|
+
* `createCluster` arm of `dataSources/Redis.mts`, which is `c8 ignore`d wholesale because it needs
|
|
8
|
+
* `REDIS_IS_CLUSTER=1` plus three live nodes; interpolating the scheme inline there would park the
|
|
9
|
+
* one security-relevant decision of that arm outside coverage.
|
|
10
|
+
*
|
|
11
|
+
* `host` and `port` are interpolated exactly as the previous inline template did, `undefined`
|
|
12
|
+
* included — an unset `REDIS_DB1_HOST` still yields the literal `'undefined'` in the URL. Validating
|
|
13
|
+
* them is out of scope here and intentionally unchanged.
|
|
14
|
+
*/
|
|
15
|
+
export const redisNodeUrl = (host, port) => `${isRedisTlsRequired() ? 'rediss' : 'redis'}://${host}:${port}`;
|
|
16
|
+
//# sourceMappingURL=redisNodeUrl.mjs.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the standalone-client connection URL, refusing a plaintext one when TLS is required.
|
|
3
|
+
*
|
|
4
|
+
* With `REDIS_TLS` off this is a pass-through: `REDIS_URL` reaches `createClient` untouched,
|
|
5
|
+
* `undefined` included, exactly as before the flag existed.
|
|
6
|
+
*
|
|
7
|
+
* With `REDIS_TLS=true` the URL must carry the `rediss:` scheme; anything else throws here, at
|
|
8
|
+
* module load of `dataSources/Redis.mts`, before a socket is ever opened. Fail-closed rather than a
|
|
9
|
+
* silent upgrade to `rediss://`: rewriting the scheme under an operator who wrote `redis://` hides
|
|
10
|
+
* the misconfiguration until the TLS handshake fails, against a host that may not be the intended
|
|
11
|
+
* one. Refusing at load turns it into a single unmissable startup error instead.
|
|
12
|
+
*
|
|
13
|
+
* The scheme is read through `new URL()` rather than `startsWith`, so it is compared the way
|
|
14
|
+
* node-redis compares it — `URL` lower-cases the protocol, which makes `REDISS://host:6379` a valid
|
|
15
|
+
* TLS URL both here and in `RedisClient.parseURL`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const resolveRedisUrl: (url: string | undefined) => string | undefined;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isRedisTlsRequired } from './isRedisTlsRequired.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the standalone-client connection URL, refusing a plaintext one when TLS is required.
|
|
4
|
+
*
|
|
5
|
+
* With `REDIS_TLS` off this is a pass-through: `REDIS_URL` reaches `createClient` untouched,
|
|
6
|
+
* `undefined` included, exactly as before the flag existed.
|
|
7
|
+
*
|
|
8
|
+
* With `REDIS_TLS=true` the URL must carry the `rediss:` scheme; anything else throws here, at
|
|
9
|
+
* module load of `dataSources/Redis.mts`, before a socket is ever opened. Fail-closed rather than a
|
|
10
|
+
* silent upgrade to `rediss://`: rewriting the scheme under an operator who wrote `redis://` hides
|
|
11
|
+
* the misconfiguration until the TLS handshake fails, against a host that may not be the intended
|
|
12
|
+
* one. Refusing at load turns it into a single unmissable startup error instead.
|
|
13
|
+
*
|
|
14
|
+
* The scheme is read through `new URL()` rather than `startsWith`, so it is compared the way
|
|
15
|
+
* node-redis compares it — `URL` lower-cases the protocol, which makes `REDISS://host:6379` a valid
|
|
16
|
+
* TLS URL both here and in `RedisClient.parseURL`.
|
|
17
|
+
*/
|
|
18
|
+
export const resolveRedisUrl = (url) => {
|
|
19
|
+
if (!isRedisTlsRequired()) {
|
|
20
|
+
return url;
|
|
21
|
+
}
|
|
22
|
+
if (typeof url !== 'string' || url.length === 0) {
|
|
23
|
+
throw new Error('[Redis] REDIS_TLS is enabled but REDIS_URL is not set — a rediss:// URL is required.');
|
|
24
|
+
}
|
|
25
|
+
let protocol;
|
|
26
|
+
try {
|
|
27
|
+
protocol = new URL(url).protocol;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
throw new Error('[Redis] REDIS_TLS is enabled but REDIS_URL is not a valid URL — a rediss:// URL is required.');
|
|
31
|
+
}
|
|
32
|
+
if (protocol !== 'rediss:') {
|
|
33
|
+
throw new Error(`[Redis] REDIS_TLS is enabled but REDIS_URL uses scheme "${protocol}" — rediss:// required.`);
|
|
34
|
+
}
|
|
35
|
+
return url;
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=resolveRedisUrl.mjs.map
|