Skip to content

Commit a57c910

Browse files
committed
Allow loopback address urls as exception to loading from secure contexts
Fixes #486
1 parent a098d4e commit a57c910

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ function SockJS(url, protocols, options) {
7777
var secure = parsedUrl.protocol === 'https:';
7878
// Step 2 - don't allow secure origin with an insecure protocol
7979
if (loc.protocol === 'https:' && !secure) {
80-
throw new Error('SecurityError: An insecure SockJS connection may not be initiated from a page loaded over HTTPS');
80+
// exception is 127.0.0.0/8 and ::1 urls
81+
if (!urlUtils.isLoopbackAddr(parsedUrl.host)) {
82+
throw new Error('SecurityError: An insecure SockJS connection may not be initiated from a page loaded over HTTPS');
83+
}
8184
}
8285

8386
// Step 3 - check port access - no need here

lib/utils/url.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ module.exports = {
4444
, addQuery: function (url, q) {
4545
return url + (url.indexOf('?') === -1 ? ('?' + q) : ('&' + q));
4646
}
47+
48+
, isLoopbackAddr: function (addr) {
49+
return /^127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || /^::1$/.test(addr);
50+
}
4751
};

tests/lib/main-node.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ describe('SockJS', function() {
2222
});
2323
});
2424

25+
// https://www.w3.org/TR/secure-contexts/#is-origin-trustworthy
26+
it('should NOT throw SecurityError for 127.0.0.1/8 url from a secure page', function () {
27+
expect(function () {
28+
sjs('http://127.0.0.1');
29+
}).to.not.throwException();
30+
});
31+
32+
it('should NOT throw SecurityError for ::1 url from a secure page', function () {
33+
expect(function () {
34+
sjs('http://::1');
35+
}).to.not.throwException();
36+
});
37+
2538
it('should throw SyntaxError for an invalid url', function () {
2639
expect(function () {
2740
sjs('//localhost');

0 commit comments

Comments
 (0)