Skip to content

fix(api): add start and end anchors to username validation regex - #1424

Open
swarnendusahu10-dotcom wants to merge 1 commit into
RocketChat:developfrom
swarnendusahu10-dotcom:fix/issue-1201-username-validation-regex-anchors
Open

swarnendusahu10-dotcom wants to merge 1 commit into
RocketChat:developfrom
swarnendusahu10-dotcom:fix/issue-1201-username-validation-regex-anchors

Conversation

@swarnendusahu10-dotcom

Copy link
Copy Markdown
Contributor

Description

Fixes #1201

In packages/api/src/EmbeddedChatApi.ts, the regex used to validate usernames in updateUsername was missing an end anchor:
/^[0-9a-zA-Z-_.]+/

Because it only anchored the start (^), any string starting with valid characters followed by invalid characters (such as user!@#$%^&* or trailing spaces) was considered valid. As a result, the function attempted to send the invalid payload to the Rocket.Chat server and threw unhandled API errors instead of falling back to updateUserNameThroughSuggestion.

Changes

  • Anchored the validation regex to /^[0-9a-zA-Z-_.]+$/ to enforce full-string validation.
  • When an invalid username is provided, the API now cleanly falls back to updateUserNameThroughSuggestion as originally intended.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Verified with 27 test vectors covering:
    • Valid usernames (john.doe, user_123, dev-team) -> passed validation.
    • Suffix injection (user!@#, valid.name?query=1) -> rejected, safely routed to suggestions.
    • Whitespace & control characters (user name, user\nname) -> rejected.
  • Rollup build compiled successfully (packages/api/dist/index.js).
  • Prettier formatting check passed cleanly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: Username validation regex missing anchors

1 participant