Repository navigation
CLDSRV-1099: Refuse malformed grant headers instead of crashing #6422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicolas2bert
wants to merge
1
commit into
development/9.4
Choose a base branch
from
bugfix/CLDSRV-1099-validate-grant-headers
base: development/9.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| const assert = require('assert'); | ||
|
|
||
| const { makeS3Request } = require('../utils/makeRequest'); | ||
|
|
||
| const authCredentials = { | ||
| accessKey: 'accessKey1', | ||
| secretKey: 'verySecretKey1', | ||
| }; | ||
| const bucket = 'rawnode-acl-grant-headers'; | ||
| const allUsers = 'http://acs.amazonaws.com/groups/global/AllUsers'; | ||
| // The canonical id of accessKey2. | ||
| const otherId = '79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2bf'; | ||
|
|
||
| // We use raw requests because an SDK refuses to send most of these headers. | ||
| // A worker that crashes closes the socket, so the request fails with no | ||
| // status code. | ||
| function request(params) { | ||
| const headers = { 'content-length': Buffer.byteLength(params.requestBody || ''), ...params.headers }; | ||
| return new Promise(resolve => { | ||
| makeS3Request({ authCredentials, bucket, ...params, headers }, (err, res) => resolve({ err, res })); | ||
| }); | ||
| } | ||
|
|
||
| function assertError({ err, res }, statusCode, code) { | ||
| assert(res, `expected a ${statusCode} ${code}, got ${err && err.message}`); | ||
| assert.strictEqual(res.statusCode, statusCode); | ||
| assert.strictEqual(err && err.code, code); | ||
| } | ||
|
|
||
| function assertOk({ err, res }) { | ||
| assert(res, `expected a 200, got ${err && err.message}`); | ||
| assert.strictEqual(res.statusCode, 200, err && err.code); | ||
| } | ||
|
|
||
| const apis = [ | ||
| { name: 'PutBucketAcl', params: { method: 'PUT', queryObj: { acl: '' } } }, | ||
| { name: 'PutObject', params: { method: 'PUT', objectKey: 'put' } }, | ||
| { name: 'PutObjectAcl', params: { method: 'PUT', objectKey: 'src', queryObj: { acl: '' } } }, | ||
| { | ||
| name: 'CopyObject', | ||
| params: { method: 'PUT', objectKey: 'copy', headers: { 'x-amz-copy-source': `/${bucket}/src` } }, | ||
| }, | ||
| { name: 'CreateMultipartUpload', params: { method: 'POST', objectKey: 'mpu', queryObj: { uploads: '' } } }, | ||
| ]; | ||
|
|
||
| function withGrant(params, value) { | ||
| return { ...params, headers: { ...params.headers, 'x-amz-grant-read': value } }; | ||
| } | ||
|
|
||
| describe('grant headers that are not type=identifier pairs', () => { | ||
| before(async () => { | ||
| assertOk(await request({ method: 'PUT' })); | ||
| assertOk(await request({ method: 'PUT', objectKey: 'src', requestBody: 'body' })); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| const list = await request({ method: 'GET', queryObj: { uploads: '' } }); | ||
| const uploads = [...list.res.body.matchAll(/<Key>([^<]*)<\/Key><UploadId>([^<]*)<\/UploadId>/g)]; | ||
| for (const [, key, uploadId] of uploads) { | ||
| await request({ method: 'DELETE', objectKey: key, queryObj: { uploadId } }); | ||
| } | ||
| for (const key of ['src', 'put', 'copy']) { | ||
| await request({ method: 'DELETE', objectKey: key }); | ||
| } | ||
| await request({ method: 'DELETE' }); | ||
| }); | ||
|
|
||
| const malformed = { | ||
| 'no equal sign': 'x', | ||
| 'two equal signs': 'id=a=b', | ||
| 'a trailing comma': `uri=${allUsers},`, | ||
| }; | ||
| Object.entries(malformed).forEach(([name, value]) => { | ||
| apis.forEach(api => { | ||
| it(`should return InvalidArgument on ${api.name} for a grant with ${name}`, async () => { | ||
| assertError(await request(withGrant(api.params, value)), 400, 'InvalidArgument'); | ||
| }); | ||
| }); | ||
|
|
||
| it(`should answer an anonymous PutBucketAcl with ${name} on a missing bucket`, async () => { | ||
| const result = await new Promise(resolve => { | ||
| makeS3Request( | ||
| { | ||
| method: 'PUT', | ||
| bucket: 'rawnode-acl-grant-headers-missing', | ||
| queryObj: { acl: '' }, | ||
| headers: { 'x-amz-grant-read': value }, | ||
| }, | ||
| (err, res) => resolve({ err, res }), | ||
| ); | ||
| }); | ||
| assert(result.res, `expected an error response, got ${result.err && result.err.message}`); | ||
| assert(result.res.statusCode >= 400 && result.res.statusCode < 500, `${result.res.statusCode}`); | ||
| }); | ||
| }); | ||
|
|
||
| ['CopyObject', 'CreateMultipartUpload'].forEach(name => { | ||
| it(`should return InvalidArgument on ${name} for a grant to id=123`, async () => { | ||
| const api = apis.find(a => a.name === name); | ||
| assertError(await request(withGrant(api.params, 'id=123')), 400, 'InvalidArgument'); | ||
| }); | ||
| }); | ||
|
|
||
| it('should ignore an empty grant header on PutObject', async () => { | ||
| assertOk(await request(withGrant(apis[1].params, ''))); | ||
| }); | ||
|
|
||
| // An empty header is no ACL at all, so PutObjectAcl has nothing to apply. | ||
| it('should return MalformedXML on PutObjectAcl with only an empty grant header', async () => { | ||
| assertError(await request(withGrant(apis[2].params, '')), 400, 'MalformedXML'); | ||
| }); | ||
|
|
||
| it('should accept a quoted identifier on PutObject', async () => { | ||
| assertOk(await request(withGrant(apis[1].params, `uri="${allUsers}"`))); | ||
| }); | ||
|
|
||
| apis.forEach(api => { | ||
| it(`should accept a canonical id in upper case on ${api.name}`, async () => { | ||
| assertOk(await request(withGrant(api.params, `id=${otherId.toUpperCase()}`))); | ||
| }); | ||
| }); | ||
|
|
||
| it('should store a canonical id given in upper case in lower case on CopyObject', async () => { | ||
| assertOk(await request(withGrant(apis[3].params, `id=${otherId.toUpperCase()}`))); | ||
| const { res } = await request({ method: 'GET', objectKey: 'copy', queryObj: { acl: '' } }); | ||
| assert(res.body.includes(`<ID>${otherId}</ID>`), res.body); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer
assert.matchoverincludesso a failure shows the body and the expected pattern.