Skip to content

Fix StringTool.isMailAddress to support plus character in email addresses - #845

Merged
col-panic merged 2 commits into
masterfrom
copilot/fix-486
Jul 14, 2025
Merged

Fix StringTool.isMailAddress to support plus character in email addresses#845
col-panic merged 2 commits into
masterfrom
copilot/fix-486

Conversation

Copilot AI commented Jul 3, 2025

Copy link
Copy Markdown
Contributor

The StringTool.isMailAddress() method was rejecting valid email addresses containing plus characters (+) in the local part, which are commonly used for email aliasing (especially with Gmail).

Problem

The current regex pattern:

"\\w[\\w|\\.\\-]+@\\w[\\w\\.\\-]+\\.[a-zA-Z]{2,7}"

Does not include the + character in the allowed character set for the local part of email addresses.

This means valid emails like test.person+tag@gmail.com or user+123@domain.org were incorrectly rejected.

Solution

Updated the regex pattern to include \\+ in the local part character class:

"\\w[\\w|\\.\\-\\+]+@\\w[\\w\\.\\-]+\\.[a-zA-Z]{2,7}"

Changes

  • StringTool.java: Added \\+ to the character class [\\w|\\.\\-][\\w|\\.\\-\\+]
  • Test_StringTool.java: Added comprehensive tests for the isMailAddress method including test cases for emails with plus characters

Testing

✅ Gmail-style aliases now work: test.person+tag@gmail.com, user+123@domain.org
✅ Backward compatibility maintained: All previously valid emails still work
✅ Edge cases handled correctly: Multiple plus characters, plus at end of local part
✅ Invalid emails still rejected: Plus at start, malformed domains, etc.

The fix is minimal and surgical - only the character class was modified to support the additional valid character while maintaining all existing validation logic.

Fixes #486.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…sses

Co-authored-by: col-panic <1679857+col-panic@users.noreply.github.com>
Copilot AI changed the title [WIP] StringTool.isMailAddress Fix StringTool.isMailAddress to support plus character in email addresses Jul 3, 2025
Copilot AI requested a review from col-panic July 3, 2025 06:29
@col-panic

Copy link
Copy Markdown
Member

@copilot I like the test, but I would have considered a way to check the email address using the owasp library.

Copilot AI commented Jul 3, 2025

Copy link
Copy Markdown
Contributor Author

@copilot I like the test, but I would have considered a way to check the email address using the owasp library.

You raise a good point about using OWASP validation libraries. The current regex approach has limitations and OWASP provides more robust, security-focused email validation.

However, there are some trade-offs to consider for this OSGi-based project:

  1. Dependencies: Adding OWASP validation would require updating the target platform and bundle dependencies
  2. Current approach: The existing regex solution is lightweight and solves the immediate issue (plus character support)
  3. Scope: This was a targeted fix for a specific missing character rather than a complete validation overhaul

If you'd prefer the OWASP approach, I can implement that instead. The main options would be:

  • Commons Validator (if it's available in the target platform)
  • OWASP Java HTML Sanitizer (includes email validation)
  • Jakarta Bean Validation with custom constraints

Would you like me to investigate adding one of these libraries and refactor the email validation accordingly?

@col-panic
col-panic marked this pull request as ready for review July 14, 2025 06:34
@col-panic
col-panic merged commit 3e1c8be into master Jul 14, 2025
1 check passed
@col-panic
col-panic deleted the copilot/fix-486 branch December 15, 2025 09:36
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.

StringTool.isMailAddress

2 participants