Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(sync): better token validation on connect (already deployed) (#5109)

teo11470c68aa77
diff --git a/services/sync-service/src/durable_object.rs b/services/sync-service/src/durable_object.rs
index 6c7d8fd3de..fd206e0f0b 100644
--- a/services/sync-service/src/durable_object.rs
+++ b/services/sync-service/src/durable_object.rs
@@ -641,6 +641,7 @@ impl DocumentSyncSession {
async fn connect_handler(&self, req: Request, document_id: &str) -> Result<Response> {
let (res, elap) = timeit!({
let claims = or_unauth!(decode_jwt(&req, &self.env, TokenFrom::QueryParams).ok());
+ or_unauth!(claims.has_document_id_access(document_id).then_some(()));
if self.maybe_set_document_id(document_id).await? {
trace!("init document_id={document_id}");
} else {
diff --git a/services/sync-service/tests/permissions.test.ts b/services/sync-service/tests/permissions.test.ts
index f8736c1dd8..15676bf5ba 100644
--- a/services/sync-service/tests/permissions.test.ts
+++ b/services/sync-service/tests/permissions.test.ts
@@ -3,6 +3,7 @@ import { test, describe, beforeAll, expect } from "vitest";
import {
connectToDocumentForTesting,
createTestUser,
+ getTokenForDocument,
setupMiniflare,
} from "./utils";
import { FromPeer, FromRemote } from "../bebop/generated/schema";
@@ -14,6 +15,18 @@ beforeAll(async () => {
});
describe("should respect permissions", () => {
+ test("should reject a WebSocket token issued for a different document", async () => {
+ const token = getTokenForDocument("authorized-document", "test-user", "owner");
+
+ const response = await mf.dispatchFetch(
+ `http://localhost:8787/document/unauthorized-document/connect?token=${token}`,
+ { headers: { Upgrade: "websocket" } },
+ );
+
+ expect(response.status).toBe(401);
+ expect(response.webSocket).toBeNull();
+ });
+
test("should not allow view only user to push updates", async () => {
const userA = await createTestUser(mf, "test", {
userId: "view-only-user",