genuine-browser
v2.2.8
Published
A package to detect browser requests using cryptographic challenge-response mechanism.
Downloads
79
Maintainers
Readme
Genuine Browser
Genuine Browser is a security-focused library designed to verify the authenticity of browser requests using a cryptographic challenge-response mechanism. It ensures that API requests originate from genuine browsers and not from automated tools or bots.
Why Use Genuine Browser?
In modern applications, especially those that rely heavily on APIs, it's critical to ensure that requests to your server are coming from legitimate clients and not from automated tools or malicious bots. Genuine Browser provides an extra layer of security by leveraging cryptographic keys to verify the browser's authenticity.
Key Benefits
- Prevent Replay Attacks: Each key pair and challenge is one-time-use, ensuring that requests cannot be replayed.
- Enhanced Security: Adds an extra layer of security on top of traditional authentication mechanisms.
- Seamless Integration: Works quietly in the background without impacting the user experience.
Ideal Use Cases
- Financial Applications: To ensure only genuine users can access secure resources.
- Healthcare Systems: To protect sensitive information with additional security.
- API-Driven Applications: To prevent abuse from automated tools and bots.
Installation
npm install genuine-browser@latest
or using yarn
yarn add genuine-browser@latest
Usage
Initialization
- Setting Up Browser Detection - Client Side
To set up browser detection and ensure the cryptographic keys are generated and registered:
import { setupBrowserDetection } from "genuine-browser";
async function initialize() {
await setupBrowserDetection();
console.log("Browser detection setup completed");
}
// setupBrowserDetection() is only available on the client side
initialize();
- Validating Browser Requests - Client Side
Once the setup is complete, you can validate browser requests before making sensitive API calls:
import { isBrowserRequestValid } from "genuine-browser";
async function validateRequest() {
const isValid = await isBrowserRequestValid();
if (isValid) {
console.log("The browser request is valid.");
} else {
console.error("Invalid browser request.");
}
}
// isBrowserRequestValid() is only available on the client side
validateRequest();
- Handling Server-Side Validation - Server Side
For server-side validation, you can use the cryptographic keys and challenge obtained from the frontend:
import { retrieveKeysForValidation } from "genuine-browser";
async function retrieveKeys() {
const { token, challenge, signature } = await retrieveKeysForValidation();
console.log("Token:", token);
console.log("Challenge:", challenge);
console.log("Signature:", signature);
// Use the retrieved keys to validate the browser from the server side or within your custom backend service
// const isValid = await validateBrowser(token, challenge, signature);
// validateBrowser() is only available on a server environment
}
Functions Exported by the Package
setupBrowserDetection(): Promise<void>
Sets up the browser detection by generating cryptographic keys and, in the case of frontend-backend mode, registering the public key with the backend.
isBrowserRequestValid(): Promise<boolean>
Performs a local challenge-response verification to validate the browser request. This is used in frontend-only mode.
retrieveKeysForValidation(): Promise<{token: string; challenge: string; signature: string;}>
Retrieves the cryptographic keys from the backend for further validation in frontend-backend mode. This function returns the token, challenge, and signature.
validateBrowser(token: string, challenge: string, signature: string): Promise<boolean>
Validates the browser by sending the token, challenge, and signature to the backend for verification. This is used in frontend-backend mode.
Important Usage Notes for Genuine Browser
Call
setupBrowserDetection()
on Every Validation Attempt:- You must call
setupBrowserDetection()
every time you want to perform browser validation. This function ensures that fresh cryptographic keys are generated for each validation attempt. - Why? The cryptographic validation process relies on one-time-use keys that are invalidated after successful use. To perform another validation, new keys must be generated.
- You must call
Token Validity:
- The authentication token generated during the
setupBrowserDetection()
process is valid for 30 minutes. After that, you must regenerate the token to continue making secure requests. - Ensure that your application handles token expiration by requesting a new token when necessary.
- The authentication token generated during the
One-Time Use Keys:
- The cryptographic keys used for validation are one-time-use. After validation, these keys are invalidated to prevent replay attacks. Always ensure that fresh keys are generated for each validation request to maintain security.
Ensure Freshness:
- Make sure that the validation occurs immediately after generating the cryptographic keys and token to avoid expiration issues. Delays could result in token expiration or validation failures.
Conclusion
By integrating Genuine Browser into your web application, you can significantly reduce the risk of unauthorized requests and provide an additional layer of protection against common attack vectors like replay attacks and request spoofing.