Skip to content

Commit

Permalink
fix(results): πŸ”’οΈ Improve delete security checks
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 18, 2022
1 parent 4e62175 commit 9352587
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
10 changes: 5 additions & 5 deletions apps/builder/layouts/results/SubmissionContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { SubmissionsTable } from 'components/results/SubmissionsTable'
import React, { useCallback, useMemo, useState } from 'react'
import {
convertResultsToTableData,
deleteAllResults,
deleteResults,
getAllResults,
useResults,
Expand Down Expand Up @@ -70,10 +69,11 @@ export const SubmissionsContent = ({
const selectedIds = (results ?? [])
.filter((_, idx) => selectedIndices.includes(idx))
.map((result) => result.id)
const { error } =
totalSelected === totalResults
? await deleteAllResults(typebotId)
: await deleteResults(typebotId, selectedIds)
const { error } = await deleteResults(
workspaceId,
typebotId,
totalSelected === totalResults ? [] : selectedIds
)
if (error) showToast({ description: error.message, title: error.name })
else {
mutate(
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { rest, setupWorker } from 'msw'
import { setupServer } from 'msw/node'

const handlers = () => [
rest.get('/api/auth/session', (req, res, ctx) => {
rest.get('http://localhost:3000/api/auth/session', (req, res, ctx) => {
const authenticatedUser = JSON.parse(
typeof localStorage !== 'undefined'
? (localStorage.getItem('authenticatedUser') as string)
Expand Down
14 changes: 7 additions & 7 deletions apps/builder/pages/api/typebots/[typebotId]/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res, 'workspaceId is required')
const workspace = await prisma.workspace.findFirst({
where: { id: workspaceId, members: { some: { userId: user.id } } },
select: { plan: true },
})
if (!workspace) return forbidden(res)
if (req.method === 'GET') {
if (!workspaceId) return badRequest(res, 'workspaceId is required')
const workspace = await prisma.workspace.findFirst({
where: { id: workspaceId, members: { some: { userId: user.id } } },
select: { plan: true },
})
if (!workspace) return forbidden(res)
const typebotId = req.query.typebotId.toString()
const lastResultId = req.query.lastResultId?.toString()
const take = parseInt(req.query.limit?.toString())
Expand All @@ -46,7 +46,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(200).send({ results })
}
if (req.method === 'DELETE') {
const typebotId = req.query.typebotId.toString()
const typebotId = req.query.typebotId as string
const ids = req.query.ids as string[]
const results = await prisma.result.deleteMany({
where: {
Expand Down
4 changes: 3 additions & 1 deletion apps/builder/playwright/tests/results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ test.describe('Results page', () => {
await deleteButtonInConfirmDialog(page).click()
await expect(page.locator('text=content199')).toBeHidden()
await expect(page.locator('text=content198')).toBeHidden()
await page.waitForTimeout(1000)
await page.click('[data-testid="checkbox"] >> nth=0')
await page.click('button:has-text("Delete198")')
await deleteButtonInConfirmDialog(page).click()
await expect(page.locator(':nth-match(tr, 2)')).toBeHidden()
await page.waitForTimeout(1000)
expect(await page.locator('tr').count()).toBe(1)
})

test('submissions table should have infinite scroll', async ({ page }) => {
Expand Down
13 changes: 6 additions & 7 deletions apps/builder/services/typebots/results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ export const useResults = ({
}
}

export const deleteResults = async (typebotId: string, ids: string[]) => {
export const deleteResults = async (
workspaceId: string,
typebotId: string,
ids: string[]
) => {
const params = stringify(
{
ids,
workspaceId,
},
{ indices: false }
)
Expand All @@ -89,12 +94,6 @@ export const deleteResults = async (typebotId: string, ids: string[]) => {
})
}

export const deleteAllResults = async (typebotId: string) =>
sendRequest({
url: `/api/typebots/${typebotId}/results`,
method: 'DELETE',
})

export const getAllResults = async (workspaceId: string, typebotId: string) => {
const results = []
let hasMore = true
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3604,9 +3604,9 @@
set-cookie-parser "^2.4.6"

"@mswjs/interceptors@^0.15.1":
version "0.15.1"
resolved "https://registry.yarnpkg.com/@mswjs/interceptors/-/interceptors-0.15.1.tgz#4a0009f56e51bc2cd3176f1507065c7d2f6c0d5e"
integrity sha512-D5B+ZJNlfvBm6ZctAfRBdNJdCHYAe2Ix4My5qfbHV5WH+3lkt3mmsjiWJzEh5ZwGDauzY487TldI275If7DJVw==
version "0.15.3"
resolved "https://registry.yarnpkg.com/@mswjs/interceptors/-/interceptors-0.15.3.tgz#bcd17b5d7558d4f598007a4bb383b42dc9264f8d"
integrity sha512-GJ1qzBq82EQ3bwhsvw5nScbrLzOSI5H/TyB2CGd1K7dDqX58DJDLJHexiN+S5Ucvl6/84FjRdIysz0RxE/L8MA==
dependencies:
"@open-draft/until" "^1.0.3"
"@xmldom/xmldom" "^0.7.5"
Expand Down

4 comments on commit 9352587

@vercel
Copy link

@vercel vercel bot commented on 9352587 Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs.typebot.io
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 9352587 Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 9352587 Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 9352587 Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.