Skip to content

Commit

Permalink
🐛 Fix allowed origins when embedded in iframe
Browse files Browse the repository at this point in the history
Closes #1518
  • Loading branch information
baptisteArno committed Jun 19, 2024
1 parent 36c9846 commit 67f37c0
Show file tree
Hide file tree
Showing 9 changed files with 874 additions and 27 deletions.
410 changes: 410 additions & 0 deletions apps/docs/openapi/builder.json

Large diffs are not rendered by default.

410 changes: 410 additions & 0 deletions apps/docs/openapi/viewer.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion apps/viewer/src/helpers/server/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export async function createContext(opts: trpcNext.CreateNextContextOptions) {

return {
user,
origin: opts.req.headers.origin,
origin:
(opts.req.headers['x-typebot-iframe-referrer-origin'] as
| string
| undefined) ?? opts.req.headers.origin,
res: opts.res,
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.2.87",
"version": "0.2.88",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down Expand Up @@ -48,4 +48,4 @@
"tailwindcss": "3.3.3",
"typescript": "5.4.5"
}
}
}
5 changes: 5 additions & 0 deletions packages/embeds/js/src/components/Bot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
defaultFontType,
defaultProgressBarPosition,
} from '@typebot.io/schemas/features/typebot/theme/constants'
import { CorsError } from '@/utils/CorsError'

export type BotProps = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -106,6 +107,10 @@ export const Bot = (props: BotProps & { class?: string }) => {
)
}

if (error instanceof CorsError) {
return setError(new Error(error.message))
}

if (!data) {
if (error) {
console.error(error)
Expand Down
54 changes: 34 additions & 20 deletions packages/embeds/js/src/queries/startChatQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
StartPreviewChatInput,
} from '@typebot.io/schemas'
import ky from 'ky'
import { CorsError } from '@/utils/CorsError'

type Props = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -102,27 +103,40 @@ export async function startChatQuery({
}

try {
const data = await ky
.post(
`${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/startChat`,
{
json: {
isStreamEnabled: true,
prefilledVariables,
resultId,
isOnlyRegistering: false,
} satisfies Omit<
StartChatInput,
'publicId' | 'textBubbleContentFormat'
>,
timeout: false,
}
)
.json<InitialChatReply>()
const iframeReferrerOrigin =
parent !== window ? new URL(document.referrer).origin : undefined
const response = await ky.post(
`${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/startChat`,
{
headers: {
'x-typebot-iframe-referrer-origin': iframeReferrerOrigin,
},
json: {
isStreamEnabled: true,
prefilledVariables,
resultId,
isOnlyRegistering: false,
} satisfies Omit<
StartChatInput,
'publicId' | 'textBubbleContentFormat'
>,
timeout: false,
}
)

return { data }
const corsAllowOrigin = response.headers.get('access-control-allow-origin')

if (
iframeReferrerOrigin &&
corsAllowOrigin &&
corsAllowOrigin !== '*' &&
!iframeReferrerOrigin.includes(corsAllowOrigin)
)
throw new CorsError(corsAllowOrigin)

return { data: await response.json<InitialChatReply>() }
} catch (error) {
return { error }
}
Expand Down
5 changes: 5 additions & 0 deletions packages/embeds/js/src/utils/CorsError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class CorsError extends Error {
constructor(origin: string) {
super('This bot can only be executed on ' + origin)
}
}
4 changes: 2 additions & 2 deletions packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.2.87",
"version": "0.2.88",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -42,4 +42,4 @@
"next": "12.x || 13.x || 14.x",
"react": "^16.0.0 || ^17.0.0 || ^18.0.0"
}
}
}
4 changes: 2 additions & 2 deletions packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.2.87",
"version": "0.2.88",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -45,4 +45,4 @@
"peerDependencies": {
"react": "^16.0.0 || ^17.0.0 || ^18.0.0"
}
}
}

0 comments on commit 67f37c0

Please sign in to comment.