npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

genuine-browser

v2.2.8

Published

A package to detect browser requests using cryptographic challenge-response mechanism.

Downloads

79

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

  1. 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();
    1. 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();
    1. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.