Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/documents.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
* Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
'use strict';

Expand Down Expand Up @@ -1302,12 +1302,13 @@ function addWriteConfig(document, hasUri, content, headers, sep) {
if (!hasUri) {
const extension = document.extension;
if (extension != null) {
writeConfig += sep+'extension='+extension;
writeConfig += sep+'extension='+
(isBody ? extension : validateUnquotedContentDispositionValue(extension));
if (isBody && sep === '?') { sep = '&'; }
const directory = document.directory;
if (directory != null) {
writeConfig += sep+'directory='+
(isBody ? encodeURIComponent(directory) : directory);
(isBody ? encodeURIComponent(directory) : validateUnquotedContentDispositionValue(directory));
}
}
}
Expand All @@ -1317,7 +1318,7 @@ function addWriteConfig(document, hasUri, content, headers, sep) {
if (isBody) {
headers['If-Match'] = versionId;
} else {
writeConfig += sep+'versionId='+versionId;
writeConfig += sep+'versionId='+validateUnquotedContentDispositionValue(versionId);
}
}

Expand Down Expand Up @@ -1404,9 +1405,10 @@ function addDocumentParts(operation, partList, document, isContentOptional) {

let disposition = '';
if (hasUri) {
disposition = 'attachment; filename="'+uri+'"';
disposition = 'attachment; filename="'+validateContentDispositionValue(uri)+'"';
if (document.temporalDocument != null) {
disposition += '; temporal-document="'+document.temporalDocument+'"';
disposition += '; temporal-document="'+
validateContentDispositionValue(document.temporalDocument)+'"';
}
} else {
disposition = 'inline';
Expand Down Expand Up @@ -1444,6 +1446,24 @@ function addDocumentParts(operation, partList, document, isContentOptional) {
}
}

function validateContentDispositionValue(value) {

Copy link
Copy Markdown

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).

const text = String(value);
// MarkLogic neither honors nor unescapes backslash escapes in quoted parameters,
// so unsafe characters are rejected and all other characters are sent unchanged.
if (/[\r\n\0"]/.test(text)) {
throw new Error('Content-Disposition values must not contain CR, LF, NUL, or double quotes');
}
return text;
}

function validateUnquotedContentDispositionValue(value) {
const text = validateContentDispositionValue(value);
if (text.indexOf(';') !== -1) {
throw new Error('Unquoted Content-Disposition values must not contain semicolons');
}
return text;
}

/** @ignore */
function collectMetadata(document) {
let metadata = null;
Expand Down Expand Up @@ -2373,8 +2393,9 @@ function onWriteAllDocs(jobState, writerId) {
}

function writeDocs(jobState, val, batchdef, writeBatchArray, writerId){
writeDocumentsImpl.call(jobState.docInstance, val, [batchdef])
.result((output) => {
Promise.resolve()
.then(() => writeDocumentsImpl.call(jobState.docInstance, val, [batchdef]).result())
.then((output) => {
jobState.docsWrittenSuccessfully+= writeBatchArray.length;
if (jobState.jobOptions.onBatchSuccess) {
const progressSoFar = {
Expand Down
48 changes: 48 additions & 0 deletions lib/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ function startRequest(operation) {
operation.inputSender = singleRequester;
break;
case 'multipart':
validatePartHeaders(operation.requestPartList);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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':
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
197 changes: 197 additions & 0 deletions test-basic/documents-multipart-header-test.js
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);
});
});
});
});
});
Loading
Loading