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

feat: add disallow hostnames #23

Merged
merged 2 commits into from
Nov 6, 2024
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
43 changes: 43 additions & 0 deletions packages/validators/src/__tests__/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,48 @@ describe('UrlValidator with custom host whitelist', () => {
})
})

describe('UrlValidator with disallowHostnames', () => {
const validator = new UrlValidator({
whitelist: {
protocols: ['http', 'https'],
disallowHostnames: true,
},
})

it('should not throw an error with a proper domain', () => {
expect(() => validator.parse('https://example.com')).not.toThrow()
})

it('should throw an error with a hostname', () => {
expect(() => validator.parse('https://tld')).toThrow(UrlValidationError)
expect(() => validator.parse('https://.tld')).toThrow(UrlValidationError)
expect(() => validator.parse('https://tld.')).toThrow(UrlValidationError)
expect(() => validator.parse('https://.tld.')).toThrow(UrlValidationError)
expect(() => validator.parse('https://tld/')).toThrow(UrlValidationError)
expect(() => validator.parse('https://.tld/')).toThrow(UrlValidationError)
expect(() => validator.parse('https://tld./')).toThrow(UrlValidationError)
expect(() => validator.parse('https://.tld./')).toThrow(UrlValidationError)
})
})

describe('UrlValidator with both hosts and disallowHostnames', () => {
const validator = new UrlValidator({
whitelist: {
protocols: ['http', 'https'],
hosts: ['example.com', 'localhost'],
disallowHostnames: true,
},
})

it('should not throw an error when the host is on the whitelist', () => {
expect(() => validator.parse('https://example.com')).not.toThrow()
})

it('should ignore the disallowHostnames option', () => {
expect(() => validator.parse('https://localhost')).not.toThrow()
})
})

describe('UrlValidator with base URL', () => {
const validator = new UrlValidator({
baseOrigin: 'https://example.com',
Expand Down Expand Up @@ -144,6 +186,7 @@ describe('createUrlSchema', () => {
whitelist: {
protocols: ['http', 'https'],
hosts: ['example.com'],
disallowHostnames: true,
},
}),
).not.toThrow()
Expand Down
9 changes: 9 additions & 0 deletions packages/validators/src/url/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod'
export const defaultOptions = {
whitelist: {
protocols: ['http', 'https'],
disallowHostnames: false,
},
}

Expand All @@ -19,6 +20,14 @@ export const whitelistSchema = z.object({
* It is recommended to provide a list of allowed hostnames to prevent open redirects.
*/
hosts: z.array(z.string()).optional(),
/**
* Whether to disallow hostnames as valid URLs.
* For example, if disallowHostnames is set to `true`, https://localhost/somepath will be invalid.
* This option is IGNORED if hosts is provided.
*
* @defaultValue false
*/
disallowHostnames: z.boolean().optional(),
})

/**
Expand Down
15 changes: 12 additions & 3 deletions packages/validators/src/url/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UrlValidationError } from '@/url/errors'
import { UrlValidatorWhitelist } from '@/url/options'

const DYNAMIC_ROUTE_SEGMENT_REGEX = /\[\[?([^\]]+)\]?\]/g
const IS_NOT_HOSTNAME_REGEX = /[^.]+\.[^.]+/g

export const resolveRelativeUrl = (url: string, baseOrigin?: URL): URL => {
if (!baseOrigin) {
Expand Down Expand Up @@ -45,10 +46,18 @@ export const isSafeUrl = (url: URL, whitelist: UrlValidatorWhitelist) => {
if (!whitelist.protocols.some(protocol => url.protocol === `${protocol}:`)) {
return false
}
// only allow whitelisted hosts
if (whitelist.hosts && !whitelist.hosts.some(host => url.host === host)) {
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 (resolveNextDynamicRoute(url).href !== url.href) {
return false
Expand Down
Loading