Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): remove invalid regex route path for public forms #3046

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend/src/app/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ADMINFORM_ROUTE,
ADMINFORM_SETTINGS_SUBROUTE,
LOGIN_ROUTE,
PUBLIC_FORM_REGEX,
PUBLICFORM_ROUTE,
ROOT_ROUTE,
} from '~constants/routes'

Expand Down Expand Up @@ -37,7 +37,7 @@ export const AppRouter = (): JSX.Element => {
element={<PublicElement strict element={<LoginPage />} />}
/>
<Route
path={PUBLIC_FORM_REGEX}
path={PUBLICFORM_ROUTE}
element={<PublicElement element={<PublicFormPage />} />}
/>
<Route
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ export const LANDING_ROUTE = '/'
export const ROOT_ROUTE = '/'
export const LOGIN_ROUTE = '/login'

const PUBLIC_FORM_PARAM = 'formId'
/** Used for typing useParams in PublicFormPage. */
export type PublicFormParam = { [PUBLIC_FORM_PARAM]: string }
export const PUBLIC_FORM_REGEX =
`/:${PUBLIC_FORM_PARAM}([a-fA-F0-9]{24})` as const
// Cannot use regex match in react-router@6, which means we need to validate
// the regex in PublicFormPage.
export const PUBLICFORM_ROUTE = '/:formId'
export const PUBLICFORM_REGEX = /^([a-fA-F0-9]{24})$/

export const ADMINFORM_ROUTE = '/admin/form'
/** Build tab has no subroute, its the index admin form route. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
component: PublicFormPage,
decorators: [
StoryRouter({
initialEntries: ['/12345'],
initialEntries: ['/61540ece3d4a6e50ac0cc6ff'],
path: '/:formId',
}),
],
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/features/public-form/PublicFormPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useParams } from 'react-router-dom'
import { Flex } from '@chakra-ui/react'

import { PUBLICFORM_REGEX } from '~constants/routes'
import { HttpError } from '~services/ApiService'

import FormFields from './components/FormFields'
Expand All @@ -8,9 +10,14 @@ import { PublicFormProvider } from './PublicFormContext'
import { usePublicFormView } from './queries'

export const PublicFormPage = (): JSX.Element => {
const { formId } = useParams()
const { error } = usePublicFormView()

if (error instanceof HttpError && error.code === 404) {
if (
!formId ||
!PUBLICFORM_REGEX.test(formId) ||
(error instanceof HttpError && error.code === 404)
) {
return <div>404</div>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default {
component: FormStartPage,
decorators: [
StoryRouter({
initialEntries: ['/12345'],
initialEntries: ['/61540ece3d4a6e50ac0cc6ff'],
path: '/:formId',
}),
(storyFn) => <PublicFormProvider>{storyFn()}</PublicFormProvider>,
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/features/public-form/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { PublicFormViewDto } from '~shared/types/form/form'

import { ApiError } from '~typings/core'

import { PUBLICFORM_REGEX } from '~constants/routes'

import { getPublicFormView } from './PublicFormService'

const publicFormKeys = {
Expand All @@ -21,8 +23,12 @@ export const usePublicFormView = (): UseQueryResult<
const { formId } = useParams()
if (!formId) throw new Error('No formId provided')

return useQuery<PublicFormViewDto, ApiError>(publicFormKeys.id(formId), () =>
getPublicFormView(formId),
return useQuery<PublicFormViewDto, ApiError>(
publicFormKeys.id(formId),
() => getPublicFormView(formId),
{
enabled: PUBLICFORM_REGEX.test(formId),
},
)
}

Expand Down