Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/core/validations/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use alloy_primitives::Address;
use ed25519_dalek::{Signature, VerifyingKey};
use fancy_regex::Regex;
use prost::Message;
use std::sync::LazyLock;

const MAX_DATA_BYTES: usize = 2048;
const MAX_DATA_BYTES_FOR_10K_CAST: usize = 16_384;
Expand All @@ -23,6 +24,15 @@ const EMBEDS_V1_CUTOFF: u32 = 73612800;
const TWITTER_USERNAME_REGEX: &str = "^[a-z0-9_]{0,15}$";
const FNAME_REGEX: &str = "^[a-z0-9][a-z0-9-]{0,15}$";
const GITHUB_USERNAME_REGEX: &str = "^[a-zA-Z\\d](?:[a-zA-Z\\d]|-(?!-)){0,38}$";

static FNAME_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(FNAME_REGEX).unwrap());
static TWITTER_USERNAME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(TWITTER_USERNAME_REGEX).unwrap());
static GITHUB_USERNAME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(GITHUB_USERNAME_REGEX).unwrap());
static GEO_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^geo:(-?\d{1,2}\.\d{2}),(-?\d{1,3}\.\d{2})$").unwrap());

/** Number of seconds (10 minutes) that is appropriate for clock skew */
const ALLOWED_CLOCK_SKEW_SECONDS: u64 = 10 * 60;

Expand Down Expand Up @@ -300,8 +310,7 @@ pub fn validate_fname(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::FnameExceedsLength(input.clone()));
}

if !Regex::new(FNAME_REGEX)
.unwrap()
if !FNAME_RE
.is_match(&input)
.map_err(|_| ValidationError::InvalidData)?
{
Expand Down Expand Up @@ -336,8 +345,7 @@ pub fn validate_ens_name(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::EnsNameExceedsLength(input.clone()));
}

if !Regex::new(FNAME_REGEX)
.unwrap()
if !FNAME_RE
.is_match(name_parts[0])
.map_err(|_| ValidationError::InvalidData)?
{
Expand Down Expand Up @@ -368,8 +376,7 @@ pub fn validate_base_name(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::EnsNameExceedsLength(input.clone()));
}

if !Regex::new(FNAME_REGEX)
.unwrap()
if !FNAME_RE
.is_match(&name_parts[0])
.map_err(|_| ValidationError::InvalidData)?
{
Expand All @@ -387,8 +394,7 @@ pub fn validate_twitter_username(input: &String) -> Result<(), ValidationError>
return Err(ValidationError::UsernameExceedsLength(input.clone(), 15));
}

if !Regex::new(TWITTER_USERNAME_REGEX)
.unwrap()
if !TWITTER_USERNAME_RE
.is_match(&input)
.map_err(|_| ValidationError::InvalidData)?
{
Expand All @@ -406,8 +412,7 @@ pub fn validate_github_username(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::UsernameExceedsLength(input.clone(), 38));
}

if !Regex::new(GITHUB_USERNAME_REGEX)
.unwrap()
if !GITHUB_USERNAME_RE
.is_match(&input)
.map_err(|_| ValidationError::InvalidData)?
{
Expand Down Expand Up @@ -535,8 +540,7 @@ pub fn validate_user_location(location: &str) -> Result<(), ValidationError> {
return Ok(());
}

let captures = Regex::new(r"^geo:(-?\d{1,2}\.\d{2}),(-?\d{1,3}\.\d{2})$")
.unwrap()
let captures = GEO_RE
.captures(location)
.map_err(|_| ValidationError::InvalidLocationString)?;

Expand Down
Loading