@arcjet/ip
v1.0.0-alpha.16@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` or next.js'
// `NextRequest`
const request = new NextRequest();
// A `Headers` object, which is passed separately for cases where it needs to be
// constructed or sanitized
const headers = new Headers();
// Returns the first non-private IP address detected
const globalIp = ip(request, headers);
console.log(globalIp);
Considerations
The IP should not be trusted as it can be spoofed in most cases, especially when
loaded via the Headers
object.
In non-production environments (NODE_ENV !== "production"
), we allow
private/internal addresses so that the SDKs work correctly locally.
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.
Metadata
- Apache-2.0
- >=18
- Arcjet
- released 6/14/2024