@blamejs/core 0.7.1 → 0.7.18
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 +423 -389
- package/README.md +150 -149
- package/bin/blamejs.js +0 -0
- package/index.js +308 -282
- package/lib/api-key.js +660 -672
- package/lib/api-snapshot.js +338 -338
- package/lib/app-shutdown.js +385 -385
- package/lib/app.js +365 -365
- package/lib/archive.js +250 -250
- package/lib/atomic-file.js +544 -544
- package/lib/audit-chain.js +177 -177
- package/lib/audit-sign.js +344 -344
- package/lib/audit-tools.js +677 -677
- package/lib/audit.js +766 -766
- package/lib/auth/jwt.js +311 -311
- package/lib/auth/lockout.js +436 -436
- package/lib/auth/oauth.js +721 -721
- package/lib/auth/passkey.js +181 -181
- package/lib/auth/password.js +594 -594
- package/lib/backup/bundle.js +217 -217
- package/lib/backup/crypto.js +176 -176
- package/lib/backup/index.js +515 -515
- package/lib/backup/manifest.js +282 -282
- package/lib/break-glass.js +1338 -1338
- package/lib/bundler.js +441 -441
- package/lib/cache-redis.js +256 -266
- package/lib/cache.js +1206 -1211
- package/lib/canonical-json.js +115 -115
- package/lib/chain-writer.js +234 -234
- package/lib/cli-helpers.js +206 -206
- package/lib/cli.js +2334 -2334
- package/lib/cluster-provider-db.js +317 -317
- package/lib/cluster-storage.js +226 -226
- package/lib/cluster.js +703 -703
- package/lib/codepoint-class.js +196 -0
- package/lib/config-drift.js +301 -301
- package/lib/consent.js +222 -222
- package/lib/constants.js +191 -191
- package/lib/cookies.js +315 -315
- package/lib/credential-hash.js +322 -322
- package/lib/crypto.js +266 -266
- package/lib/csv.js +275 -286
- package/lib/db-declare-row-policy.js +267 -267
- package/lib/db-declare-view.js +420 -421
- package/lib/db-query.js +406 -406
- package/lib/db-schema.js +319 -319
- package/lib/db.js +1288 -1288
- package/lib/deprecate.js +222 -222
- package/lib/dev.js +335 -335
- package/lib/dual-control.js +473 -473
- package/lib/error-page.js +420 -420
- package/lib/external-db-migrate.js +441 -441
- package/lib/external-db.js +1061 -1061
- package/lib/file-type.js +273 -273
- package/lib/file-upload.js +1136 -0
- package/lib/forms.js +422 -422
- package/lib/framework-error.js +293 -202
- package/lib/framework-schema.js +717 -717
- package/lib/gate-contract.js +971 -0
- package/lib/guard-all.js +405 -0
- package/lib/guard-archive.js +739 -0
- package/lib/guard-csv.js +816 -0
- package/lib/guard-email.js +744 -0
- package/lib/guard-filename.js +724 -0
- package/lib/guard-html.js +976 -0
- package/lib/guard-json.js +729 -0
- package/lib/guard-markdown.js +586 -0
- package/lib/guard-svg.js +976 -0
- package/lib/guard-xml.js +405 -0
- package/lib/guard-yaml.js +529 -0
- package/lib/handlers.js +350 -350
- package/lib/http-client-cookie-jar.js +508 -508
- package/lib/http-client.js +1195 -1195
- package/lib/i18n.js +878 -878
- package/lib/jobs.js +185 -185
- package/lib/log-stream-cloudwatch.js +369 -369
- package/lib/log-stream-local.js +146 -146
- package/lib/log-stream-otlp-grpc.js +410 -410
- package/lib/log-stream-otlp.js +286 -286
- package/lib/log-stream-syslog.js +302 -302
- package/lib/log-stream-webhook.js +199 -199
- package/lib/log-stream.js +330 -330
- package/lib/log.js +500 -500
- package/lib/mail-bounce.js +528 -528
- package/lib/mail-dkim.js +369 -362
- package/lib/mail.js +981 -962
- package/lib/metrics.js +683 -683
- package/lib/middleware/api-encrypt.js +936 -573
- package/lib/middleware/attach-user.js +157 -157
- package/lib/middleware/body-parser.js +1170 -1091
- package/lib/middleware/bot-guard.js +178 -178
- package/lib/middleware/compression.js +452 -452
- package/lib/middleware/cors.js +314 -314
- package/lib/middleware/csp-nonce.js +348 -348
- package/lib/middleware/csrf-protect.js +316 -316
- package/lib/middleware/db-role-for.js +264 -269
- package/lib/middleware/health.js +392 -392
- package/lib/middleware/index.js +79 -79
- package/lib/middleware/rate-limit.js +358 -358
- package/lib/middleware/request-id.js +61 -61
- package/lib/middleware/request-log.js +168 -168
- package/lib/middleware/require-auth.js +104 -104
- package/lib/middleware/security-headers.js +116 -116
- package/lib/middleware/sse.js +166 -166
- package/lib/migrations.js +383 -383
- package/lib/mtls-ca.js +518 -518
- package/lib/mtls-engine-default.js +481 -481
- package/lib/network-dns.js +632 -632
- package/lib/network-heartbeat.js +290 -290
- package/lib/network-nts.js +574 -574
- package/lib/network-proxy.js +265 -265
- package/lib/network-tls.js +328 -328
- package/lib/network.js +233 -233
- package/lib/notify.js +612 -614
- package/lib/ntp-check.js +229 -229
- package/lib/numeric-bounds.js +111 -91
- package/lib/object-store/azure-blob-bucket-ops.js +349 -349
- package/lib/object-store/azure-blob.js +488 -451
- package/lib/object-store/gcs-bucket-ops.js +351 -351
- package/lib/object-store/gcs.js +519 -479
- package/lib/object-store/http-put.js +153 -153
- package/lib/object-store/index.js +197 -197
- package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
- package/lib/object-store/sigv4.js +903 -855
- package/lib/observability.js +151 -151
- package/lib/otel-export.js +269 -269
- package/lib/pagination.js +464 -464
- package/lib/parsers/index.js +80 -80
- package/lib/parsers/safe-env.js +642 -642
- package/lib/parsers/safe-ini.js +292 -292
- package/lib/parsers/safe-toml.js +784 -784
- package/lib/parsers/safe-xml.js +390 -390
- package/lib/parsers/safe-yaml.js +1015 -1015
- package/lib/permissions.js +708 -708
- package/lib/pqc-agent.js +87 -87
- package/lib/pqc-gate.js +279 -279
- package/lib/protobuf-encoder.js +190 -190
- package/lib/protocol-dispatcher.js +161 -161
- package/lib/pubsub-redis.js +167 -177
- package/lib/pubsub.js +429 -429
- package/lib/queue-local.js +476 -476
- package/lib/queue-redis.js +745 -752
- package/lib/queue-sqs.js +319 -319
- package/lib/queue.js +695 -695
- package/lib/redis-client.js +519 -489
- package/lib/request-helpers.js +340 -336
- package/lib/restore-bundle.js +237 -237
- package/lib/restore-rollback.js +259 -259
- package/lib/restore.js +409 -409
- package/lib/retry.js +376 -376
- package/lib/router.js +748 -748
- package/lib/safe-async.js +735 -735
- package/lib/safe-buffer.js +237 -237
- package/lib/safe-json.js +541 -541
- package/lib/safe-schema.js +1266 -1266
- package/lib/safe-url.js +159 -159
- package/lib/scheduler.js +706 -706
- package/lib/security-assert.js +373 -373
- package/lib/seeders.js +618 -630
- package/lib/session.js +478 -478
- package/lib/slug.js +269 -269
- package/lib/ssrf-guard.js +401 -401
- package/lib/static.js +879 -114
- package/lib/storage.js +471 -471
- package/lib/subject.js +281 -281
- package/lib/template.js +791 -791
- package/lib/testing.js +798 -798
- package/lib/time.js +310 -310
- package/lib/totp.js +302 -302
- package/lib/tracing.js +494 -494
- package/lib/uuid.js +132 -132
- package/lib/validate-opts.js +340 -270
- package/lib/vault/index.js +308 -308
- package/lib/vault/rotate.js +784 -784
- package/lib/vault/wrap.js +296 -296
- package/lib/vendor/noble-ciphers.cjs +9 -9
- package/lib/webhook.js +595 -598
- package/lib/websocket.js +1048 -1048
- package/package.json +77 -77
- package/sbom.cyclonedx.json +7 -7
package/lib/guard-xml.js
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* guard-xml — XML content-safety primitive (b.guardXml).
|
|
4
|
+
*
|
|
5
|
+
* Threat catalog grounded in current research (XXE remains active in
|
|
6
|
+
* 2025-2026 despite 20+ years of awareness):
|
|
7
|
+
* - CVE-2026-24400 AssertJ XXE via toXmlDocument default parser
|
|
8
|
+
* - CVE-2025-3225 sitemap parser XXE
|
|
9
|
+
* - CVE-2024-1455 LangChain XXE
|
|
10
|
+
* - CVE-2024-25062 libxml2 use-after-free with DTD + XInclude
|
|
11
|
+
* - CVE-2024-56171 libxml2 schema use-after-free
|
|
12
|
+
* - CVE-2025-24928 libxml2 stack overflow on DTD validation
|
|
13
|
+
* - CVE-2025-32415 libxml2 schema heap under-read
|
|
14
|
+
* - CVE-2025-27113 libxml2 NULL deref in pattern.c
|
|
15
|
+
* - CVE-2024-8176 libexpat stack overflow (recursive entity expansion)
|
|
16
|
+
*
|
|
17
|
+
* var rv = b.guardXml.validate(input, { profile: "strict" });
|
|
18
|
+
* var safe = b.guardXml.sanitize(input, { profile: "balanced" });
|
|
19
|
+
* var g = b.guardXml.gate({ profile: "strict" });
|
|
20
|
+
*
|
|
21
|
+
* Threat catalog covered:
|
|
22
|
+
*
|
|
23
|
+
* 1. DOCTYPE declarations — refuse unconditionally regardless of
|
|
24
|
+
* profile. Catches billion-laughs entity expansion + external
|
|
25
|
+
* entity loading (XXE) + SYSTEM identifier exfil.
|
|
26
|
+
* 2. <!ENTITY> declarations including parameter entities (% prefix).
|
|
27
|
+
* 3. External entities — file:// / http:// / SYSTEM identifiers in
|
|
28
|
+
* DOCTYPE subset.
|
|
29
|
+
* 4. XInclude — <xi:include href="..."/> remote inclusion.
|
|
30
|
+
* 5. xsi:schemaLocation / xsi:noNamespaceSchemaLocation — operator-
|
|
31
|
+
* controlled schema fetch.
|
|
32
|
+
* 6. Processing instructions — <?xml-stylesheet ...?> CSS injection
|
|
33
|
+
* vector.
|
|
34
|
+
* 7. CDATA sections — often used to hide payloads from naive
|
|
35
|
+
* scanners.
|
|
36
|
+
* 8. XML signature wrapping (xmldsig) — surface that requires
|
|
37
|
+
* careful operator handling; flagged as audit.
|
|
38
|
+
* 9. Bidi / null / control / zero-width chars in element text +
|
|
39
|
+
* attribute values.
|
|
40
|
+
* 10. Anti-DoS caps — total document size, max element count, max
|
|
41
|
+
* attribute count per element, max depth, max attribute value
|
|
42
|
+
* length.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
var codepointClass = require("./codepoint-class");
|
|
46
|
+
var lazyRequire = require("./lazy-require");
|
|
47
|
+
var gateContract = require("./gate-contract");
|
|
48
|
+
var C = require("./constants");
|
|
49
|
+
var numericBounds = require("./numeric-bounds");
|
|
50
|
+
var { GuardXmlError } = require("./framework-error");
|
|
51
|
+
|
|
52
|
+
var observability = lazyRequire(function () { return require("./observability"); });
|
|
53
|
+
void observability;
|
|
54
|
+
|
|
55
|
+
var _err = GuardXmlError.factory;
|
|
56
|
+
|
|
57
|
+
// ---- Source-level threat detectors ----
|
|
58
|
+
|
|
59
|
+
var DOCTYPE_RE = /<!DOCTYPE\b/i;
|
|
60
|
+
var ENTITY_DECL_RE = /<!ENTITY\b/i;
|
|
61
|
+
var PARAM_ENTITY_RE = /<!ENTITY\s+%/i;
|
|
62
|
+
var EXTERNAL_ENTITY_RE = /\b(SYSTEM|PUBLIC)\s+["'](file|http|https|ftp|gopher|jar|netdoc):/i;
|
|
63
|
+
var XINCLUDE_RE = /<xi:include\b/i;
|
|
64
|
+
var SCHEMA_LOCATION_RE = /\bxsi:(noNamespace)?[Ss]chemaLocation\s*=/;
|
|
65
|
+
var PROCESSING_INSTR_RE = /<\?[A-Za-z][\w:-]*/;
|
|
66
|
+
var CDATA_RE = /<!\[CDATA\[/;
|
|
67
|
+
var XMLDSIG_RE = /<\w*:?Signature\b[^>]*xmldsig/i;
|
|
68
|
+
|
|
69
|
+
// ---- Profile presets ----
|
|
70
|
+
|
|
71
|
+
var PROFILES = Object.freeze({
|
|
72
|
+
"strict": {
|
|
73
|
+
doctypePolicy: "reject",
|
|
74
|
+
entityPolicy: "reject",
|
|
75
|
+
externalEntityPolicy: "reject",
|
|
76
|
+
xincludePolicy: "reject",
|
|
77
|
+
schemaLocationPolicy: "reject",
|
|
78
|
+
processingInstrPolicy: "reject",
|
|
79
|
+
cdataPolicy: "reject",
|
|
80
|
+
xmlDsigPolicy: "audit",
|
|
81
|
+
bidiPolicy: "reject",
|
|
82
|
+
controlPolicy: "reject",
|
|
83
|
+
nullBytePolicy: "reject",
|
|
84
|
+
zeroWidthPolicy: "reject",
|
|
85
|
+
maxBytes: C.BYTES.mib(2),
|
|
86
|
+
maxDepth: 64, // allow:raw-byte-literal — recursion depth, not byte size
|
|
87
|
+
maxElements: 8192, // allow:raw-byte-literal — element count cap, not byte size
|
|
88
|
+
maxAttrsPerElement: 64, // allow:raw-byte-literal — attr count, not byte size
|
|
89
|
+
maxAttrValueBytes: C.BYTES.kib(8),
|
|
90
|
+
},
|
|
91
|
+
"balanced": {
|
|
92
|
+
doctypePolicy: "reject", // DOCTYPE is XXE vector regardless
|
|
93
|
+
entityPolicy: "reject",
|
|
94
|
+
externalEntityPolicy: "reject",
|
|
95
|
+
xincludePolicy: "reject",
|
|
96
|
+
schemaLocationPolicy: "audit",
|
|
97
|
+
processingInstrPolicy: "audit",
|
|
98
|
+
cdataPolicy: "audit",
|
|
99
|
+
xmlDsigPolicy: "audit",
|
|
100
|
+
bidiPolicy: "strip",
|
|
101
|
+
controlPolicy: "strip",
|
|
102
|
+
nullBytePolicy: "strip",
|
|
103
|
+
zeroWidthPolicy: "strip",
|
|
104
|
+
maxBytes: C.BYTES.mib(8),
|
|
105
|
+
maxDepth: 256, // allow:raw-byte-literal — recursion depth, not byte size
|
|
106
|
+
maxElements: 65536, // allow:raw-byte-literal — element count cap, not byte size
|
|
107
|
+
maxAttrsPerElement: 128, // allow:raw-byte-literal — attr count, not byte size
|
|
108
|
+
maxAttrValueBytes: C.BYTES.kib(32),
|
|
109
|
+
},
|
|
110
|
+
"permissive": {
|
|
111
|
+
doctypePolicy: "reject", // billion-laughs class always
|
|
112
|
+
entityPolicy: "reject",
|
|
113
|
+
externalEntityPolicy: "reject",
|
|
114
|
+
xincludePolicy: "audit",
|
|
115
|
+
schemaLocationPolicy: "audit",
|
|
116
|
+
processingInstrPolicy: "audit",
|
|
117
|
+
cdataPolicy: "audit",
|
|
118
|
+
xmlDsigPolicy: "audit",
|
|
119
|
+
bidiPolicy: "audit",
|
|
120
|
+
controlPolicy: "strip",
|
|
121
|
+
nullBytePolicy: "reject",
|
|
122
|
+
zeroWidthPolicy: "strip",
|
|
123
|
+
maxBytes: C.BYTES.mib(64),
|
|
124
|
+
maxDepth: 1024, // allow:raw-byte-literal — recursion depth, not byte size
|
|
125
|
+
maxElements: 262144, // allow:raw-byte-literal — element count cap, not byte size
|
|
126
|
+
maxAttrsPerElement: 256, // allow:raw-byte-literal — attr count, not byte size
|
|
127
|
+
maxAttrValueBytes: C.BYTES.kib(64),
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
var DEFAULTS = Object.freeze(Object.assign({}, PROFILES["strict"], {
|
|
132
|
+
mode: "enforce",
|
|
133
|
+
maxRuntimeMs: C.TIME.seconds(10),
|
|
134
|
+
}));
|
|
135
|
+
|
|
136
|
+
var COMPLIANCE_POSTURES = Object.freeze({
|
|
137
|
+
"hipaa": Object.assign({}, PROFILES["strict"], {
|
|
138
|
+
forensicSnippetBytes: C.BYTES.bytes(256),
|
|
139
|
+
}),
|
|
140
|
+
"pci-dss": Object.assign({}, PROFILES["strict"], {
|
|
141
|
+
forensicSnippetBytes: C.BYTES.bytes(256),
|
|
142
|
+
}),
|
|
143
|
+
"gdpr": Object.assign({}, PROFILES["balanced"], {
|
|
144
|
+
forensicSnippetBytes: C.BYTES.bytes(128),
|
|
145
|
+
}),
|
|
146
|
+
"soc2-cc7": Object.assign({}, PROFILES["strict"], {
|
|
147
|
+
forensicSnippetBytes: C.BYTES.bytes(512),
|
|
148
|
+
}),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
function _resolveOpts(opts) {
|
|
152
|
+
return gateContract.resolveProfileAndPosture(opts, {
|
|
153
|
+
profiles: PROFILES,
|
|
154
|
+
compliancePostures: COMPLIANCE_POSTURES,
|
|
155
|
+
defaults: DEFAULTS,
|
|
156
|
+
errorClass: GuardXmlError,
|
|
157
|
+
errCodePrefix: "xml",
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function _detectIssues(input, opts) {
|
|
162
|
+
var issues = [];
|
|
163
|
+
if (typeof input !== "string") {
|
|
164
|
+
return [{ kind: "bad-input", severity: "high",
|
|
165
|
+
snippet: "input is not a string" }];
|
|
166
|
+
}
|
|
167
|
+
if (input.length > opts.maxBytes) {
|
|
168
|
+
return [{ kind: "too-large", severity: "high", ruleId: "xml.too-large",
|
|
169
|
+
snippet: "input " + input.length +
|
|
170
|
+
" bytes exceeds maxBytes " + opts.maxBytes }];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 1. DOCTYPE.
|
|
174
|
+
if (opts.doctypePolicy !== "allow" && DOCTYPE_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
175
|
+
issues.push({
|
|
176
|
+
kind: "doctype", severity: "critical", ruleId: "xml.doctype",
|
|
177
|
+
snippet: "DOCTYPE declaration (XXE / billion-laughs vector — " +
|
|
178
|
+
"CVE-2026-24400 / CVE-2024-25062 class)",
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// 2. <!ENTITY> declarations.
|
|
183
|
+
if (opts.entityPolicy !== "allow" && ENTITY_DECL_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
184
|
+
issues.push({
|
|
185
|
+
kind: "entity-declaration", severity: "critical",
|
|
186
|
+
ruleId: "xml.entity",
|
|
187
|
+
snippet: "<!ENTITY> declaration (entity-expansion DoS vector)",
|
|
188
|
+
});
|
|
189
|
+
if (PARAM_ENTITY_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
190
|
+
issues.push({
|
|
191
|
+
kind: "parameter-entity", severity: "critical",
|
|
192
|
+
ruleId: "xml.parameter-entity",
|
|
193
|
+
snippet: "parameter entity (% prefix) — out-of-band exfil vector",
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 3. External entity references.
|
|
199
|
+
if (opts.externalEntityPolicy !== "allow" && EXTERNAL_ENTITY_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
200
|
+
issues.push({
|
|
201
|
+
kind: "external-entity", severity: "critical",
|
|
202
|
+
ruleId: "xml.external-entity",
|
|
203
|
+
snippet: "SYSTEM/PUBLIC external entity reference (XXE — file:// / http:// exfil)",
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 4. XInclude.
|
|
208
|
+
if (opts.xincludePolicy !== "allow" && XINCLUDE_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
209
|
+
issues.push({
|
|
210
|
+
kind: "xinclude",
|
|
211
|
+
severity: opts.xincludePolicy === "reject" ? "critical" : "high",
|
|
212
|
+
ruleId: "xml.xinclude",
|
|
213
|
+
snippet: "<xi:include> remote inclusion (XXE-shaped — CVE-2024-25062 class)",
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 5. xsi:schemaLocation.
|
|
218
|
+
if (opts.schemaLocationPolicy !== "allow" && SCHEMA_LOCATION_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
219
|
+
issues.push({
|
|
220
|
+
kind: "schema-location",
|
|
221
|
+
severity: opts.schemaLocationPolicy === "reject" ? "high" : "warn",
|
|
222
|
+
ruleId: "xml.schema-location",
|
|
223
|
+
snippet: "xsi:schemaLocation — operator-controlled schema fetch",
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 6. Processing instructions.
|
|
228
|
+
if (opts.processingInstrPolicy !== "allow" && PROCESSING_INSTR_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
229
|
+
// Skip the standard `<?xml ... ?>` declaration at byte 0.
|
|
230
|
+
var trimmed = input.replace(/^\s*<\?xml\s[^?]*\?>/, "");
|
|
231
|
+
if (PROCESSING_INSTR_RE.test(trimmed)) { // allow:regex-no-length-cap — trimmed input bounded by maxBytes above
|
|
232
|
+
issues.push({
|
|
233
|
+
kind: "processing-instruction",
|
|
234
|
+
severity: opts.processingInstrPolicy === "reject" ? "critical" : "high",
|
|
235
|
+
ruleId: "xml.pi",
|
|
236
|
+
snippet: "XML processing instruction (e.g. xml-stylesheet — CSS injection vector)",
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 7. CDATA sections.
|
|
242
|
+
if (opts.cdataPolicy !== "allow" && CDATA_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
243
|
+
issues.push({
|
|
244
|
+
kind: "cdata",
|
|
245
|
+
severity: opts.cdataPolicy === "reject" ? "critical" : "warn",
|
|
246
|
+
ruleId: "xml.cdata",
|
|
247
|
+
snippet: "CDATA section (often hides payloads from naive scanners)",
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// 8. XML signature.
|
|
252
|
+
if (opts.xmlDsigPolicy !== "allow" && XMLDSIG_RE.test(input)) { // allow:regex-no-length-cap — input bounded by maxBytes above
|
|
253
|
+
issues.push({
|
|
254
|
+
kind: "xml-signature", severity: "warn",
|
|
255
|
+
ruleId: "xml.xmldsig",
|
|
256
|
+
snippet: "XML signature element — operator must guard against signature wrapping (xmldsig)",
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// 9. Codepoint-class threats.
|
|
261
|
+
issues.push.apply(issues, codepointClass.detectCharThreats(input, opts, "xml"));
|
|
262
|
+
|
|
263
|
+
// 10. Element + depth + attribute caps via tag count.
|
|
264
|
+
var openTags = (input.match(/<[A-Za-z][\w:-]*/g) || []).length;
|
|
265
|
+
if (openTags > opts.maxElements) {
|
|
266
|
+
issues.push({
|
|
267
|
+
kind: "element-cap", severity: "high",
|
|
268
|
+
ruleId: "xml.element-cap",
|
|
269
|
+
snippet: "element count " + openTags + " exceeds maxElements " + opts.maxElements,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
// Depth: count consecutive nested-open without close (rough estimate).
|
|
273
|
+
var depthEstimate = 0;
|
|
274
|
+
var maxDepthSeen = 0;
|
|
275
|
+
var i = 0;
|
|
276
|
+
while (i < input.length) {
|
|
277
|
+
var lt = input.indexOf("<", i);
|
|
278
|
+
if (lt === -1) break;
|
|
279
|
+
if (input.charAt(lt + 1) === "/") depthEstimate -= 1;
|
|
280
|
+
else if (input.charAt(lt + 1) !== "!" && input.charAt(lt + 1) !== "?") {
|
|
281
|
+
depthEstimate += 1;
|
|
282
|
+
if (depthEstimate > maxDepthSeen) maxDepthSeen = depthEstimate;
|
|
283
|
+
}
|
|
284
|
+
var gt = input.indexOf(">", lt);
|
|
285
|
+
if (gt === -1) break;
|
|
286
|
+
if (input.charAt(gt - 1) === "/") depthEstimate -= 1;
|
|
287
|
+
i = gt + 1;
|
|
288
|
+
}
|
|
289
|
+
if (maxDepthSeen > opts.maxDepth) {
|
|
290
|
+
issues.push({
|
|
291
|
+
kind: "depth-cap", severity: "high", ruleId: "xml.depth-cap",
|
|
292
|
+
snippet: "estimated nesting depth " + maxDepthSeen +
|
|
293
|
+
" exceeds maxDepth " + opts.maxDepth,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return issues;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ---- Public surface ----
|
|
301
|
+
|
|
302
|
+
function validate(input, opts) {
|
|
303
|
+
opts = _resolveOpts(opts);
|
|
304
|
+
numericBounds.requireAllPositiveFiniteIntIfPresent(opts,
|
|
305
|
+
["maxBytes", "maxDepth", "maxElements", "maxAttrsPerElement",
|
|
306
|
+
"maxAttrValueBytes"],
|
|
307
|
+
"guardXml.validate", GuardXmlError, "xml.bad-opt");
|
|
308
|
+
if (typeof input !== "string") {
|
|
309
|
+
return {
|
|
310
|
+
ok: false,
|
|
311
|
+
issues: [{ kind: "bad-input", severity: "high",
|
|
312
|
+
snippet: "input is not a string" }],
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
return gateContract.aggregateIssues(_detectIssues(input, opts));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function sanitize(input, opts) {
|
|
319
|
+
opts = _resolveOpts(opts);
|
|
320
|
+
if (typeof input !== "string") {
|
|
321
|
+
throw _err("xml.bad-input", "sanitize requires string input");
|
|
322
|
+
}
|
|
323
|
+
// XML sanitization — strip what's strip-able per policy. Critical
|
|
324
|
+
// shapes (DOCTYPE / ENTITY / external / parameter-entity) have no
|
|
325
|
+
// safe sanitization; throw.
|
|
326
|
+
var issues = _detectIssues(input, opts);
|
|
327
|
+
for (var i = 0; i < issues.length; i += 1) {
|
|
328
|
+
if (issues[i].severity === "critical") {
|
|
329
|
+
throw _err(issues[i].ruleId || "xml.refused",
|
|
330
|
+
"guardXml.sanitize: " + issues[i].snippet);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// Strip character-class threats per policy via the shared helper.
|
|
334
|
+
return codepointClass.applyCharStripPolicies(input, opts);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function gate(opts) {
|
|
338
|
+
opts = _resolveOpts(opts);
|
|
339
|
+
return gateContract.buildGuardGate(
|
|
340
|
+
opts.name || "guardXml:" + (opts.profile || "default"),
|
|
341
|
+
opts,
|
|
342
|
+
async function (ctx) {
|
|
343
|
+
var text = gateContract.extractBytesAsText(ctx);
|
|
344
|
+
if (!text) return { ok: true, action: "serve" };
|
|
345
|
+
var rv = validate(text, opts);
|
|
346
|
+
if (rv.issues.length === 0) return { ok: true, action: "serve" };
|
|
347
|
+
var hasCritical = rv.issues.some(function (i) {
|
|
348
|
+
return i.severity === "critical" || i.severity === "high";
|
|
349
|
+
});
|
|
350
|
+
if (!hasCritical) return { ok: true, action: "audit-only", issues: rv.issues };
|
|
351
|
+
|
|
352
|
+
// Sanitize-eligibility: every reject-policy off.
|
|
353
|
+
var canSanitize = opts.doctypePolicy !== "reject" &&
|
|
354
|
+
opts.entityPolicy !== "reject" &&
|
|
355
|
+
opts.externalEntityPolicy !== "reject";
|
|
356
|
+
if (canSanitize) {
|
|
357
|
+
try {
|
|
358
|
+
var clean = sanitize(text, opts);
|
|
359
|
+
return { ok: true, action: "sanitize",
|
|
360
|
+
sanitized: Buffer.from(clean, "utf8"),
|
|
361
|
+
issues: rv.issues };
|
|
362
|
+
} catch (_e) { /* fall through */ }
|
|
363
|
+
}
|
|
364
|
+
return { ok: false, action: "refuse", issues: rv.issues };
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
var buildProfile = gateContract.makeProfileBuilder(PROFILES);
|
|
369
|
+
|
|
370
|
+
function compliancePosture(name) {
|
|
371
|
+
return gateContract.lookupCompliancePosture(name, COMPLIANCE_POSTURES, _err, "xml");
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
var _xmlRulePacks = gateContract.makeRulePackLoader(GuardXmlError, "xml");
|
|
375
|
+
var loadRulePack = _xmlRulePacks.load;
|
|
376
|
+
|
|
377
|
+
module.exports = {
|
|
378
|
+
// ---- guard-* family registry exports ----
|
|
379
|
+
NAME: "xml",
|
|
380
|
+
KIND: "content",
|
|
381
|
+
MIME_TYPES: Object.freeze(["application/xml", "text/xml"]),
|
|
382
|
+
EXTENSIONS: Object.freeze([".xml"]),
|
|
383
|
+
INTEGRATION_FIXTURES: Object.freeze({
|
|
384
|
+
kind: "content",
|
|
385
|
+
contentType: "application/xml",
|
|
386
|
+
extension: ".xml",
|
|
387
|
+
benignBytes: Buffer.from('<?xml version="1.0"?><root><x>1</x></root>', "utf8"),
|
|
388
|
+
// Hostile: DOCTYPE with internal-subset entity declaration (XXE +
|
|
389
|
+
// billion-laughs vector — CVE-2026-24400 / CVE-2024-25062 class).
|
|
390
|
+
hostileBytes: Buffer.from(
|
|
391
|
+
'<?xml version="1.0"?>\n<!DOCTYPE root [<!ENTITY xx "yy">]>\n<root/>',
|
|
392
|
+
"utf8"),
|
|
393
|
+
}),
|
|
394
|
+
// ---- primitive surface ----
|
|
395
|
+
validate: validate,
|
|
396
|
+
sanitize: sanitize,
|
|
397
|
+
gate: gate,
|
|
398
|
+
buildProfile: buildProfile,
|
|
399
|
+
compliancePosture: compliancePosture,
|
|
400
|
+
loadRulePack: loadRulePack,
|
|
401
|
+
PROFILES: PROFILES,
|
|
402
|
+
DEFAULTS: DEFAULTS,
|
|
403
|
+
COMPLIANCE_POSTURES: COMPLIANCE_POSTURES,
|
|
404
|
+
GuardXmlError: GuardXmlError,
|
|
405
|
+
};
|