Skip to content

Commit

Permalink
fix(webhook): 🐛 Properly escape backslash and quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 20, 2022
1 parent 18c5ea5 commit 20dbfe6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions apps/builder/playwright/tests/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ test.describe.parallel('Settings page', () => {
}
)
await page.goto(`/typebots/${typebotId}/settings`)
await expect(
typebotViewer(page).locator('text="What\'s your name?"')
).toBeVisible()
await page.click('button:has-text("General")')
await expect(page.locator('text=Pro')).toBeVisible()
await page.click('text=Typebot.io branding')
Expand Down
3 changes: 3 additions & 0 deletions apps/builder/playwright/tests/templates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { typebotViewer } from '../services/selectorUtils'
test.describe.parallel('Templates page', () => {
test('From scratch should create a blank typebot', async ({ page }) => {
await page.goto('/typebots/create')
await expect(
page.locator('button >> text="Settings & Members"')
).toBeEnabled()
await page.click('text=Start from scratch')
await expect(page).toHaveURL(new RegExp(`/edit`))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ export const executeWebhook =
json:
contentType !== 'x-www-form-urlencoded' && body
? safeJsonParse(
parseVariables(variables, { escapeLineBreaks: true })(body)
parseVariables(variables, { escapeForJson: true })(body)
)
: undefined,
form:
contentType === 'x-www-form-urlencoded' && body
? safeJsonParse(
parseVariables(variables, { escapeLineBreaks: true })(body)
parseVariables(variables, { escapeForJson: true })(body)
)
: undefined,
}
Expand Down
13 changes: 9 additions & 4 deletions packages/bot-engine/src/services/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export const stringContainsVariable = (str: string): boolean =>
export const parseVariables =
(
variables: Variable[],
options: { fieldToParse?: 'value' | 'id'; escapeLineBreaks?: boolean } = {
options: { fieldToParse?: 'value' | 'id'; escapeForJson?: boolean } = {
fieldToParse: 'value',
escapeLineBreaks: false,
escapeForJson: false,
}
) =>
(text: string | undefined): string => {
Expand All @@ -23,12 +23,17 @@ export const parseVariables =
if (options.fieldToParse === 'id') return variable.id
const { value } = variable
if (isNotDefined(value)) return ''
if (options.escapeLineBreaks)
return value.toString().replace(/\n/g, '\\n')
if (options.escapeForJson) return jsonParse(value.toString())
return value.toString()
})
}

const jsonParse = (str: string) =>
str
.replace(/\n/g, `\\n`)
.replace(/"/g, `\\"`)
.replace(/\\[^n"]/g, `\\\\ `)

export const evaluateExpression = (variables: Variable[]) => (str: string) => {
const evaluating = parseVariables(variables, { fieldToParse: 'id' })(
str.includes('return ') ? str : `return ${str}`
Expand Down

5 comments on commit 20dbfe6

@vercel
Copy link

@vercel vercel bot commented on 20dbfe6 Jun 20, 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.typebot.io
docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 20dbfe6 Jun 20, 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 20dbfe6 Jun 20, 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:

viewer-v2-alpha – ./apps/viewer

vhpage.cr8.ai
apr.nigerias.io
bot.aws.bj
am.nigerias.io
sat.cr8.ai
an.nigerias.io
bt.id8rs.com
aso.nigerias.io
ar.nigerias.io
apo.nigerias.io
eventhub.com.au
bot.piccinato.co
sakuranembro.it
games.klujo.com
chat.sureb4.com
bot.upfunnel.art
clo.closeer.work
feedback.ofx.one
faqs.nigerias.io
stan.vselise.com
app.chatforms.net
voicehelp.cr8.ai
this-is-a-test.com
bot.eventhub.com.au
goalsettingbot.com
zap.techadviser.in
forms.webisharp.com
typebot.stillio.com
bot.ansuraniphone.my
bot.cotemeuplano.com
get.freebotoffer.xyz
chat.hayurihijab.com
bot.incusservices.com
bot.meuesocial.com.br
cdd.searchcube.com.sg
chat.missarkansas.org
apply.ansuraniphone.my
c23111azqw.nigerias.io
gcase.barrettamario.it
form.searchcube.com.sg
info.clickasuransi.com
kodawariab736.skeep.it
mainmenu.diddancing.com
83242573.actualizar.xyz
view.onlinebotdemo.xyz
serviziaziendali.online
form.sergiolimajr.com.br
aibot.angrybranding.co.uk
hunterbot.saleshunter.ai
type.opaulovieira.com.br
boyfriend-breakup.riku.ai
type.dericsoncalari.com.br
designguide.techyscouts.com
piazzatorre.barrettamario.it
type.talitasouzamarques.com.br
bookings.littlepartymonkeys.com
personal-trainer.barrettamario.it
bot.comercializadoraomicron.com
agendamento.sergiolimajr.com.br
preagendamento.sergiolimajr.com.br
viewer-v2-alpha-typebot-io.vercel.app
viewer-v2-alpha-git-main-typebot-io.vercel.app
studiotecnicoimmobiliaremerelli.it
onboarding.libertydreamcare.ie

@vercel
Copy link

@vercel vercel bot commented on 20dbfe6 Jun 20, 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 20dbfe6 Jun 20, 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

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

Please sign in to comment.