Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🔒️ Reject participant writes once a poll leaves open (#3246)
Luke Vellad818a5fe81ae
diff --git a/apps/web/src/features/poll/data.ts b/apps/web/src/features/poll/data.tsindex d4a8cd8a7..fb224e24d 100644--- a/apps/web/src/features/poll/data.ts+++ b/apps/web/src/features/poll/data.ts@@ -479,7 +479,12 @@ export async function getParticipant({}) {return prisma.participant.findFirst({where: { id: participantId },- select: { id: true, pollId: true, userId: true },+ select: {+ id: true,+ pollId: true,+ userId: true,+ poll: { select: { status: true, deleted: true } },+ },});}diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.tsindex 1a0b4b631..484e2f331 100644--- a/apps/web/src/trpc/routers/polls/participants.ts+++ b/apps/web/src/trpc/routers/polls/participants.ts@@ -309,6 +309,29 @@ export const participants = router({ctx,input: { pollId, votes, name, email, note, timeZone, token },}) => {+ const poll = await prisma.poll.findUnique({+ where: { id: pollId },+ select: { status: true, deleted: true },+ });++ // A deleted poll never accepts responses.+ if (!poll || poll.deleted) {+ throw new TRPCError({+ code: "NOT_FOUND",+ message: "Poll not found",+ });+ }++ // The voting window is the organizer's control, so it has to hold+ // here and not only in the client, which hides the form via+ // `canAddNewParticipant`.+ if (poll.status !== "open") {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "This poll is no longer accepting responses",+ });+ }+const participantCount = await prisma.participant.count({where: {pollId,diff --git a/apps/web/src/trpc/routers/polls/utils.ts b/apps/web/src/trpc/routers/polls/utils.tsindex 8bed5c173..8939fdafb 100644--- a/apps/web/src/trpc/routers/polls/utils.ts+++ b/apps/web/src/trpc/routers/polls/utils.ts@@ -13,6 +13,11 @@ type Actor = { id: string; isGuest: boolean };* owns the response or administers its poll. Admin access is bound to the* session only, so a link never unlocks other people's responses.*+ * A poll that is no longer open accepts no edits from anyone, admins+ * included — the same rule `canEditParticipant` applies in the client. The+ * lifecycle check comes first: whether the voting window has closed does not+ * depend on who is asking.+ ** The returned actor is for attribution (activity log, analytics): the* session when there is one, otherwise the user the response was created* under, who was a guest at the time.@@ -35,6 +40,20 @@ export async function authorizeParticipantEdit({});}+ if (participant.poll.deleted) {+ throw new TRPCError({+ code: "NOT_FOUND",+ message: "Participant not found",+ });+ }++ if (participant.poll.status !== "open") {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "This poll is no longer accepting changes",+ });+ }+const ownedBySession = ctxUser? participant.userId === ctxUser.id ||(await hasPollAdminAccess(participant.pollId, ctxUser.id))diff --git a/apps/web/tests/closed-poll-writes.spec.ts b/apps/web/tests/closed-poll-writes.spec.tsnew file mode 100644index 000000000..1c3a959df--- /dev/null+++ b/apps/web/tests/closed-poll-writes.spec.ts@@ -0,0 +1,117 @@+import type { Page } from "@playwright/test";+import { expect, test } from "@playwright/test";+import { NewPollPage } from "./new-poll-page";++// The client hides the voting form once a poll leaves "open"+// (`canAddNewParticipant` / `canEditParticipant`). These tests pin the+// server-side rule behind that, so a caller reaching the mutations directly+// cannot write to a poll whose voting window the organizer has ended.++async function getPollId(page: Page) {+ const pollId = page.url().match(/\/poll\/([^/?]+)/)?.[1];+ expect(pollId).toBeTruthy();+ return pollId as string;+}++async function closePoll(page: Page, pollId: string) {+ const response = await page.request.post("/api/trpc/polls.close", {+ data: { json: { pollId } },+ });+ expect(response.ok()).toBe(true);+}++test.describe("writes to a closed poll", () => {+ test("the API rejects a new response once the poll is closed", async ({+ page,+ }) => {+ const newPollPage = new NewPollPage(page);+ await newPollPage.goto();+ const pollPage = await newPollPage.create({ name: "Closed Poll Meetup" });+ await pollPage.closeShareDialog();+ const pollId = await getPollId(page);++ // A response added while the poll is still open, so the edit paths below+ // have a real target that only the poll's status disqualifies.+ await pollPage.addParticipant("Anne");+ const participants = await page.request.get(+ `/api/trpc/polls.participants.list?input=${encodeURIComponent(+ JSON.stringify({ json: { pollId } }),+ )}`,+ );+ expect(participants.ok()).toBe(true);+ const participantId = (await participants.json()).result.data.json[0].id;+ expect(participantId).toBeTruthy();++ await closePoll(page, pollId);++ const options = await page.request.get(+ `/api/trpc/polls.get?input=${encodeURIComponent(+ JSON.stringify({ json: { urlId: pollId } }),+ )}`,+ );+ const optionId = (await options.json()).result.data.json.options[0].id;++ const add = await page.request.post("/api/trpc/polls.participants.add", {+ data: {+ json: {+ pollId,+ name: "Test Participant",+ votes: [{ optionId, type: "yes" }],+ },+ },+ });+ expect(add.status()).toBe(400);++ // The same rule has to hold for every write path that reaches an existing+ // response, not just the one the report named.+ const update = await page.request.post(+ "/api/trpc/polls.participants.update",+ {+ data: {+ json: { pollId, participantId, votes: [{ optionId, type: "no" }] },+ },+ },+ );+ expect(update.status()).toBe(400);++ const rename = await page.request.post(+ "/api/trpc/polls.participants.rename",+ { data: { json: { participantId, newName: "Renamed" } } },+ );+ expect(rename.status()).toBe(400);++ const remove = await page.request.post(+ "/api/trpc/polls.participants.delete",+ { data: { json: { participantId } } },+ );+ expect(remove.status()).toBe(400);+ });++ test("the API still accepts a response while the poll is open", async ({+ page,+ }) => {+ const newPollPage = new NewPollPage(page);+ await newPollPage.goto();+ const pollPage = await newPollPage.create({ name: "Open Poll Meetup" });+ await pollPage.closeShareDialog();+ const pollId = await getPollId(page);++ const options = await page.request.get(+ `/api/trpc/polls.get?input=${encodeURIComponent(+ JSON.stringify({ json: { urlId: pollId } }),+ )}`,+ );+ const optionId = (await options.json()).result.data.json.options[0].id;++ const add = await page.request.post("/api/trpc/polls.participants.add", {+ data: {+ json: {+ pollId,+ name: "Test Participant",+ votes: [{ optionId, type: "yes" }],+ },+ },+ });+ expect(add.ok()).toBe(true);+ });+});