mastercontroller 1.3.7 โ†’ 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.
@@ -13,7 +13,10 @@
13
13
  "Bash(node test-circular-dependency.js:*)",
14
14
  "Bash(/tmp/verify_fix.sh)",
15
15
  "Bash(node test-v1.3.4-fixes.js:*)",
16
- "Bash(npm install)"
16
+ "Bash(npm install)",
17
+ "Bash(node test-json-empty-body.js)",
18
+ "Bash(npm install:*)",
19
+ "Bash(node test-raw-body-preservation.js:*)"
17
20
  ],
18
21
  "deny": [],
19
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
 
@@ -335,8 +337,18 @@ class MasterRequest{
335
337
  request.on('end', () => {
336
338
  if (errorOccurred) return;
337
339
 
340
+ // Handle empty body (GET requests, etc.)
341
+ if (buffer.trim() === '') {
342
+ func({});
343
+ return;
344
+ }
345
+
338
346
  try {
339
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;
340
352
  func(buff);
341
353
  } catch (e) {
342
354
  // Security: Don't fallback to qs.parse to avoid prototype pollution
@@ -0,0 +1,2 @@
1
+ {"timestamp":"2026-01-16T04:37:33.476Z","sessionId":"1768538253474-z0w973cmg","level":"INFO","code":"MC_INFO_FRAMEWORK_START","message":"MasterController framework initializing","component":null,"file":null,"line":null,"route":null,"context":{"version":"1.0.247","nodeVersion":"v20.19.4","platform":"darwin","env":"development"},"stack":null,"originalError":null,"environment":"development","nodeVersion":"v20.19.4","platform":"darwin","memory":{"rss":46104576,"heapTotal":10027008,"heapUsed":5720488,"external":2111800,"arrayBuffers":65875},"uptime":0.022275917}
2
+ {"timestamp":"2026-01-16T04:37:33.477Z","sessionId":"1768538253474-z0w973cmg","level":"INFO","code":"MC_INFO_SECURITY_INITIALIZED","message":"Security features initialized","component":null,"file":null,"line":null,"route":null,"context":{"environment":"development","features":{"securityHeaders":true,"csp":true,"csrf":true,"rateLimit":true,"sessionSecurity":true}},"stack":null,"originalError":null,"environment":"development","nodeVersion":"v20.19.4","platform":"darwin","memory":{"rss":46186496,"heapTotal":10027008,"heapUsed":5729744,"external":2111840,"arrayBuffers":65875},"uptime":0.022643334}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "dependencies": {
3
- "qs" : "^6.14.1",
4
- "formidable": "^3.5.4",
3
+ "content-type": "^1.0.5",
5
4
  "cookie": "^1.1.1",
6
- "winston": "^3.19.0",
7
- "glob" :"^13.0.0"
5
+ "formidable": "^3.5.4",
6
+ "glob": "^13.0.0",
7
+ "qs": "^6.14.1",
8
+ "winston": "^3.19.0"
8
9
  },
9
10
  "description": "A class library that makes using the Master Framework a breeze",
10
11
  "homepage": "https://github.com/Tailor/MasterController#readme",
@@ -18,5 +19,5 @@
18
19
  "scripts": {
19
20
  "test": "echo \"Error: no test specified\" && exit 1"
20
21
  },
21
- "version": "1.3.7"
22
- }
22
+ "version": "1.3.9"
23
+ }
@@ -0,0 +1,76 @@
1
+ // Test for JSON parsing bug fix - empty body on GET requests
2
+
3
+ const http = require('http');
4
+ const EventEmitter = require('events');
5
+
6
+ console.log('๐Ÿงช Testing MasterRequest jsonStream with empty body...\n');
7
+
8
+ // Load MasterRequest
9
+ const { MasterRequest } = require('./MasterRequest');
10
+
11
+ // Create mock request with empty body (simulates GET request)
12
+ class MockRequest extends EventEmitter {
13
+ constructor() {
14
+ super();
15
+ this.headers = {
16
+ 'content-type': 'application/json'
17
+ };
18
+ }
19
+
20
+ simulateEmptyBody() {
21
+ // Simulate empty body - no data events, just end
22
+ setImmediate(() => {
23
+ this.emit('end');
24
+ });
25
+ }
26
+ }
27
+
28
+ // Test 1: Empty body should not throw error
29
+ console.log('Test 1: Empty body (GET request scenario)...');
30
+ const masterRequest1 = new MasterRequest();
31
+ masterRequest1.init({ maxJsonSize: 1024 * 1024 });
32
+ const mockReq1 = new MockRequest();
33
+
34
+ masterRequest1.jsonStream(mockReq1, (result) => {
35
+ if (result && typeof result === 'object' && !result.error) {
36
+ console.log('โœ… Empty body handled correctly');
37
+ console.log(' Result:', JSON.stringify(result));
38
+
39
+ // Test 2: Valid JSON body
40
+ console.log('\nTest 2: Valid JSON body...');
41
+ const masterRequest2 = new MasterRequest();
42
+ masterRequest2.init({ maxJsonSize: 1024 * 1024 });
43
+ const mockReq2 = new MockRequest();
44
+
45
+ masterRequest2.jsonStream(mockReq2, (result) => {
46
+ if (result && result.name === 'test' && result.value === 123) {
47
+ console.log('โœ… Valid JSON parsed correctly');
48
+ console.log(' Result:', JSON.stringify(result));
49
+
50
+ console.log('\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”');
51
+ console.log('โœจ ALL TESTS PASSED');
52
+ console.log('โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”');
53
+ console.log('\nFix verified:');
54
+ console.log('โœ… Empty body returns {} instead of throwing error');
55
+ console.log('โœ… Valid JSON still parses correctly');
56
+ console.log('โœ… No more "Unexpected end of JSON input" errors on GET requests');
57
+ } else {
58
+ console.log('โŒ Valid JSON parsing failed');
59
+ console.log(' Result:', JSON.stringify(result));
60
+ }
61
+ });
62
+
63
+ // Simulate valid JSON body
64
+ setImmediate(() => {
65
+ mockReq2.emit('data', Buffer.from('{"name":"test","value":123}'));
66
+ mockReq2.emit('end');
67
+ });
68
+
69
+ } else {
70
+ console.log('โŒ Empty body test failed');
71
+ console.log(' Result:', JSON.stringify(result));
72
+ }
73
+ });
74
+
75
+ // Start test
76
+ mockReq1.simulateEmptyBody();
@@ -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);