Skip to content

Commit

Permalink
Fix email validation (#84)
Browse files Browse the repository at this point in the history
This pull request fixes email validation (`celext.validateEmail`).

I found that some unexpected strings like `[email protected] ` (with
trailing spaces) got allowed by the email validation, those should not
be valid.

`mail.ParseAddress` can parse the string without any errors and the
result's address (`a.Address` ) is valid, but it's already different
from the original string, so I think checking if `a.Address` and the
original `addr` is same is necessary.
  • Loading branch information
dragon3 authored Nov 29, 2023
1 parent 2ffb456 commit 681726f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion celext/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (l lib) uniqueBytes(list traits.Lister) ref.Val {

func (l lib) validateEmail(addr string) bool {
a, err := mail.ParseAddress(addr)
if err != nil || strings.ContainsRune(addr, '<') {
if err != nil || strings.ContainsRune(addr, '<') || a.Address != addr {
return false
}

Expand Down
16 changes: 16 additions & 0 deletions celext/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ func TestCELLib(t *testing.T) {
"'1.2.3.0/24'.isIpPrefix(6)",
false,
},
{
"'[email protected]'.isEmail()",
true,
},
{
"'<[email protected]>'.isEmail()",
false,
},
{
"' [email protected]'.isEmail()",
false,
},
{
"'[email protected] '.isEmail()",
false,
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 681726f

Please sign in to comment.