Skip to content
Closed
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
70 changes: 60 additions & 10 deletions convex/accessControl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { makeFunctionReference } from "convex/server";
import { describe, expect, test } from "vitest";
import type { DataModel } from "./_generated/dataModel";
import { CURRENT_CLOUD_PROTOCOL_VERSION } from "./lib/cloudProtocol";
import schema from "./schema";
import { modules } from "./test.setup";

Expand Down Expand Up @@ -55,9 +56,15 @@ async function createHarness(): Promise<{
const b = t.withIdentity(identity("b"));
const c = t.withIdentity(identity("c"));
await Promise.all([
a.mutation(ensureCurrentUser, {}),
b.mutation(ensureCurrentUser, {}),
c.mutation(ensureCurrentUser, {}),
a.mutation(ensureCurrentUser, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
}),
b.mutation(ensureCurrentUser, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
}),
c.mutation(ensureCurrentUser, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
}),
]);
return { t, a, b, c };
}
Expand All @@ -69,6 +76,7 @@ async function seedStrategy(
folderPublicId?: string,
) {
await owner.mutation(createStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
publicId: strategyPublicId,
name: "A private Strategy",
mapData: "ascent",
Expand Down Expand Up @@ -100,12 +108,16 @@ describe("A/B/C access boundary", () => {
).rejects.toThrow("Forbidden");

await a.mutation(createShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "strategy",
targetPublicId: strategyPublicId,
token: "strategy-viewer-token",
role: "viewer",
});
await b.mutation(redeemShare, { token: "strategy-viewer-token" });
await b.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "strategy-viewer-token",
});

