Skip to content

Commit

Permalink
Merge pull request #81 from VKCOM/di/fix-websocket-auth-crash/QA-16055
Browse files Browse the repository at this point in the history
Add try catch to cookie parser
  • Loading branch information
DaniilSmirnov authored Jan 21, 2025
2 parents 33a51ea + 995ccc9 commit ee75e45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bin/stf.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env -S node --import ./lib/util/instrument.mjs
console.log('Starting stf')
console.log('Starting DeviceHub')
import '../lib/cli/index.js'
1 change: 0 additions & 1 deletion lib/units/websocket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const request = Promise.promisifyAll(postmanRequest)
export default (function(options) {
var log = logger.createLogger('websocket')
var server = http.createServer()
console.log(options)
// eslint-disable-next-line camelcase
const io_options = {
serveClient: false
Expand Down
25 changes: 18 additions & 7 deletions lib/units/websocket/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
import * as dbapi from '../../../db/api.js'
import * as jwtutil from '../../../util/jwtutil.js'
import * as cookie from 'cookie'
import logger from '../../../util/logger.js'


export default (function(options) {
const log = logger.createLogger('websocket')
return function(socket, next) {
let req = socket.request
let token
const cookies = cookie.parse(req.headers.cookie)
let token, cookies
try {
cookies = cookie.parse(req.headers.cookie)
}
catch (e) {
return next(new Error('Missing authorization token'))
}
if (cookies.token) {
token = jwtutil.decode(cookies.token, options.secret)
req.internalJwt = cookies.token
}
else {
next(new Error('Missing authorization token'))
return next(new Error('Missing authorization token'))
}
if (token) {
return dbapi.loadUser(token.email)
.then(function(user) {
if (user) {
req.user = user
next()
return next()
}
else {
next(new Error('Invalid user'))
return next(new Error('Invalid user'))
}
})
.catch(next)
.catch((e) => {
log.error(e)
return next(new Error('Unknown error'))
})
}
else {
next(new Error('Missing authorization token'))
return next(new Error('Missing authorization token'))
}
}
})

0 comments on commit ee75e45

Please sign in to comment.