jsgotty 1.1.9 → 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gotty.js +26 -6
- package/package.json +1 -1
- package/tests/websocket-origin.test.js +54 -0
package/gotty.js
CHANGED
|
@@ -800,6 +800,28 @@ function basicAuthAuthorized(req, credential) {
|
|
|
800
800
|
return payload === credential;
|
|
801
801
|
}
|
|
802
802
|
|
|
803
|
+
// Match gorilla/websocket's default policy used by upstream GoTTY: non-browser
|
|
804
|
+
// clients without Origin are accepted, while browser connections must come
|
|
805
|
+
// from the same host. --ws-origin intentionally replaces this default check.
|
|
806
|
+
function webSocketOriginAllowed(req, originMatcher = null) {
|
|
807
|
+
const origin = req.headers.origin;
|
|
808
|
+
|
|
809
|
+
if (originMatcher) {
|
|
810
|
+
return originMatcher.test(origin || "");
|
|
811
|
+
}
|
|
812
|
+
if (!origin) {
|
|
813
|
+
return true;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
try {
|
|
817
|
+
const originHost = new URL(origin).host;
|
|
818
|
+
const requestHost = req.headers.host || "";
|
|
819
|
+
return originHost !== "" && originHost.toLowerCase() === requestHost.toLowerCase();
|
|
820
|
+
} catch {
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
803
825
|
function sendBasicAuthRequired(res) {
|
|
804
826
|
res.writeHead(401, {
|
|
805
827
|
"WWW-Authenticate": 'Basic realm="GoTTY"',
|
|
@@ -2940,12 +2962,9 @@ function createServerRuntime(command, argv, options) {
|
|
|
2940
2962
|
return;
|
|
2941
2963
|
}
|
|
2942
2964
|
|
|
2943
|
-
if (originMatcher) {
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
socket.destroy();
|
|
2947
|
-
return;
|
|
2948
|
-
}
|
|
2965
|
+
if (!webSocketOriginAllowed(req, originMatcher)) {
|
|
2966
|
+
socket.destroy();
|
|
2967
|
+
return;
|
|
2949
2968
|
}
|
|
2950
2969
|
|
|
2951
2970
|
if (options.once && acceptedOnce && !options.reconnect) {
|
|
@@ -3295,6 +3314,7 @@ async function bootstrap() {
|
|
|
3295
3314
|
|
|
3296
3315
|
module.exports.CursorStateTracker = CursorStateTracker;
|
|
3297
3316
|
module.exports.KittyGraphicsParser = KittyGraphicsParser;
|
|
3317
|
+
module.exports.webSocketOriginAllowed = webSocketOriginAllowed;
|
|
3298
3318
|
|
|
3299
3319
|
if (require.main === module) {
|
|
3300
3320
|
if (KITTY_TRACE) {
|
package/package.json
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const assert = require("node:assert/strict");
|
|
2
|
+
const test = require("node:test");
|
|
3
|
+
const { webSocketOriginAllowed } = require("../gotty.js");
|
|
4
|
+
|
|
5
|
+
function request(host, origin) {
|
|
6
|
+
return {
|
|
7
|
+
headers: {
|
|
8
|
+
host,
|
|
9
|
+
...(origin === undefined ? {} : { origin }),
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
test("WebSocket permits clients without Origin", () => {
|
|
15
|
+
assert.equal(webSocketOriginAllowed(request("127.0.0.1:8080")), true);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("WebSocket permits same-origin browsers", () => {
|
|
19
|
+
assert.equal(
|
|
20
|
+
webSocketOriginAllowed(request("127.0.0.1:8080", "http://127.0.0.1:8080")),
|
|
21
|
+
true,
|
|
22
|
+
);
|
|
23
|
+
assert.equal(
|
|
24
|
+
webSocketOriginAllowed(request("Example.COM:8080", "https://example.com:8080")),
|
|
25
|
+
true,
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("WebSocket rejects cross-origin and malformed browser origins", () => {
|
|
30
|
+
assert.equal(
|
|
31
|
+
webSocketOriginAllowed(request("127.0.0.1:8080", "https://attacker.example")),
|
|
32
|
+
false,
|
|
33
|
+
);
|
|
34
|
+
assert.equal(webSocketOriginAllowed(request("127.0.0.1:8080", "null")), false);
|
|
35
|
+
assert.equal(webSocketOriginAllowed(request("127.0.0.1:8080", "not a URL")), false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("--ws-origin regex replaces the same-origin default", () => {
|
|
39
|
+
const matcher = /^https:\/\/trusted\.example$/;
|
|
40
|
+
assert.equal(
|
|
41
|
+
webSocketOriginAllowed(
|
|
42
|
+
request("127.0.0.1:8080", "https://trusted.example"),
|
|
43
|
+
matcher,
|
|
44
|
+
),
|
|
45
|
+
true,
|
|
46
|
+
);
|
|
47
|
+
assert.equal(
|
|
48
|
+
webSocketOriginAllowed(
|
|
49
|
+
request("127.0.0.1:8080", "http://127.0.0.1:8080"),
|
|
50
|
+
matcher,
|
|
51
|
+
),
|
|
52
|
+
false,
|
|
53
|
+
);
|
|
54
|
+
});
|