Skip to content

[Backend] username-validator.ts regex allows consecutive hyphens (e.g. user--name) — breaks slug conventions #860

Description

@OlaGreat

Summary

The format regex in isValidUsername():

const validFormat = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/;

allows consecutive hyphens (user--name). Most URL slug conventions and username policies disallow -- because it looks like a typo and can clash with CSS/HTML encoding rules.

Fix

Update the regex to prohibit consecutive hyphens:

const validFormat = /^[a-z0-9]([a-z0-9]|-(?!-))*[a-z0-9]$|^[a-z0-9]$/;

Or add an explicit check:

if (username.includes("--")) return { valid: false, error: "Username cannot contain consecutive hyphens" };

File

backend/src/utils/username-validator.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendExpress / Prisma / API workbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions