Skip to content

Commit f58d3b1

Browse files
LaszloLangoyichoi
authored andcommitted
Add argument validation to 'ServerResponse.prototype.writeHead' (#954)
IoT.js-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 26e9881 commit f58d3b1

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/js/http_outgoing.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ OutgoingMessage.prototype._storeHeader = function(statusLine) {
151151

152152

153153
OutgoingMessage.prototype.setHeader = function(name, value) {
154+
if ((typeof name) != 'string') {
155+
throw new TypeError('Name must be string.');
156+
}
157+
158+
if (!value) {
159+
throw new Error('value required in setHeader(' + name + ', value)');
160+
}
154161

155162
if (this._headers === null) {
156163
this._headers = {};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// Copyright Joyent, Inc. and other Node contributors.
17+
//
18+
// Permission is hereby granted, free of charge, to any person obtaining a
19+
// copy of this software and associated documentation files (the
20+
// "Software"), to deal in the Software without restriction, including
21+
// without limitation the rights to use, copy, modify, merge, publish,
22+
// distribute, sublicense, and/or sell copies of the Software, and to permit
23+
// persons to whom the Software is furnished to do so, subject to the
24+
// following conditions:
25+
//
26+
// The above copyright notice and this permission notice shall be included
27+
// in all copies or substantial portions of the Software.
28+
//
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
32+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
33+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
34+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
35+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
37+
'use strict';
38+
39+
var common = require('node/common');
40+
var assert = require('assert');
41+
var http = require('http');
42+
43+
// Verify that ServerResponse.writeHead() works as setHeader.
44+
// Issue 5036 on github.
45+
46+
var s = http.createServer(common.mustCall(function (req, res) {
47+
res.setHeader('test', '1');
48+
49+
// toLowerCase() is used on the name argument, so it must be a string.
50+
var threw = false;
51+
try {
52+
res.setHeader(0xf00, 'bar');
53+
} catch (e) {
54+
assert.ok(e instanceof TypeError);
55+
threw = true;
56+
}
57+
assert.ok(threw, 'Non-string names should throw');
58+
59+
// undefined value should throw, via 979d0ca8
60+
threw = false;
61+
try {
62+
res.setHeader('foo', undefined);
63+
} catch (e) {
64+
assert.ok(e instanceof Error);
65+
assert.strictEqual(e.message, 'value required in setHeader(foo, value)');
66+
threw = true;
67+
}
68+
assert.ok(threw, 'Undefined value should throw');
69+
70+
res.writeHead(200, { Test: '2' });
71+
72+
// assert.throws(function () {
73+
assert.doesNotThrow(function () {
74+
res.writeHead(100, {});
75+
});
76+
77+
res.end();
78+
}));
79+
80+
s.listen(0, common.mustCall(runTest));
81+
82+
function runTest() {
83+
http.get({ port: this.address().port }, common.mustCall(function (response) {
84+
response.on('end', common.mustCall(function () {
85+
assert.strictEqual(response.headers['test'], '1' /*'2'*/);
86+
// assert.notStrictEqual(response.rawHeaders.indexOf('Test'), -1);
87+
s.close();
88+
}));
89+
response.resume();
90+
}));
91+
}

test/testsets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
],
114114
"node/parallel": [
115115
{ "name": "test-assert.js" },
116+
{ "name": "test-http-write-head.js" },
116117
{ "name": "test-net-bind-twice.js" },
117118
{ "name": "test-net-end-without-connect.js" }
118119
]

0 commit comments

Comments
 (0)