Skip to content

Commit

Permalink
Disallow WS creation if room has restricted access
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleMalt committed Jul 9, 2022
1 parent 7a1e0a1 commit 162d1ff
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pantry/services/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,10 @@ function addWebsocket(server) {
const wss = new WebSocket.Server({noServer: true});
wss.on('connection', handleConnection);

server.on('upgrade', (req, socket, head) => {
server.on('upgrade', async (req, socket, head) => {
let [path, query] = req.url.split('?');
let [roomId] = path.split('/').filter(t => t);
let params = querystring.parse(query);

let {id: peerId, subs, token} = params;

// this is for forwarding messages to other containers
Expand All @@ -234,10 +233,15 @@ function addWebsocket(server) {
internal = true;
}

let roomInfo = await get('rooms/' + roomId);

let publicKey = peerId?.split('.')[0];
if (
peerId === undefined ||
((roomId === undefined || !ssrVerifyToken(token, publicKey)) && !internal)
((roomId === undefined || !ssrVerifyToken(token, publicKey)) &&
!internal) ||
(roomInfo.access?.identities &&
!roomInfo.access?.identities.includes(publicKey))
) {
console.log('ws rejected!', req.url, 'room', roomId, 'peer', peerId);
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
Expand Down

0 comments on commit 162d1ff

Please sign in to comment.