await expect(
b.query(getStrategyShell, { strategyPublicId }),
Expand All @@ -121,13 +133,15 @@ describe("A/B/C access boundary", () => {
});
await expect(
b.mutation(updateStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 0,
name: "Viewer must not write",
}),
).rejects.toThrow("Forbidden");
await expect(
b.mutation(addPage, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 0,
pagePublicId: "viewer-page",
Expand All @@ -138,21 +152,27 @@ describe("A/B/C access boundary", () => {
).rejects.toThrow("Forbidden");

await a.mutation(createShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "strategy",
targetPublicId: strategyPublicId,
token: "strategy-editor-token",
role: "editor",
});
await b.mutation(redeemShare, { token: "strategy-editor-token" });
await b.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "strategy-editor-token",
});
await expect(
b.mutation(updateStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 0,
name: "Edited by B",
}),
).resolves.toMatchObject({ ok: true, revision: 1 });
await expect(
b.mutation(addPage, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 1,
pagePublicId: "editor-page",
Expand All @@ -172,15 +192,20 @@ describe("A/B/C access boundary", () => {
});

await a.mutation(revokeShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "strategy",
targetPublicId: strategyPublicId,
token: "strategy-editor-token",
});
await expect(
c.mutation(redeemShare, { token: "strategy-editor-token" }),
c.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "strategy-editor-token",
}),
).rejects.toThrow("Share link revoked");
await expect(
b.mutation(updateStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 2,
name: "B keeps redeemed access",
Expand All @@ -195,10 +220,12 @@ describe("A/B/C access boundary", () => {
const strategyPublicId = "nested-strategy";

await a.mutation(createFolder, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
publicId: rootFolderPublicId,
name: "A root",
});
await a.mutation(createFolder, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
publicId: childFolderPublicId,
name: "A child",
parentFolderPublicId: rootFolderPublicId,
Expand All @@ -218,12 +245,16 @@ describe("A/B/C access boundary", () => {
).rejects.toThrow("Forbidden");

await a.mutation(createShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "folder",
targetPublicId: rootFolderPublicId,
token: "folder-viewer-token",
role: "viewer",
});
await b.mutation(redeemShare, { token: "folder-viewer-token" });
await b.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "folder-viewer-token",
});

await expect(
b.query(listFolderTree, { scope: "shared" }),
Expand All @@ -245,40 +276,49 @@ describe("A/B/C access boundary", () => {
]);
await expect(
b.mutation(updateStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 0,
name: "Viewer must not write",
}),
).rejects.toThrow("Forbidden");
await expect(
b.mutation(updateFolder, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
folderPublicId: childFolderPublicId,
name: "Viewer must not rename",
}),
).rejects.toThrow("Forbidden");

await a.mutation(createShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "folder",
targetPublicId: rootFolderPublicId,
token: "folder-editor-token",
role: "editor",
});
await b.mutation(redeemShare, { token: "folder-editor-token" });
await b.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "folder-editor-token",
});
await expect(
b.mutation(updateStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 0,
name: "Inherited editor write",
}),
).resolves.toMatchObject({ ok: true, revision: 1 });
await expect(
b.mutation(updateFolder, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
folderPublicId: rootFolderPublicId,
name: "Editors are not owners",
}),
).rejects.toThrow("Forbidden");
await expect(
b.mutation(createShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "folder",
targetPublicId: rootFolderPublicId,
token: "editor-cannot-reshare",
Expand All @@ -287,18 +327,23 @@ describe("A/B/C access boundary", () => {
).rejects.toThrow("Forbidden");

await a.mutation(revokeShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "folder",
targetPublicId: rootFolderPublicId,
token: "folder-editor-token",
});
await expect(
c.mutation(redeemShare, { token: "folder-editor-token" }),
c.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "folder-editor-token",
}),
).rejects.toThrow("Share link revoked");
await expect(
c.query(listFolderTree, { scope: "shared" }),
).resolves.toEqual([]);
await expect(
b.mutation(updateStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 1,
name: "Redeemed editor remains durable",
Expand All @@ -311,14 +356,19 @@ describe("A/B/C access boundary", () => {
const strategyPublicId = "deleted-strategy";
await seedStrategy(a, strategyPublicId, "deleted-page");
await a.mutation(createShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
targetType: "strategy",
targetPublicId: strategyPublicId,
token: "deleted-strategy-token",
role: "editor",
});
await b.mutation(redeemShare, { token: "deleted-strategy-token" });
await b.mutation(redeemShare, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
token: "deleted-strategy-token",
});

await a.mutation(deleteStrategy, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION,
strategyPublicId,
expectedRevision: 0,
});
Expand Down
147 changes: 147 additions & 0 deletions convex/cloudProtocol.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { convexTest } from "convex-test";
import { makeFunctionReference } from "convex/server";
import { describe, expect, test } from "vitest";
import { CURRENT_CLOUD_PROTOCOL_VERSION } from "./lib/cloudProtocol";
import schema from "./schema";
import { modules } from "./test.setup";

const publicMutations = [
["folders:create", { publicId: "folder", name: "Folder" }],
["folders:update", { folderPublicId: "folder" }],
["folders:move", { folderPublicId: "folder" }],
["folders:delete", { folderPublicId: "folder" }],
[
"invites:create",
{ strategyPublicId: "strategy", token: "token", role: "viewer" },
],
["invites:redeem", { token: "token" }],
["invites:revoke", { strategyPublicId: "strategy", token: "token" }],
[
"ops:applyBatch",
{ strategyPublicId: "strategy", clientId: "client", ops: [] },
],
[
"pages:add",
{
strategyPublicId: "strategy",
expectedRevision: 0,
pagePublicId: "page",
name: "Page",
sortIndex: 0,
isAttack: true,
},
],
[
"pages:rename",
{
strategyPublicId: "strategy",
pagePublicId: "page",
name: "Page",
expectedRevision: 0,
},
],
[
"pages:delete",
{
strategyPublicId: "strategy",
pagePublicId: "page",
expectedRevision: 0,
},
],
[
"pages:reorder",
{
strategyPublicId: "strategy",
orderedPagePublicIds: [],
expectedRevision: 0,
},
],
[
"shares:create",
{
targetType: "strategy",
targetPublicId: "strategy",
token: "token",
role: "viewer",
},
],
[
"shares:revoke",
{ targetType: "strategy", targetPublicId: "strategy", token: "token" },
],
["shares:redeem", { token: "token" }],
[
"strategies:create",
{ publicId: "strategy", name: "Strategy", mapData: "ascent" },
],
[
"strategies:createWithInitialPage",
{
publicId: "strategy",
name: "Strategy",
mapData: "ascent",
initialPagePublicId: "page",
initialPageName: "Page 1",
initialPageIsAttack: true,
},
],
[
"strategies:update",
{ strategyPublicId: "strategy", expectedRevision: 0 },
],
[
"strategies:move",
{ strategyPublicId: "strategy", expectedRevision: 0 },
],
[
"strategies:delete",
{ strategyPublicId: "strategy", expectedRevision: 0 },
],
["users:ensureCurrentUser", {}],
] as const;

describe("public cloud mutation protocol gate", () => {
test.each(publicMutations)("%s rejects an old protocol canonically", async (
identifier,
args,
) => {
const t = convexTest(schema, modules);
const mutation = makeFunctionReference<"mutation">(identifier);
const error = await t
.mutation(mutation, {
...args,
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION - 1,
})
.then(
() => null,
(caught: unknown) => caught as { data?: unknown },
);

expect(error).not.toBeNull();
expect(typeof error?.data).toBe("string");
expect(JSON.parse(error?.data as string)).toEqual({
code: "CLIENT_UPGRADE_REQUIRED",
message: "Client upgrade required",
});
});

test("a newer unknown protocol receives the same canonical error", async () => {
const t = convexTest(schema, modules);
const mutation = makeFunctionReference<"mutation">(
"users:ensureCurrentUser",
);
const error = await t
.mutation(mutation, {
clientProtocolVersion: CURRENT_CLOUD_PROTOCOL_VERSION + 1,
})
.then(
() => null,
(caught: unknown) => caught as { data?: unknown },
);

expect(JSON.parse(error?.data as string)).toEqual({
code: "CLIENT_UPGRADE_REQUIRED",
message: "Client upgrade required",
});
});
});
Loading
Loading