Skip to content

Commit

Permalink
fix(viewer): 🐛 Forwarded host checking
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed May 23, 2022
1 parent 447e87d commit 37dea9c
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions apps/viewer/pages/[[...publicId]].tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingMessage } from 'http'
import { NotFoundPage } from 'layouts/NotFoundPage'
import { PublicTypebot } from 'models'
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
Expand All @@ -11,17 +12,14 @@ export const getServerSideProps: GetServerSideProps = async (
let typebot: Omit<PublicTypebot, 'createdAt' | 'updatedAt'> | null
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
const pathname = context.resolvedUrl.split('?')[0]
const host = getHost(context.req)
try {
if (!context.req.headers.host) return { props: {} }
if (!host) return { props: {} }
const viewerUrls = (process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split(',')
const isMatchingViewerUrl = viewerUrls.some((url) =>
(context.req.headers.host ?? '')
.split(':')[0]
.includes(url.split('//')[1].split(':')[0])
host.split(':')[0].includes(url.split('//')[1].split(':')[0])
)
const customDomain = `${context.req.headers.host}${
pathname === '/' ? '' : pathname
}`
const customDomain = `${host}${pathname === '/' ? '' : pathname}`
typebot = isMatchingViewerUrl
? await getTypebotFromPublicId(context.query.publicId?.toString())
: await getTypebotFromCustomDomain(customDomain)
Expand All @@ -35,7 +33,7 @@ export const getServerSideProps: GetServerSideProps = async (
props: {
typebot,
isIE,
url: `https://${context.req.headers.host}${pathname}`,
url: `https://${host}${pathname}`,
},
}
} catch (err) {
Expand All @@ -44,7 +42,7 @@ export const getServerSideProps: GetServerSideProps = async (
return {
props: {
isIE,
url: `https://${context.req.headers.host}${pathname}`,
url: `https://${host}${pathname}`,
},
}
}
Expand All @@ -66,6 +64,20 @@ const getTypebotFromCustomDomain = async (customDomain: string) => {
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
}

const getHost = (req?: IncomingMessage): string | undefined => {
let host = req?.headers ? req.headers.host : window.location.host
if (!host) return
if (
req &&
req.headers['x-forwarded-host'] &&
typeof req.headers['x-forwarded-host'] === 'string'
) {
host = req.headers['x-forwarded-host']
}

return host
}

const App = ({ typebot, ...props }: TypebotPageProps) =>
isDefined(typebot) ? (
<TypebotPage typebot={typebot} {...props} />
Expand Down

4 comments on commit 37dea9c

@vercel
Copy link

@vercel vercel bot commented on 37dea9c May 23, 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
builder-v2-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on 37dea9c May 23, 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 37dea9c May 23, 2022

@vercel
Copy link

@vercel vercel bot commented on 37dea9c May 23, 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.