Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 4843ac4

Browse files
committed
improve origin check in telemetry.js (follow up to PR #236 and issue #223)
1 parent e3f39a6 commit 4843ac4

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

Assets/WebGLTemplates/WebVR/lib/telemetry.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ var endsWith = function (str, suffix) {
2626
return str.indexOf(suffix, str.length - suffix.length) !== -1;
2727
};
2828

29-
// Check if the origin looks like a production, non-development host (i.e., public and served over HTTPS).
29+
// Check if the origin looks like a non-public development host.
3030
// Relevant reading: https://w3c.github.io/webappsec-secure-contexts/#localhost
31-
var isInsecureOrigin = function (win) {
32-
// Allow HTTPS and HTTP.
33-
if (win.isSecureContext === true || win.location.protocol === 'http:') {
31+
var isDevOrigin = function (win) {
32+
if (win.location.hostname === 'localhost' ||
33+
endsWith(win.location.hostname, '.localhost') ||
34+
win.location.hostname === '127.0.1' ||
35+
win.location.hostname.indexOf('192.168.') === 0 ||
36+
win.location.hostname === '0.0.0.0' ||
37+
win.location.host.indexOf('::1') === 0 ||
38+
endsWith(win.location.hostname, '.ngrok.io') ||
39+
endsWith(win.location.hostname, '.localtunnel.me')) {
40+
return true;
41+
}
42+
// A production URL can start with `http://` or `https://` (but not `file:///`).
43+
if (win.location.protocol === 'http:') {
44+
return false;
45+
}
46+
// Do not allow insecure-context origin (e.g., `file:///` paths).
47+
if ('isSecureContext' in win && win.isSecureContext === true) {
3448
return false;
3549
}
36-
return (
37-
win.location.hostname === 'localhost' ||
38-
endsWith(win.location.hostname, '.localhost') ||
39-
win.location.hostname === '127.0.1' ||
40-
win.location.hostname === '0.0.0.0' ||
41-
win.location.host.indexOf('::1') === 0 ||
42-
endsWith(win.location.hostname, '.ngrok.io') ||
43-
endsWith(win.location.hostname, '.localtunnel.me')
44-
);
50+
return true;
4551
};
4652

4753
var CURRENT_VERSION = '1.1.0';
@@ -228,7 +234,7 @@ function doNotTrack () {
228234

229235
function isTelemetryDisabled () {
230236
// Telemetry is disabled if DNT is enabled or if the origin appears to be for a development environment.
231-
return doNotTrack() || isInsecureOrigin(window);
237+
return doNotTrack() || isDevOrigin(window);
232238
}
233239

234240
})(window);

Build/lib/telemetry.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ var endsWith = function (str, suffix) {
2626
return str.indexOf(suffix, str.length - suffix.length) !== -1;
2727
};
2828

29-
// Check if the origin looks like a production, non-development host (i.e., public and served over HTTPS).
29+
// Check if the origin looks like a non-public development host.
3030
// Relevant reading: https://w3c.github.io/webappsec-secure-contexts/#localhost
31-
var isInsecureOrigin = function (win) {
32-
// Allow HTTPS and HTTP.
33-
if (win.isSecureContext === true || win.location.protocol === 'http:') {
31+
var isDevOrigin = function (win) {
32+
if (win.location.hostname === 'localhost' ||
33+
endsWith(win.location.hostname, '.localhost') ||
34+
win.location.hostname === '127.0.1' ||
35+
win.location.hostname.indexOf('192.168.') === 0 ||
36+
win.location.hostname === '0.0.0.0' ||
37+
win.location.host.indexOf('::1') === 0 ||
38+
endsWith(win.location.hostname, '.ngrok.io') ||
39+
endsWith(win.location.hostname, '.localtunnel.me')) {
40+
return true;
41+
}
42+
// A production URL can start with `http://` or `https://` (but not `file:///`).
43+
if (win.location.protocol === 'http:') {
44+
return false;
45+
}
46+
// Do not allow insecure-context origin (e.g., `file:///` paths).
47+
if ('isSecureContext' in win && win.isSecureContext === true) {
3448
return false;
3549
}
36-
return (
37-
win.location.hostname === 'localhost' ||
38-
endsWith(win.location.hostname, '.localhost') ||
39-
win.location.hostname === '127.0.1' ||
40-
win.location.hostname === '0.0.0.0' ||
41-
win.location.host.indexOf('::1') === 0 ||
42-
endsWith(win.location.hostname, '.ngrok.io') ||
43-
endsWith(win.location.hostname, '.localtunnel.me')
44-
);
50+
return true;
4551
};
4652

4753
var CURRENT_VERSION = '1.1.0';
@@ -228,7 +234,7 @@ function doNotTrack () {
228234

229235
function isTelemetryDisabled () {
230236
// Telemetry is disabled if DNT is enabled or if the origin appears to be for a development environment.
231-
return doNotTrack() || isInsecureOrigin(window);
237+
return doNotTrack() || isDevOrigin(window);
232238
}
233239

234240
})(window);

0 commit comments

Comments
 (0)