Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make email validator case-insensitive #35790

Merged
merged 6 commits into from
Feb 18, 2025

Conversation

nospame
Copy link
Contributor

@nospame nospame commented Feb 17, 2025

Product Description

Addresses the issue of capital letters in an email address potentially causing a validation error when inviting a web user. Previously, any email address typed with the name portion ending in a capital letter or the domain portion containing any capital letters would be marked invalid, e.g.
image

image

Both of which are now valid.

Technical Summary

SAAS-15628
Updates the regular expression for email in validators.ko to allow capital letters. Also lowercases the email address as part of the web user invitation form's clean_email, which while not required here because of the AdminInvitesUserFormValidator, makes it consistent with other clean_email methods.

Safety Assurance

Safety story

This is a small change to make email addresses validation more accurate and not block the entry of some valid email addresses. I think the risks are quite low.

Automated test coverage

We don't have tests for this monster of a regular expression.

QA Plan

Not planning it.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@dimagimon dimagimon added the Risk: High Change affects files that have been flagged as high risk. label Feb 17, 2025
Copy link
Contributor

@orangejenny orangejenny left a comment

Choose a reason for hiding this comment

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

Tests would be pretty nice given the complexity of the regex.

If you extracted the regex into a constant somewhere, I think it'd be pretty straightforward to set up utility tests, assert_properties.js is a nice example. But I defer to you on whether or not you want to increase the scope of this, this change looks fine.

@@ -16,7 +16,7 @@ hqDefine("hqwebapp/js/bootstrap5/validators.ko", [
ko.validation.rules['emailRFC2822'] = {
validator: function (val) {
if (val === undefined || val.length === 0) {return true;} // do separate validation for required
var re = /(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
var re = /(?:[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-Za-z0-9-]*[A-Za-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, you could run test on value.toLowerCase() and make the regex a smidge simpler. But it's already a monstrosity, so this is just a suggestion.

@nospame
Copy link
Contributor Author

nospame commented Feb 18, 2025

Tests would be pretty nice given the complexity of the regex.

@orangejenny That's totally reasonable. Worked on it a bit this afternoon, setup and basic assertions have been super simple, but having a bit of a time deciphering what this regex does relative to the actual email standard (with the examples given on Wikipedia, at least). It seems like it's a fair bit less restrictive in some senses, and then more strict in others.

Would you think it makes more sense to write tests according to what the expression actually does and retain most of existing behavior, or write them according to what the standard "should be" and change the expression to fit?

@orangejenny
Copy link
Contributor

@nospame I'd write them to document the current behavior and not change the functionality. I'm curious what kinds of differences there are, but that's just idle curiosity talking.

@nospame nospame force-pushed the em/email-validator-case-sensitivity branch from 02df21f to 453f37a Compare February 18, 2025 20:14
@nospame
Copy link
Contributor Author

nospame commented Feb 18, 2025

@orangejenny In general it's just not as explicit as the actual standard, which is almost impossibly comprehensive.

Moved the expression to constants c39df56, Added an email_validator_spec 453f37a, also updated and force-pushed the lint commit e92b8b3 because I'd accidentally reverted the expression to its older version.

Copy link
Contributor

@orangejenny orangejenny left a comment

Choose a reason for hiding this comment

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

this looks great

});

it('should allow specified special characters in the local part', function () {
assert.ok(testEmail('.\x01!#$%&"\'*+/=?^_`{|}[email protected]'));
Copy link
Contributor

Choose a reason for hiding this comment

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

wild

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed! I think this is one of the places where, strictly speaking, some of these characters should only be allowed based on context rather than all the time, e.g. a quote should always be paired. But the existing expression doesn't test for that.

@nospame nospame marked this pull request as ready for review February 18, 2025 21:15
@nospame nospame requested a review from biyeun as a code owner February 18, 2025 21:15
@nospame nospame merged commit c7f3725 into master Feb 18, 2025
14 checks passed
@nospame nospame deleted the em/email-validator-case-sensitivity branch February 18, 2025 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Risk: High Change affects files that have been flagged as high risk.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants