mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-08 08:39:59 +00:00
Restrict daemon HTTP requests to extension origins
This commit is contained in:
@@ -71,34 +71,40 @@ export function init(port: number, serverPassword?: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server.on("upgrade", (req, socket, head) => {
|
server.on("upgrade", (req, socket, head) => {
|
||||||
|
/**
|
||||||
|
* Only accept authenticated WebSocket requests from extension
|
||||||
|
* origins.
|
||||||
|
*/
|
||||||
if (
|
if (
|
||||||
// Only accept WebSocket requests from extension origins
|
req.headers.origin?.startsWith("moz-extension://") &&
|
||||||
!req.headers.origin?.startsWith("moz-extension://") ||
|
authenticate(req)
|
||||||
!authenticate(req)
|
|
||||||
) {
|
) {
|
||||||
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
wss.handleUpgrade(req, socket, head, ws => {
|
||||||
socket.destroy();
|
wss.emit("connection", ws, req);
|
||||||
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
wss.handleUpgrade(req, socket, head, ws => {
|
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
||||||
wss.emit("connection", ws, req);
|
socket.destroy();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JS WebSocket API does not allow access to connection errors, so
|
* Browser WebSocket API does not allow access to connection errors,
|
||||||
* provide an endpoint for feedback on invalid authentication.
|
* so provide an endpoint for feedback on invalid authentication.
|
||||||
*/
|
*/
|
||||||
server.on("request", (req, res) => {
|
server.on("request", (req, res) => {
|
||||||
if (!authenticate(req)) {
|
/**
|
||||||
res.writeHead(401);
|
* Requests from extensions have their origin header stripped,
|
||||||
res.end();
|
* so block all requests with origin headers.
|
||||||
|
*/
|
||||||
|
if ("origin" in req.headers) {
|
||||||
|
req.destroy();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.writeHead(200);
|
res.writeHead(authenticate(req) ? 200 : 401);
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user