Repository navigation
MLE-33479: Bugcrowd: node-client-api: the document URI is interpolated raw into a quoted Content-Disposition parameter - a URI containing a double quote or CRLF injects MIME headers into the request #1133
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,8 @@ function startRequest(operation) { | |
| operation.inputSender = singleRequester; | ||
| break; | ||
| case 'multipart': | ||
| validatePartHeaders(operation.requestPartList); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In multipartRequester(), when operation.bindingParam is set (lines 427–484), operation.requestPartList is undefined, so validatePartHeaders(operation.requestPartList) is a no-op. In that branch, attachment keys (filename: key at lines 450–465) and bindingParam.key (line 441) are passed into form.append(), which writes them into Content-Disposition headers without rejecting \r, \n, \0, or ". We should validate bindingParam.key and attachment keys as well. |
||
| validateBindingParam(operation.bindingParam); | ||
| operation.inputSender = multipartRequester; | ||
| break; | ||
| case 'chunked': | ||
|
|
@@ -155,6 +157,7 @@ function startRequest(operation) { | |
| operation.inputSender = chunkedRequester; | ||
| break; | ||
| case 'chunkedMultipart': | ||
| validatePartHeaders(operation.requestDocument); | ||
| operationResultPromise = responder.operationResultPromise; | ||
| started = through2(); | ||
| started.result = mlutil.callbackOn(operation, operationResultPromise); | ||
|
|
@@ -218,6 +221,51 @@ function startRequest(operation) { | |
|
|
||
| return started; | ||
| } | ||
| // Multipart part headers are written into the request body, where Node.js does not | ||
| // reject the CR/LF/NUL characters it rejects in real HTTP headers. | ||
| function validatePartHeaders(parts) { | ||
| if (!Array.isArray(parts)) { | ||
| return; | ||
| } | ||
| for (const part of parts) { | ||
| const headers = (part == null) ? null : part.headers; | ||
| if (headers == null) { | ||
| continue; | ||
| } | ||
| for (const [name, value] of Object.entries(headers)) { | ||
| if (/[\r\n\0]/.test(name) || /[\r\n\0]/.test(String(value))) { | ||
| throw new Error('multipart part header "' + JSON.stringify(name).slice(1, -1) + | ||
| '" must not contain CR, LF, or NUL characters'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| function validateBindingParam(bindingParam) { | ||
| if (bindingParam == null) { | ||
| return; | ||
| } | ||
| validateMultipartParameter(bindingParam.key); | ||
|
|
||
| const attachments = bindingParam.attachments; | ||
| if (Array.isArray(attachments)) { | ||
| for (const attachment of attachments) { | ||
| if (attachment != null && typeof attachment === 'object') { | ||
| for (const key of Object.keys(attachment)) { | ||
| validateMultipartParameter(key); | ||
| } | ||
| } | ||
| } | ||
| } else if (attachments != null && typeof attachments === 'object') { | ||
| for (const key of Object.keys(attachments)) { | ||
| validateMultipartParameter(key); | ||
| } | ||
| } | ||
| } | ||
| function validateMultipartParameter(value) { | ||
| if (/[\r\n\0"]/.test(String(value))) { | ||
| throw new Error('Content-Disposition values must not contain CR, LF, NUL, or double quotes'); | ||
| } | ||
| } | ||
| function challengeRequest(operation) { | ||
| const isRead = (operation.inputSender === null); | ||
| const options = operation.options; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. | ||
| */ | ||
| const http = require('http'); | ||
| const assert = require('assert'); | ||
| const should = require('should'); | ||
| const marklogic = require('../'); | ||
| const op = marklogic.planBuilder; | ||
|
|
||
| describe('documents.write multipart headers', function() { | ||
| const db = marklogic.createDatabaseClient({ | ||
| host: 'localhost', | ||
| port: 1, | ||
| user: 'u', | ||
| password: 'p', | ||
| authType: 'basic' | ||
| }); | ||
|
|
||
| it('rejects CR, LF, NUL, and double quotes in uri and temporalDocument', function() { | ||
| const invalidSuffixes = ['\r', '\n', '\0', '"', '"; filename="y.json']; | ||
| for (const suffix of invalidSuffixes) { | ||
| for (const field of ['uri', 'temporalDocument']) { | ||
| for (const index of [0, 1]) { | ||
| const documents = [{ | ||
| uri: '/poc/first.json', | ||
| contentType: 'application/json', | ||
| content: {a: 1} | ||
| }, { | ||
| uri: '/poc/second.json', | ||
| contentType: 'application/json', | ||
| content: {b: 2} | ||
| }]; | ||
| documents[index][field] = '/poc/x.json' + suffix; | ||
| assert.throws(function() { | ||
| db.documents.write(documents); | ||
| }, /Content-Disposition values must not contain/); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it('rejects CR, LF, and NUL in any multipart part header value', function() { | ||
| for (const invalidCharacter of ['\r', '\n', '\0']) { | ||
| const injected = invalidCharacter + 'X-Injected: yes'; | ||
| const cases = [ | ||
| {uri: '/poc/file.json', contentType: 'application/json' + injected, content: {a: 1}}, | ||
| {extension: 'json' + injected, contentType: 'application/json', content: {a: 1}}, | ||
| {extension: 'json', directory: '/poc/' + injected, contentType: 'application/json', content: {a: 1}}, | ||
| {uri: '/poc/file.json', versionId: '1' + injected, contentType: 'application/json', content: {a: 1}} | ||
| ]; | ||
| for (const document of cases) { | ||
| assert.throws(function() { | ||
| db.documents.write([document, { | ||
| uri: '/poc/second.json', | ||
| contentType: 'application/json', | ||
| content: {b: 2} | ||
| }]); | ||
| }, /must not contain/); | ||
| } | ||
|
|
||
| for (const method of ['post', 'put']) { | ||
| assert.throws(function() { | ||
| db.resources[method]({ | ||
| name: 'example', | ||
| documents: [{contentType: 'application/json' + injected, content: {a: 1}}] | ||
| }); | ||
| }, /must not contain/); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it('rejects quoted content-disposition parameters in multipart writes', function() { | ||
| const documents = [ | ||
| {extension: 'json"', contentType: 'application/json', content: {a: 1}}, | ||
| {extension: 'json', directory: '/poc/"files', contentType: 'application/json', content: {a: 1}}, | ||
| {uri: '/poc/file.json', versionId: '"1', contentType: 'application/json', content: {a: 1}} | ||
| ]; | ||
| for (const document of documents) { | ||
| assert.throws(function() { | ||
| db.documents.write([document, { | ||
| uri: '/poc/second.json', | ||
| contentType: 'application/json', | ||
| content: {b: 2} | ||
| }]); | ||
| }, /Content-Disposition values must not contain/); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects semicolons in unquoted content-disposition parameters', function() { | ||
| const documents = [ | ||
| {extension: 'json; category=metadata', contentType: 'application/json', content: {a: 1}}, | ||
| {extension: 'json', directory: '/poc/; category=metadata', contentType: 'application/json', content: {a: 1}}, | ||
| {uri: '/poc/file.json', versionId: '1; category=metadata', contentType: 'application/json', content: {a: 1}} | ||
| ]; | ||
| for (const document of documents) { | ||
| assert.throws(function() { | ||
| db.documents.write([document, { | ||
| uri: '/poc/second.json', | ||
| contentType: 'application/json', | ||
| content: {b: 2} | ||
| }]); | ||
| }, /Unquoted Content-Disposition values must not contain semicolons/); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects unsafe binding parameter and attachment names', function() { | ||
| for (const invalidCharacter of ['\r', '\n', '\0', '"']) { | ||
| const bindingKey = 'binding' + invalidCharacter; | ||
| assert.throws(function() { | ||
| db.rows.query(op.fromParam(bindingKey, null, op.docColTypes()), null, { | ||
| [bindingKey]: [] | ||
| }); | ||
| }, /Content-Disposition values must not contain/); | ||
|
|
||
| const invalidAttachmentKey = 'file' + invalidCharacter; | ||
| for (const attachments of [ | ||
| {[invalidAttachmentKey]: 'content'}, | ||
| [{[invalidAttachmentKey]: 'content'}] | ||
| ]) { | ||
| assert.throws(function() { | ||
| db.rows.query(op.fromParam('bindingParam', null, op.docColTypes()), null, { | ||
| bindingParam: [], | ||
| attachments: attachments | ||
| }); | ||
| }, /Content-Disposition values must not contain/); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it('rejects unsafe part headers in multipart streaming writes', function() { | ||
| for (const invalidCharacter of ['\r', '\n', '\0']) { | ||
| assert.throws(function() { | ||
| // properties selects the chunkedMultipart request type. | ||
| db.documents.createWriteStream({ | ||
| uri: '/poc/stream.json', | ||
| properties: {}, | ||
| contentType: 'application/json' + invalidCharacter + 'X-Injected: yes' | ||
| }); | ||
| }, /must not contain CR, LF, or NUL/); | ||
| } | ||
| }); | ||
|
|
||
| it('preserves backslashes and ordinary URI paths unchanged', function(done) { | ||
| const uri = '/poc/file\\name.json'; | ||
| const temporalDocument = '/poc/temporal\\name.json'; | ||
| let body = ''; | ||
| let assertionError; | ||
|
|
||
| const server = http.createServer(function(request, response) { | ||
| request.setEncoding('utf8'); | ||
| request.on('data', function(chunk) { | ||
| body += chunk; | ||
| }); | ||
| request.on('end', function() { | ||
| try { | ||
| body.should.containEql('filename="/poc/file\\name.json"'); | ||
| body.should.containEql('temporal-document="/poc/temporal\\name.json"'); | ||
| body.should.containEql('filename="/poc/second; filename=ordinary.json"'); | ||
| body.should.not.containEql('%2Fpoc'); | ||
| } catch (error) { | ||
| assertionError = error; | ||
| } | ||
| response.writeHead(204); | ||
| response.end(); | ||
| }); | ||
| }); | ||
|
|
||
| server.listen(0, '127.0.0.1', function() { | ||
| const db = marklogic.createDatabaseClient({ | ||
| host: '127.0.0.1', | ||
| port: server.address().port, | ||
| user: 'u', | ||
| password: 'p', | ||
| authType: 'basic' | ||
| }); | ||
|
|
||
| db.documents.write([{ | ||
| uri, | ||
| temporalDocument, | ||
| contentType: 'application/json', | ||
| content: {a: 1} | ||
| }, { | ||
| uri: '/poc/second; filename=ordinary.json', | ||
| contentType: 'application/json', | ||
| content: {b: 2} | ||
| }]).result().then(function() { | ||
| server.close(function() { | ||
| done(assertionError); | ||
| }); | ||
| }, function(error) { | ||
| server.close(function() { | ||
| done(assertionError || error); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
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.
addDocumentParts() validates uri and temporalDocument here, but at line 1433 it also appends addWriteConfig(document, hasUri, content, headers, '; ') directly to disposition.
In multipart mode (sep === '; ', isBody === false), addWriteConfig() interpolates document.extension, document.directory (unencoded when isBody === false at line 1310), and document.versionId (line 1320) directly into Content-Disposition without quoting or validation. Because validatePartHeaders() in lib/requester.js only checks for [\r\n\0], passing ; or " in directory, extension, or versionId (e.g., versionId: '1; filename="/hijacked.json"' or directory: '/a; filename="/hijacked.json"') still injects arbitrary Content-Disposition parameters on the same header line.
We should validate extension, directory, and versionId in addWriteConfig() (rejecting [;\r\n\0"] or quoting and passing through validateContentDispositionValue).