Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require all traffic to provide a
User-Agent
header.
We want to be able to actually differentiate crawlers from each other, so I've nudged them towards actually using a unique user agent (we probably won't ever actually block generic UAs since folks sometimes do actually use curl/wget from the command line). Additionally, I've had a lot of cases lately where a crawler has been outside of what we allow, but wasn't actually causing a service impact. If I could contact those people without having to block their traffic, I would. So I've also worded the message to try and nudge folks towards including contact info, which most commercial bots already do.
- Loading branch information
Showing
6 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
We require that all requests include a `User-Agent` header. To allow us to determine the impact your bot has on our service, we ask that your user agent actually identify your bot, and not just report the HTTP client library you're using. Including contact information will also reduce the chance that we will need to take action against your bot. | ||
|
||
Bad: | ||
User-Agent: reqwest/0.9.1 | ||
|
||
Better: | ||
User-Agent: my_crawler | ||
|
||
Best: | ||
User-Agent: my_crawler (my_crawler.com/info) | ||
User-Agent: my_crawler (help@my_crawler.com) | ||
|
||
If you believe you've received this message in error, please email help@crates.io and include the request id {}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Middleware that blocks requests with no user-agent header | ||
|
||
use super::prelude::*; | ||
|
||
use std::collections::HashMap; | ||
use std::io::Cursor; | ||
use util::request_header; | ||
|
||
// Can't derive debug because of Handler. | ||
#[allow(missing_debug_implementations)] | ||
#[derive(Default)] | ||
pub struct RequireUserAgent { | ||
handler: Option<Box<dyn Handler>>, | ||
} | ||
|
||
impl AroundMiddleware for RequireUserAgent { | ||
fn with_handler(&mut self, handler: Box<dyn Handler>) { | ||
self.handler = Some(handler); | ||
} | ||
} | ||
|
||
impl Handler for RequireUserAgent { | ||
fn call(&self, req: &mut dyn Request) -> Result<Response, Box<dyn Error + Send>> { | ||
let has_user_agent = request_header(req, "User-Agent") != ""; | ||
if !has_user_agent { | ||
let body = format!( | ||
include_str!("no_user_agent_message.txt"), | ||
request_header(req, "X-Request-Id"), | ||
); | ||
let mut headers = HashMap::new(); | ||
headers.insert("Content-Length".to_string(), vec![body.len().to_string()]); | ||
Ok(Response { | ||
status: (403, "Forbidden"), | ||
headers, | ||
body: Box::new(Cursor::new(body.into_bytes())), | ||
}) | ||
} else { | ||
self.handler.as_ref().unwrap().call(req) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use conduit::Method; | ||
|
||
use {app, req}; | ||
|
||
#[test] | ||
fn user_agent_is_required() { | ||
let (_b, _app, middle) = app(); | ||
|
||
let mut req = req(Method::Get, "/api/v1/crates"); | ||
req.header("User-Agent", ""); | ||
bad_resp!(middle.call(&mut req)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters