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

safe-request

v0.0.1

Published

`request` wrapper to protect from SSRF and similar attacks

Downloads

56

Readme

safe-request

Drop-in replacemnet for request to protect from SSRF and similar attacks. It's like sanitizing user input, but for URIs.

npm install safe-request
const request = require('safe-request');

request('http://example.org/terabyte-of-zeroes.gzip', (error, response, body) => {
  console.log(error);
  // Error: PayloadTooBig
});

API

If you are fine with default restrictions (only http(s) requests to public networks not bigger than 1 MiB), you can pretty much use this the same way as request with callback interface. The differences are:

  • gzip support is enabled by default;
  • timeout is set to 20 seconds;
  • errors are wrapped with original accessible via error.reason.
const request = require('safe-request');

request('https://example.com/?something=interesting', (err, res, body) => {/*…*/});

To specify your own restrictions, provide optional first argument.

// Require a function.
const request = require('safe-request');

// Set-up restrictions.
const restrictions = {
  checkUri: (uri) => uri.protocol === 'https:', // HTTPs requests only
  checkAddress: (addr) => addr === '127.0.0.1', // to localhost and nowhere else
  networkLimit: 20 * (2 << 20),                 // with up to 20 MiB of HTTP traffic
  decodedLimit: 128 * (2 << 20)                 // where payload is no bigger than 128 MiB.
};

// Pass that in as first argument.
request(
  restrictions,
  {
    method: 'post',
    uri: '/create',
    qs: {admin: true},
    formData: {
      json: require('fs').createReadStream(__dirname + '/blob.bin')
    },
    timeout: 60e3
  },
  (err, res, body) => {/*…*/}
);

// Or bind it and use much like `request`, with automatic validation.
const boundRequest = request.bind(null, restrictions);
boundRequest('https://localhost:443/path?and=query', (err, res, body) => {/*…*/});

Customizing Restrictions

Limits

These are approximate tresholds, and not exact numbers. Exceeding any of those will abort the request and return PayloadTooBig error.

Defaults are 1 MiB (1048576 bytes).

  • networkLimit — maximum number bytes to read from sockets;
  • encodedLimit — maximum size of encoded payload, in bytes (e.g. gzipped);
  • decodedLimit — maximum size of decoded payload, in bytes (e.g. un-gzipped).

In addition to the above, Node's HTTP parser has a hard limit on headers size of 80 KiB, exceeding it will result in PayloadTooBig error with reason.code === 'HPE_HEADER_OVERFLOW'.

URI and Address Validation

You can specify your own validation for hostnames and URIs by providing synchronous functions that return true in case request should proceed, or anything else to end it with an appropriate error. It is possible to disable any check by providing false instead of a function.

By default, any of http and https requests are allowed, except for Private Networks.

  • checkUri — receives single argument, URL object; invoked to validate starting URI, and before every redirect;
  • checkAddress — receives last 3 arguments from Socket#lookup event (address, family, host) and invoked after every successful hostname resolution.

Testing

Unfortunately, you can't just run npm test for now. The workaround is to add something like 127.0.0.1 my-pc to your /etc/hosts and run tests with

HOST=my-pc npm test