Protected socket.io connection with csrf-sync to prevent cross-site connections.

This commit is contained in:
rainbow napkin 2024-12-29 23:06:11 -05:00
parent 6c379321f7
commit 4a865e8aa8
3 changed files with 16 additions and 3 deletions

View file

@ -19,6 +19,7 @@ const channelModel = require('../../schemas/channel/channelSchema');
const emoteModel = require('../../schemas/emoteSchema');
const {userModel} = require('../../schemas/user/userSchema');
const loggerUtils = require('../../utils/loggerUtils');
const csrfUtils = require('../../utils/csrfUtils');
const activeChannel = require('./activeChannel');
const chatHandler = require('./chatHandler');
@ -80,6 +81,11 @@ module.exports = class{
}
async authSocket(socket){
//Check for Cross-Site Request Forgery
if(!csrfUtils.isRequestValid(socket.request)){
throw new Error("Invalid CSRF Token!");
}
//Find the user in the Database since the session won't store enough data to fulfill our needs :P
const userDB = await userModel.findOne({user: socket.request.session.user.user});