Skip to content

Commit 8bee7b9

Browse files
author
Sebastian Ziebell
committed
Replace cidr-utils crate with ipnetwork
1 parent cc88169 commit 8bee7b9

File tree

4 files changed

+21
-41
lines changed

4 files changed

+21
-41
lines changed

Cargo.lock

Lines changed: 10 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ cargo-registry-index = { path = "cargo-registry-index" }
3535
cargo-registry-markdown = { path = "cargo-registry-markdown" }
3636
cargo-registry-s3 = { path = "cargo-registry-s3" }
3737
chrono = { version = "=0.4.19", features = ["serde"] }
38-
cidr-utils = "=0.5.6"
3938
clap = { version = "=3.1.18", features = ["derive", "unicode"] }
4039

4140
conduit = "=0.10.0"
@@ -62,6 +61,7 @@ hex = "=0.4.3"
6261
http = "=0.2.7"
6362
hyper = { version = "=0.14.18", features = ["client", "http1"] }
6463
indexmap = { version = "=1.8.1", features = ["serde-1"] }
64+
ipnetwork = "=0.19.0"
6565
tikv-jemallocator = { version = "=0.4.3", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
6666
lettre = { version = "=0.10.0-rc.6", default-features = false, features = ["file-transport", "smtp-transport", "native-tls", "hostname", "builder"] }
6767
minijinja = "=0.15.0"

src/config.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cidr_utils::cidr::Ipv4Cidr;
1+
use ipnetwork::Ipv4Network;
22

33
use crate::publish_rate_limit::PublishRateLimit;
44
use crate::{env, env_optional, uploaders::Uploader, Env};
@@ -27,7 +27,7 @@ pub struct Server {
2727
pub blocked_traffic: Vec<(String, Vec<String>)>,
2828
pub max_allowed_page_offset: u32,
2929
pub page_offset_ua_blocklist: Vec<String>,
30-
pub page_offset_cidr_blocklist: Vec<Ipv4Cidr>,
30+
pub page_offset_cidr_blocklist: Vec<Ipv4Network>,
3131
pub excluded_crate_names: Vec<String>,
3232
pub domain_name: String,
3333
pub allowed_origins: Vec<String>,
@@ -166,12 +166,12 @@ pub(crate) fn domain_name() -> String {
166166
/// * Only IPv4 blocks are currently supported.
167167
/// * The minimum number of host prefix bits must be at least 16.
168168
///
169-
fn parse_cidr_blocks(blocks: &[String]) -> Vec<Ipv4Cidr> {
169+
fn parse_cidr_blocks(blocks: &[String]) -> Vec<Ipv4Network> {
170170
blocks
171171
.iter()
172-
.map(|block| match Ipv4Cidr::from_str(block) {
172+
.map(|block| match block.parse::<Ipv4Network>() {
173173
Ok(cidr) => {
174-
if cidr.get_bits() < 16 {
174+
if cidr.prefix() < 16 {
175175
panic!(
176176
"WEB_PAGE_OFFSET_CIDR_BLOCKLIST must only contain CIDR blocks with \
177177
a host prefix of at least 16 bits."
@@ -232,8 +232,8 @@ fn parse_cidr_block_list_successfully() {
232232
let blocks = parse_cidr_blocks(&cidr_blocks);
233233
assert_eq!(
234234
vec![
235-
Ipv4Cidr::from_str("127.0.0.1/24").unwrap(),
236-
Ipv4Cidr::from_str("192.168.0.1/31").unwrap(),
235+
Ipv4Network::new(std::net::Ipv4Addr::new(127, 0, 0, 1), 24).unwrap(),
236+
Ipv4Network::new(std::net::Ipv4Addr::new(192, 168, 0, 1), 31).unwrap(),
237237
],
238238
blocks,
239239
);

src/tests/krate/search.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::util::{RequestHelper, TestApp};
33
use crate::{new_category, new_user};
44
use cargo_registry::models::Category;
55
use cargo_registry::schema::crates;
6-
use cidr_utils::cidr::Ipv4Cidr;
76
use diesel::{dsl::*, prelude::*, update};
87
use http::StatusCode;
8+
use ipnetwork::Ipv4Network;
99

1010
#[test]
1111
fn index() {
@@ -829,7 +829,8 @@ fn pagination_blocks_ip_from_cidr_block_list() {
829829
let (app, anon, user) = TestApp::init()
830830
.with_config(|config| {
831831
config.max_allowed_page_offset = 1;
832-
config.page_offset_cidr_blocklist = vec![Ipv4Cidr::from_str("127.0.0.1/24").unwrap()];
832+
config.page_offset_cidr_blocklist =
833+
vec!["127.0.0.1/24".parse::<Ipv4Network>().unwrap()];
833834
})
834835
.with_user();
835836
let user = user.as_model();

0 commit comments

Comments
 (0)