Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions backend/routes/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func JoinRoomHandler(c *gin.Context) {

emailStr, ok := email.(string)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid email format"})
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal error: email context value is not a string"})
return
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func GetRoomParticipantsHandler(c *gin.Context) {
// Get user ID from email
emailStr, ok := email.(string)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid email format"})
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal error: email context value is not a string"})
return
}

Expand Down
2 changes: 1 addition & 1 deletion backend/utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func getJWTSecret() string {

// ExtractNameFromEmail extracts the username before '@'
func ExtractNameFromEmail(email string) string {
re := regexp.MustCompile(`^([^@]+)`)
re := regexp.MustCompile(`^([a-zA-Z0-9._%+-]+)@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Move regex compilation to package level and address pattern edge cases.

The regex is compiled on every function call, which is inefficient. Additionally, the current pattern has edge cases that allow technically invalid emails:

Apply this diff to move the regex to package level and tighten the pattern:

+var emailRegex = regexp.MustCompile(`^([a-zA-Z0-9]+(?:[._%-][a-zA-Z0-9]+)*)@[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$`)
+
 // ExtractNameFromEmail extracts the username before '@'
 func ExtractNameFromEmail(email string) string {
-	re := regexp.MustCompile(`^([a-zA-Z0-9._%+-]+)@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
-	match := re.FindStringSubmatch(email)
+	match := emailRegex.FindStringSubmatch(email)
 	if len(match) < 2 {
 		return email
 	}

The improved pattern:

  • Prevents consecutive special characters
  • Prevents leading/trailing special characters
  • Ensures domain labels don't start/end with hyphens

Alternatively, if the current pattern is intentionally lenient for your use case, at minimum move the compilation to package level for performance.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In backend/utils/auth.go around line 40, the email regexp is compiled on each
call and allows edge cases (consecutive or leading/trailing dots in local part
and domain labels starting/ending with hyphens); move the regexp compilation to
a package-level variable and replace the per-call Compile/Match with that
precompiled var, and tighten the pattern to disallow consecutive special
characters and leading/trailing dots in the local part and disallow domain
labels that start or end with hyphens (i.e. use a single compiled pattern that
enforces no consecutive special chars, no leading/trailing special chars, and
domain label rules).

match := re.FindStringSubmatch(email)
if len(match) < 2 {
return email
Expand Down