correctly mutex-lock and modify valid_domains

thanks to @fdb-hiroshima for the review
This commit is contained in:
Igor Galić 2019-08-19 14:00:04 +02:00
parent ed30284386
commit 444a4673f4
2 changed files with 18 additions and 13 deletions

View File

@ -1,5 +1,5 @@
#![allow(clippy::too_many_arguments)] #![allow(clippy::too_many_arguments)]
#![feature(decl_macro, proc_macro_hygiene, checked_duration_since, try_trait)] #![feature(decl_macro, proc_macro_hygiene, try_trait)]
extern crate activitypub; extern crate activitypub;
extern crate askama_escape; extern crate askama_escape;
@ -197,7 +197,7 @@ Then try to restart Plume
} }
}); });
let mut valid_domains: HashMap<&str, Instant> = HashMap::new(); let valid_domains: HashMap<String, Instant> = HashMap::new();
let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()) let rocket = rocket::custom(CONFIG.rocket.clone().unwrap())
.mount( .mount(
"/custom_domains/", "/custom_domains/",
@ -324,7 +324,7 @@ Then try to restart Plume
.manage(dbpool) .manage(dbpool)
.manage(Arc::new(workpool)) .manage(Arc::new(workpool))
.manage(searcher) .manage(searcher)
.manage(valid_domains) .manage(Mutex::new(valid_domains))
.manage(include_i18n!()) .manage(include_i18n!())
.attach( .attach(
CsrfFairingBuilder::new() CsrfFairingBuilder::new()

View File

@ -2,12 +2,13 @@ use activitypub::collection::OrderedCollection;
use atom_syndication::{Entry, FeedBuilder}; use atom_syndication::{Entry, FeedBuilder};
use diesel::SaveChangesDsl; use diesel::SaveChangesDsl;
use rocket::{ use rocket::{
http::ContentType, http::{ContentType, Status},
request::LenientForm, request::LenientForm,
response::{content::Content, Flash, Redirect}, response::{content::Content, Flash, Redirect},
State, State,
}; };
use rocket_i18n::I18n; use rocket_i18n::I18n;
use std::sync::Mutex;
use std::time::Instant; use std::time::Instant;
use std::{borrow::Cow, collections::HashMap}; use std::{borrow::Cow, collections::HashMap};
use validator::{Validate, ValidationError, ValidationErrors}; use validator::{Validate, ValidationError, ValidationErrors};
@ -100,11 +101,15 @@ pub fn new(rockets: PlumeRocket, _user: User) -> Ructe {
#[get("/validate/<validation_id>")] #[get("/validate/<validation_id>")]
pub fn domain_validation( pub fn domain_validation(
validation_id: String, validation_id: String,
valid_domains: State<HashMap<&str, Instant>>, valid_domains: State<Mutex<HashMap<String, Instant>>>,
) -> Result<String, String> { ) -> Status {
let value = valid_domains.inner().get(validation_id.as_str()); let mutex = valid_domains.inner().lock();
let mut validation_map = mutex.unwrap();
let validation_getter = validation_map.clone();
let value = validation_getter.get(&validation_id);
if value.is_none() { if value.is_none() {
return Err(String::from("404")); return Status::new(404, "validation id not found");
} }
// we have valid id, now check the time // we have valid id, now check the time
@ -112,13 +117,13 @@ pub fn domain_validation(
let now = Instant::now(); let now = Instant::now();
// nope, expired (410: gone) // nope, expired (410: gone)
if now.checked_duration_since(*valid_until).unwrap().as_secs() > 0 { if now.duration_since(*valid_until).as_secs() > 0 {
valid_domains.inner().remove(validation_id.as_str()); validation_map.remove(&validation_id);
return Err(String::from("410")); return Status::new(410, "validation expired");
} }
valid_domains.inner().remove(validation_id.as_str()); validation_map.remove(&validation_id);
return Ok(String::from("200")); Status::Ok
} }
pub mod custom { pub mod custom {