Skip to content

Commit

Permalink
fix: validate callback urls with starter-kitty validator (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 authored Jul 22, 2024
1 parent b8cf312 commit d666cf3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
50 changes: 44 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@hookform/resolvers": "^3.3.4",
"@opengovsg/design-system-react": "^1.15.0",
"@opengovsg/sgid-client": "^2.2.0",
"@opengovsg/starter-kitty-validators": "^1.0.1",
"@paralleldrive/cuid2": "^2.2.2",
"@prisma/client": "^5.7.1",
"@sendgrid/mail": "^8.1.3",
Expand Down
27 changes: 24 additions & 3 deletions src/schemas/url.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import { z } from 'zod'
import { HOME } from '~/lib/routes'
import { isRelativeUrl } from '~/utils/url'
import { UrlValidator } from '@opengovsg/starter-kitty-validators'
import { getBaseUrl } from '~/utils/getBaseUrl'

const baseUrl = getBaseUrl()

const validator = new UrlValidator({
baseOrigin: new URL(baseUrl).origin,
whitelist: {
protocols: ['http', 'https'],
hosts: [new URL(baseUrl).host],
},
})

export const callbackUrlSchema = z
.string()
.optional()
.default(HOME)
.refine((url) => url && isRelativeUrl(url))
.catch(HOME)
.transform((url, ctx) => {
try {
return validator.parse(url)
} catch (error) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: (error as Error).message,
})
return z.NEVER
}
})
.catch(new URL(HOME, baseUrl))

0 comments on commit d666cf3

Please sign in to comment.