From 681726f231b5ca17b9f92356127687132233a6be Mon Sep 17 00:00:00 2001 From: Ryuzo Yamamoto Date: Wed, 29 Nov 2023 09:20:57 +0900 Subject: [PATCH] Fix email validation (#84) This pull request fixes email validation (`celext.validateEmail`). I found that some unexpected strings like `foo@example.com ` (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. --- celext/lib.go | 2 +- celext/lib_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/celext/lib.go b/celext/lib.go index eb53e6b..444f09b 100644 --- a/celext/lib.go +++ b/celext/lib.go @@ -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 } diff --git a/celext/lib_test.go b/celext/lib_test.go index ec369c1..bc1e87e 100644 --- a/celext/lib_test.go +++ b/celext/lib_test.go @@ -177,6 +177,22 @@ func TestCELLib(t *testing.T) { "'1.2.3.0/24'.isIpPrefix(6)", false, }, + { + "'foo@example.com'.isEmail()", + true, + }, + { + "''.isEmail()", + false, + }, + { + "' foo@example.com'.isEmail()", + false, + }, + { + "'foo@example.com '.isEmail()", + false, + }, } for _, tc := range tests {