Skip to content

Conversation

lunika
Copy link
Member

@lunika lunika commented Sep 8, 2025

Purpose

Only the input data min length was checked. We also have to check the max length because the levenshtein does not accept more than 254 characters, and the email field has a max length of 254.

Proposal

  • 🐛(backend) validate user search input data

Fixes #1348

@lunika lunika requested review from AntoLC and Copilot September 8, 2025 14:19
@lunika lunika self-assigned this Sep 8, 2025
@lunika lunika added the bug Something isn't working label Sep 8, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds validation for user search input data to ensure queries are between 5 and 254 characters. The validation prevents issues with the Levenshtein distance algorithm which doesn't accept more than 254 characters, and aligns with the email field's max length of 254.

  • Implemented a UserSearchFilter to validate query parameter length constraints
  • Updated user search endpoint to return 400 status with validation errors for invalid queries
  • Added comprehensive test coverage for both short and long query validation

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/backend/core/api/filters.py Added UserSearchFilter class with min/max length validation for query parameter
src/backend/core/api/viewsets.py Integrated the new filter to validate search queries and return proper error responses
src/backend/core/tests/test_api_users.py Updated existing tests and added new test for long query validation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


def test_api_users_list_query_long_queries():
"""
Queries longer than 255 characters should return an empty result set.
Copy link
Preview

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states queries longer than 255 characters should return an empty result set, but the implementation actually returns a 400 error with validation message. The comment should be updated to reflect the actual behavior: 'Queries longer than 254 characters should return a validation error.'

Suggested change
Queries longer than 255 characters should return an empty result set.
Queries longer than 254 characters should return a validation error.

Copilot uses AI. Check for mistakes.

factories.UserFactory(email="[email protected]")
factories.UserFactory(email="[email protected]")

query = "a" * 244
Copy link
Preview

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The test creates a 244-character string and appends '@example.com' (12 characters) for a total of 256 characters, but this logic is not immediately clear. Consider using a more explicit approach like query = 'a' * (254 + 1 + 1) with a comment explaining the calculation (254 max + 1 to exceed + 1 for '@') or calculate the exact length needed.

Suggested change
query = "a" * 244
# The maximum allowed query length is 254 characters.
# '@example.com' is 12 characters, so we construct a string that exceeds the limit:
# (254 max) + 1 to exceed + 1 for '@' = 256 total length.
query = "a" * (254 + 1 + 1 - len("@example.com"))

Copilot uses AI. Check for mistakes.

Copy link

github-actions bot commented Sep 8, 2025

Size Change: +4 B (0%)

Total Size: 3.65 MB

Filename Size Change
apps/impress/out/_next/static/ef7012fe/_buildManifest.js 0 B -867 B (removed) 🏆
apps/impress/out/_next/static/2803f45a/_buildManifest.js 867 B +867 B (new file) 🆕

compressed-size-action

@AntoLC AntoLC force-pushed the fix/levenshtein-error branch from 6563e1d to 356b84b Compare September 8, 2025 16:12
lunika and others added 3 commits September 10, 2025 16:11
Only the input data min length was checked. We also have to check the
mex length because the levenshtein dos not accept more than 254
characters and the email field has a max length of 254
When we create a new user in the demo environment,
the email address will now follow the format
[email protected] instead of [email protected].
"user" was only 4 characters long, it created failing
tests in the e2e suite.
254 characters should be sufficient for most
of our usecases.
Limit input search to 254 characters to prevent
errors caused by overly long email addresses.
@lunika lunika force-pushed the fix/levenshtein-error branch from 356b84b to 4f2e07f Compare September 10, 2025 14:11
@lunika lunika merged commit 4f2e07f into main Sep 10, 2025
20 of 22 checks passed
@lunika lunika deleted the fix/levenshtein-error branch September 10, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataError: levenshtein argument exceeds maximum length of 255 characters
2 participants