Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): isolate Sentry user scope to per-request async context

justin56e045b5535e
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 0a2909596..0d67199be 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -580,21 +580,24 @@ export function createApp() {
) {
return next();
}
- try {
- await authenticateApiRequest(c);
- } catch (error) {
- if (error instanceof HTTPException) {
+ return Sentry.withScope(async () => {
+ Sentry.setUser(null);
+ try {
+ await authenticateApiRequest(c);
+ const windowId = c.req.header("X-Kaneo-Window-Id");
+ const userId = c.get("userId");
+ const initiatorId = windowId ? `${userId}:${windowId}` : userId;
+ return await eventContext.run({ initiatorId }, next);
+ } catch (error) {
+ if (!(error instanceof HTTPException)) {
+ console.error("API authentication failed:", error);
+ throw new HTTPException(500, { message: "Internal Server Error" });
+ }
throw error;
+ } finally {
+ Sentry.setUser(null);
}
- console.error("API authentication failed:", error);
- throw new HTTPException(500, { message: "Internal Server Error" });
- }
-
- const windowId = c.req.header("X-Kaneo-Window-Id");
- const userId = c.get("userId");
- const initiatorId = windowId ? `${userId}:${windowId}` : userId;
-
- return eventContext.run({ initiatorId }, next);
+ });
});
const oauthApi = api.route("/oauth", oauth);
diff --git a/apps/api/src/utils/authenticate-api-request.ts b/apps/api/src/utils/authenticate-api-request.ts
index b8c6718e3..908f099f7 100644
--- a/apps/api/src/utils/authenticate-api-request.ts
+++ b/apps/api/src/utils/authenticate-api-request.ts
@@ -5,11 +5,9 @@ import { HTTPException } from "hono/http-exception";
import { auth } from "../auth";
import { verifyApiKey } from "./verify-api-key";
-// ponytail: Sentry's user scope is per-process in @sentry/node. Concurrent
-// requests from different users can race the per-context user id. The proper
-// fix is to wrap each request in Sentry.runWithAsyncContext, which requires
-// wiring AsyncLocalStorage through Hono's middleware chain. Tagging here is
-// best-effort attribution; do not rely on it for cross-user filtering.
+// User is tagged on Sentry's current scope; the per-request scope is forked
+// by Sentry.withScope in the api.use("*", ...) middleware, so this only
+// affects the in-flight request.
function attachUserToScope(userId: string) {
Sentry.setUser({ id: userId });
}