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
11 changes: 10 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const {
kUniqueHeaders,
parseUniqueHeadersOption,
OutgoingMessage,
validateHeaderName,
validateHeaderValue,
} = require('_http_outgoing');
const {
kOutHeaders,
Expand Down Expand Up @@ -333,13 +335,20 @@ ServerResponse.prototype.writeEarlyHints = function writeEarlyHints(hints, cb) {
return;
}

if (checkInvalidHeaderChar(link)) {
throw new ERR_INVALID_CHAR('header content', 'Link');
}

head += 'Link: ' + link + '\r\n';

const keys = ObjectKeys(hints);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (key !== 'link') {
head += key + ': ' + hints[key] + '\r\n';
validateHeaderName(key);
const value = hints[key];
validateHeaderValue(key, value);
head += key + ': ' + value + '\r\n';
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ function validateUnion(value, name, union) {
(not necessarily a valid URI reference) followed by zero or more
link-params separated by semicolons.
*/
const linkValueRegExp = /^(?:<[^>]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/;
const linkValueRegExp = /^(?:<[^>\r\n]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/;

/**
* @param {any} value
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-http-early-hints-invalid-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,44 @@
req.on('information', common.mustNotCall());
}));
}

{
const server = http.createServer(common.mustCall((req, res) => {
debug('Server sending early hints with CRLF injection...');

assert.throws(() => {
res.writeEarlyHints({
link: '</styles.css>; rel=preload; as=style',

Check failure on line 57 in test/parallel/test-http-early-hints-invalid-argument.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Inconsistently quoted property 'link' found
'X-Custom': 'valid\r\nSet-Cookie: session=evil',
});
}, (err) => err.code === 'ERR_INVALID_CHAR');

assert.throws(() => {
res.writeEarlyHints({
link: '</styles.css>; rel=preload; as=style',

Check failure on line 64 in test/parallel/test-http-early-hints-invalid-argument.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Inconsistently quoted property 'link' found
'X-Custom\r\nSet-Cookie: session=evil': 'value',
});
}, (err) => err.code === 'ERR_INVALID_HTTP_TOKEN');

assert.throws(() => {
res.writeEarlyHints({
link: '</styles.css\r\nSet-Cookie: session=evil>; rel=preload; as=style',
});
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');

debug('Server sending full response...');
res.end(testResBody);
server.close();
}));

server.listen(0, common.mustCall(() => {
const req = http.request({
port: server.address().port, path: '/'
});

req.end();
debug('Client sending request...');

req.on('information', common.mustNotCall());
}));
}
Loading