Skip to content

Commit

Permalink
fix: crash on error in sessionPublicContext (#3271)
Browse files Browse the repository at this point in the history
* fix: crash on error in sessionPublicContext

* chore: throw on web-socket

---------

Co-authored-by: Nicolas Burtey <[email protected]>
  • Loading branch information
nicolasburtey and Nicolas Burtey authored Sep 27, 2023
1 parent 102643b commit 7496429
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/servers/graphql-main-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ACCOUNT_USERNAME,
SemanticAttributes,
addAttributesToCurrentSpanAndPropagate,
recordExceptionInCurrentSpan,
} from "@services/tracing"

import { NextFunction, Request, Response } from "express"
Expand Down Expand Up @@ -45,6 +46,15 @@ const setGqlContext = async (
ip,
})

if (gqlContext instanceof Error) {
recordExceptionInCurrentSpan({
error: gqlContext,
fallbackMsg: "error executing sessionPublicContext",
})
next(gqlContext)
return
}

req.gqlContext = gqlContext

return addAttributesToCurrentSpanAndPropagate(
Expand Down
8 changes: 3 additions & 5 deletions src/servers/middlewares/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Accounts, Transactions } from "@app"
import { recordExceptionInCurrentSpan } from "@services/tracing"
import jsonwebtoken from "jsonwebtoken"

import { mapError } from "@graphql/error-map"

import { maybeExtendSession } from "@app/authentication"
import { checkedToUserId } from "@domain/accounts"
import { ValidationError } from "@domain/shared"
Expand All @@ -18,7 +16,7 @@ export const sessionPublicContext = async ({
}: {
tokenPayload: jsonwebtoken.JwtPayload
ip: IpAddress | undefined
}): Promise<GraphQLPublicContext> => {
}): Promise<GraphQLPublicContext | Error> => {
const logger = baseLogger.child({ tokenPayload })

let domainAccount: Account | undefined
Expand All @@ -35,7 +33,7 @@ export const sessionPublicContext = async ({
const userId = maybeUserId
const account = await Accounts.getAccountFromUserId(userId)
if (account instanceof Error) {
throw mapError(account)
return account
} else {
domainAccount = account
// not awaiting on purpose. just updating metadata
Expand All @@ -51,7 +49,7 @@ export const sessionPublicContext = async ({
}

const userRes = await UsersRepository().findById(account.kratosUserId)
if (userRes instanceof Error) throw mapError(userRes)
if (userRes instanceof Error) return userRes
user = userRes
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/servers/ws-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ const getContext = async (
sub: kratosCookieRes.kratosUserId,
}

return sessionPublicContext({
const context = await sessionPublicContext({
tokenPayload,
ip,
})

if (context instanceof Error) throw context
return context
}

const kratosToken = authz?.slice(7) as AuthToken
Expand All @@ -106,10 +109,13 @@ const getContext = async (
return false
}

return sessionPublicContext({
const context = await sessionPublicContext({
tokenPayload,
ip,
})

if (context instanceof Error) throw context
return context
},
})()
}
Expand Down

0 comments on commit 7496429

Please sign in to comment.