-
Notifications
You must be signed in to change notification settings - Fork 22
hg: add email validation for hg users (bug 1691635) #217
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
base: main
Are you sure you want to change the base?
Changes from all commits
cf24ce6
d47780d
9da7a38
d0c77ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| from __future__ import annotations | ||
| import copy | ||
| from contextlib import contextmanager | ||
| import configparser | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
| from re import search | ||
| import shlex | ||
| import shutil | ||
| import tempfile | ||
|
|
@@ -17,6 +19,7 @@ | |
| import hglib | ||
|
|
||
| from landoapi.hgexports import PatchHelper | ||
| from landoapi.validation import is_valid_email | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -353,10 +356,15 @@ def apply_patch(self, patch_io_buf): | |
| # --landing_system is provided by the set_landing_system hgext. | ||
| date = patch_helper.header("Date") | ||
| user = patch_helper.header("User") | ||
|
|
||
| if not user: | ||
| raise ValueError("Missing `User` header!") | ||
|
|
||
| email = self.extract_email_from_username(user) | ||
| if not is_valid_email(email): | ||
| raise ValueError( | ||
| f"Invalid email ({email}) configured for Mercurial user!" | ||
| ) | ||
|
|
||
| if not date: | ||
| raise ValueError("Missing `Date` header!") | ||
|
|
||
|
|
@@ -519,3 +527,16 @@ def read_checkout_file(self, path: str) -> str: | |
|
|
||
| with checkout_file_path.open() as f: | ||
| return f.read() | ||
|
|
||
| @staticmethod | ||
| def extract_email_from_username(username: str | bytes) -> str: | ||
| """Extracts an email from a Mercurial username, if it exists. | ||
|
|
||
| Not guaranteed to return a valid email, make sure to validate.""" | ||
| email = search(r"<.*?>", str(username)) | ||
| if email: | ||
| return email.group(0).replace("<", "").replace(">", "") | ||
|
Comment on lines
+536
to
+538
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified/cleaned up using grouping. |
||
|
|
||
| # If there is no value between angle brackets in the string, | ||
| # then there is no Mercurial email configured | ||
| return "" | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |||||||||
| import pytest | ||||||||||
| from connexion import ProblemException | ||||||||||
|
|
||||||||||
| from landoapi.validation import revision_id_to_int | ||||||||||
| from landoapi.validation import revision_id_to_int, is_valid_email | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def test_convertion_success(): | ||||||||||
|
|
@@ -20,3 +20,28 @@ def test_convertion_failure_string(id): | |||||||||
| def test_convertion_failure_integer(): | ||||||||||
| with pytest.raises(TypeError): | ||||||||||
| revision_id_to_int(123) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def test_is_valid_email(): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More test cases can be added here (for example |
||||||||||
| invalid_emails = [ | ||||||||||
| "Test User", | ||||||||||
| "test <test>", | ||||||||||
| "test <test@test>", | ||||||||||
| "test@", | ||||||||||
| "-@...", | ||||||||||
| "test@mozilla.", | ||||||||||
| "test@.com", | ||||||||||
| ] | ||||||||||
| valid_emails = [ | ||||||||||
| "test@test.com", | ||||||||||
| "test-email@test.com", | ||||||||||
| "test_email@test.com", | ||||||||||
| "test@test-domain.com", | ||||||||||
| "test.name@test.co.uk", | ||||||||||
| "colombia@test.co", | ||||||||||
| "iceland@test.is", | ||||||||||
| "deutsch@test.de", | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| assert True not in [is_valid_email(value) for value in invalid_emails] | ||||||||||
| assert False not in [is_valid_email(value) for value in valid_emails] | ||||||||||
|
Comment on lines
+46
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: more of a personal preference than anything else, but could be a little more human readable.
Suggested change
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.