Introducing Nehonix URI Processor: Simplify URI Validation and Security in TypeScript
Hey Dev.to community! đ Iâm thrilled to share a project Iâve been working on: nehonix-uri-processor, a TypeScript library that makes URI validation, decoding, and security analysis a breeze. Whether youâre building a web app, securing an API, or conducting security tests, this library is here to help. Letâs dive in!
What is nehonix-uri-processor?
nehonix-uri-processor
is a lightweight TypeScript library for handling URIs securely and efficiently. Itâs packed with features to validate, decode, and analyze URIs, making it an essential tool for web developers and security researchers.
Key Features
- Validate URIs: Check URIs against custom rules (e.g., HTTPS-only, max length).
- Auto-Decode URIs: Automatically detect and decode encodings like Base64, percent encoding, and more.
- Security Analysis: Identify potential vulnerabilities in URIs.
- WAF Testing: Generate encoding variants to test Web Application Firewalls (WAFs).
The library is at version 2.1.3
, published under the MIT License, so itâs free to use in any project!
Why I Built This
Handling URIs can be trickyâwhether itâs validating user inputs, decoding encoded parameters, or checking for security risks. I often found myself writing repetitive code to handle these tasks, and existing solutions either lacked TypeScript support or were too complex. So, I created nehonix-uri-processor
to simplify URI processing while keeping security in mind.
Getting Started
Installation
Install the library via npm:
npm i nehonix-uri-processor
Youâll also need to install the punycode
dependency:
npm install punycode
A Quick Example
Hereâs how you can use nehonix-uri-processor
to decode a Base64-encoded URI parameter:
import { NehonixURIProcessor } from "nehonix-uri-processor";
// Decode a Base64-encoded query parameter
const encodedUri = "SGVsbG8gV29ybGQ="; // "Hello World" in Base64
const decoded = NehonixURIProcessor.autoDetectAndDecode(encodedUri);
console.log(decoded); // Outputs: "Hello World"
This example uses the recommended autoDetectAndDecode
method, which automatically detects the encoding (Base64 in this case) and decodes it to plaintext.
Use Case: Securing an API Endpoint
Letâs say youâre building an API that accepts URLs as query parameters. You want to validate and decode the URIs before processing them to avoid security issues. Hereâs how nehonix-uri-processor
can help:
import { NehonixURIProcessor } from "nehonix-uri-processor";
// Example API endpoint
async function handleRequest(queryParam: string) {
try {
// Decode the query parameter
const decoded = NehonixURIProcessor.autoDetectAndDecode(queryParam);
console.log("Decoded:", decoded);
// Validate the URI (e.g., only allow HTTPS URLs)
const isValid = NehonixURIProcessor.isValidUri(decoded, { httpsOnly: true });
if (!isValid) {
throw new Error("Invalid URI");
}
// Process the URI safely
return { status: "success", data: decoded };
} catch (error) {
return { status: "error", message: error.message };
}
}
// Test with an encoded URI
const result = handleRequest("https://example.com?data=SGVsbG8gV29ybGQ=");
console.log(result); // { status: "success", data: "https://example.com?data=Hello World" }
This code ensures the URI is decoded and validated before being processed, reducing risks like injection attacks or malformed inputs.
Whatâs New in v2.1.3?
The latest version includes several improvements:
- Added MIT License for open-source usage.
- Fixed circular dependency issues for better reliability.
- Improved decoding to handle edge cases (e.g., correctly converting
Hello+World
toHello World
). - Switched to named exports for a clearer API (
import { NehonixURIProcessor } from "nehonix-uri-processor"
).
Try It Out!
You can install nehonix-uri-processor
now and give it a try:
npm i nehonix-uri-processor
For more examples and details, check out the documentation. Iâd love to hear your feedbackâlet me know how it works for you in the comments! đ
Contribute
Want to help make this library even better? Contributions are welcome! Fork the repo on GitHub and submit a pull request.
Thanks for reading! If youâre working on a project that involves URIs, I hope nehonix-uri-processor
can save you some time and keep your app secure. Letâs discuss in the commentsâwhatâs your go-to tool for URI handling?
Top comments (0)