Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apis/link/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export function handleRelaySocket(socket: WebSocket, app: FastifyInstance) {
return;
}

if (session.status !== "approved") {
sendJson(socket, { type: "relay.reject", reason: "Session not approved" });
socket.close();
return;
}

// Verify deviceId matches session participants
if (role === "desktop" && session.desktopDeviceId !== deviceId) {
sendJson(socket, { type: "relay.reject", reason: "deviceId mismatch" });
Expand Down
19 changes: 19 additions & 0 deletions convex/linkSessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,23 @@ describe("get", () => {

expect(result).toBeNull();
});

it("does not return expired sessions", async () => {
const ctx = makeCtx(userId);
const id = await ctx.db.insert("linkSessions", {
userId,
desktopDeviceId: "d1",
mobileDeviceId: "m1",
mode: "mirror",
status: "approved",
transport: "websocket",
createdAt: Date.now() - 7200000,
expiresAt: Date.now() - 3600000,
});

const result = await (get as any).handler(ctx, { sessionId: id });

expect(result).toBeNull();
});

});
7 changes: 7 additions & 0 deletions convex/linkSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { query, mutation } from "./_generated/server";
import { v } from "convex/values";
import { getAuthUserId } from "@convex-dev/auth/server";

function isExpired(session: { expiresAt: number }) {
return session.expiresAt <= Date.now();
}

export const create = mutation({
args: {
desktopDeviceId: v.string(),
Expand Down Expand Up @@ -41,6 +45,7 @@ export const updateStatus = mutation({
if (!userId) throw new Error("Not authenticated");
const session = await ctx.db.get(args.sessionId);
if (!session || session.userId !== userId) throw new Error("Session not found");
if (isExpired(session)) throw new Error("Session expired");
await ctx.db.patch(args.sessionId, { status: args.status });
},
});
Expand All @@ -52,6 +57,7 @@ export const updateTransport = mutation({
if (!userId) throw new Error("Not authenticated");
const session = await ctx.db.get(args.sessionId);
if (!session || session.userId !== userId) throw new Error("Session not found");
if (isExpired(session)) throw new Error("Session expired");
await ctx.db.patch(args.sessionId, { transport: args.transport });
},
});
Expand All @@ -63,6 +69,7 @@ export const get = query({
if (!userId) throw new Error("Not authenticated");
const session = await ctx.db.get(args.sessionId);
if (!session || session.userId !== userId) return null;
if (isExpired(session)) return null;
return session;
},
});
Expand Down
Loading