@blamejs/core 0.6.13 → 0.6.21
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 +8 -0
- package/NOTICE +16 -0
- package/README.md +27 -18
- package/index.js +12 -0
- package/lib/archive.js +8 -7
- package/lib/audit.js +4 -0
- package/lib/auth/password.js +449 -4
- package/lib/bundler.js +8 -8
- package/lib/cache.js +105 -20
- package/lib/cli.js +598 -4
- package/lib/config-drift.js +309 -0
- package/lib/crypto-field.js +37 -0
- package/lib/crypto.js +8 -0
- package/lib/db-query.js +21 -2
- package/lib/db.js +32 -2
- package/lib/dual-control.js +475 -0
- package/lib/file-type.js +265 -0
- package/lib/framework-schema.js +38 -6
- package/lib/http-client-cookie-jar.js +117 -17
- package/lib/http-client.js +81 -3
- package/lib/internal-sha1-hibp.js +34 -0
- package/lib/mail.js +5 -4
- package/lib/middleware/csp-nonce.js +7 -4
- package/lib/middleware/index.js +2 -0
- package/lib/middleware/network-allowlist.js +199 -0
- package/lib/network-dns.js +564 -0
- package/lib/network-heartbeat.js +290 -0
- package/lib/network-nts.js +552 -0
- package/lib/network-proxy.js +246 -0
- package/lib/network-tls.js +326 -0
- package/lib/network.js +233 -0
- package/lib/ntp-check.js +50 -4
- package/lib/object-store/azure-blob.js +16 -42
- package/lib/pagination.js +136 -76
- package/lib/parsers/index.js +16 -2
- package/lib/parsers/safe-ini.js +273 -0
- package/lib/permissions.js +223 -9
- package/lib/pqc-agent.js +4 -4
- package/lib/retention.js +439 -0
- package/lib/security-assert.js +368 -0
- package/lib/session.js +138 -8
- package/lib/ssrf-guard.js +9 -0
- package/lib/vault/index.js +3 -3
- package/lib/vendor/MANIFEST.json +12 -0
- package/lib/vendor/common-passwords-top-10000.txt +10000 -0
- package/package.json +3 -2
- package/sbom.cyclonedx.json +61 -0
package/lib/parsers/index.js
CHANGED
|
@@ -42,19 +42,33 @@
|
|
|
42
42
|
* opted in. Dev-tooling — production secrets should still
|
|
43
43
|
* come through the operator's secrets-management; this is
|
|
44
44
|
* the local-development convenience.
|
|
45
|
-
* ini —
|
|
45
|
+
* ini — INI / .gitconfig / systemd-unit / php.ini / tox.ini parser.
|
|
46
|
+
* Sections (incl. [parent.child] / [parent "child"] nesting),
|
|
47
|
+
* ; or # comments (inline + leading), single + double quoting
|
|
48
|
+
* with \n / \t / \\ / \" / \' escapes, boolean coercion
|
|
49
|
+
* (true/false/yes/no/on/off), decimal + hex integers + floats.
|
|
50
|
+
* Prototype-pollution defense (__proto__/constructor/prototype
|
|
51
|
+
* rejected); duplicate-key policy throws by default
|
|
52
|
+
* (onDuplicate: "first"|"last" opts in to silent
|
|
53
|
+
* shadowing); section + per-section key + value-bytes
|
|
54
|
+
* caps configurable via opts.
|
|
46
55
|
*
|
|
47
56
|
* Public API:
|
|
48
57
|
* parsers.xml.parse(input, opts?) → object
|
|
58
|
+
* parsers.ini.parse(input, opts?) → object
|
|
59
|
+
* parsers.toml.parse(input, opts?) → object
|
|
60
|
+
* parsers.yaml.parse(input, opts?) → object
|
|
61
|
+
* parsers.env.load(filepath, opts?) → { values, diff }
|
|
49
62
|
*
|
|
50
63
|
* (CSV moved to top-level `b.csv` in v0.5.17 — same surface unified.)
|
|
51
64
|
*
|
|
52
65
|
* Error types: each parser exports its own *SafeError class with .code
|
|
53
|
-
* matching the format (xml/..., toml/...).
|
|
66
|
+
* matching the format (xml/..., toml/..., ini/..., yaml/..., env/...).
|
|
54
67
|
*/
|
|
55
68
|
module.exports = {
|
|
56
69
|
xml: require("./safe-xml"),
|
|
57
70
|
toml: require("./safe-toml"),
|
|
58
71
|
yaml: require("./safe-yaml"),
|
|
59
72
|
env: require("./safe-env"),
|
|
73
|
+
ini: require("./safe-ini"),
|
|
60
74
|
};
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* INI parser — same security defaults as the framework's other parsers.
|
|
4
|
+
*
|
|
5
|
+
* INI is the Windows config-file format (also used by systemd unit
|
|
6
|
+
* files, .gitconfig, php.ini, tox.ini, and a long tail of operator
|
|
7
|
+
* tooling). The format has no formal spec — we implement the
|
|
8
|
+
* widely-compatible subset:
|
|
9
|
+
*
|
|
10
|
+
* - Sections: [section-name] -> object key
|
|
11
|
+
* - Subsections: [parent.child] / [parent "child"] -> nested object
|
|
12
|
+
* - Key-value: key = value / key: value
|
|
13
|
+
* - Comments: ; or # at the start of a line, or after a value
|
|
14
|
+
* - Quoted values: "double" / 'single' (escapes: \\ \" \' \n \t)
|
|
15
|
+
* - Booleans: true/false/yes/no/on/off (case-insensitive)
|
|
16
|
+
* - Numbers: decimal integers + floats; hex (0xABCD)
|
|
17
|
+
*
|
|
18
|
+
* Security defaults:
|
|
19
|
+
* - maxBytes: 1 MiB (operator override via opts.maxBytes)
|
|
20
|
+
* - maxSections: 500 (depth + breadth limit)
|
|
21
|
+
* - maxKeysPerSection: 1000
|
|
22
|
+
* - maxValueBytes: 64 KiB
|
|
23
|
+
* - prototype-pollution: keys named __proto__, constructor, prototype
|
|
24
|
+
* are rejected (would otherwise let an attacker poison Object.prototype
|
|
25
|
+
* when a downstream consumer uses bracket access)
|
|
26
|
+
* - Duplicate-key policy: opts.onDuplicate = "throw" (default) | "first" | "last".
|
|
27
|
+
* Silent overwrite (the bare-INI default) is OFF — operators almost
|
|
28
|
+
* never want a config error to silently shadow earlier values.
|
|
29
|
+
*
|
|
30
|
+
* Public API:
|
|
31
|
+
* parsers.ini.parse(input, opts?) -> object
|
|
32
|
+
* parsers.ini.IniSafeError (with .code matching ini/...)
|
|
33
|
+
*
|
|
34
|
+
* Validation tier: A (config-time throw) — every malformed input
|
|
35
|
+
* surfaces at parse time, never silently coerces to a usable shape.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
var { defineClass } = require("../framework-error");
|
|
39
|
+
|
|
40
|
+
var IniSafeError = defineClass("IniSafeError", { alwaysPermanent: true });
|
|
41
|
+
|
|
42
|
+
var DEFAULT_MAX_BYTES = 1024 * 1024;
|
|
43
|
+
var DEFAULT_MAX_SECTIONS = 500;
|
|
44
|
+
var DEFAULT_MAX_KEYS_SECTION = 1000;
|
|
45
|
+
var DEFAULT_MAX_VALUE_BYTES = 64 * 1024;
|
|
46
|
+
|
|
47
|
+
var FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
48
|
+
|
|
49
|
+
var TRUE_VALUES = new Set(["true", "yes", "on"]);
|
|
50
|
+
var FALSE_VALUES = new Set(["false", "no", "off"]);
|
|
51
|
+
|
|
52
|
+
function _err(code, message) { return new IniSafeError(code, message); }
|
|
53
|
+
|
|
54
|
+
function _stripComment(line) {
|
|
55
|
+
var inSingle = false, inDouble = false, escape = false;
|
|
56
|
+
for (var i = 0; i < line.length; i++) {
|
|
57
|
+
var c = line.charAt(i);
|
|
58
|
+
if (escape) { escape = false; continue; }
|
|
59
|
+
if (c === "\\" && (inSingle || inDouble)) { escape = true; continue; }
|
|
60
|
+
if (c === "\"" && !inSingle) { inDouble = !inDouble; continue; }
|
|
61
|
+
if (c === "'" && !inDouble) { inSingle = !inSingle; continue; }
|
|
62
|
+
if (!inSingle && !inDouble && (c === ";" || c === "#")) {
|
|
63
|
+
if (i === 0 || /\s/.test(line.charAt(i - 1))) {
|
|
64
|
+
return line.slice(0, i);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return line;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function _unquote(raw) {
|
|
72
|
+
var s = raw.trim();
|
|
73
|
+
if (s.length === 0) return s;
|
|
74
|
+
var first = s.charAt(0), last = s.charAt(s.length - 1);
|
|
75
|
+
if ((first === "\"" && last === "\"") || (first === "'" && last === "'")) {
|
|
76
|
+
if (s.length < 2) {
|
|
77
|
+
throw _err("ini/bad-quote", "unbalanced quote in value: " + JSON.stringify(s));
|
|
78
|
+
}
|
|
79
|
+
var inner = s.slice(1, -1);
|
|
80
|
+
var out = "";
|
|
81
|
+
var i = 0;
|
|
82
|
+
while (i < inner.length) {
|
|
83
|
+
var c = inner.charAt(i);
|
|
84
|
+
if (c === "\\" && i + 1 < inner.length) {
|
|
85
|
+
var next = inner.charAt(i + 1);
|
|
86
|
+
if (next === "\\") out += "\\";
|
|
87
|
+
else if (next === "n") out += "\n";
|
|
88
|
+
else if (next === "t") out += "\t";
|
|
89
|
+
else if (next === "r") out += "\r";
|
|
90
|
+
else if (next === "\"") out += "\"";
|
|
91
|
+
else if (next === "'") out += "'";
|
|
92
|
+
else throw _err("ini/bad-escape", "unknown escape sequence \\" + next);
|
|
93
|
+
i += 2;
|
|
94
|
+
} else {
|
|
95
|
+
out += c;
|
|
96
|
+
i += 1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
return s;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function _coerceValue(raw) {
|
|
105
|
+
if (raw.length === 0) return raw;
|
|
106
|
+
var first = raw.charAt(0);
|
|
107
|
+
if (first === "\"" || first === "'") return _unquote(raw);
|
|
108
|
+
var lower = raw.toLowerCase();
|
|
109
|
+
if (TRUE_VALUES.has(lower)) return true;
|
|
110
|
+
if (FALSE_VALUES.has(lower)) return false;
|
|
111
|
+
if (/^0x[0-9a-f]+$/i.test(raw)) {
|
|
112
|
+
var hex = parseInt(raw, 16);
|
|
113
|
+
if (!Number.isSafeInteger(hex)) {
|
|
114
|
+
throw _err("ini/value-out-of-range", "hex integer exceeds safe-integer range: " + raw);
|
|
115
|
+
}
|
|
116
|
+
return hex;
|
|
117
|
+
}
|
|
118
|
+
if (/^-?\d+$/.test(raw)) {
|
|
119
|
+
var n = Number(raw);
|
|
120
|
+
if (!Number.isSafeInteger(n)) {
|
|
121
|
+
throw _err("ini/value-out-of-range", "integer exceeds safe-integer range: " + raw);
|
|
122
|
+
}
|
|
123
|
+
return n;
|
|
124
|
+
}
|
|
125
|
+
if (/^-?\d+\.\d+([eE][+-]?\d+)?$/.test(raw) || /^-?\d+[eE][+-]?\d+$/.test(raw)) {
|
|
126
|
+
return Number(raw);
|
|
127
|
+
}
|
|
128
|
+
return _unquote(raw);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function _validateKey(name) {
|
|
132
|
+
if (FORBIDDEN_KEYS.has(name)) {
|
|
133
|
+
throw _err("ini/forbidden-key", "key '" + name + "' is reserved (prototype pollution defense)");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function _ensureSection(root, sectionPath) {
|
|
138
|
+
if (sectionPath.length === 0) return root;
|
|
139
|
+
var node = root;
|
|
140
|
+
for (var i = 0; i < sectionPath.length; i++) {
|
|
141
|
+
var seg = sectionPath[i];
|
|
142
|
+
_validateKey(seg);
|
|
143
|
+
if (Object.prototype.hasOwnProperty.call(node, seg)) {
|
|
144
|
+
var existing = node[seg];
|
|
145
|
+
if (typeof existing !== "object" || existing === null || Array.isArray(existing)) {
|
|
146
|
+
throw _err("ini/section-conflict",
|
|
147
|
+
"section path [" + sectionPath.join(".") + "] collides with existing scalar at '" + seg + "'");
|
|
148
|
+
}
|
|
149
|
+
node = existing;
|
|
150
|
+
} else {
|
|
151
|
+
var child = {};
|
|
152
|
+
node[seg] = child;
|
|
153
|
+
node = child;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return node;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function _parseSectionHeader(line) {
|
|
160
|
+
var inner = line.slice(1, line.lastIndexOf("]")).trim();
|
|
161
|
+
if (inner.length === 0) {
|
|
162
|
+
throw _err("ini/empty-section", "section header [] has no name");
|
|
163
|
+
}
|
|
164
|
+
var quotedMatch = /^([A-Za-z0-9._-]+)\s+"([^"\\]*(?:\\.[^"\\]*)*)"$/.exec(inner);
|
|
165
|
+
if (quotedMatch) {
|
|
166
|
+
return [quotedMatch[1], quotedMatch[2]];
|
|
167
|
+
}
|
|
168
|
+
var parts = inner.split(".");
|
|
169
|
+
for (var i = 0; i < parts.length; i++) {
|
|
170
|
+
if (parts[i].length === 0) {
|
|
171
|
+
throw _err("ini/bad-section", "section name has empty segment: " + JSON.stringify(inner));
|
|
172
|
+
}
|
|
173
|
+
if (!/^[A-Za-z0-9_-]+$/.test(parts[i])) {
|
|
174
|
+
throw _err("ini/bad-section",
|
|
175
|
+
"section segment must match [A-Za-z0-9_-]+ (got " + JSON.stringify(parts[i]) + ")");
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return parts;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function parse(input, opts) {
|
|
182
|
+
opts = opts || {};
|
|
183
|
+
var maxBytes = opts.maxBytes || DEFAULT_MAX_BYTES;
|
|
184
|
+
var maxSections = opts.maxSections || DEFAULT_MAX_SECTIONS;
|
|
185
|
+
var maxKeysPerSect = opts.maxKeysPerSection || DEFAULT_MAX_KEYS_SECTION;
|
|
186
|
+
var maxValueBytes = opts.maxValueBytes || DEFAULT_MAX_VALUE_BYTES;
|
|
187
|
+
var onDuplicate = opts.onDuplicate || "throw";
|
|
188
|
+
|
|
189
|
+
if (typeof input !== "string") {
|
|
190
|
+
throw _err("ini/bad-input", "ini.parse: input must be a string, got " + typeof input);
|
|
191
|
+
}
|
|
192
|
+
if (Buffer.byteLength(input, "utf8") > maxBytes) {
|
|
193
|
+
throw _err("ini/too-large",
|
|
194
|
+
"ini.parse: input exceeds " + maxBytes + " bytes");
|
|
195
|
+
}
|
|
196
|
+
if (onDuplicate !== "throw" && onDuplicate !== "first" && onDuplicate !== "last") {
|
|
197
|
+
throw _err("ini/bad-opt",
|
|
198
|
+
"ini.parse: onDuplicate must be 'throw' | 'first' | 'last', got " + JSON.stringify(onDuplicate));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
var root = {};
|
|
202
|
+
var currentSectionPath = [];
|
|
203
|
+
var currentSection = root;
|
|
204
|
+
var sectionCount = 0;
|
|
205
|
+
var keysInCurrentSection = 0;
|
|
206
|
+
|
|
207
|
+
var lines = input.split(/\r?\n/);
|
|
208
|
+
for (var li = 0; li < lines.length; li++) {
|
|
209
|
+
var raw = lines[li];
|
|
210
|
+
var stripped = _stripComment(raw).trim();
|
|
211
|
+
if (stripped.length === 0) continue;
|
|
212
|
+
|
|
213
|
+
if (stripped.charAt(0) === "[") {
|
|
214
|
+
if (stripped.charAt(stripped.length - 1) !== "]") {
|
|
215
|
+
throw _err("ini/bad-section", "section header at line " + (li + 1) + " missing closing ']'");
|
|
216
|
+
}
|
|
217
|
+
sectionCount += 1;
|
|
218
|
+
if (sectionCount > maxSections) {
|
|
219
|
+
throw _err("ini/too-many-sections",
|
|
220
|
+
"ini.parse: section count exceeds " + maxSections);
|
|
221
|
+
}
|
|
222
|
+
currentSectionPath = _parseSectionHeader(stripped);
|
|
223
|
+
currentSection = _ensureSection(root, currentSectionPath);
|
|
224
|
+
keysInCurrentSection = 0;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
var eqIdx = stripped.indexOf("=");
|
|
229
|
+
var coIdx = stripped.indexOf(":");
|
|
230
|
+
var sepIdx;
|
|
231
|
+
if (eqIdx === -1) sepIdx = coIdx;
|
|
232
|
+
else if (coIdx === -1) sepIdx = eqIdx;
|
|
233
|
+
else sepIdx = Math.min(eqIdx, coIdx);
|
|
234
|
+
if (sepIdx === -1) {
|
|
235
|
+
throw _err("ini/bad-line",
|
|
236
|
+
"line " + (li + 1) + " is neither blank nor key=value: " + JSON.stringify(stripped));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
var key = stripped.slice(0, sepIdx).trim();
|
|
240
|
+
var valueRaw = stripped.slice(sepIdx + 1).trim();
|
|
241
|
+
if (key.length === 0) {
|
|
242
|
+
throw _err("ini/empty-key", "line " + (li + 1) + " has empty key");
|
|
243
|
+
}
|
|
244
|
+
_validateKey(key);
|
|
245
|
+
if (Buffer.byteLength(valueRaw, "utf8") > maxValueBytes) {
|
|
246
|
+
throw _err("ini/value-too-large",
|
|
247
|
+
"line " + (li + 1) + " value exceeds " + maxValueBytes + " bytes");
|
|
248
|
+
}
|
|
249
|
+
keysInCurrentSection += 1;
|
|
250
|
+
if (keysInCurrentSection > maxKeysPerSect) {
|
|
251
|
+
throw _err("ini/too-many-keys",
|
|
252
|
+
"section [" + currentSectionPath.join(".") + "] exceeds " + maxKeysPerSect + " keys");
|
|
253
|
+
}
|
|
254
|
+
var value = _coerceValue(valueRaw);
|
|
255
|
+
|
|
256
|
+
if (Object.prototype.hasOwnProperty.call(currentSection, key)) {
|
|
257
|
+
if (onDuplicate === "throw") {
|
|
258
|
+
throw _err("ini/duplicate-key",
|
|
259
|
+
"section [" + currentSectionPath.join(".") + "] redefines key '" + key + "' " +
|
|
260
|
+
"(opt onDuplicate:'first' or 'last' to allow)");
|
|
261
|
+
}
|
|
262
|
+
if (onDuplicate === "first") continue;
|
|
263
|
+
}
|
|
264
|
+
currentSection[key] = value;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return root;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
module.exports = {
|
|
271
|
+
parse: parse,
|
|
272
|
+
IniSafeError: IniSafeError,
|
|
273
|
+
};
|
package/lib/permissions.js
CHANGED
|
@@ -117,7 +117,8 @@ function _validateScopePattern(scope, ctx) {
|
|
|
117
117
|
|
|
118
118
|
function _normalizeRoleEntry(name, entry) {
|
|
119
119
|
if (Array.isArray(entry)) {
|
|
120
|
-
return { extends: [], permissions: entry.slice(), dbRole: null
|
|
120
|
+
return { extends: [], permissions: entry.slice(), dbRole: null,
|
|
121
|
+
requireMfa: false, mfaWindowMs: null };
|
|
121
122
|
}
|
|
122
123
|
if (entry && typeof entry === "object") {
|
|
123
124
|
var ext = entry.extends || [];
|
|
@@ -146,9 +147,19 @@ function _normalizeRoleEntry(name, entry) {
|
|
|
146
147
|
}
|
|
147
148
|
dbRole = entry.dbRole;
|
|
148
149
|
}
|
|
149
|
-
|
|
150
|
+
var requireMfa = entry.requireMfa === true;
|
|
151
|
+
var mfaWindowMs = null;
|
|
152
|
+
if (entry.mfaWindowMs !== undefined && entry.mfaWindowMs !== null) {
|
|
153
|
+
if (typeof entry.mfaWindowMs !== "number" || !isFinite(entry.mfaWindowMs) || entry.mfaWindowMs <= 0) {
|
|
154
|
+
throw _err("BAD_ROLE",
|
|
155
|
+
"role '" + name + "': mfaWindowMs must be a positive finite number");
|
|
156
|
+
}
|
|
157
|
+
mfaWindowMs = entry.mfaWindowMs;
|
|
158
|
+
}
|
|
159
|
+
return { extends: ext.slice(), permissions: perms.slice(), dbRole: dbRole,
|
|
160
|
+
requireMfa: requireMfa, mfaWindowMs: mfaWindowMs };
|
|
150
161
|
}
|
|
151
|
-
throw _err("BAD_ROLE", "role '" + name + "' must be an array of scopes or { extends?, permissions, dbRole? }");
|
|
162
|
+
throw _err("BAD_ROLE", "role '" + name + "' must be an array of scopes or { extends?, permissions, dbRole?, requireMfa?, mfaWindowMs? }");
|
|
152
163
|
}
|
|
153
164
|
|
|
154
165
|
function _validateRoles(roles) {
|
|
@@ -279,6 +290,33 @@ function create(opts) {
|
|
|
279
290
|
var missingActorStatus = opts.missingActorStatus || DEFAULTS.missingActorStatus;
|
|
280
291
|
var responder = opts.responder || _defaultResponder;
|
|
281
292
|
|
|
293
|
+
// ABAC predicate registry. Each entry: scope-string → async predicate
|
|
294
|
+
// function (actor, context) → boolean. The middleware evaluates the
|
|
295
|
+
// predicate AFTER the RBAC scope check passes — so a route protected
|
|
296
|
+
// by `perms.require("orders.read")` first checks the actor has the
|
|
297
|
+
// orders:read scope, then (if the scope has a policy registered)
|
|
298
|
+
// evaluates the predicate with the actor + a per-request context
|
|
299
|
+
// built by the route's `context` middleware opt. ABAC + RBAC stack
|
|
300
|
+
// — a route needs to pass BOTH layers when both are configured.
|
|
301
|
+
var policies = {};
|
|
302
|
+
|
|
303
|
+
function policy(scope, predicate) {
|
|
304
|
+
_validateScopePattern(scope, "permissions.policy");
|
|
305
|
+
if (typeof predicate !== "function") {
|
|
306
|
+
throw _err("BAD_OPT", "permissions.policy: predicate must be a function (actor, context) -> bool");
|
|
307
|
+
}
|
|
308
|
+
if (policies[scope]) {
|
|
309
|
+
throw _err("DUPLICATE_POLICY", "permissions.policy: '" + scope + "' is already registered");
|
|
310
|
+
}
|
|
311
|
+
policies[scope] = predicate;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function _findPolicy(requestedScope) {
|
|
315
|
+
// Exact match wins; no wildcard expansion (a wildcard policy
|
|
316
|
+
// gating arbitrary scopes is too easy to misconfigure).
|
|
317
|
+
return policies[requestedScope] || null;
|
|
318
|
+
}
|
|
319
|
+
|
|
282
320
|
function _auditEmit(action, info) {
|
|
283
321
|
if (!audit) return;
|
|
284
322
|
if (info && info.outcome === "success" && !auditSuccess) return;
|
|
@@ -332,7 +370,7 @@ function create(opts) {
|
|
|
332
370
|
|
|
333
371
|
// Middleware factory. `mode` is "single" | "all" | "any"; `requested`
|
|
334
372
|
// is the scope or scope list. Throw at registration time on bad shape.
|
|
335
|
-
function _middleware(mode, requested) {
|
|
373
|
+
function _middleware(mode, requested, mwOpts) {
|
|
336
374
|
if (mode === "single") {
|
|
337
375
|
_validateScopePattern(requested, "permissions.require");
|
|
338
376
|
} else {
|
|
@@ -345,7 +383,31 @@ function create(opts) {
|
|
|
345
383
|
}
|
|
346
384
|
}
|
|
347
385
|
|
|
348
|
-
|
|
386
|
+
// Per-route MFA enforcement opts: { requireMfa, mfaWindowMs }.
|
|
387
|
+
// When set, the middleware blocks unless the actor's mfaAuthenticated
|
|
388
|
+
// flag is truthy AND (when mfaWindowMs is set) actor.mfaAt is fresher
|
|
389
|
+
// than (now - mfaWindowMs). The actor signal is operator-set: after
|
|
390
|
+
// a successful TOTP / passkey step-up, the route handler stamps
|
|
391
|
+
// req.user.mfaAuthenticated = true and req.user.mfaAt = Date.now().
|
|
392
|
+
mwOpts = mwOpts || {};
|
|
393
|
+
var routeRequireMfa = mwOpts.requireMfa === true;
|
|
394
|
+
var routeMfaWindowMs = null;
|
|
395
|
+
if (mwOpts.mfaWindowMs !== undefined && mwOpts.mfaWindowMs !== null) {
|
|
396
|
+
if (typeof mwOpts.mfaWindowMs !== "number" || !isFinite(mwOpts.mfaWindowMs) || mwOpts.mfaWindowMs <= 0) {
|
|
397
|
+
throw _err("BAD_OPT", "permissions middleware: mfaWindowMs must be a positive finite number");
|
|
398
|
+
}
|
|
399
|
+
routeMfaWindowMs = mwOpts.mfaWindowMs;
|
|
400
|
+
}
|
|
401
|
+
// ABAC context provider — operator-supplied function (req)→object.
|
|
402
|
+
// The function runs once per request, AFTER scope/MFA pass, BEFORE
|
|
403
|
+
// the policy predicate. Whatever it returns is passed to the
|
|
404
|
+
// policy as `context`. Async functions are awaited.
|
|
405
|
+
var contextProvider = mwOpts.context;
|
|
406
|
+
if (contextProvider !== undefined && typeof contextProvider !== "function") {
|
|
407
|
+
throw _err("BAD_OPT", "permissions middleware: context must be a function (req) -> object");
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return async function permissionsMiddleware(req, res, next) {
|
|
349
411
|
var actor = resolver(req);
|
|
350
412
|
if (!actor) {
|
|
351
413
|
// Diagnostic: the most common cause of a null actor is that
|
|
@@ -392,13 +454,164 @@ function create(opts) {
|
|
|
392
454
|
});
|
|
393
455
|
}
|
|
394
456
|
|
|
457
|
+
// MFA enforcement gate. Two sources of "this needs MFA":
|
|
458
|
+
// 1. Per-route opt: perms.require("scope", { requireMfa: true })
|
|
459
|
+
// 2. Per-role flag: a role spec with requireMfa:true that
|
|
460
|
+
// contributes to satisfying the requested scope
|
|
461
|
+
// Either source enabling MFA forces the gate. mfaWindowMs (per-route
|
|
462
|
+
// OR per-role, route wins on conflict) bounds freshness — without
|
|
463
|
+
// it, ANY past MFA stamp counts (which is too permissive for high-
|
|
464
|
+
// value routes; operators set a window like C.TIME.minutes(15)).
|
|
465
|
+
var enforceMfa = routeRequireMfa;
|
|
466
|
+
var enforceWindowMs = routeMfaWindowMs;
|
|
467
|
+
if (!enforceMfa) {
|
|
468
|
+
// Walk the actor's roles and check whether any role with
|
|
469
|
+
// requireMfa=true contributes a permission that matches the
|
|
470
|
+
// requested scope. If so, MFA is required regardless of the
|
|
471
|
+
// route-level opt.
|
|
472
|
+
var actorRoles = Array.isArray(actor.roles) ? actor.roles : [];
|
|
473
|
+
for (var ri = 0; ri < actorRoles.length; ri++) {
|
|
474
|
+
var rname = actorRoles[ri];
|
|
475
|
+
if (typeof rname !== "string") continue;
|
|
476
|
+
var rspec = roleTable[rname];
|
|
477
|
+
if (!rspec || !rspec.requireMfa) continue;
|
|
478
|
+
// Cheap match: if the role grants any scope that satisfies the
|
|
479
|
+
// requested scope (single mode) or any of the requested
|
|
480
|
+
// (all/any modes), MFA is required for this route.
|
|
481
|
+
var visited = new Set();
|
|
482
|
+
var roleScopes = [];
|
|
483
|
+
_expandOne(rname, roleTable, visited, roleScopes);
|
|
484
|
+
var roleMatches = false;
|
|
485
|
+
var requestedList = mode === "single" ? [requested] : requested;
|
|
486
|
+
outer: for (var rj = 0; rj < roleScopes.length; rj++) {
|
|
487
|
+
for (var rk = 0; rk < requestedList.length; rk++) {
|
|
488
|
+
if (match(roleScopes[rj], requestedList[rk])) {
|
|
489
|
+
roleMatches = true; break outer;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (roleMatches) {
|
|
494
|
+
enforceMfa = true;
|
|
495
|
+
if (enforceWindowMs === null && rspec.mfaWindowMs !== null) {
|
|
496
|
+
enforceWindowMs = rspec.mfaWindowMs;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (enforceMfa) {
|
|
503
|
+
var mfaOk = actor.mfaAuthenticated === true;
|
|
504
|
+
if (mfaOk && enforceWindowMs !== null) {
|
|
505
|
+
var mfaAt = typeof actor.mfaAt === "number" ? actor.mfaAt : 0;
|
|
506
|
+
if (Date.now() - mfaAt > enforceWindowMs) {
|
|
507
|
+
mfaOk = false;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
if (!mfaOk) {
|
|
511
|
+
_emitEvent("permissions.mfa_required", 1,
|
|
512
|
+
{ requested: _labelize(requested), mode: mode });
|
|
513
|
+
_auditEmit("permissions.mfa.required", {
|
|
514
|
+
actor: _actorAuditShape(actor, req),
|
|
515
|
+
resource: { kind: "permission", id: _labelize(requested) },
|
|
516
|
+
outcome: "denied",
|
|
517
|
+
reason: "mfa-required",
|
|
518
|
+
metadata: { mode: mode, windowMs: enforceWindowMs },
|
|
519
|
+
});
|
|
520
|
+
return responder(req, res, denyStatus, {
|
|
521
|
+
error: "mfa_required",
|
|
522
|
+
status: denyStatus,
|
|
523
|
+
requested: _labelize(requested),
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// ABAC layer fires for every requested scope that has a
|
|
529
|
+
// registered policy predicate. Single-mode evaluates the one
|
|
530
|
+
// scope; requireAll evaluates each scope's policy (every must
|
|
531
|
+
// pass); requireAny evaluates only the policies on scopes the
|
|
532
|
+
// actor's RBAC layer satisfied (so a failing policy on a scope
|
|
533
|
+
// the actor doesn't even hold doesn't leak the policy's
|
|
534
|
+
// existence). Each predicate failure short-circuits with a
|
|
535
|
+
// policy.deny audit row naming the failing scope.
|
|
536
|
+
var policyTargets = [];
|
|
537
|
+
if (mode === "single" && _findPolicy(requested)) {
|
|
538
|
+
policyTargets.push(requested);
|
|
539
|
+
} else if (mode === "all" || mode === "any") {
|
|
540
|
+
for (var pi = 0; pi < requested.length; pi++) {
|
|
541
|
+
if (_findPolicy(requested[pi])) {
|
|
542
|
+
if (mode === "any" && !check(actor, requested[pi])) continue;
|
|
543
|
+
policyTargets.push(requested[pi]);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
if (policyTargets.length > 0) {
|
|
548
|
+
var policyContext = null;
|
|
549
|
+
if (contextProvider) {
|
|
550
|
+
try {
|
|
551
|
+
policyContext = await contextProvider(req);
|
|
552
|
+
} catch (e) {
|
|
553
|
+
_emitEvent("permissions.policy_context_error", 1,
|
|
554
|
+
{ requested: _labelize(requested) });
|
|
555
|
+
_auditEmit("permissions.policy.error", {
|
|
556
|
+
actor: _actorAuditShape(actor, req),
|
|
557
|
+
resource: { kind: "permission", id: _labelize(requested) },
|
|
558
|
+
outcome: "failure",
|
|
559
|
+
reason: "context-provider-threw",
|
|
560
|
+
metadata: { error: (e && e.message) || String(e), mode: mode },
|
|
561
|
+
});
|
|
562
|
+
return responder(req, res, denyStatus, {
|
|
563
|
+
error: "policy_context_error",
|
|
564
|
+
status: denyStatus,
|
|
565
|
+
requested: _labelize(requested),
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
for (var pti = 0; pti < policyTargets.length; pti++) {
|
|
570
|
+
var thisScope = policyTargets[pti];
|
|
571
|
+
var pred = _findPolicy(thisScope);
|
|
572
|
+
var verdict;
|
|
573
|
+
try {
|
|
574
|
+
verdict = await pred(actor, policyContext);
|
|
575
|
+
} catch (e2) {
|
|
576
|
+
_emitEvent("permissions.policy_error", 1, { requested: thisScope });
|
|
577
|
+
_auditEmit("permissions.policy.error", {
|
|
578
|
+
actor: _actorAuditShape(actor, req),
|
|
579
|
+
resource: { kind: "permission", id: thisScope },
|
|
580
|
+
outcome: "failure",
|
|
581
|
+
reason: "predicate-threw",
|
|
582
|
+
metadata: { error: (e2 && e2.message) || String(e2), mode: mode },
|
|
583
|
+
});
|
|
584
|
+
return responder(req, res, denyStatus, {
|
|
585
|
+
error: "policy_error",
|
|
586
|
+
status: denyStatus,
|
|
587
|
+
requested: thisScope,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
if (verdict !== true) {
|
|
591
|
+
_emitEvent("permissions.policy_denied", 1, { requested: thisScope });
|
|
592
|
+
_auditEmit("permissions.policy.deny", {
|
|
593
|
+
actor: _actorAuditShape(actor, req),
|
|
594
|
+
resource: { kind: "permission", id: thisScope },
|
|
595
|
+
outcome: "failure",
|
|
596
|
+
reason: "policy-predicate-returned-falsy",
|
|
597
|
+
metadata: { mode: mode, scopeIndex: pti },
|
|
598
|
+
});
|
|
599
|
+
return responder(req, res, denyStatus, {
|
|
600
|
+
error: "policy_denied",
|
|
601
|
+
status: denyStatus,
|
|
602
|
+
requested: thisScope,
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
395
608
|
_emitEvent("permissions.check", 1,
|
|
396
609
|
{ outcome: "success", mode: mode });
|
|
397
610
|
_auditEmit("permissions.check.success", {
|
|
398
611
|
actor: _actorAuditShape(actor, req),
|
|
399
612
|
resource: { kind: "permission", id: _labelize(requested) },
|
|
400
613
|
outcome: "success",
|
|
401
|
-
metadata: { mode: mode },
|
|
614
|
+
metadata: { mode: mode, mfaEnforced: enforceMfa },
|
|
402
615
|
});
|
|
403
616
|
next();
|
|
404
617
|
};
|
|
@@ -443,9 +656,10 @@ function create(opts) {
|
|
|
443
656
|
}
|
|
444
657
|
|
|
445
658
|
return {
|
|
446
|
-
require: function (scope) { return _middleware("single", scope); },
|
|
447
|
-
requireAll: function (scopes) { return _middleware("all", scopes); },
|
|
448
|
-
requireAny: function (scopes) { return _middleware("any", scopes); },
|
|
659
|
+
require: function (scope, mwOpts) { return _middleware("single", scope, mwOpts); },
|
|
660
|
+
requireAll: function (scopes, mwOpts) { return _middleware("all", scopes, mwOpts); },
|
|
661
|
+
requireAny: function (scopes, mwOpts) { return _middleware("any", scopes, mwOpts); },
|
|
662
|
+
policy: policy,
|
|
449
663
|
check: check,
|
|
450
664
|
checkAll: checkAll,
|
|
451
665
|
checkAny: checkAny,
|
package/lib/pqc-agent.js
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
var https = require("node:https");
|
|
32
32
|
var http = require("node:http");
|
|
33
33
|
var C = require("./constants");
|
|
34
|
+
var networkTls = require("./network-tls");
|
|
34
35
|
|
|
35
36
|
// Defaults for connection pooling. These ARE overridable via opts —
|
|
36
37
|
// only the cryptographic posture (ecdhCurve / minVersion) is locked.
|
|
@@ -45,12 +46,11 @@ var DEFAULT_OPTS = {
|
|
|
45
46
|
function _buildAgentOpts(opts) {
|
|
46
47
|
opts = opts || {};
|
|
47
48
|
var merged = Object.assign({}, DEFAULT_OPTS, opts);
|
|
48
|
-
// Cryptographic posture cannot be relaxed via opts. Even if the
|
|
49
|
-
// operator passes ecdhCurve: 'P-256' or minVersion: 'TLSv1.2', the
|
|
50
|
-
// framework defaults win. This is deliberate: the primitive's whole
|
|
51
|
-
// value is that you can't accidentally ship a downgraded agent.
|
|
52
49
|
merged.ecdhCurve = C.TLS_GROUP_CURVE_STR;
|
|
53
50
|
merged.minVersion = "TLSv1.3";
|
|
51
|
+
if (networkTls && typeof networkTls.applyToContext === "function") {
|
|
52
|
+
merged = networkTls.applyToContext({ base: merged });
|
|
53
|
+
}
|
|
54
54
|
return merged;
|
|
55
55
|
}
|
|
56
56
|
|