|
| 1 | +const assert = require('assert'); |
| 2 | +const http = require('http'); |
| 3 | + |
| 4 | +const bucket = 'test-bucket'; |
| 5 | +const objectKey = 'test-file.txt'; |
| 6 | + |
| 7 | +describe('malformed Date header:', () => { |
| 8 | + it('should return AccessDenied for bad date with x-amz-content-sha256 header', done => { |
| 9 | + const options = { |
| 10 | + hostname: 'localhost', |
| 11 | + port: 8000, |
| 12 | + path: `/${bucket}/${objectKey}`, |
| 13 | + method: 'GET', |
| 14 | + headers: { |
| 15 | + 'Date': 'BAD_DATE', |
| 16 | + 'Authorization': 'AWS4-HMAC-SHA256 Credential=accessKey1/20260211/us-east-1/s3/aws4_request, ' + |
| 17 | + 'SignedHeaders=host, Signature=d459d5b2a2395b4c65d8f8aa2729b22c5abb04614fafbd93ab4fe203e76d21a3', |
| 18 | + 'X-Amz-Content-Sha256': 'fa8d015f89da2a769d1cea7e3bd77a5670d098d7844cda148a40c1304e5b778b', |
| 19 | + 'Host': 'localhost:8000' |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + const req = http.request(options, res => { |
| 24 | + let body = ''; |
| 25 | + res.on('data', chunk => { |
| 26 | + body += chunk; |
| 27 | + }); |
| 28 | + res.on('end', () => { |
| 29 | + assert.strictEqual(res.statusCode, 403, 'Server should return 403 AccessDenied for malformed Date'); |
| 30 | + assert(body.includes('AccessDenied'), 'Response should contain AccessDenied'); |
| 31 | + assert(body.includes('Authentication requires a valid Date or x-amz-date header')); |
| 32 | + done(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + req.on('error', err => { |
| 37 | + // If we get ECONNRESET or similar, it means the server crashed |
| 38 | + assert.fail(`Server crashed or connection error: ${err.message}`); |
| 39 | + }); |
| 40 | + |
| 41 | + req.end(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return AccessDenied for bad x-amz-date with x-amz-content-sha256 header', done => { |
| 45 | + const options = { |
| 46 | + hostname: 'localhost', |
| 47 | + port: 8000, |
| 48 | + path: `/${bucket}/${objectKey}`, |
| 49 | + method: 'GET', |
| 50 | + headers: { |
| 51 | + 'X-Amz-Date': 'BAD_DATE', |
| 52 | + 'Authorization': 'AWS4-HMAC-SHA256 Credential=accessKey1/20260211/us-east-1/s3/aws4_request, ' + |
| 53 | + 'SignedHeaders=host;x-amz-date, ' + |
| 54 | + 'Signature=d459d5b2a2395b4c65d8f8aa2729b22c5abb04614fafbd93ab4fe203e76d21a3', |
| 55 | + 'X-Amz-Content-Sha256': 'fa8d015f89da2a769d1cea7e3bd77a5670d098d7844cda148a40c1304e5b778b', |
| 56 | + 'Host': 'localhost:8000' |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const req = http.request(options, res => { |
| 61 | + let body = ''; |
| 62 | + res.on('data', chunk => { |
| 63 | + body += chunk; |
| 64 | + }); |
| 65 | + res.on('end', () => { |
| 66 | + assert.strictEqual(res.statusCode, 403, 'Server should return 403 AccessDenied for malformed Date'); |
| 67 | + assert(body.includes('AccessDenied'), 'Response should contain AccessDenied'); |
| 68 | + assert(body.includes('Authentication requires a valid Date or x-amz-date header')); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + req.on('error', err => { |
| 74 | + // If we get ECONNRESET or similar, it means the server crashed |
| 75 | + assert.fail(`Server crashed or connection error: ${err.message}`); |
| 76 | + }); |
| 77 | + |
| 78 | + req.end(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments