SanitizeUrl.NET is a lightweight .NET library that sanitizes URLs by removing potentially harmful or unwanted characters. Inspired by Braintree's Sanitize URL for JavaScript, this package ensures URLs are safe for use in web applications, preventing XSS attacks and other vulnerabilities. Easy to integrate, with minimal overhead, making it ideal for secure URL handling in any .NET project.
To use SanitizeUrl.NET in your C# project, you need to install the NuGet package. Follow these simple steps:
- Open Your Project: Open your project in Visual Studio or your preferred IDE.
- Open the Package Manager Console: Navigate to
Tools
->NuGet Package Manager
->Package Manager Console
. - Install SanitizeUrl.NET: Type the following command and press Enter:
Install-Package SanitizeUrl.NET
Alternatively, you can use .NET Core CLI to install SanitizeUrl.NET. Open your command prompt or terminal and run:
dotnet add package SanitizeUrl.NET
After installation, make sure that SanitizeUrl.NET is listed in your project dependencies to confirm successful installation.
using SanitizeUrl;
Console.WriteLine(UrlSanitizer.SanitizeUrl("https://example.com")); // 'https://example.com'
Console.WriteLine(UrlSanitizer.SanitizeUrl("http://example.com")); // 'http://example.com'
Console.WriteLine(UrlSanitizer.SanitizeUrl("www.example.com")); // 'http://www.example.com'
Console.WriteLine(UrlSanitizer.SanitizeUrl("mailto:hello@example.com")); // 'mailto:hello@example.com'
Console.WriteLine(
UrlSanitizer.SanitizeUrl("https://example.com")
); // 'https://example.com'
Console.WriteLine(UrlSanitizer.SanitizeUrl("javascript:alert(document.domain)")); // 'about:blank'
Console.WriteLine(UrlSanitizer.SanitizeUrl("jAvasCrIPT:alert(document.domain)")); // 'about:blank'
Console.WriteLine(UrlSanitizer.SanitizeUrl("JaVaScRiP%0at:alert(document.domain)")); // 'about:blank'
// HTML encoded javascript:alert('XSS')
Console.WriteLine(
UrlSanitizer.SanitizeUrl("javascript:alert('XSS')")
); // 'about:blank'
If you find this project useful, consider supporting it by buying me a coffee. Your support is greatly appreciated!
Contributions are welcome! If you have a feature to propose or a bug to fix, create a new pull request.
This project is licensed under the MIT License.
This project is inspired by and built upon the sanitize-url project.