-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add allowedCharactersInPath whitelist
- Loading branch information
1 parent
56f5d3a
commit 9d6f03b
Showing
6 changed files
with
245 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,61 @@ | ||
import { z } from 'zod' | ||
|
||
import { ParsedUrlValidatorOptions } from '@/url/options' | ||
import { isSafeUrl, resolveRelativeUrl } from '@/url/utils' | ||
import { createAllowedCharsSchema, getErrorMessage, IS_NOT_HOSTNAME_REGEX, resolveRelativeUrl } from '@/url/utils' | ||
|
||
import { isDynamicRoute } from './nextjs-dynamic-route' | ||
|
||
export const toSchema = (options: ParsedUrlValidatorOptions) => { | ||
const { whitelist } = options | ||
|
||
// create and cache this zod schema beforehand | ||
const zAllowedCharsString = createAllowedCharsSchema(whitelist.allowedCharactersInPath) | ||
|
||
return z | ||
.string() | ||
.transform(url => resolveRelativeUrl(url, options.baseOrigin)) | ||
.refine(url => isSafeUrl(url, options.whitelist), { | ||
message: 'Unsafe URL', | ||
.transform((url, ctx) => { | ||
try { | ||
return resolveRelativeUrl(url, options.baseOrigin) | ||
} catch (error) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: getErrorMessage(error), | ||
}) | ||
return z.NEVER | ||
} | ||
}) | ||
.refine( | ||
url => { | ||
// only allow whitelisted protocols | ||
if (!whitelist.protocols.some(protocol => url.protocol === `${protocol}:`)) { | ||
return false | ||
} | ||
// only allow whitelisted characters in the path | ||
const onlyHasAllowedChars = zAllowedCharsString.safeParse(url.pathname).success | ||
if (!onlyHasAllowedChars) { | ||
return false | ||
} | ||
|
||
if (whitelist.hosts) { | ||
// only allow whitelisted hosts | ||
if (!whitelist.hosts.some(host => url.host === host)) { | ||
return false | ||
} | ||
} else { | ||
// no hosts provided | ||
if (whitelist.disallowHostnames && !url.host.match(IS_NOT_HOSTNAME_REGEX)) { | ||
return false | ||
} | ||
} | ||
|
||
// don't allow dynamic routes | ||
if (isDynamicRoute(url)) { | ||
return false | ||
} | ||
return true | ||
}, | ||
{ | ||
message: 'Unsafe URL', | ||
}, | ||
) | ||
} |
Oops, something went wrong.