Skip to content

Allow configuring the application's domain name #2543

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 1 commit into from
Jun 12, 2020
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
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export DATABASE_URL=
# If you are running a mirror of crates.io, uncomment this line.
# export MIRROR=1

# If you're running an instance of the application on a domain different than
# crates.io, uncomment this line and set the variable to your domain name.
#export DOMAIN_NAME=staging.crates.io

# Key to sign and encrypt cookies with. Must be at least 32 bytes. Change this
# to a long, random string for production.
export SESSION_KEY=badkeyabcdefghijklmnopqrstuvwxyzabcdef
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Config {
pub api_protocol: String,
pub publish_rate_limit: PublishRateLimit,
pub blocked_traffic: Vec<(String, Vec<String>)>,
pub domain_name: String,
}

impl Default for Config {
Expand Down Expand Up @@ -134,10 +135,15 @@ impl Default for Config {
api_protocol,
publish_rate_limit: Default::default(),
blocked_traffic: blocked_traffic(),
domain_name: domain_name(),
}
}
}

pub(crate) fn domain_name() -> String {
dotenv::var("DOMAIN_NAME").unwrap_or_else(|_| "crates.io".into())
}

fn blocked_traffic() -> Vec<(String, Vec<String>)> {
let pattern_list = dotenv::var("BLOCKED_TRAFFIC").unwrap_or_default();
parse_traffic_patterns(&pattern_list)
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {

let verified_email_address = user.verified_email(&conn)?;
let verified_email_address = verified_email_address.ok_or_else(|| {
cargo_err(
cargo_err(&format!(
"A verified email address is required to publish crates to crates.io. \
Visit https://crates.io/me to set and verify your email address.",
)
Visit https://{}/me to set and verify your email address.",
app.config.domain_name,
))
})?;

// Create a transaction on the database, if there are no errors,
Expand Down
15 changes: 10 additions & 5 deletions src/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ pub fn try_send_user_confirm_email(email: &str, user_name: &str, token: &str) ->
let body = format!(
"Hello {}! Welcome to Crates.io. Please click the
link below to verify your email address. Thank you!\n
https://crates.io/confirm/{}",
user_name, token
https://{}/confirm/{}",
user_name,
crate::config::domain_name(),
token
);

send_email(email, subject, &body)
Expand All @@ -94,9 +96,12 @@ pub fn send_owner_invite_email(email: &str, user_name: &str, crate_name: &str, t
let subject = "Crate ownership invitation";
let body = format!(
"{} has invited you to become an owner of the crate {}!\n
Visit https://crates.io/accept-invite/{} to accept this invitation,
or go to https://crates.io/me/pending-invites to manage all of your crate ownership invitations.",
user_name, crate_name, token
Visit https://{domain}/accept-invite/{} to accept this invitation,
or go to https://{domain}/me/pending-invites to manage all of your crate ownership invitations.",
user_name,
crate_name,
token,
domain = crate::config::domain_name()
);

let _ = send_email(email, subject, &body);
Expand Down
11 changes: 6 additions & 5 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ where
.header(header::USER_AGENT, "crates.io (https://crates.io)")
.send()?
.error_for_status()
.map_err(|e| handle_error_response(&e))?
.map_err(|e| handle_error_response(app, &e))?
.json()
.map_err(Into::into)
}

fn handle_error_response(error: &reqwest::Error) -> Box<dyn AppError> {
fn handle_error_response(app: &App, error: &reqwest::Error) -> Box<dyn AppError> {
use reqwest::StatusCode as Status;

match error.status() {
Some(Status::UNAUTHORIZED) | Some(Status::FORBIDDEN) => cargo_err(
Some(Status::UNAUTHORIZED) | Some(Status::FORBIDDEN) => cargo_err(&format!(
"It looks like you don't have permission \
to query a necessary property from Github \
to complete this request. \
You may need to re-authenticate on \
crates.io to grant permission to read \
github org memberships. Just go to \
https://crates.io/login",
),
https://{}/login",
app.config.domain_name,
)),
Some(Status::NOT_FOUND) => Box::new(NotFound),
_ => internal(&format_args!(
"didn't get a 200 result from github: {}",
Expand Down
8 changes: 7 additions & 1 deletion src/middleware/block_traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//! examples). Values of the headers must match exactly.

use super::prelude::*;
use crate::App;
use std::sync::Arc;

// Can't derive debug because of Handler.
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -37,6 +39,9 @@ impl AroundMiddleware for BlockTraffic {

impl Handler for BlockTraffic {
fn call(&self, req: &mut dyn RequestExt) -> AfterResult {
let app = req.extensions().find::<Arc<App>>().expect("Missing app");
let domain_name = app.config.domain_name.clone();

let has_blocked_value = req
.headers()
.get_all(&self.header_name)
Expand All @@ -49,10 +54,11 @@ impl Handler for BlockTraffic {
let body = format!(
"We are unable to process your request at this time. \
This usually means that you are in violation of our crawler \
policy (https://crates.io/policies#crawlers). \
policy (https://{}/policies#crawlers). \
Please open an issue at https://github.com/rust-lang/crates.io \
or email help@crates.io \
and provide the request id {}",
domain_name,
// Heroku should always set this header
req.headers()
.get("x-request-id")
Expand Down
1 change: 1 addition & 0 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn simple_config() -> Config {
api_protocol: String::from("http"),
publish_rate_limit: Default::default(),
blocked_traffic: Default::default(),
domain_name: "crates.io".into(),
}
}

Expand Down