From 5e8d2d398fe1b5aaa91df7812e27696403b04837 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 26 Feb 2024 17:07:40 -0800 Subject: [PATCH] make session key unguessable --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 ++ src/handlers.rs | 11 +++++++---- src/main.rs | 3 +++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 921221a..459e08c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -466,6 +466,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" @@ -674,6 +680,8 @@ dependencies = [ "askama_axum", "axum", "justerror", + "lazy_static", + "rand", "serde", "thiserror", "time", diff --git a/Cargo.toml b/Cargo.toml index 65cbb76..6868672 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,8 @@ askama = { version = "0.12", default-features = false, features = ["with-axum", askama_axum = { version = "0.4.0", default-features = false } axum = { version = "0.7", default-features = false, features = ["tokio", "http1", "form"] } justerror = { version = "1" } +lazy_static = "1.4.0" +rand = { version = "0.8.5", default-features = false, features = ["getrandom"] } serde = { version = "1", default-features = false, features = ["derive"] } thiserror = { version = "1" } time = { version = "0.3.34", default-features = false } diff --git a/src/handlers.rs b/src/handlers.rs index 17a5ada..8733d87 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -5,19 +5,22 @@ use axum::{ http::StatusCode, response::{IntoResponse, Redirect, Response}, }; +use rand::random; use serde::Deserialize; use tower_sessions::Session; use unicode_segmentation::UnicodeSegmentation; use crate::{templates::*, User}; -const SIGNUP_KEY: &str = "meow"; - const PASSWORD_LEN: RangeInclusive = 4..=100; const USERNAME_LEN: RangeInclusive = 1..=50; const DISPLAYNAME_LEN: RangeInclusive = 0..=100; const EMAIL_LEN: RangeInclusive = 4..=50; +lazy_static! { + static ref SIGNUP_KEY: String = format!("meow-{}", random::()); +} + #[Error(desc = "Could not create user.")] #[non_exhaustive] pub struct CreateUserError(#[from] CreateUserErrorKind); @@ -66,7 +69,7 @@ pub async fn post_signup( Form(form): Form, ) -> Result { let user = validate_signup(&form).await?; - session.insert(SIGNUP_KEY, user).await.unwrap(); + session.insert(&SIGNUP_KEY, user).await.unwrap(); Ok(Redirect::to( "https://buy.stripe.com/test_eVa6rrb7ygjNbwk000", @@ -89,7 +92,7 @@ pub async fn post_edit_signup( /// Called from Stripe with the receipt of payment. pub async fn payment_success(session: Session, receipt: Option>) -> impl IntoResponse { - let user: User = session.get(SIGNUP_KEY).await.unwrap().unwrap_or_default(); + let user: User = session.get(&SIGNUP_KEY).await.unwrap().unwrap_or_default(); if user == User::default() { return SignupErrorPage("who you?".to_string()).into_response(); } diff --git a/src/main.rs b/src/main.rs index 31c1b9f..f02da4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,9 @@ use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer}; #[macro_use] extern crate justerror; +#[macro_use] +extern crate lazy_static; + mod handlers; use handlers::{get_signup, payment_success, post_signup};