Return error for primary keys with a length greater than 512 bytes

This commit is contained in:
F. Levi 2024-09-09 15:27:47 +03:00
parent db0cf3b2ed
commit dcb61f8b3a
2 changed files with 4 additions and 1 deletions

View File

@ -151,6 +151,7 @@ fn starts_with(selector: &str, key: &str) -> bool {
fn validate_document_id(document_id: &str) -> Option<&str> { fn validate_document_id(document_id: &str) -> Option<&str> {
if !document_id.is_empty() if !document_id.is_empty()
&& document_id.len() <= 512
&& document_id.chars().all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_')) && document_id.chars().all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_'))
{ {
Some(document_id) Some(document_id)
@ -166,6 +167,7 @@ pub fn validate_document_id_value(document_id: Value) -> StdResult<String, UserE
Some(s) => Ok(s.to_string()), Some(s) => Ok(s.to_string()),
None => Err(UserError::InvalidDocumentId { document_id: Value::String(string) }), None => Err(UserError::InvalidDocumentId { document_id: Value::String(string) }),
}, },
// a `u64` or `i64` cannot be more than 512 bytes once converted to a string
Value::Number(number) if !number.is_f64() => Ok(number.to_string()), Value::Number(number) if !number.is_f64() => Ok(number.to_string()),
content => Err(UserError::InvalidDocumentId { document_id: content }), content => Err(UserError::InvalidDocumentId { document_id: content }),
} }

View File

@ -106,7 +106,8 @@ pub enum UserError {
#[error( #[error(
"Document identifier `{}` is invalid. \ "Document identifier `{}` is invalid. \
A document identifier can be of type integer or string, \ A document identifier can be of type integer or string, \
only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and underscores (_).", .document_id.to_string() only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and underscores (_), \
and can not be more than 512 bytes.", .document_id.to_string()
)] )]
InvalidDocumentId { document_id: Value }, InvalidDocumentId { document_id: Value },
#[error("Invalid facet distribution, {}", format_invalid_filter_distribution(.invalid_facets_name, .valid_facets_name))] #[error("Invalid facet distribution, {}", format_invalid_filter_distribution(.invalid_facets_name, .valid_facets_name))]