mastercontroller 1.3.8 โ†’ 1.3.9

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.
@@ -15,7 +15,8 @@
15
15
  "Bash(node test-v1.3.4-fixes.js:*)",
16
16
  "Bash(npm install)",
17
17
  "Bash(node test-json-empty-body.js)",
18
- "Bash(npm install:*)"
18
+ "Bash(npm install:*)",
19
+ "Bash(node test-raw-body-preservation.js:*)"
19
20
  ],
20
21
  "deny": [],
21
22
  "ask": []
package/MasterRequest.js CHANGED
@@ -290,6 +290,8 @@ class MasterRequest{
290
290
 
291
291
  buffer += decoder.end();
292
292
  var buff = qs.parse(buffer);
293
+ // Preserve raw body for signature verification
294
+ buff._rawBody = buffer;
293
295
  func(buff);
294
296
  });
295
297
 
@@ -343,6 +345,10 @@ class MasterRequest{
343
345
 
344
346
  try {
345
347
  var buff = JSON.parse(buffer);
348
+ // IMPORTANT: Preserve raw body for webhook signature verification
349
+ // Many webhook providers (Stripe, GitHub, Shopify, etc.) require the
350
+ // exact raw body string to verify HMAC signatures
351
+ buff._rawBody = buffer;
346
352
  func(buff);
347
353
  } catch (e) {
348
354
  // Security: Don't fallback to qs.parse to avoid prototype pollution
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "scripts": {
20
20
  "test": "echo \"Error: no test specified\" && exit 1"
21
21
  },
22
- "version": "1.3.8"
22
+ "version": "1.3.9"
23
23
  }
@@ -0,0 +1,128 @@
1
+ // Test for raw body preservation - Critical for webhook signature verification
2
+
3
+ const crypto = require('crypto');
4
+ const EventEmitter = require('events');
5
+ const { MasterRequest } = require('./MasterRequest');
6
+
7
+ console.log('๐Ÿงช Testing Raw Body Preservation for Webhook Signatures...\n');
8
+
9
+ // Mock request
10
+ class MockRequest extends EventEmitter {
11
+ constructor(contentType) {
12
+ super();
13
+ this.headers = { 'content-type': contentType };
14
+ }
15
+
16
+ sendData(data) {
17
+ setImmediate(() => {
18
+ if (Array.isArray(data)) {
19
+ data.forEach(chunk => this.emit('data', Buffer.from(chunk)));
20
+ } else {
21
+ this.emit('data', Buffer.from(data));
22
+ }
23
+ this.emit('end');
24
+ });
25
+ }
26
+ }
27
+
28
+ // Test 1: JSON body with raw preservation
29
+ console.log('Test 1: JSON body preserves raw string...');
30
+ const masterReq1 = new MasterRequest();
31
+ masterReq1.init({ maxJsonSize: 1024 * 1024 });
32
+ const mockReq1 = new MockRequest('application/json');
33
+
34
+ const testPayload = '{"event":"payment.success","amount":1000,"currency":"USD"}';
35
+
36
+ masterReq1.jsonStream(mockReq1, (result) => {
37
+ if (result._rawBody === testPayload) {
38
+ console.log('โœ… Raw body preserved correctly');
39
+ console.log(' Parsed:', JSON.stringify(result));
40
+ console.log(' Raw:', result._rawBody);
41
+
42
+ // Test 2: Verify webhook signature (Stripe-style HMAC)
43
+ console.log('\nTest 2: Webhook signature verification...');
44
+ const secret = 'webhook_secret_key';
45
+ const signature = crypto
46
+ .createHmac('sha256', secret)
47
+ .update(result._rawBody)
48
+ .digest('hex');
49
+
50
+ // Verify signature
51
+ const verifySignature = crypto
52
+ .createHmac('sha256', secret)
53
+ .update(result._rawBody)
54
+ .digest('hex');
55
+
56
+ if (signature === verifySignature) {
57
+ console.log('โœ… Webhook signature verified successfully');
58
+ console.log(' Signature:', signature.substring(0, 20) + '...');
59
+
60
+ // Test 3: URL-encoded body
61
+ console.log('\nTest 3: URL-encoded body preserves raw string...');
62
+ const masterReq3 = new MasterRequest();
63
+ masterReq3.init({ maxBodySize: 1024 * 1024 });
64
+ const mockReq3 = new MockRequest('application/x-www-form-urlencoded');
65
+
66
+ const formData = 'name=John+Doe&email=john%40example.com&amount=100';
67
+
68
+ masterReq3.urlEncodeStream(mockReq3, (result) => {
69
+ if (result._rawBody === formData) {
70
+ console.log('โœ… URL-encoded raw body preserved');
71
+ console.log(' Parsed:', JSON.stringify(result));
72
+ console.log(' Raw:', result._rawBody);
73
+
74
+ // Test 4: Chunked JSON (simulates streaming)
75
+ console.log('\nTest 4: Chunked data reassembly...');
76
+ const masterReq4 = new MasterRequest();
77
+ masterReq4.init({ maxJsonSize: 1024 * 1024 });
78
+ const mockReq4 = new MockRequest('application/json');
79
+
80
+ const chunks = ['{"big":"', 'payload","with":', '"multiple","chunks":', '123}'];
81
+ const fullPayload = chunks.join('');
82
+
83
+ masterReq4.jsonStream(mockReq4, (result) => {
84
+ if (result._rawBody === fullPayload) {
85
+ console.log('โœ… Chunked data reassembled correctly');
86
+ console.log(' Chunks sent:', chunks.length);
87
+ console.log(' Raw body length:', result._rawBody.length);
88
+ console.log(' Parsed:', JSON.stringify(result));
89
+
90
+ console.log('\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”');
91
+ console.log('โœจ ALL TESTS PASSED - Raw Body Preservation Works!');
92
+ console.log('โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”');
93
+ console.log('\nUse Cases Enabled:');
94
+ console.log('โœ… Stripe webhook signature verification');
95
+ console.log('โœ… GitHub webhook signature verification');
96
+ console.log('โœ… Shopify HMAC verification');
97
+ console.log('โœ… PayPal IPN verification');
98
+ console.log('โœ… Any cryptographic signature verification');
99
+ console.log('โœ… Content hashing (MD5, SHA256)');
100
+ console.log('โœ… Audit logging of exact payloads');
101
+ console.log('\nAccess raw body: request.body._rawBody');
102
+ } else {
103
+ console.log('โŒ Chunked data test failed');
104
+ console.log(' Expected:', fullPayload);
105
+ console.log(' Got:', result._rawBody);
106
+ }
107
+ });
108
+
109
+ mockReq4.sendData(chunks);
110
+ } else {
111
+ console.log('โŒ URL-encoded raw body test failed');
112
+ console.log(' Expected:', formData);
113
+ console.log(' Got:', result._rawBody);
114
+ }
115
+ });
116
+
117
+ mockReq3.sendData(formData);
118
+ } else {
119
+ console.log('โŒ Signature verification failed');
120
+ }
121
+ } else {
122
+ console.log('โŒ Raw body preservation failed');
123
+ console.log(' Expected:', testPayload);
124
+ console.log(' Got:', result._rawBody);
125
+ }
126
+ });
127
+
128
+ mockReq1.sendData(testPayload);