@arcjet/ip
v1.0.0-alpha.32
Published
Arcjet utilities for finding the originating IP of a request
Downloads
17,349
Readme
@arcjet/ip
Arcjet utilities for finding the originating IP of a request.
Installation
npm install -S @arcjet/ip
Example
import ip from "@arcjet/ip";
// Some Request-like object, such as node's `http.IncomingMessage`, `Request` or
// Next.js' `NextRequest`
const request = new Request();
// Returns the first non-private IP address detected
const globalIp = ip(request);
console.log(globalIp);
// Also optionally takes a platform for additional protection
const platformGuardedGloablIp = ip(request, { platform: "fly-io" });
Considerations
The IP should not be trusted as it can be spoofed in most cases, especially when
loaded via the Headers
object. We apply additional platform guards if a
platform is supplied in the options
argument.
If a private/internal address is encountered, it will be skipped. If only those are detected, an empty string is returned.
Implementation
The implementation of this library is based on the Parser, global ipv4 comparisons, and global ipv6 comparisons in the Rust stdlib and the header lookups in the request-ip package. Both licensed MIT with licenses included in our source code.
We've chosen the approach of porting Rust's IP Parser because capturing RegExps can be the source of ReDoS attacks, which we need to avoid. We also wanted to keep our implementation as close to Rust as possible because we will be relying on the Rust stdlib implementation in the future, with a fallback to this implementation. As such, we'll need to track changes in Rust's implementation, even though it seems to change infrequently.
License
Licensed under the Apache License, Version 2.0.