Skip to content

Commit

Permalink
fix: api crashes (#2338)
Browse files Browse the repository at this point in the history
* fix: api crashes

* Update app/api/views/alias.py

Co-authored-by: Adrià Casajús <acasajus@users.noreply.github.com>

---------

Co-authored-by: Adrià Casajús <acasajus@users.noreply.github.com>
  • Loading branch information
cquintana92 and acasajus authored Dec 2, 2024
1 parent 3b0b5f4 commit b52340c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion app/api/views/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ def update_alias(alias_id):
changed = True

if "mailbox_ids" in data:
mailbox_ids = [int(m_id) for m_id in data.get("mailbox_ids")]
try:
mailbox_ids = [int(m_id) for m_id in data.get("mailbox_ids")]
except ValueError:
return jsonify(error="Invalid mailbox_id"), 400
err = set_mailboxes_for_alias(
user_id=user.id, alias=alias, mailbox_ids=mailbox_ids
)
Expand Down
24 changes: 18 additions & 6 deletions app/api/views/new_custom_alias.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from email_validator import EmailNotValidError
from flask import g
from flask import jsonify, request

Expand Down Expand Up @@ -93,12 +94,15 @@ def new_custom_alias_v2():
400,
)

alias = Alias.create(
user_id=user.id,
email=full_alias,
mailbox_id=user.default_mailbox_id,
note=note,
)
try:
alias = Alias.create(
user_id=user.id,
email=full_alias,
mailbox_id=user.default_mailbox_id,
note=note,
)
except EmailNotValidError:
return jsonify(error="Email is not valid"), 400

Session.commit()

Expand Down Expand Up @@ -154,8 +158,16 @@ def new_custom_alias_v3():
return jsonify(error="request body does not follow the required format"), 400

alias_prefix_data = data.get("alias_prefix", "") or ""

if not isinstance(alias_prefix_data, str):
return jsonify(error="request body does not follow the required format"), 400

alias_prefix = alias_prefix_data.strip().lower().replace(" ", "")
signed_suffix = data.get("signed_suffix", "") or ""

if not isinstance(signed_suffix, str):
return jsonify(error="request body does not follow the required format"), 400

signed_suffix = signed_suffix.strip()

mailbox_ids = data.get("mailbox_ids")
Expand Down
2 changes: 1 addition & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ def pgp_enabled(self) -> bool:
return False

@staticmethod
def get_custom_domain(alias_address) -> Optional["CustomDomain"]:
def get_custom_domain(alias_address: str) -> Optional["CustomDomain"]:
alias_domain = validate_email(
alias_address, check_deliverability=False, allow_smtputf8=False
).domain
Expand Down

0 comments on commit b52340c

Please sign in to comment.