IP Restrictions
Overview
The IP Restrictions module allows or denies traffic based on the source IP of the connection that was initiated to your ngrok endpoints. You define rules which allow or deny connections from IPv4 or IPv6 CIDR blocks.
A connection is allowed only if its source IP matches at least one rule with an 'allow' action and does not match any rule with a 'deny' action.
Example Usage
Allow TCP connections from 110.0.0.0/8
and 220.12.0.0/16
but not from
110.2.3.4/32
.
- Agent CLI
- Agent Config
- SSH
- Go
- Javascript
- Python
- Rust
- Kubernetes Controller
ngrok http 80 --cidr-allow 110.0.0.0/8 --cidr-allow 220.12.0.0/16 --cidr-deny 110.2.3.4/32
tunnels:
example:
proto: http
addr: 80
ip_restriction:
allow_cidrs: [110.0.0.0/8, 220.12.0.0/16]
deny_cidrs: [110.2.3.4/32]
ssh -R 443:localhost:80 v2@connect.ngrok-agent.com http \
--cidr-allow 110.0.0.0/8 \
--cidr-allow 220.12.0.0/16 \
--cidr-deny 110.2.3.4/32
import (
"context"
"net"
"golang.ngrok.com/ngrok"
"golang.ngrok.com/ngrok/config"
)
func ngrokListener(ctx context.Context) (net.Listener, error) {
return ngrok.Listen(ctx,
config.HTTPEndpoint(
config.WithAllowCIDRString("110.0.0.0/8", "220.12.0.0/16"),
config.WithDenyCIDRString("110.2.3.4/32"),
),
ngrok.WithAuthtokenFromEnv(),
)
}
Go Package Docs:
const ngrok = require("@ngrok/ngrok");
(async function () {
const listener = await ngrok.forward({
addr: 8080,
authtoken_from_env: true,
ip_restriction_allow_cidrs: ["110.0.0.0/8", "220.12.0.0/16"],
ip_restriction_deny_cidrs: "110.2.3.4/32",
});
console.log(`Ingress established at: ${listener.url()}`);
})();
Javascript SDK Docs:
- https://ngrok.github.io/ngrok-javascript/interfaces/Config.html#ip_restriction_allow_cidrs
- https://ngrok.github.io/ngrok-javascript/interfaces/Config.html#ip_restriction_deny_cidrs
- https://ngrok.github.io/ngrok-javascript/classes/HttpListenerBuilder.html#allowCidr
- https://ngrok.github.io/ngrok-javascript/classes/HttpListenerBuilder.html#denyCidr
import ngrok
listener = ngrok.forward("localhost:8080", authtoken_from_env=True,
ip_restriction_allow_cidrs=["110.0.0.0/8", "220.12.0.0/16"],
ip_restriction_deny_cidrs="110.2.3.4/32")
print(f"Ingress established at: {listener.url()}");
Python SDK Docs:
use ngrok::prelude::*;
async fn listen_ngrok() -> anyhow::Result<impl Tunnel> {
let sess = ngrok::Session::builder()
.authtoken_from_env()
.connect()
.await?;
let tun = sess
.http_endpoint()
.allow_cidr("110.0.0.0/8"),
.allow_cidr("220.12.0.0/16"),
.deny_cidr("110.2.3.4/32"),
.listen()
.await?;
println!("Listening on URL: {:?}", tun.url());
Ok(tun)
}
Rust Crate Docs:
kind: IPPolicy
apiVersion: ingress.k8s.ngrok.com/v1alpha1
metadata:
name: policy-1
spec:
description: "My Trusted IPs"
rules:
- action: "allow"
cidr: "110.0.0.0/8"
- action: "allow"
cidr: "220.12.0.0/16"
- action: "deny"
cidr: "110.2.3.4/32"
---
kind: NgrokModuleSet
apiVersion: ingress.k8s.ngrok.com/v1alpha1
metadata:
name: ngrok-module-set
modules:
ipRestriction:
policies:
- "policy-1" # Reference to the `ippolicy.ingress.k8s.ngrok.com` Custom Resource above
- "ipp_1234567890" # Reference to an IP Policy by its ngrok API ID
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
k8s.ngrok.com/modules: ngrok-module-set
spec:
ingressClassName: ngrok
rules:
- host: your-domain.ngrok.app
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
Behavior
Rule Evaluation
A connection is allowed only if its source IP matches at least one rule with an 'allow' action and does not match any rule with a 'deny' action.
When using Edges and the Kubernetes Ingress Controller, if the IP Restrictions module references multiple IP Policies, then the rules of all referenced IP Policies are unioned together for evaluation.
IPv6
ngrok supports IPv6 addresses for all IP rules. You may use standard abbreviated notations.
ngrok http 80 --allow-cidr "::/0" --deny-cidr "2600:1f16:d83:1202::6e:2/128"
Don't forget to create IPv6 rules. It's easy to test only with IPv4 and then suddenly things don't work when your software starts using IPv6 because you've forgotten to create rules to allow traffic from IPv6 addresses.
Forwarded-For
The IP Restrictions always evaluates its rules against the layer 4 source IP of
a connection. HTTP headers like forwarded-for
are never consulted by this
module.
Reference
Configuration
Agent Configuration
Parameter | Description |
---|---|
Allow CIDRs | A set of IPv4 and IPv6 CIDRs to allow. |
Deny CIDRs | A set of IPv4 and IPv6 CIDRs to deny. |
Edge Configuration
Parameter | Description |
---|---|
IP Policy IDs | A set of IP policies that will be used to check if a source IP is allowed access. See the HTTPS Edge IP Restrictions Module API Resource for additional details. |
Upstream Headers
This module does not add any upstream headers.
Errors
The following errors are returned on HTTP endpoints.
Code | HTTP Status | Error |
---|---|---|
ERR_NGROK_3205 | 403 | This error is returned if a connection is disallowed by this module. |
Events
When the IP Restrictions module is enforced, it populates the following fields in both the http_request_complete.v0 and the tcp_connection_closed.v0 events.
Fields |
---|
ip_policy.decision |
Edges
IP Restrictions is an HTTPS Edge module which can be applied to Routes.
When using IP Restrictions via Edges, you specify a set of references to one or more IP Policy objects, each of which contains a list of IP Policy Rule objects.
IP Restrictions and the IP Policy and IP Policy Rule objects they reference can be configured via the ngrok dashboard or API.
Pricing
This module is available on the Pro and Enterprise plans.
Try it out
First, grab your IPv4 and IPv6 addresses:
curl -4 icanhazip.com
curl -6 icanhazip.com
Then run ngrok with IP Restrictions with the IPv4 and IPv6 addresses you got in the previous step:
ngrok http 80 \
--domain your-domain.ngrok.app \
--allow-cidr 2600:8c00::a03c:91ee:fe69:9695/32 \
--allow-cidr 78.227.75.230/32
Then make requests to your ngrok domain using the -4
and -6
flags to test both IPv4 and IPv6:
curl -4 https://your-domain.ngrok.app
curl -6 https://your-domain.ngrok.app