UrlSanitizer Class

Validates URLs to prevent XSS attacks by checking for dangerous protocols and patterns.

public static class UrlSanitizer

Example

Check if a URL is safe:

Assert.True(UrlSanitizer.IsSafeUrl("https://example.com"));
Assert.False(UrlSanitizer.IsSafeUrl("javascript:alert('xss')"));

Methods

NameDescription
IsSafeSrcset(string?)Determines whether a srcset value (used for responsive images) is safe by validating all URLs in the comma-separated list.
IsSafeUrl(string?)Determines whether a URL is safe by checking if it uses an allowed protocol (http, https, mailto, ftp, tel, file) or is a relative URL, or a safe data URL.

IsSafeSrcset(string?)

UrlSanitizer.cs

Determines whether a srcset value (used for responsive images) is safe by validating all URLs in the comma-separated list.

public static bool IsSafeSrcset(string? url)

Parameters

url string?
The srcset value to validate.

Returns

bool true if all URLs in the srcset are safe; otherwise, false.

IsSafeUrl(string?)

UrlSanitizer.cs

Determines whether a URL is safe by checking if it uses an allowed protocol (http, https, mailto, ftp, tel, file) or is a relative URL, or a safe data URL.

public static bool IsSafeUrl(string? url)

Parameters

url string?
The URL to validate.

Returns

bool true if the URL is safe; otherwise, false.