Skip to content

Commit

Permalink
Add error handling to authentik.lib.utils.email.mask_email
Browse files Browse the repository at this point in the history
  • Loading branch information
melizeche committed Feb 15, 2025
1 parent 9ca9b5f commit 5e35976
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions authentik/lib/utils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,46 @@ def mask_email(email: str | None) -> str | None:
if not email:
return None

local, domain = email.split("@")
domain_parts = domain.split(".")
limit = 2

# Mask local part (keep first char)
if len(local) <= limit:
masked_local = "*" * len(local)
else:
masked_local = local[0] + "*" * (len(local) - 1)

# Mask each domain part except the last one (TLD)
masked_domain_parts = []
for _i, part in enumerate(domain_parts[:-1]): # Process all parts except TLD
if len(part) <= limit:
masked_part = "*" * len(part)
else:
masked_part = part[0] + "*" * (len(part) - 1)
masked_domain_parts.append(masked_part)

# Add TLD unchanged
masked_domain_parts.append(domain_parts[-1])
try:
# Basic email format validation
if email.count("@") != 1:
raise ValueError("Invalid email format: Must contain exactly one '@' symbol")

Check warning on line 22 in authentik/lib/utils/email.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/utils/email.py#L22

Added line #L22 was not covered by tests

local, domain = email.split("@")
if not local or not domain:
raise ValueError("Invalid email format: Local and domain parts cannot be empty")

Check warning on line 26 in authentik/lib/utils/email.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/utils/email.py#L26

Added line #L26 was not covered by tests

domain_parts = domain.split(".")
if len(domain_parts) < 2: # noqa: PLR2004
raise ValueError("Invalid email format: Domain must contain at least one dot")

Check warning on line 30 in authentik/lib/utils/email.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/utils/email.py#L30

Added line #L30 was not covered by tests

return f"{masked_local}@{'.'.join(masked_domain_parts)}"
limit = 2

# Mask local part (keep first char)
if len(local) <= limit:
masked_local = "*" * len(local)
else:
masked_local = local[0] + "*" * (len(local) - 1)

# Mask each domain part except the last one (TLD)
masked_domain_parts = []
for _i, part in enumerate(domain_parts[:-1]): # Process all parts except TLD
if not part: # Check for empty parts (consecutive dots)
raise ValueError("Invalid email format: Domain parts cannot be empty")

Check warning on line 44 in authentik/lib/utils/email.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/utils/email.py#L44

Added line #L44 was not covered by tests
if len(part) <= limit:
masked_part = "*" * len(part)
else:
masked_part = part[0] + "*" * (len(part) - 1)
masked_domain_parts.append(masked_part)

# Add TLD unchanged
if not domain_parts[-1]: # Check if TLD is empty
raise ValueError("Invalid email format: TLD cannot be empty")

Check warning on line 53 in authentik/lib/utils/email.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/utils/email.py#L53

Added line #L53 was not covered by tests
masked_domain_parts.append(domain_parts[-1])

return f"{masked_local}@{'.'.join(masked_domain_parts)}"
except ValueError:
raise
except Exception as e:
raise ValueError(f"Invalid email format: {str(e)}") from e

Check warning on line 60 in authentik/lib/utils/email.py

View check run for this annotation

Codecov / codecov/patch

authentik/lib/utils/email.py#L57-L60

Added lines #L57 - L60 were not covered by tests

0 comments on commit 5e35976

Please sign in to comment.