Skip to content

Commit f6c6af6

Browse files
committed
fix: regression in for checking Origin header
1 parent 56a92c2 commit f6c6af6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/Server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,12 @@ Server.prototype.checkHost = function (headers, headerToCheck) {
521521
if (!hostHeader) return false;
522522

523523
// use the node url-parser to retrieve the hostname from the host-header.
524-
const hostname = url.parse(`//${hostHeader}`, false, true).hostname;
524+
const hostname = url.parse(
525+
// if hostHeader doesn't have scheme, add // for parsing.
526+
/^(.+:)?\/\//.test(hostHeader) ? hostHeader : `//${hostHeader}`,
527+
false,
528+
true,
529+
).hostname;
525530

526531
// always allow requests with explicit IPv4 or IPv6-address.
527532
// A note on IPv6 addresses: hostHeader will always contain the brackets denoting

test/Validation.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ describe('Validation', () => {
144144
}
145145
});
146146

147+
it('should allow urls with scheme for checking origin', () => {
148+
const options = {
149+
public: 'test.host:80'
150+
};
151+
const headers = {
152+
origin: 'https://test.host'
153+
};
154+
const server = new Server(compiler, options);
155+
if (!server.checkHost(headers, 'origin')) {
156+
throw new Error("Validation didn't fail");
157+
}
158+
});
159+
147160
describe('allowedHosts', () => {
148161
it('should allow hosts in allowedHosts', () => {
149162
const testHosts = [

0 commit comments

Comments
 (0)