diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-10-19 01:51:30 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-21 00:49:05 -0400 |
| commit | 379e97c2cb145bc3a495aa14746273d83b508214 (patch) | |
| tree | 218bbe2572af9dd4b165ff05495d084dc0bd8905 /src/login | |
| parent | 98af8ff80da919a1126ba7c6afa65e6654b5ecde (diff) | |
Unicode normalization on input.
This normalizes the following values:
* login names
* passwords
* channel names
* message bodies, because why not
The goal here is to have a canonical representation of these values, so that, for example, the service does not inadvertently host two channels whose names are semantically identical but differ in the specifics of how diacritics are encoded, or two users whose names are identical.
Normalization is done on input from the wire, using Serde hooks, and when reading from the database. The `crate::nfc::String` type implements these normalizations (as well as normalizing whenever converted from a `std::string::String` generally).
This change does not cover:
* Trying to cope with passwords that were created as non-normalized strings, which are now non-verifiable as all the paths to verify passwords normalize the input.
* Trying to ensure that non-normalized data in the database compares reasonably to normalized data. Fortunately, we don't _do_ very many string comparisons (I think only login names), so this isn't a huge deal at this stage. Login names will probably have to Get Fixed later on, when we figure out how to handle case folding for login name verification.
Diffstat (limited to 'src/login')
| -rw-r--r-- | src/login/app.rs | 4 | ||||
| -rw-r--r-- | src/login/mod.rs | 4 | ||||
| -rw-r--r-- | src/login/name.rs | 28 | ||||
| -rw-r--r-- | src/login/password.rs | 7 | ||||
| -rw-r--r-- | src/login/repo.rs | 10 | ||||
| -rw-r--r-- | src/login/routes/login/post.rs | 4 | ||||
| -rw-r--r-- | src/login/snapshot.rs | 4 |
7 files changed, 46 insertions, 15 deletions
diff --git a/src/login/app.rs b/src/login/app.rs index b6f7e1c..ebc1c00 100644 --- a/src/login/app.rs +++ b/src/login/app.rs @@ -1,6 +1,6 @@ use sqlx::sqlite::SqlitePool; -use super::{repo::Provider as _, Login, Password}; +use super::{repo::Provider as _, Login, Name, Password}; use crate::{ clock::DateTime, event::{repo::Provider as _, Broadcaster, Event}, @@ -18,7 +18,7 @@ impl<'a> Logins<'a> { pub async fn create( &self, - name: &str, + name: &Name, password: &Password, created_at: &DateTime, ) -> Result<Login, CreateError> { diff --git a/src/login/mod.rs b/src/login/mod.rs index 98cc3d7..71d5bfc 100644 --- a/src/login/mod.rs +++ b/src/login/mod.rs @@ -4,11 +4,13 @@ pub mod event; pub mod extract; mod history; mod id; +mod name; pub mod password; pub mod repo; mod routes; mod snapshot; pub use self::{ - event::Event, history::History, id::Id, password::Password, routes::router, snapshot::Login, + event::Event, history::History, id::Id, name::Name, password::Password, routes::router, + snapshot::Login, }; diff --git a/src/login/name.rs b/src/login/name.rs new file mode 100644 index 0000000..d882ff9 --- /dev/null +++ b/src/login/name.rs @@ -0,0 +1,28 @@ +use std::fmt; + +use crate::nfc; + +#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize, sqlx::Type)] +#[serde(transparent)] +#[sqlx(transparent)] +pub struct Name(nfc::String); + +impl fmt::Display for Name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Self(name) = self; + name.fmt(f) + } +} + +impl From<String> for Name { + fn from(name: String) -> Self { + Self(name.into()) + } +} + +impl From<Name> for String { + fn from(name: Name) -> Self { + let Name(name) = name; + name.into() + } +} diff --git a/src/login/password.rs b/src/login/password.rs index 14fd981..f9ecf37 100644 --- a/src/login/password.rs +++ b/src/login/password.rs @@ -4,6 +4,8 @@ use argon2::Argon2; use password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString}; use rand_core::OsRng; +use crate::nfc; + #[derive(sqlx::Type)] #[sqlx(transparent)] pub struct StoredHash(String); @@ -31,7 +33,7 @@ impl fmt::Debug for StoredHash { #[derive(serde::Deserialize)] #[serde(transparent)] -pub struct Password(String); +pub struct Password(nfc::String); impl Password { pub fn hash(&self) -> Result<StoredHash, password_hash::Error> { @@ -56,9 +58,8 @@ impl fmt::Debug for Password { } } -#[cfg(test)] impl From<String> for Password { fn from(password: String) -> Self { - Self(password) + Password(password.into()) } } diff --git a/src/login/repo.rs b/src/login/repo.rs index 7d0fcb1..204329f 100644 --- a/src/login/repo.rs +++ b/src/login/repo.rs @@ -3,7 +3,7 @@ use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction}; use crate::{ clock::DateTime, event::{Instant, ResumePoint, Sequence}, - login::{password::StoredHash, History, Id, Login}, + login::{password::StoredHash, History, Id, Login, Name}, }; pub trait Provider { @@ -21,7 +21,7 @@ pub struct Logins<'t>(&'t mut SqliteConnection); impl<'c> Logins<'c> { pub async fn create( &mut self, - name: &str, + name: &Name, password_hash: &StoredHash, created: &Instant, ) -> Result<History, sqlx::Error> { @@ -34,7 +34,7 @@ impl<'c> Logins<'c> { values ($1, $2, $3, $4, $5) returning id as "id: Id", - name, + name as "name: Name", created_sequence as "created_sequence: Sequence", created_at as "created_at: DateTime" "#, @@ -62,7 +62,7 @@ impl<'c> Logins<'c> { r#" select id as "id: Id", - name, + name as "name: Name", created_sequence as "created_sequence: Sequence", created_at as "created_at: DateTime" from login @@ -88,7 +88,7 @@ impl<'c> Logins<'c> { r#" select id as "id: Id", - name, + name as "name: Name", created_sequence as "created_sequence: Sequence", created_at as "created_at: DateTime" from login diff --git a/src/login/routes/login/post.rs b/src/login/routes/login/post.rs index 67eaa6d..7a685e2 100644 --- a/src/login/routes/login/post.rs +++ b/src/login/routes/login/post.rs @@ -8,7 +8,7 @@ use crate::{ app::App, clock::RequestedAt, error::Internal, - login::{Login, Password}, + login::{Login, Name, Password}, token::{app, extract::IdentityToken}, }; @@ -29,7 +29,7 @@ pub async fn handler( #[derive(serde::Deserialize)] pub struct Request { - pub name: String, + pub name: Name, pub password: Password, } diff --git a/src/login/snapshot.rs b/src/login/snapshot.rs index 1a92f5c..85800e4 100644 --- a/src/login/snapshot.rs +++ b/src/login/snapshot.rs @@ -1,6 +1,6 @@ use super::{ event::{Created, Event}, - Id, + Id, Name, }; // This also implements FromRequestParts (see `./extract.rs`). As a result, it @@ -10,7 +10,7 @@ use super::{ #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] pub struct Login { pub id: Id, - pub name: String, + pub name: Name, // The omission of the hashed password is deliberate, to minimize the // chance that it ends up tangled up in debug output or in some other chunk // of logic elsewhere. |
