@lowdefy/node-utils 6.0.0 → 6.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.
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import { ConfigError } from '@lowdefy/errors';
|
|
17
|
+
import collectEnvironmentGuards from './collectEnvironmentGuards.js';
|
|
18
|
+
// Checks the current environment's guards against this process's environment variables. The build
|
|
19
|
+
// runs it before anything deploys, and the production server again at startup: an image built once
|
|
20
|
+
// can be started with different variables. Values are never echoed: a failure names the variable,
|
|
21
|
+
// not what it holds.
|
|
22
|
+
function checkEnvironmentGuards({ name, guards, configKey }) {
|
|
23
|
+
const failures = collectEnvironmentGuards({
|
|
24
|
+
guards
|
|
25
|
+
}).filter(({ variable, pattern })=>{
|
|
26
|
+
const value = process.env[variable];
|
|
27
|
+
return !type.isString(value) || !new RegExp(pattern).test(value);
|
|
28
|
+
});
|
|
29
|
+
if (failures.length === 0) return;
|
|
30
|
+
throw new ConfigError(`Environment "${name}" guards failed: ${failures.map(({ label, variable })=>type.isString(process.env[variable]) ? `${label} does not match its guard` : `${label} is not set (${variable})`).join('; ')}. Update the guard in config.environments.${name}.guards together with the value, or fix the value.`, {
|
|
31
|
+
configKey
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
export default checkEnvironmentGuards;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ // Guards pin an environment's secrets and environment variables to a pattern kept in code: the
|
|
16
|
+
// value must match the regular expression, so changing a guarded value (the prod database URI,
|
|
17
|
+
// say) takes a change to the deployment's variables AND a reviewed change to the app config — a
|
|
18
|
+
// second factor for deploy-time configuration. Checked at build, before anything deploys.
|
|
19
|
+
//
|
|
20
|
+
// guards:
|
|
21
|
+
// secrets: { MONGODB_URI: 'acme-prod\.a1b2c\.mongodb\.net' } # LOWDEFY_SECRET_<NAME>
|
|
22
|
+
// env: { BETTER_AUTH_URL: '^https://app\.acme\.com$' }
|
|
23
|
+
//
|
|
24
|
+
// Values are never echoed: a failure names the variable, not what it holds.
|
|
25
|
+
function collectEnvironmentGuards({ guards }) {
|
|
26
|
+
const checks = [];
|
|
27
|
+
Object.entries(guards?.secrets ?? {}).forEach(([name, pattern])=>{
|
|
28
|
+
if (name.startsWith('~')) return;
|
|
29
|
+
checks.push({
|
|
30
|
+
label: `secret "${name}"`,
|
|
31
|
+
variable: `LOWDEFY_SECRET_${name}`,
|
|
32
|
+
pattern
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
Object.entries(guards?.env ?? {}).forEach(([name, pattern])=>{
|
|
36
|
+
if (name.startsWith('~')) return;
|
|
37
|
+
checks.push({
|
|
38
|
+
label: `environment variable "${name}"`,
|
|
39
|
+
variable: name,
|
|
40
|
+
pattern
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
return checks;
|
|
44
|
+
}
|
|
45
|
+
export default collectEnvironmentGuards;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
const REDACTED = '[REDACTED]';
|
|
17
|
+
// Shorter values, like `true` or a port number, would shred unrelated text.
|
|
18
|
+
const MIN_SECRET_LENGTH = 8;
|
|
19
|
+
function collectStringLeaves(value, leaves) {
|
|
20
|
+
if (type.isString(value)) {
|
|
21
|
+
leaves.push(value);
|
|
22
|
+
return leaves;
|
|
23
|
+
}
|
|
24
|
+
if (type.isArray(value)) {
|
|
25
|
+
value.forEach((item)=>collectStringLeaves(item, leaves));
|
|
26
|
+
return leaves;
|
|
27
|
+
}
|
|
28
|
+
if (type.isObject(value)) {
|
|
29
|
+
Object.values(value).forEach((item)=>collectStringLeaves(item, leaves));
|
|
30
|
+
}
|
|
31
|
+
return leaves;
|
|
32
|
+
}
|
|
33
|
+
// Authors JSON-encode structured secrets and pick a leaf with `_json.parse`, so the leaf, not
|
|
34
|
+
// the whole encoded string, is what ends up in an error message.
|
|
35
|
+
function parseJsonLeaves(value) {
|
|
36
|
+
let parsed;
|
|
37
|
+
try {
|
|
38
|
+
parsed = JSON.parse(value);
|
|
39
|
+
} catch {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
if (!type.isObject(parsed) && !type.isArray(parsed)) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
return collectStringLeaves(parsed, []);
|
|
46
|
+
}
|
|
47
|
+
// A secret embedded in a larger base64 payload (a Basic auth header built from
|
|
48
|
+
// `user:password`) only matches the secret's own base64 when both start on the same 3-byte
|
|
49
|
+
// boundary. Encoding at each offset and keeping only the characters whose 6 bits fall wholly
|
|
50
|
+
// inside the secret's bytes gives a fragment that matches whatever surrounds it.
|
|
51
|
+
function base64Fragments(value) {
|
|
52
|
+
const bytes = Buffer.from(value, 'utf8');
|
|
53
|
+
const fragments = [];
|
|
54
|
+
[
|
|
55
|
+
0,
|
|
56
|
+
1,
|
|
57
|
+
2
|
|
58
|
+
].forEach((offset)=>{
|
|
59
|
+
const padded = Buffer.concat([
|
|
60
|
+
Buffer.alloc(offset),
|
|
61
|
+
bytes
|
|
62
|
+
]);
|
|
63
|
+
const start = Math.ceil(8 * offset / 6);
|
|
64
|
+
const end = Math.floor(8 * (offset + bytes.length) / 6);
|
|
65
|
+
[
|
|
66
|
+
'base64',
|
|
67
|
+
'base64url'
|
|
68
|
+
].forEach((encoding)=>{
|
|
69
|
+
fragments.push(padded.toString(encoding).replace(/=+$/, '').slice(start, end));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
return fragments;
|
|
73
|
+
}
|
|
74
|
+
function secretForms(value) {
|
|
75
|
+
return [
|
|
76
|
+
value,
|
|
77
|
+
encodeURIComponent(value),
|
|
78
|
+
JSON.stringify(value).slice(1, -1),
|
|
79
|
+
...base64Fragments(value)
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
function createSecretScrubber({ secrets, env = process.env }) {
|
|
83
|
+
const values = collectStringLeaves(secrets, []);
|
|
84
|
+
if (!type.isNone(env.CRON_SECRET)) values.push(env.CRON_SECRET);
|
|
85
|
+
if (!type.isNone(env.AUTH_SECRET)) values.push(env.AUTH_SECRET);
|
|
86
|
+
const expanded = values.flatMap((value)=>[
|
|
87
|
+
value,
|
|
88
|
+
...parseJsonLeaves(value)
|
|
89
|
+
]);
|
|
90
|
+
const secretValues = new Set(expanded.filter((value)=>value.length >= MIN_SECRET_LENGTH));
|
|
91
|
+
const forms = new Set();
|
|
92
|
+
secretValues.forEach((value)=>{
|
|
93
|
+
secretForms(value).forEach((form)=>{
|
|
94
|
+
if (form.length > 0) forms.add(form);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
// No secrets means no pattern: an empty alternation would match at every position.
|
|
98
|
+
if (forms.size === 0) {
|
|
99
|
+
return function scrub(value) {
|
|
100
|
+
return value;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// One alternation, so every log line and Sentry string takes a single pass however many
|
|
104
|
+
// secrets there are. Longest first, so a form is replaced whole before a shorter form it
|
|
105
|
+
// contains can match at the same position.
|
|
106
|
+
const pattern = new RegExp([
|
|
107
|
+
...forms
|
|
108
|
+
].sort((a, b)=>b.length - a.length).map((form)=>form.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'g');
|
|
109
|
+
return function scrub(value) {
|
|
110
|
+
if (!type.isString(value)) return value;
|
|
111
|
+
return value.replace(pattern, REDACTED);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export default createSecretScrubber;
|
package/dist/index.js
CHANGED
|
@@ -12,8 +12,11 @@
|
|
|
12
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
|
-
*/ import
|
|
15
|
+
*/ import checkEnvironmentGuards from './checkEnvironmentGuards.js';
|
|
16
|
+
import cleanDirectory from './cleanDirectory.js';
|
|
17
|
+
import collectEnvironmentGuards from './collectEnvironmentGuards.js';
|
|
16
18
|
import copyFileOrDirectory from './copyFileOrDirectory.js';
|
|
19
|
+
import createSecretScrubber from './createSecretScrubber.js';
|
|
17
20
|
import findAvailablePort from './findAvailablePort.js';
|
|
18
21
|
import getFileExtension, { getFileSubExtension } from './getFileExtension.js';
|
|
19
22
|
import getSecretsFromEnv from './getSecretsFromEnv.js';
|
|
@@ -23,4 +26,4 @@ import spawnProcess from './spawnProcess.js';
|
|
|
23
26
|
import readFile from './readFile.js';
|
|
24
27
|
import writeFile from './writeFile.js';
|
|
25
28
|
import writeFileIfChanged from './writeFileIfChanged.js';
|
|
26
|
-
export { cleanDirectory, copyFileOrDirectory, findAvailablePort, getFileExtension, getFileSubExtension, getSecretsFromEnv, installIfPackageJsonChanged, isPortAvailable, spawnProcess, readFile, writeFile, writeFileIfChanged };
|
|
29
|
+
export { checkEnvironmentGuards, cleanDirectory, collectEnvironmentGuards, copyFileOrDirectory, createSecretScrubber, findAvailablePort, getFileExtension, getFileSubExtension, getSecretsFromEnv, installIfPackageJsonChanged, isPortAvailable, spawnProcess, readFile, writeFile, writeFileIfChanged };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/node-utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"dist/*"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@lowdefy/errors": "6.
|
|
38
|
-
"@lowdefy/helpers": "6.
|
|
37
|
+
"@lowdefy/errors": "6.1.0",
|
|
38
|
+
"@lowdefy/helpers": "6.1.0",
|
|
39
39
|
"fs-extra": "11.3.4"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|