|
|
|
|
|
by austincheney
2222 days ago
|
|
I just tested an approach to deny access to WebSockets in the browser. This only applies if the JavaScript and the page comes from a location you both control and your goal is to limit access from third party scripts and you don't have access to the page's server to add a Content Security Policy (CSP) rule restricting web socket addresses/ports to specified rules. TypeScript code: const sock:WebSocketLocal = (function local_socket():WebSocketLocal {
// A minor security circumvention.
const socket:WebSocketLocal = <WebSocketLocal>WebSocket;
WebSocket = null;
return socket;
}());
TypeScript definitions (index.d.ts): interface WebSocketLocal extends WebSocket {
new (address:string): WebSocket;
}
If the 'sock' variable is not globally scoped it cannot be globally accessed. This means third party scripts must know the name of the variable and be able to access the scope where the variable is declared, because the global variable name "WebSockets" is reassigned to null and any attempts to access it will break those third party scripts. |
|