Skip to content

Commit

Permalink
DEV-1247: Case Sensitive Username Fix (#45)
Browse files Browse the repository at this point in the history
* prevent case sensitive username collisions

* clean up read me

* fix lint

* remove unique

* fix docker
  • Loading branch information
andy-t-wang authored Dec 31, 2024
1 parent df171e5 commit 624bee1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ This is our open source implementation of ENS compatible Usernames
```
cp .env.example .env
docker compose up --detach
sqlx migrate run
cargo run
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Add migration script here
CREATE INDEX names_username_lower_idx ON names (LOWER(username));

CREATE INDEX old_names_username_lower_idx ON old_names (LOWER(old_username));
16 changes: 8 additions & 8 deletions src/routes/api/v1/register_username.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ pub async fn register_username(
.map_err(|e| ErrorResponse::validation_error(e.to_string()))?;

let uniqueness_check = sqlx::query!(
"SELECT
EXISTS(SELECT 1 FROM names WHERE nullifier_hash = $2) AS world_id,
EXISTS(SELECT 1 FROM names WHERE username = $1 UNION SELECT 1 FROM old_names where old_username = $1) AS username",
&payload.username,
&payload.nullifier_hash
)
.fetch_one(&db.read_write)
.await?;
"SELECT
EXISTS(SELECT 1 FROM names WHERE nullifier_hash = $2) AS world_id,
EXISTS(SELECT 1 FROM names WHERE LOWER(username) = LOWER($1) UNION SELECT 1 FROM old_names where LOWER(old_username) = LOWER($1)) AS username",
&payload.username,
&payload.nullifier_hash
)
.fetch_one(&db.read_write)
.await?;

if uniqueness_check.username.unwrap_or_default() {
return Err(ErrorResponse::validation_error(
Expand Down
8 changes: 6 additions & 2 deletions src/routes/api/v1/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
verify,
};

#[allow(clippy::too_many_lines)] // TODO: refactor
#[allow(dependency_on_unit_never_type_fallback)]
pub async fn rename(
Extension(config): ConfigExt,
Expand Down Expand Up @@ -81,8 +82,11 @@ pub async fn rename(

let uniqueness_check = sqlx::query!(
"SELECT
EXISTS(SELECT 1 FROM old_names where new_username = $1) AS has_old_username,
EXISTS(SELECT 1 FROM names WHERE username = $2 UNION SELECT 1 FROM old_names where old_username = $2 AND new_username != $1) AS username
EXISTS(SELECT 1 FROM old_names where LOWER(new_username) = LOWER($1)) AS has_old_username,
EXISTS(SELECT 1 FROM names WHERE LOWER(username) = LOWER($2)
UNION
SELECT 1 FROM old_names where LOWER(old_username) = LOWER($2) AND LOWER(new_username) != LOWER($1)
) AS username
",
&payload.old_username,
&payload.new_username,
Expand Down

0 comments on commit 624bee1

Please sign in to comment.