Skip to content

Commit 193b0d9

Browse files
committed
Better integration of disabling header blacklist, use setDisableHeaderCheck
1 parent 7d9c882 commit 193b0d9

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# node-XMLHttpRequest #
22

3-
this is a fork with support for disabling header checking.
4-
53
node-XMLHttpRequest is a wrapper for the built-in http client to emulate the
64
browser XMLHttpRequest object.
75

lib/XMLHttpRequest.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ exports.XMLHttpRequest = function() {
3131
// Request settings
3232
var settings = {};
3333

34-
//headerscheck
35-
var disableHeaderChecking = false;
34+
// Disable header blacklist.
35+
// Not part of XHR specs.
36+
var disableHeaderCheck = false;
3637

3738
// Set some default headers
3839
var defaultHeaders = {
@@ -120,14 +121,7 @@ exports.XMLHttpRequest = function() {
120121
* @return boolean False if not allowed, otherwise true
121122
*/
122123
var isAllowedHttpHeader = function(header) {
123-
if (disableHeaderChecking)
124-
{
125-
return true
126-
}
127-
else
128-
{
129-
return (header && forbiddenRequestHeaders.indexOf(header.toLowerCase()) === -1);
130-
}
124+
return disableHeaderCheck || (header && forbiddenRequestHeaders.indexOf(header.toLowerCase()) === -1);
131125
};
132126

133127
/**
@@ -173,18 +167,15 @@ exports.XMLHttpRequest = function() {
173167

174168
setState(this.OPENED);
175169
};
176-
177-
170+
178171
/**
179172
* Disables or enables isAllowedHttpHeader() check the request. Enabled by default.
173+
* This does not conform to the W3C spec.
180174
*
181-
* @param State true or false
182-
*
183-
* This is a more advance feature. This does not conform to the W3C spec
184-
*
175+
* @param boolean state Enable or disable header checking.
185176
*/
186-
this.disableHeaderCheck = function(state) {
187-
disableHeaderChecking = state;
177+
this.setDisableHeaderCheck = function(state) {
178+
disableHeaderCheck = state;
188179
}
189180

190181
/**

tests/header_set.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/test-headers.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var sys = require("util")
88
var server = http.createServer(function (req, res) {
99
// Test setRequestHeader
1010
assert.equal("Foobar", req.headers["x-test"]);
11+
// Test non-conforming allowed header
12+
assert.equal("node-XMLHttpRequest-test", req.headers["user-agent"]);
13+
// Test header set with blacklist disabled
14+
assert.equal("http://github.com", req.headers["referer"]);
1115

1216
var body = "Hello World";
1317
res.writeHead(200, {
@@ -17,6 +21,7 @@ var server = http.createServer(function (req, res) {
1721
// Actual values don't matter
1822
"Set-Cookie": "foo=bar",
1923
"Set-Cookie2": "bar=baz",
24+
"Date": "Thu, 30 Aug 2012 18:17:53 GMT",
2025
"Connection": "close"
2126
});
2227
res.write("Hello World");
@@ -28,7 +33,7 @@ var server = http.createServer(function (req, res) {
2833
xhr.onreadystatechange = function() {
2934
if (this.readyState == 4) {
3035
// Test getAllResponseHeaders()
31-
var headers = "content-type: text/plain\r\ncontent-length: 11\r\nconnection: close";
36+
var headers = "content-type: text/plain\r\ncontent-length: 11\r\ndate: Thu, 30 Aug 2012 18:17:53 GMT\r\nconnection: close";
3237
assert.equal(headers, this.getAllResponseHeaders());
3338

3439
// Test case insensitivity
@@ -53,8 +58,18 @@ try {
5358
xhr.setRequestHeader("X-Test", "Foobar");
5459
// Invalid header
5560
xhr.setRequestHeader("Content-Length", 0);
61+
// Allowed header outside of specs
62+
xhr.setRequestHeader("user-agent", "node-XMLHttpRequest-test");
5663
// Test getRequestHeader
5764
assert.equal("Foobar", xhr.getRequestHeader("X-Test"));
65+
// Test invalid header
66+
assert.equal("", xhr.getRequestHeader("Content-Length"));
67+
68+
// Test allowing all headers
69+
xhr.setDisableHeaderCheck(true);
70+
xhr.setRequestHeader("Referer", "http://github.com");
71+
assert.equal("http://github.com", xhr.getRequestHeader("Referer"));
72+
5873
xhr.send();
5974
} catch(e) {
6075
console.log("ERROR: Exception raised", e);

0 commit comments

Comments
 (0)