Crates.io | http-acl-reqwest |
lib.rs | http-acl-reqwest |
version | 0.5.10 |
source | src |
created_at | 2023-08-05 15:45:18.691191 |
updated_at | 2024-10-02 10:14:19.701411 |
description | An ACL middleware for reqwest. |
homepage | |
repository | https://github.com/nethunterslabs/http-acl |
max_upload_size | |
id | 936507 |
size | 11,822 |
An ACL middleware for reqwest.
Systems which allow users to create arbitrary HTTP requests or specify arbitrary URLs to fetch like webhooks are vulnerable to SSRF attacks. An example is a malicious user could own a domain which resolves to a private IP address and then use that domain to make requests to internal services.
This crate provides a simple ACL to allow you to specify which hosts, ports, and IP ranges are allowed to be accessed. The ACL can then be used to ensure that the user's request meets the ACL's requirements before the request is made.
Warning:
The DNS resolver needs to be set on the reqwest Client to ensure that the ACL is enforced. If the DNS resolver is not set, the ACL will not be enforced on IP addresses resolved by the DNS resolver.
use http_acl_reqwest::{HttpAcl, HttpAclMiddleware};
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an HTTP ACL
let acl = HttpAcl::builder()
.add_denied_host("example.com".to_string())
.unwrap()
.build();
// Create the HTTP ACL middleware
let middleware = HttpAclMiddleware::new(acl.clone());
// Create a reqwest client with the DNS resolver
let client = Client::builder()
.dns_resolver(middleware.dns_resolver())
.build()
.unwrap();
// Create a reqwest client with the middleware
let client_with_middleware = ClientBuilder::new(client)
.with(middleware)
.build();
// Make a request to a denied host
assert!(client_with_middleware.get("http://example.com/").send().await.is_err());
Ok(())
}
See docs.rs.