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

fastify-net-acl

v0.0.7

Published

Fastify plugin that restricts access according to IP address/subnet allow and block lists.

Downloads

231

Readme

fastify-net-acl

js-standard-style

Fastify plugin that restricts access according to IP address/subnet allow and block lists.

Install

npm i fastify-net-acl

Usage

Blocking

Block IP address:

fastify.register(require('fastify-net-acl'), {
  blockList: '1.2.3.4'
})

Block multiple IP addresses:

fastify.register(require('fastify-net-acl'), {
  blockList: [
    '1.2.3.4',
    '2.3.4.5'
  ]
})

Block subnet:

fastify.register(require('fastify-net-acl'), {
  blockList: '1.2.3.4/24'
})

Block a bunch of stuff:

fastify.register(require('fastify-net-acl'), {
  blockList: [
    '::1',
    '4.3.2.1',
    '4.2.3.4',
    '1.2.3.4/24',
    '2.3.4.5/16'
  ]
})

Allowing

Only allow 1 IP address:

fastify.register(require('fastify-net-acl'), {
  allowList: '1.2.3.4'
})

Only allow 1 subnet:

fastify.register(require('fastify-net-acl'), {
  allowList: '1.2.3.4/24'
})

Only allow specified IP addresses and subnets:

fastify.register(require('fastify-net-acl'), {
  allowList: [
    '::1',
    '2.3.4.5',
    '1.2.3.4/24'
  ]
})

Route specific rules

Note: you must await the plugin registration so the decorator function is accessible for the route definition.

await fastify.register(require('fastify-net-acl'), {
  blockList: '1.2.3.4/24',
  global: false
})

const onRequest = fastify.createNetAclRequestHandler()

fastify.get('/has-blocking', { onRequest }, (req, reply) => {})
fastify.get('/no-blocking', (req, reply) => {})

Use multiple allow/block lists:

await fastify.register(require('fastify-net-acl'), {
  allowList: {
    foo: '1.2.3.4/24'
  },

  blockList: {
    bar: '2.3.4.5/24'
  },

  global: false
})

{
  const onRequest = fastify.createNetAclRequestHandler('allow:foo')
  fastify.get('/foo', { onRequest }, (req, reply) => {})
}

{
  const onRequest = fastify.createNetAclRequestHandler('block:bar')
  fastify.get('/bar', { onRequest }, (req, reply) => {})
}

fastify.get('/no-blocking', (req, reply) => {})

Reference

This plugin decorates the fastify instance with allowList and/or blockList, depending on which properties are specified in options. Both are instances of net.BlockList and determine which IP addresses/subnets are allowed or blocked, respectively.

Note: fastify-net-acl requires Node >= 15.0.0 since net.Blocklist was introduced in 15.0.0.

The options object has the following properties. Either allowList xor blockList must be specified if global is true. Both may be specified if global is false since you may want to use different allow/block lists on different routes.

  • allowList is an array of IPv4/IPv6 addresses and/or subnets in CIDR format indicating which IPs should be allowed
  • blockList is an array of IPv4/IPv6 addresses and/or subnets in CIDR format indicating which IPs should be blocked
  • errorCode is the HTTP status code when an IP is blocked/not allowed (default: 403)
  • errorMessage is the status message when an IP is blocked/not allowed (default: generic status message for errorCode)
  • global is a boolean indicating whether the ACL applies globally, i.e. for all routes (default: true)

Test

npm test

Lint

npm run lint or npm run lint:fix

License

Licensed under MIT.