From 444a4673f45bc97b2152cecc1b8c20cb596bfc99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Mon, 19 Aug 2019 14:00:04 +0200 Subject: [PATCH] correctly mutex-lock and modify valid_domains thanks to @fdb-hiroshima for the review --- src/main.rs | 6 +++--- src/routes/blogs.rs | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index fc3ab79f..f9aaba9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ #![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 askama_escape; @@ -197,7 +197,7 @@ Then try to restart Plume } }); - let mut valid_domains: HashMap<&str, Instant> = HashMap::new(); + let valid_domains: HashMap = HashMap::new(); let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()) .mount( "/custom_domains/", @@ -324,7 +324,7 @@ Then try to restart Plume .manage(dbpool) .manage(Arc::new(workpool)) .manage(searcher) - .manage(valid_domains) + .manage(Mutex::new(valid_domains)) .manage(include_i18n!()) .attach( CsrfFairingBuilder::new() diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 76736b34..e2988db0 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -2,12 +2,13 @@ use activitypub::collection::OrderedCollection; use atom_syndication::{Entry, FeedBuilder}; use diesel::SaveChangesDsl; use rocket::{ - http::ContentType, + http::{ContentType, Status}, request::LenientForm, response::{content::Content, Flash, Redirect}, State, }; use rocket_i18n::I18n; +use std::sync::Mutex; use std::time::Instant; use std::{borrow::Cow, collections::HashMap}; use validator::{Validate, ValidationError, ValidationErrors}; @@ -100,11 +101,15 @@ pub fn new(rockets: PlumeRocket, _user: User) -> Ructe { #[get("/validate/")] pub fn domain_validation( validation_id: String, - valid_domains: State>, -) -> Result { - let value = valid_domains.inner().get(validation_id.as_str()); + valid_domains: State>>, +) -> Status { + 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() { - return Err(String::from("404")); + return Status::new(404, "validation id not found"); } // we have valid id, now check the time @@ -112,13 +117,13 @@ pub fn domain_validation( let now = Instant::now(); // nope, expired (410: gone) - if now.checked_duration_since(*valid_until).unwrap().as_secs() > 0 { - valid_domains.inner().remove(validation_id.as_str()); - return Err(String::from("410")); + if now.duration_since(*valid_until).as_secs() > 0 { + validation_map.remove(&validation_id); + return Status::new(410, "validation expired"); } - valid_domains.inner().remove(validation_id.as_str()); - return Ok(String::from("200")); + validation_map.remove(&validation_id); + Status::Ok } pub mod custom {