Skip to content

Home > @opengovsg/validators > UrlValidator

UrlValidator class ​

Parses URLs according to WHATWG standards and validates against a whitelist of allowed protocols and hostnames, preventing open redirects, XSS, SSRF, and other security vulnerabilities.

Signature:

typescript
export declare class UrlValidator

Constructors ​

ConstructorModifiersDescription
(constructor)(options)

Creates a new UrlValidator instance. If no options are provided, the validator will use the default options:

ts
{
   whitelist: {
     protocols: ['http', 'https'],
   },
}

In most cases, you should provide your own whitelist with a list of allowed hostnames to prevent open redirects.

|

Methods ​

MethodModifiersDescription
parse(url, fallbackUrl)Parses a URL string with a fallback option.
parse(url)
parse(url, fallbackUrl)
parsePathname(url, fallbackUrl)Parses a URL string and returns the pathname with a fallback option.
parsePathname(url)
parsePathname(url, fallbackUrl)