-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add real-time issue updates across API, live service, and web #7912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { CollaborationController } from "./collaboration.controller"; | ||
| import { ConvertDocumentController } from "./convert-document.controller"; | ||
| import { HealthController } from "./health.controller"; | ||
| import { IssueEventsController } from "./issue-events.controller"; | ||
|
|
||
| export const CONTROLLERS = [CollaborationController, ConvertDocumentController, HealthController]; | ||
| export const CONTROLLERS = [CollaborationController, ConvertDocumentController, HealthController, IssueEventsController]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import type Redis from "ioredis"; | ||
| import type { Request } from "express"; | ||
| import type WebSocket from "ws"; | ||
| // plane imports | ||
| import { Controller, WebSocket as WSDecorator } from "@plane/decorators"; | ||
| import { logger } from "@plane/logger"; | ||
| // redis | ||
| import { redisManager } from "@/redis"; | ||
| // auth | ||
| import { handleAuthentication } from "@/lib/auth"; | ||
|
|
||
| type TokenPayload = { | ||
| id?: string; | ||
| cookie?: string; | ||
| }; | ||
|
|
||
| type ConnectionParams = { | ||
| projectId: string; | ||
| workspaceSlug: string; | ||
| token: string; | ||
| }; | ||
|
|
||
| const getFirstQueryValue = (value?: string | string[]) => (Array.isArray(value) ? value[0] : value); | ||
|
|
||
| const extractConnectionParams = (req: Request): ConnectionParams | null => { | ||
| const query = req.query as Record<string, string | string[]>; | ||
| const projectId = getFirstQueryValue(query.projectId); | ||
| const workspaceSlug = getFirstQueryValue(query.workspaceSlug); | ||
| const token = getFirstQueryValue(query.token); | ||
|
|
||
| if (!projectId || !workspaceSlug || !token) { | ||
| return null; | ||
| } | ||
|
|
||
| return { projectId, workspaceSlug, token }; | ||
| }; | ||
|
|
||
| const parseToken = (rawToken: string): TokenPayload | null => { | ||
| try { | ||
| const parsed: unknown = JSON.parse(rawToken); | ||
| if (!parsed || typeof parsed !== "object") { | ||
| return null; | ||
| } | ||
| return parsed as TokenPayload; | ||
| } catch (error) { | ||
| logger.error("Invalid token payload for issue events", error); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| const closeSocket = (ws: WebSocket, code: number, reason: string) => { | ||
| if (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| ws.close(code, reason); | ||
| } catch (error) { | ||
| logger.error("Issue events websocket close failure", error); | ||
| } | ||
| }; | ||
|
|
||
| const ensureAuthenticated = async (ws: WebSocket, token: TokenPayload, req: Request) => { | ||
| const cookie = token?.cookie || req.headers.cookie || ""; | ||
| if (cookie) { | ||
| try { | ||
| await handleAuthentication({ | ||
| cookie, | ||
| userId: token?.id ?? "", | ||
| }); | ||
| return true; | ||
| } catch (error) { | ||
| logger.error("Failed to authenticate issue events connection", error); | ||
| closeSocket(ws, 4003, "Unauthorized"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (!token?.id) { | ||
| closeSocket(ws, 4003, "Unauthorized"); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Controller("/issues") | ||
| export class IssueEventsController { | ||
| @WSDecorator("/") | ||
| async handleConnection(ws: WebSocket, req: Request) { | ||
| const params = extractConnectionParams(req); | ||
| if (!params) { | ||
| closeSocket(ws, 4001, "Missing required parameters"); | ||
| return; | ||
| } | ||
|
|
||
| const tokenPayload = parseToken(params.token); | ||
| if (!tokenPayload) { | ||
| closeSocket(ws, 4002, "Invalid token"); | ||
| return; | ||
| } | ||
|
|
||
| const authenticated = await ensureAuthenticated(ws, tokenPayload, req); | ||
| if (!authenticated) { | ||
| return; | ||
| } | ||
|
|
||
| const redisClient = redisManager.getClient(); | ||
| if (!redisClient) { | ||
| closeSocket(ws, 1011, "Realtime service unavailable"); | ||
| return; | ||
| } | ||
|
|
||
| let subscriber: Redis; | ||
| try { | ||
| subscriber = redisClient.duplicate(); | ||
| } catch (error) { | ||
| logger.error("Failed to create issue events redis subscriber", error); | ||
| closeSocket(ws, 1011, "Realtime service unavailable"); | ||
| return; | ||
| } | ||
|
|
||
| const channel = `issue_events:${params.projectId}`; | ||
| let cleanupStarted = false; | ||
|
|
||
| const cleanup = async () => { | ||
| if (cleanupStarted) return; | ||
| cleanupStarted = true; | ||
|
|
||
| subscriber.removeAllListeners("message"); | ||
| subscriber.removeAllListeners("error"); | ||
|
|
||
| try { | ||
| await subscriber.unsubscribe(channel); | ||
| } catch (error) { | ||
| logger.error("Failed to unsubscribe issue events channel", error); | ||
| } | ||
|
|
||
| try { | ||
| subscriber.disconnect(); | ||
| } catch (error) { | ||
| logger.error("Failed to disconnect issue events subscriber", error); | ||
| } | ||
| }; | ||
|
|
||
| try { | ||
| subscriber.on("error", (error) => { | ||
| logger.error("Issue events redis subscriber error", error); | ||
| closeSocket(ws, 1011, "Realtime service unavailable"); | ||
| void cleanup(); | ||
| }); | ||
|
|
||
| await subscriber.connect(); | ||
|
|
||
| subscriber.on("message", (incomingChannel, message) => { | ||
| if (incomingChannel === channel && ws.readyState === ws.OPEN) { | ||
| ws.send(message); | ||
| } | ||
| }); | ||
|
|
||
| await subscriber.subscribe(channel); | ||
|
|
||
| ws.on("close", () => { | ||
| void cleanup(); | ||
| }); | ||
|
|
||
| ws.on("error", (error) => { | ||
| logger.error("Issue events websocket error", error); | ||
| closeSocket(ws, 1011, "Issue events websocket error"); | ||
| void cleanup(); | ||
| }); | ||
| } catch (error) { | ||
| logger.error("Failed to subscribe to issue events channel", error); | ||
| closeSocket(ws, 1011, "Subscription failure"); | ||
| void cleanup(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: WebSocket Authentication Bypass
The
ensureAuthenticatedfunction allows WebSocket connections to proceed without proper authentication when atoken.idis present but no cookie is provided. This bypasses thehandleAuthenticationcheck, potentially allowing unauthorized access.