| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b50e89e commit 6192c98
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -364,6 +364,9 @@ or | |||
| 364 | 364 | ||
| 365 | 365 | response.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]); | |
| 366 | 366 | ||
| 367 | + Attempting to set a header field name that contains invalid characters will | ||
| 368 | + result in a `TypeError` being thrown. | ||
| 369 | + | ||
| 367 | 370 | ### response.headersSent | |
| 368 | 371 | ||
| 369 | 372 | Boolean (read-only). True if headers were sent, false otherwise. | |
@@ -439,6 +442,8 @@ emit trailers, with a list of the header fields in its value. E.g., | |||
| 439 | 442 | response.addTrailers({'Content-MD5': "7895bf4b8828b55ceaf47747b4bca667"}); | |
| 440 | 443 | response.end(); | |
| 441 | 444 | ||
| 445 | + Attempting to set a trailer field name that contains invalid characters will | ||
| 446 | + result in a `TypeError` being thrown. | ||
| 442 | 447 | ||
| 443 | 448 | ### response.end([data][, encoding][, callback]) | |
| 444 | 449 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,6 +67,9 @@ function ClientRequest(options, cb) { | |||
| 67 | 67 | self.socketPath = options.socketPath; | |
| 68 | 68 | ||
| 69 | 69 | var method = self.method = (options.method || 'GET').toUpperCase(); | |
| 70 | + if (!common._checkIsHttpToken(method)) { | ||
| 71 | + throw new TypeError('Method must be a valid HTTP token'); | ||
| 72 | + } | ||
| 70 | 73 | self.path = options.path || '/'; | |
| 71 | 74 | if (cb) { | |
| 72 | 75 | self.once('response', cb); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -195,3 +195,13 @@ function httpSocketSetup(socket) { | |||
| 195 | 195 | socket.on('drain', ondrain); | |
| 196 | 196 | } | |
| 197 | 197 | exports.httpSocketSetup = httpSocketSetup; | |
| 198 | + | ||
| 199 | + /** | ||
| 200 | + * Verifies that the given val is a valid HTTP token | ||
| 201 | + * per the rules defined in RFC 7230 | ||
| 202 | + **/ | ||
| 203 | + const token = /^[a-zA-Z0-9_!#$%&'*+.^`|~-]+$/; | ||
| 204 | + function checkIsHttpToken(val) { | ||
| 205 | + return typeof val === 'string' && token.test(val); | ||
| 206 | + } | ||
| 207 | + exports._checkIsHttpToken = checkIsHttpToken; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -295,6 +295,10 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) { | |||
| 295 | 295 | }; | |
| 296 | 296 | ||
| 297 | 297 | function storeHeader(self, state, field, value) { | |
| 298 | + if (!common._checkIsHttpToken(field)) { | ||
| 299 | + throw new TypeError( | ||
| 300 | + 'Header name must be a valid HTTP Token ["' + field + '"]'); | ||
| 301 | + } | ||
| 298 | 302 | value = escapeHeaderValue(value); | |
| 299 | 303 | state.messageHeader += field + ': ' + value + CRLF; | |
| 300 | 304 | ||
@@ -323,6 +327,9 @@ function storeHeader(self, state, field, value) { | |||
| 323 | 327 | ||
| 324 | 328 | ||
| 325 | 329 | OutgoingMessage.prototype.setHeader = function(name, value) { | |
| 330 | + if (!common._checkIsHttpToken(name)) | ||
| 331 | + throw new TypeError( | ||
| 332 | + 'Header name must be a valid HTTP Token ["' + name + '"]'); | ||
| 326 | 333 | if (typeof name !== 'string') | |
| 327 | 334 | throw new TypeError('`name` should be a string in setHeader(name, value).'); | |
| 328 | 335 | if (value === undefined) | |
@@ -498,7 +505,10 @@ OutgoingMessage.prototype.addTrailers = function(headers) { | |||
| 498 | 505 | field = key; | |
| 499 | 506 | value = headers[key]; | |
| 500 | 507 | } | |
| 501 | - | ||
| 508 | + if (!common._checkIsHttpToken(field)) { | ||
| 509 | + throw new TypeError( | ||
| 510 | + 'Trailer name must be a valid HTTP Token ["' + field + '"]'); | ||
| 511 | + } | ||
| 502 | 512 | this._trailer += field + ': ' + escapeHeaderValue(value) + CRLF; | |
| 503 | 513 | } | |
| 504 | 514 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,56 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const EventEmitter = require('events'); | ||
| 5 | + const http = require('http'); | ||
| 6 | + | ||
| 7 | + const ee = new EventEmitter(); | ||
| 8 | + var count = 3; | ||
| 9 | + | ||
| 10 | + const server = http.createServer(function(req, res) { | ||
| 11 | + assert.doesNotThrow(function() { | ||
| 12 | + res.setHeader('testing_123', 123); | ||
| 13 | + }); | ||
| 14 | + assert.throws(function() { | ||
| 15 | + res.setHeader('testing 123', 123); | ||
| 16 | + }, TypeError); | ||
| 17 | + res.end(''); | ||
| 18 | + }); | ||
| 19 | + server.listen(common.PORT, function() { | ||
| 20 | + | ||
| 21 | + http.get({port: common.PORT}, function() { | ||
| 22 | + ee.emit('done'); | ||
| 23 | + }); | ||
| 24 | + | ||
| 25 | + assert.throws( | ||
| 26 | + function() { | ||
| 27 | + var options = { | ||
| 28 | + port: common.PORT, | ||
| 29 | + headers: {'testing 123': 123} | ||
| 30 | + }; | ||
| 31 | + http.get(options, function() {}); | ||
| 32 | + }, | ||
| 33 | + function(err) { | ||
| 34 | + ee.emit('done'); | ||
| 35 | + if (err instanceof TypeError) return true; | ||
| 36 | + } | ||
| 37 | + ); | ||
| 38 | + | ||
| 39 | + assert.doesNotThrow( | ||
| 40 | + function() { | ||
| 41 | + var options = { | ||
| 42 | + port: common.PORT, | ||
| 43 | + headers: {'testing_123': 123} | ||
| 44 | + }; | ||
| 45 | + http.get(options, function() { | ||
| 46 | + ee.emit('done'); | ||
| 47 | + }); | ||
| 48 | + }, TypeError | ||
| 49 | + ); | ||
| 50 | + }); | ||
| 51 | + | ||
| 52 | + ee.on('done', function() { | ||
| 53 | + if (--count === 0) { | ||
| 54 | + server.close(); | ||
| 55 | + } | ||
| 56 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments