Skip to content

Fix IP-based request blocking for large page numbers #7434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ impl Server {
/// list will block *all* user-agents exceeding the offset. If not set or empty, no blocking
/// will occur.
/// - `WEB_PAGE_OFFSET_CIDR_BLOCKLIST`: A comma separated list of CIDR blocks that will be used
/// to block IP addresses given in the `X-Real-Ip` HTTP header, e.g. `192.168.1.0/24`.
/// If not set or empty, no blocking will occur.
/// to block IP addresses, e.g. `192.168.1.0/24`. If not set or empty, no blocking will occur.
/// - `INSTANCE_METRICS_LOG_EVERY_SECONDS`: How frequently should instance metrics be logged.
/// If the environment variable is not present instance metrics are not logged.
/// - `FORCE_UNCONDITIONAL_REDIRECTS`: Whether to force unconditional redirects in the download
Expand Down
16 changes: 7 additions & 9 deletions src/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::Server;
use crate::controllers::prelude::*;
use crate::controllers::util::RequestPartsExt;
use crate::middleware::log_request::RequestLogExt;
use crate::middleware::real_ip::RealIp;
use crate::models::helpers::with_count::*;
use crate::util::errors::{bad_request, AppResult};
use crate::util::HeaderMapExt;
Expand All @@ -11,11 +12,8 @@ use diesel::pg::Pg;
use diesel::query_builder::*;
use diesel::query_dsl::LoadQuery;
use diesel::sql_types::BigInt;
use http::HeaderMap;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::str::FromStr;

const MAX_PAGE_BEFORE_SUSPECTED_BOT: u32 = 10;
const DEFAULT_PER_PAGE: i64 = 10;
Expand Down Expand Up @@ -102,7 +100,7 @@ impl PaginationOptionsBuilder {
if self.limit_page_numbers {
let config = &req.app().config;
if numeric_page > config.max_allowed_page_offset
&& is_useragent_or_ip_blocked(config, req.headers())
&& is_useragent_or_ip_blocked(config, req)
{
req.request_log().add("cause", "large page offset");
return Err(bad_request("requested page offset is too large"));
Expand Down Expand Up @@ -258,9 +256,9 @@ impl RawSeekPayload {
///
/// A request can be blocked if either the User Agent is on the User Agent block list or if the client
/// IP is on the CIDR block list.
fn is_useragent_or_ip_blocked(config: &Server, headers: &HeaderMap) -> bool {
let user_agent = headers.get_str_or_default(header::USER_AGENT);
let client_ip = headers.get_str_or_default("x-real-ip");
fn is_useragent_or_ip_blocked<T: RequestPartsExt>(config: &Server, req: &T) -> bool {
let user_agent = req.headers().get_str_or_default(header::USER_AGENT);
let client_ip = req.extensions().get::<RealIp>();

// check if user agent is blocked
if config
Expand All @@ -272,11 +270,11 @@ fn is_useragent_or_ip_blocked(config: &Server, headers: &HeaderMap) -> bool {
}

// check if client ip is blocked, needs to be an IPv4 address
if let Ok(client_ip) = IpAddr::from_str(client_ip) {
if let Some(client_ip) = client_ip {
if config
.page_offset_cidr_blocklist
.iter()
.any(|blocked| blocked.contains(client_ip))
.any(|blocked| blocked.contains(**client_ip))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here I thought I'd left asterisk combinations back in my C days.

{
return true;
}
Expand Down
1 change: 0 additions & 1 deletion src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ fn req(method: Method, path: &str) -> MockRequest {
.method(method)
.uri(path)
.header(header::USER_AGENT, "conduit-test")
.header("x-real-ip", "127.0.0.1")
.body(Bytes::new())
.unwrap()
}
Expand Down