diff --git a/MatrixSDK/Utils/MXTools.m b/MatrixSDK/Utils/MXTools.m index e17e7134c..22435cd7d 100644 --- a/MatrixSDK/Utils/MXTools.m +++ b/MatrixSDK/Utils/MXTools.m @@ -30,7 +30,7 @@ #warning File has not been annotated with nullability, see MX_ASSUME_MISSING_NULLABILITY_BEGIN #pragma mark - Constant definition -NSString *const kMXToolsRegexStringForEmailAddress = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}"; +NSString *const kMXToolsRegexStringForEmailAddress = @"^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; // The HS domain part in Matrix identifiers #define MATRIX_HOMESERVER_DOMAIN_REGEX @"[A-Z0-9]+((\\.|\\-)[A-Z0-9]+){0,}(:[0-9]{2,5})?" diff --git a/MatrixSDKTests/MXToolsUnitTests.m b/MatrixSDKTests/MXToolsUnitTests.m index 4e15a4e10..efcf636df 100644 --- a/MatrixSDKTests/MXToolsUnitTests.m +++ b/MatrixSDKTests/MXToolsUnitTests.m @@ -68,6 +68,58 @@ - (void)testMatrixIdentifiers XCTAssertTrue([MXTools isMatrixGroupIdentifier:@"+matrix:matrix.org"]); } +- (void)testEmailAddresses +{ + XCTAssertTrue([MXTools isEmailAddress:@"alice@matrix.org"]); + XCTAssertTrue([MXTools isEmailAddress:@"alice@matrix"]); + XCTAssertTrue([MXTools isEmailAddress:@"al-i_ce@matrix"]); + XCTAssertTrue([MXTools isEmailAddress:@"al+ice@matrix.org"]); + XCTAssertTrue([MXTools isEmailAddress:@"al=ice@matrix.org"]); + XCTAssertTrue([MXTools isEmailAddress:@"*@example.net"]); + XCTAssertTrue([MXTools isEmailAddress:@"fred&barny@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"---@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"foo-bar@example.net"]); + XCTAssertTrue([MXTools isEmailAddress:@"mailbox.sub1.sub2@this-domain"]); + XCTAssertTrue([MXTools isEmailAddress:@"prettyandsimple@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"very.common@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"disposable.style.email.with+symbol@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"other.email-with-dash@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"x@example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"example-indeed@strange-example.com"]); + XCTAssertTrue([MXTools isEmailAddress:@"admin@mailserver1"]); + XCTAssertTrue([MXTools isEmailAddress:@"#!$%&'*+-/=?^_`{}|~@example.org"]); + XCTAssertTrue([MXTools isEmailAddress:@"example@localhost"]); + XCTAssertTrue([MXTools isEmailAddress:@"example@s.solutions"]); + XCTAssertTrue([MXTools isEmailAddress:@"user@localserver"]); + XCTAssertTrue([MXTools isEmailAddress:@"user@tt"]); + XCTAssertTrue([MXTools isEmailAddress:@"xn--80ahgue5b@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq"]); + XCTAssertTrue([MXTools isEmailAddress:@"nothing@xn--fken-gra.no"]); + + XCTAssertFalse([MXTools isEmailAddress:@"alice.matrix.org"]); + XCTAssertFalse([MXTools isEmailAddress:@"al ice@matrix.org"]); + XCTAssertFalse([MXTools isEmailAddress:@"al(ice@matrix.org"]); + XCTAssertFalse([MXTools isEmailAddress:@"alice@"]); + XCTAssertFalse([MXTools isEmailAddress:@"al\nice@matrix.org"]); + XCTAssertFalse([MXTools isEmailAddress:@"al@ice@matrix.org"]); + XCTAssertFalse([MXTools isEmailAddress:@"al@ice@.matrix.org"]); + XCTAssertFalse([MXTools isEmailAddress:@"Just a string"]); + XCTAssertFalse([MXTools isEmailAddress:@"string"]); + XCTAssertFalse([MXTools isEmailAddress:@"me@"]); + XCTAssertFalse([MXTools isEmailAddress:@"@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"me.@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@".me@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"me@example..com"]); + XCTAssertFalse([MXTools isEmailAddress:@"me\\@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"Abc.example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"A@b@c@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"a\"b(c)d,e:f;gi[j\\k]l@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"just\"not\"right@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"this is\"not\\allowed@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"this\\ still\\\"not\\\\allowed@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"john..doe@example.com"]); + XCTAssertFalse([MXTools isEmailAddress:@"john.doe@example..com"]); +} + #pragma mark - Strings encoding diff --git a/changelog.d/pr-1826.bugfix b/changelog.d/pr-1826.bugfix new file mode 100644 index 000000000..f73b7d941 --- /dev/null +++ b/changelog.d/pr-1826.bugfix @@ -0,0 +1 @@ +Update regex for email address to be aligned email format in RFC 5322 \ No newline at end of file