@blamejs/core 0.4.9 → 0.4.10
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 +1 -0
- package/lib/middleware/body-parser.js +150 -9
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,7 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.4.x
|
|
10
10
|
|
|
11
|
+
- **0.4.9** (2026-04-30) — Origin-Agent-Cluster + DNS-Prefetch-Control headers; b.auth.lockout primitive
|
|
11
12
|
- **0.4.8** (2026-04-30) — wiki SEO surface: per-page OG / Twitter / JSON-LD + sitemap.xml + robots.txt
|
|
12
13
|
- **0.4.7** (2026-04-30) — audit-fix the welcome page's "what's in the box" table
|
|
13
14
|
- **0.4.6** (2026-04-30) — wiki gets the brand-flare on every page + substantive content additions
|
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
* multipart/form-data → req.body = { field: value }
|
|
16
16
|
* req.files = [{ field, filename,
|
|
17
17
|
* mimeType, path, size, hash }]
|
|
18
|
+
* req.filesRejected = [{ field,
|
|
19
|
+
* filename, mimeType, code, message }]
|
|
18
20
|
*
|
|
19
21
|
* Multipart parses incrementally — file parts stream to a tmp dir
|
|
20
22
|
* rather than buffering in memory. Per-file + total-request size caps
|
|
@@ -42,6 +44,33 @@
|
|
|
42
44
|
* fieldCount: 100,
|
|
43
45
|
* fieldSize: 1024 * 1024,
|
|
44
46
|
* mimeAllowlist: ["image/jpeg", "image/png", "application/pdf"], // null = any
|
|
47
|
+
*
|
|
48
|
+
* // Per-part predicate. Runs after sanitization + MIME checks but
|
|
49
|
+
* // BEFORE the tmp file opens. Rejected parts are SKIPPED — the body
|
|
50
|
+
* // bytes are consumed (we still must scan past them to find the
|
|
51
|
+
* // next boundary) but never written to disk; the part metadata
|
|
52
|
+
* // lands in req.filesRejected. Surviving files appear in req.files
|
|
53
|
+
* // as usual. Sync only — async filtering goes in the route handler.
|
|
54
|
+
* fileFilter: function (part) {
|
|
55
|
+
* // part = { field, filename, mimeType, partHeaders }
|
|
56
|
+
* // return true / undefined → accept
|
|
57
|
+
* // return false → reject silently (entry in req.filesRejected)
|
|
58
|
+
* // return { reject: true, code, message } → reject with custom info
|
|
59
|
+
* return part.field === "avatar" && part.mimeType.startsWith("image/");
|
|
60
|
+
* },
|
|
61
|
+
*
|
|
62
|
+
* // Per-field overrides. maxBytes overrides global fileSize for file
|
|
63
|
+
* // parts and fieldSize for text parts. mimeTypes overrides the
|
|
64
|
+
* // global mimeAllowlist for the named field; other fields still
|
|
65
|
+
* // use the global list.
|
|
66
|
+
* fields: {
|
|
67
|
+
* avatar: { maxBytes: 2 * 1024 * 1024, mimeTypes: ["image/jpeg", "image/png"] },
|
|
68
|
+
* document: { maxBytes: 25 * 1024 * 1024 },
|
|
69
|
+
* },
|
|
70
|
+
*
|
|
71
|
+
* // When wired, fileFilter rejections emit body-parser.multipart.file_rejected
|
|
72
|
+
* // on the audit chain with the field, filename, mime, and reason.
|
|
73
|
+
* audit: b.audit,
|
|
45
74
|
* },
|
|
46
75
|
* // Stash the raw bytes for webhook-signature paths that need to
|
|
47
76
|
* // verify the wire bytes rather than the parsed shape.
|
|
@@ -128,6 +157,9 @@ var DEFAULTS = Object.freeze({
|
|
|
128
157
|
fieldCount: 100,
|
|
129
158
|
fieldSize: C.BYTES.mib(1),
|
|
130
159
|
mimeAllowlist: null,
|
|
160
|
+
fileFilter: null, // fn({ field, filename, mimeType, partHeaders }) → bool | { reject, code, message }
|
|
161
|
+
fields: null, // per-field overrides: { name: { maxBytes?, mimeTypes? } }
|
|
162
|
+
audit: null, // when wired, file-rejection emits an audit event
|
|
131
163
|
contentTypes: ["multipart/form-data"],
|
|
132
164
|
},
|
|
133
165
|
});
|
|
@@ -446,6 +478,7 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
446
478
|
|
|
447
479
|
var fields = {};
|
|
448
480
|
var files = [];
|
|
481
|
+
var filesRejected = [];
|
|
449
482
|
var totalRead = 0;
|
|
450
483
|
var fileCount = 0;
|
|
451
484
|
var fieldCount = 0;
|
|
@@ -455,6 +488,9 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
455
488
|
var fieldLimit = opts.fieldCount;
|
|
456
489
|
var fieldSize = opts.fieldSize;
|
|
457
490
|
var mimeAllowlist = Array.isArray(opts.mimeAllowlist) ? opts.mimeAllowlist : null;
|
|
491
|
+
var fileFilter = typeof opts.fileFilter === "function" ? opts.fileFilter : null;
|
|
492
|
+
var perField = (opts.fields && typeof opts.fields === "object") ? opts.fields : null;
|
|
493
|
+
var auditInst = (opts.audit && typeof opts.audit.safeEmit === "function") ? opts.audit : null;
|
|
458
494
|
|
|
459
495
|
var state = MP_INITIAL;
|
|
460
496
|
var pending = Buffer.alloc(0);
|
|
@@ -467,6 +503,10 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
467
503
|
var currentSize = 0;
|
|
468
504
|
var currentHash = null;
|
|
469
505
|
var currentBuf = null; // for fields (in-memory accumulator)
|
|
506
|
+
var currentDiscarded = false; // true when fileFilter rejected the part — body bytes are
|
|
507
|
+
// still consumed (we have to read past them to find the next
|
|
508
|
+
// boundary) but never written to disk.
|
|
509
|
+
var currentEffectiveLimit = 0; // per-field-or-global cap; recomputed at part start.
|
|
470
510
|
|
|
471
511
|
function _resetCurrent() {
|
|
472
512
|
currentHeaders = null;
|
|
@@ -478,6 +518,28 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
478
518
|
currentSize = 0;
|
|
479
519
|
currentHash = null;
|
|
480
520
|
currentBuf = null;
|
|
521
|
+
currentDiscarded = false;
|
|
522
|
+
currentEffectiveLimit = 0;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function _emitRejection(field, filename, mimeType, code, message) {
|
|
526
|
+
filesRejected.push({
|
|
527
|
+
field: field,
|
|
528
|
+
filename: filename,
|
|
529
|
+
mimeType: mimeType,
|
|
530
|
+
code: code,
|
|
531
|
+
message: message || null,
|
|
532
|
+
});
|
|
533
|
+
if (auditInst) {
|
|
534
|
+
try {
|
|
535
|
+
auditInst.safeEmit({
|
|
536
|
+
action: "body-parser.multipart.file_rejected",
|
|
537
|
+
outcome: "denied",
|
|
538
|
+
resource: { kind: "multipart.file", id: field + (filename ? ":" + filename : "") },
|
|
539
|
+
metadata: { field: field, filename: filename, mimeType: mimeType, code: code, message: message || null },
|
|
540
|
+
});
|
|
541
|
+
} catch (_e) { /* audit best-effort */ }
|
|
542
|
+
}
|
|
481
543
|
}
|
|
482
544
|
|
|
483
545
|
function _cleanup() {
|
|
@@ -527,7 +589,7 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
527
589
|
if (pending.length < 2) return;
|
|
528
590
|
if (pending[0] === 0x2d && pending[1] === 0x2d) { // "--"
|
|
529
591
|
state = MP_DONE;
|
|
530
|
-
done(null, { fields: fields, files: files });
|
|
592
|
+
done(null, { fields: fields, files: files, filesRejected: filesRejected });
|
|
531
593
|
return;
|
|
532
594
|
}
|
|
533
595
|
if (pending[0] === 0x0d && pending[1] === 0x0a) { // "\r\n"
|
|
@@ -581,7 +643,20 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
581
643
|
return;
|
|
582
644
|
}
|
|
583
645
|
currentMime = currentHeaders["content-type"] || "application/octet-stream";
|
|
584
|
-
|
|
646
|
+
// Per-field MIME allowlist takes precedence over the global one
|
|
647
|
+
// for this field; global applies to fields without an entry.
|
|
648
|
+
var fieldRule = perField ? perField[currentField] : null;
|
|
649
|
+
var perFieldMime = (fieldRule && Array.isArray(fieldRule.mimeTypes))
|
|
650
|
+
? fieldRule.mimeTypes : null;
|
|
651
|
+
if (perFieldMime) {
|
|
652
|
+
if (perFieldMime.indexOf(currentMime) === -1) {
|
|
653
|
+
done(new BodyParserError("body-parser/multipart-mime-not-allowed",
|
|
654
|
+
"multipart file '" + currentField + "' MIME '" + currentMime +
|
|
655
|
+
"' is not on the per-field allowlist",
|
|
656
|
+
true, 415));
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
} else if (mimeAllowlist && mimeAllowlist.indexOf(currentMime) === -1) {
|
|
585
660
|
done(new BodyParserError("body-parser/multipart-mime-not-allowed",
|
|
586
661
|
"multipart file MIME '" + currentMime + "' is not on the allowlist",
|
|
587
662
|
true, 415));
|
|
@@ -594,6 +669,43 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
594
669
|
true, 413));
|
|
595
670
|
return;
|
|
596
671
|
}
|
|
672
|
+
// Per-field cap overrides global fileSize for this field.
|
|
673
|
+
currentEffectiveLimit = (fieldRule && typeof fieldRule.maxBytes === "number")
|
|
674
|
+
? fieldRule.maxBytes : fileSize;
|
|
675
|
+
|
|
676
|
+
// fileFilter runs AFTER sanitize + MIME checks but BEFORE the
|
|
677
|
+
// tmp file opens. Synchronous so the parser can decide between
|
|
678
|
+
// disk-write and discard-bytes without buffering the part.
|
|
679
|
+
if (fileFilter) {
|
|
680
|
+
var filterVerdict;
|
|
681
|
+
try {
|
|
682
|
+
filterVerdict = fileFilter({
|
|
683
|
+
field: currentField,
|
|
684
|
+
filename: currentFilename,
|
|
685
|
+
mimeType: currentMime,
|
|
686
|
+
partHeaders: currentHeaders,
|
|
687
|
+
});
|
|
688
|
+
} catch (e) {
|
|
689
|
+
done(new BodyParserError("body-parser/multipart-file-filter-throw",
|
|
690
|
+
"fileFilter threw: " + ((e && e.message) || String(e)),
|
|
691
|
+
true, 500));
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
if (filterVerdict === false ||
|
|
695
|
+
(filterVerdict && typeof filterVerdict === "object" && filterVerdict.reject)) {
|
|
696
|
+
var rejCode = (filterVerdict && filterVerdict.code) || "fileFilter";
|
|
697
|
+
var rejMessage = (filterVerdict && filterVerdict.message) || null;
|
|
698
|
+
_emitRejection(currentField, currentFilename, currentMime, rejCode, rejMessage);
|
|
699
|
+
// Read past the body bytes (we still must find the next
|
|
700
|
+
// boundary) but never open a tmp file or push to req.files.
|
|
701
|
+
currentDiscarded = true;
|
|
702
|
+
fileCount--; // doesn't count toward the limit since it didn't land
|
|
703
|
+
currentSize = 0;
|
|
704
|
+
state = MP_BODY;
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
597
709
|
// Generate the tmp path — never derived from the
|
|
598
710
|
// operator-supplied filename.
|
|
599
711
|
var unique = nodeCrypto.randomBytes(16).toString("hex");
|
|
@@ -616,6 +728,10 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
616
728
|
true, 413));
|
|
617
729
|
return;
|
|
618
730
|
}
|
|
731
|
+
// Per-field cap overrides global fieldSize for text parts too.
|
|
732
|
+
var textFieldRule = perField ? perField[currentField] : null;
|
|
733
|
+
currentEffectiveLimit = (textFieldRule && typeof textFieldRule.maxBytes === "number")
|
|
734
|
+
? textFieldRule.maxBytes : fieldSize;
|
|
619
735
|
currentBuf = [];
|
|
620
736
|
currentSize = 0;
|
|
621
737
|
}
|
|
@@ -640,12 +756,27 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
640
756
|
}
|
|
641
757
|
if (emitLen > 0) {
|
|
642
758
|
var bodyChunk = pending.slice(0, emitLen);
|
|
643
|
-
if (
|
|
759
|
+
if (currentDiscarded) {
|
|
760
|
+
// fileFilter rejected this part — read past the bytes to find
|
|
761
|
+
// the next boundary but never write to disk. totalSize still
|
|
762
|
+
// applies as a per-request DoS guard.
|
|
763
|
+
totalRead += bodyChunk.length;
|
|
764
|
+
if (totalRead > totalSize) {
|
|
765
|
+
done(new BodyParserError("body-parser/multipart-total-too-large",
|
|
766
|
+
"multipart total request size exceeds totalSize (" + totalSize + ")",
|
|
767
|
+
true, 413));
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
} else if (currentFd !== null) {
|
|
644
771
|
// File part — write to disk.
|
|
645
772
|
currentSize += bodyChunk.length;
|
|
646
|
-
if (currentSize >
|
|
773
|
+
if (currentSize > currentEffectiveLimit) {
|
|
774
|
+
var perFieldFile = (perField && perField[currentField] &&
|
|
775
|
+
typeof perField[currentField].maxBytes === "number");
|
|
647
776
|
done(new BodyParserError("body-parser/multipart-file-too-large",
|
|
648
|
-
"multipart file '" + currentField + "' exceeds
|
|
777
|
+
"multipart file '" + currentField + "' exceeds " +
|
|
778
|
+
(perFieldFile ? "per-field maxBytes" : "fileSize") +
|
|
779
|
+
" (" + currentEffectiveLimit + ")",
|
|
649
780
|
true, 413));
|
|
650
781
|
return;
|
|
651
782
|
}
|
|
@@ -669,11 +800,15 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
669
800
|
}
|
|
670
801
|
currentHash.update(bodyChunk);
|
|
671
802
|
} else {
|
|
672
|
-
// Field part — buffer in memory up to
|
|
803
|
+
// Field part — buffer in memory up to per-field-or-global cap.
|
|
673
804
|
currentSize += bodyChunk.length;
|
|
674
|
-
if (currentSize >
|
|
805
|
+
if (currentSize > currentEffectiveLimit) {
|
|
806
|
+
var perFieldText = (perField && perField[currentField] &&
|
|
807
|
+
typeof perField[currentField].maxBytes === "number");
|
|
675
808
|
done(new BodyParserError("body-parser/multipart-field-too-large",
|
|
676
|
-
"multipart field '" + currentField + "' exceeds
|
|
809
|
+
"multipart field '" + currentField + "' exceeds " +
|
|
810
|
+
(perFieldText ? "per-field maxBytes" : "fieldSize") +
|
|
811
|
+
" (" + currentEffectiveLimit + ")",
|
|
677
812
|
true, 413));
|
|
678
813
|
return;
|
|
679
814
|
}
|
|
@@ -692,7 +827,10 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
692
827
|
// Consume the boundary delimiter; transition to AFTER_BD.
|
|
693
828
|
pending = pending.slice(boundaryDelimBuf.length);
|
|
694
829
|
// Finalize the current part.
|
|
695
|
-
if (
|
|
830
|
+
if (currentDiscarded) {
|
|
831
|
+
// fileFilter rejected — already recorded in filesRejected; no
|
|
832
|
+
// tmp file was opened, nothing to clean up here.
|
|
833
|
+
} else if (currentFd !== null) {
|
|
696
834
|
try { fs.closeSync(currentFd); } catch (_e) {}
|
|
697
835
|
currentFd = null;
|
|
698
836
|
files.push({
|
|
@@ -723,6 +861,8 @@ async function _parseMultipart(req, opts, ctParams) {
|
|
|
723
861
|
currentSize = 0;
|
|
724
862
|
currentHash = null;
|
|
725
863
|
currentBuf = null;
|
|
864
|
+
currentDiscarded = false;
|
|
865
|
+
currentEffectiveLimit = 0;
|
|
726
866
|
state = MP_AFTER_BD;
|
|
727
867
|
continue;
|
|
728
868
|
}
|
|
@@ -804,6 +944,7 @@ function create(opts) {
|
|
|
804
944
|
var mpResult = await _parseMultipart(req, multipartOpts, ct.params);
|
|
805
945
|
req.body = mpResult.fields;
|
|
806
946
|
req.files = mpResult.files;
|
|
947
|
+
req.filesRejected = mpResult.filesRejected || [];
|
|
807
948
|
// Cleanup tmp files when the response finishes / closes / errors,
|
|
808
949
|
// regardless of whether the handler returned cleanly. Operators
|
|
809
950
|
// who want to KEEP a file move it elsewhere inside the handler.
|