Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🔒️ Enforce disableComments in the comments.add mutation (#3245)

Luke Vella24ec403734a3
diff --git a/apps/web/src/trpc/routers/polls/comments.ts b/apps/web/src/trpc/routers/polls/comments.ts
index d4ea5e2e0..8114d8764 100644
--- a/apps/web/src/trpc/routers/polls/comments.ts
+++ b/apps/web/src/trpc/routers/polls/comments.ts
@@ -98,6 +98,30 @@ export const comments = router({
}),
)
.mutation(async ({ ctx, input }) => {
+ const targetPoll = await prisma.poll.findUnique({
+ where: {
+ id: input.pollId,
+ },
+ select: {
+ disableComments: true,
+ deleted: true,
+ },
+ });
+
+ // A deleted poll never accepts new comments.
+ if (!targetPoll || targetPoll.deleted) {
+ throw new TRPCError({ code: "NOT_FOUND", message: "Poll not found" });
+ }
+
+ // The comment UI is hidden when the host turns comments off, but the
+ // setting is only real if the mutation enforces it too.
+ if (targetPoll.disableComments) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "Comments are disabled for this poll",
+ });
+ }
+
let authorName = input.authorName;
if (!ctx.user.isGuest) {
diff --git a/apps/web/tests/comments-disabled.spec.ts b/apps/web/tests/comments-disabled.spec.ts
new file mode 100644
index 000000000..6ca236ae7
--- /dev/null
+++ b/apps/web/tests/comments-disabled.spec.ts
@@ -0,0 +1,64 @@
+import { expect, test } from "@playwright/test";
+import { NewPollPage } from "./new-poll-page";
+
+// The comment UI is hidden when the host turns comments off. That is a
+// presentation choice; these tests pin the server-side rule behind it, so a
+// caller going straight at the mutation cannot write past the setting.
+test.describe("comments disabled", () => {
+ test("the API rejects a comment on a poll with comments off", async ({
+ page,
+ }) => {
+ const newPollPage = new NewPollPage(page);
+ await newPollPage.goto();
+ // Comments are off by default, so this poll has disableComments = true.
+ const pollPage = await newPollPage.create({ name: "No Comments Meetup" });
+ await pollPage.closeShareDialog();
+
+ const pollId = page.url().match(/\/poll\/([^/?]+)/)?.[1];
+ expect(pollId).toBeTruthy();
+
+ // page.request shares the page's cookie jar, so this carries the same
+ // session the hidden UI would have used.
+ const response = await page.request.post("/api/trpc/polls.comments.add", {
+ data: {
+ json: {
+ pollId,
+ authorName: "Test User",
+ content: "This comment should be rejected",
+ },
+ },
+ });
+
+ expect(response.status()).toBe(403);
+
+ await page.reload();
+ await expect(page.getByRole("button", { name: "Comments" })).toBeHidden();
+ });
+
+ test("the API still accepts a comment when comments are on", async ({
+ page,
+ }) => {
+ const newPollPage = new NewPollPage(page);
+ await newPollPage.goto();
+ const pollPage = await newPollPage.create({
+ name: "Comments Allowed Meetup",
+ enableComments: true,
+ });
+ await pollPage.closeShareDialog();
+
+ const pollId = page.url().match(/\/poll\/([^/?]+)/)?.[1];
+ expect(pollId).toBeTruthy();
+
+ const response = await page.request.post("/api/trpc/polls.comments.add", {
+ data: {
+ json: {
+ pollId,
+ authorName: "Test User",
+ content: "This comment should be accepted",
+ },
+ },
+ });
+
+ expect(response.ok()).toBe(true);
+ });
+});