|
| 1 | +const { S3 } = require('aws-sdk'); |
| 2 | +const assert = require('assert'); |
| 3 | +const async = require('async'); |
| 4 | + |
| 5 | +const getConfig = require('../support/config'); |
| 6 | +const { methodRequest, generateCorsParams } = |
| 7 | + require('../../lib/utility/cors-util'); |
| 8 | + |
| 9 | +const config = getConfig('default', { signatureVersion: 'v4' }); |
| 10 | +const s3 = new S3(config); |
| 11 | + |
| 12 | +const bucket = 'corserrorheadertest'; |
| 13 | +const objectKey = 'objectKey'; |
| 14 | +const allowedOrigin = 'http://www.allowed.test'; |
| 15 | +const vary = 'Origin, Access-Control-Request-Headers, ' |
| 16 | + + 'Access-Control-Request-Method'; |
| 17 | + |
| 18 | +const expectedCorsHeaders = { |
| 19 | + 'access-control-allow-origin': allowedOrigin, |
| 20 | + 'access-control-allow-methods': 'GET, PUT, POST, DELETE, HEAD', |
| 21 | + 'access-control-allow-credentials': 'true', |
| 22 | + vary, |
| 23 | +}; |
| 24 | + |
| 25 | +const corsParams = generateCorsParams(bucket, { |
| 26 | + allowedMethods: ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'], |
| 27 | + allowedOrigins: [allowedOrigin], |
| 28 | + allowedHeaders: ['*'], |
| 29 | +}); |
| 30 | + |
| 31 | +// Raw unauthenticated requests - they always return 403. |
| 32 | +// Each spec describes (method, path, query) against the bucket. |
| 33 | +const unauthenticatedRequests = [ |
| 34 | + { description: 'GET bucket (list objects)', |
| 35 | + method: 'GET', query: null, objectKey: null }, |
| 36 | + { description: 'HEAD bucket', |
| 37 | + method: 'HEAD', query: null, objectKey: null }, |
| 38 | + { description: 'DELETE bucket', |
| 39 | + method: 'DELETE', query: null, objectKey: null }, |
| 40 | + { description: 'GET bucket ACL', |
| 41 | + method: 'GET', query: 'acl', objectKey: null }, |
| 42 | + { description: 'GET bucket CORS', |
| 43 | + method: 'GET', query: 'cors', objectKey: null }, |
| 44 | + { description: 'GET bucket versioning', |
| 45 | + method: 'GET', query: 'versioning', objectKey: null }, |
| 46 | + { description: 'GET bucket website', |
| 47 | + method: 'GET', query: 'website', objectKey: null }, |
| 48 | + { description: 'GET bucket tagging', |
| 49 | + method: 'GET', query: 'tagging', objectKey: null }, |
| 50 | + { description: 'GET object', |
| 51 | + method: 'GET', query: null, objectKey }, |
| 52 | + { description: 'HEAD object', |
| 53 | + method: 'HEAD', query: null, objectKey }, |
| 54 | + { description: 'PUT object', |
| 55 | + method: 'PUT', query: null, objectKey }, |
| 56 | + { description: 'DELETE object', |
| 57 | + method: 'DELETE', query: null, objectKey }, |
| 58 | + { description: 'GET bucket uploads (list multipart uploads)', |
| 59 | + method: 'GET', query: 'uploads', objectKey: null }, |
| 60 | + // GET bucket policy and POST multi-delete are not covered here: the |
| 61 | + // first returns 405 (method rejected pre-auth), the second returns 400 |
| 62 | + // (missing XML body fails validation pre-auth). Neither reaches the |
| 63 | + // 403 path. Both are exercised via the unit test that stubs auth |
| 64 | + // failure directly. |
| 65 | +]; |
| 66 | + |
| 67 | +function _waitForAWS(callback, err) { |
| 68 | + if (err) { |
| 69 | + return setTimeout(() => callback(err), 500); |
| 70 | + } |
| 71 | + return setTimeout(() => callback(), 500); |
| 72 | +} |
| 73 | + |
| 74 | +describe('CORS headers on 403 responses when bucket has CORS configured', () => { |
| 75 | + before(done => async.series([ |
| 76 | + cb => s3.createBucket({ Bucket: bucket }, err => _waitForAWS(cb, err)), |
| 77 | + cb => s3.putBucketCors(corsParams, err => _waitForAWS(cb, err)), |
| 78 | + ], done)); |
| 79 | + |
| 80 | + after(done => s3.deleteBucket({ Bucket: bucket }, |
| 81 | + err => _waitForAWS(done, err))); |
| 82 | + |
| 83 | + unauthenticatedRequests.forEach(spec => { |
| 84 | + it(`returns CORS headers on 403 for ${spec.description} ` |
| 85 | + + 'when Origin matches a rule', done => { |
| 86 | + methodRequest({ |
| 87 | + method: spec.method, |
| 88 | + bucket, |
| 89 | + objectKey: spec.objectKey, |
| 90 | + query: spec.query, |
| 91 | + headers: { origin: allowedOrigin }, |
| 92 | + // Use numeric status: HEAD responses have no body, and some |
| 93 | + // endpoints (bucket policy, multi-delete) can fail with a |
| 94 | + // non-AccessDenied body before auth even runs. We only care |
| 95 | + // about the 403 status and the CORS headers here. |
| 96 | + code: 403, |
| 97 | + headersResponse: expectedCorsHeaders, |
| 98 | + }, done); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('omits CORS headers on 403 when Origin does not match any rule', |
| 103 | + done => { |
| 104 | + methodRequest({ |
| 105 | + method: 'GET', |
| 106 | + bucket, |
| 107 | + query: null, |
| 108 | + objectKey: null, |
| 109 | + headers: { origin: 'http://not-allowed.test' }, |
| 110 | + code: 403, |
| 111 | + // headersResponse unset -> cors-util asserts CORS headers |
| 112 | + // are NOT present. |
| 113 | + }, done); |
| 114 | + }); |
| 115 | + |
| 116 | + it('omits CORS headers on 403 when no Origin header is sent', |
| 117 | + done => { |
| 118 | + methodRequest({ |
| 119 | + method: 'GET', |
| 120 | + bucket, |
| 121 | + query: null, |
| 122 | + objectKey: null, |
| 123 | + headers: {}, |
| 124 | + code: 403, |
| 125 | + }, done); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe('CORS headers on 200 responses (regression guard)', () => { |
| 130 | + before(done => async.series([ |
| 131 | + cb => s3.createBucket({ Bucket: bucket }, err => _waitForAWS(cb, err)), |
| 132 | + cb => s3.putBucketCors(corsParams, err => _waitForAWS(cb, err)), |
| 133 | + ], done)); |
| 134 | + |
| 135 | + after(done => s3.deleteBucket({ Bucket: bucket }, |
| 136 | + err => _waitForAWS(done, err))); |
| 137 | + |
| 138 | + it('returns CORS headers on a successful list objects (200)', done => { |
| 139 | + const request = s3.listObjects({ Bucket: bucket }); |
| 140 | + request.on('build', () => { |
| 141 | + request.httpRequest.headers.origin = allowedOrigin; |
| 142 | + }); |
| 143 | + request.on('success', response => { |
| 144 | + const h = response.httpResponse.headers; |
| 145 | + assert.strictEqual(h['access-control-allow-origin'], |
| 146 | + allowedOrigin); |
| 147 | + done(); |
| 148 | + }); |
| 149 | + request.on('error', err => done(err)); |
| 150 | + request.send(); |
| 151 | + }); |
| 152 | +}); |
0 commit comments