2020-01-29 17:57:11 +01:00
|
|
|
from flask import render_template, redirect, url_for, flash, request
|
2019-07-06 23:25:52 +02:00
|
|
|
from flask_login import login_required, current_user
|
2020-05-02 12:15:03 +02:00
|
|
|
from itsdangerous import TimestampSigner, SignatureExpired
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-05-02 12:15:03 +02:00
|
|
|
from app.config import (
|
|
|
|
DISABLE_ALIAS_SUFFIX,
|
|
|
|
ALIAS_DOMAINS,
|
|
|
|
CUSTOM_ALIAS_SECRET,
|
|
|
|
)
|
2019-07-06 23:25:52 +02:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2020-05-07 20:48:11 +02:00
|
|
|
from app.email_utils import email_belongs_to_alias_domains
|
2019-07-06 23:25:52 +02:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-05-23 11:51:35 +02:00
|
|
|
from app.models import (
|
|
|
|
Alias,
|
|
|
|
CustomDomain,
|
|
|
|
DeletedAlias,
|
|
|
|
Mailbox,
|
|
|
|
User,
|
|
|
|
AliasMailbox,
|
|
|
|
DomainDeletedAlias,
|
|
|
|
)
|
2019-12-31 17:11:42 +01:00
|
|
|
from app.utils import convert_to_id, random_word, word_exist
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-05-02 12:15:03 +02:00
|
|
|
signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
|
|
|
|
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-05-02 12:34:11 +02:00
|
|
|
def available_suffixes(user: User) -> [bool, str, str]:
|
|
|
|
"""Return (is_custom_domain, alias-suffix, time-signed alias-suffix)"""
|
|
|
|
user_custom_domains = [cd.domain for cd in user.verified_custom_domains()]
|
|
|
|
|
|
|
|
# List of (is_custom_domain, alias-suffix, time-signed alias-suffix)
|
|
|
|
suffixes = []
|
|
|
|
|
|
|
|
# put custom domain first
|
|
|
|
for alias_domain in user_custom_domains:
|
|
|
|
suffix = "@" + alias_domain
|
|
|
|
suffixes.append((True, suffix, signer.sign(suffix).decode()))
|
|
|
|
|
|
|
|
# then default domain
|
|
|
|
for domain in ALIAS_DOMAINS:
|
|
|
|
suffix = ("" if DISABLE_ALIAS_SUFFIX else "." + random_word()) + "@" + domain
|
|
|
|
suffixes.append((False, suffix, signer.sign(suffix).decode()))
|
|
|
|
|
|
|
|
return suffixes
|
|
|
|
|
|
|
|
|
2019-07-06 23:25:52 +02:00
|
|
|
@dashboard_bp.route("/custom_alias", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def custom_alias():
|
2020-01-22 00:03:25 +01:00
|
|
|
# check if user has not exceeded the alias quota
|
2019-12-22 17:27:55 +01:00
|
|
|
if not current_user.can_create_new_alias():
|
2020-07-15 19:14:37 +02:00
|
|
|
LOG.warning("user %s tries to create custom alias", current_user)
|
2020-01-30 04:52:56 +01:00
|
|
|
flash(
|
|
|
|
"You have reached free plan limit, please upgrade to create new aliases",
|
|
|
|
"warning",
|
|
|
|
)
|
2019-07-06 23:25:52 +02:00
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-01-22 00:03:25 +01:00
|
|
|
user_custom_domains = [cd.domain for cd in current_user.verified_custom_domains()]
|
2020-05-02 12:15:03 +02:00
|
|
|
# List of (is_custom_domain, alias-suffix, time-signed alias-suffix)
|
2020-05-02 12:34:11 +02:00
|
|
|
suffixes = available_suffixes(current_user)
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-05-03 16:50:39 +02:00
|
|
|
mailboxes = current_user.mailboxes()
|
2020-02-10 17:19:42 +01:00
|
|
|
|
2019-12-02 01:34:54 +01:00
|
|
|
if request.method == "POST":
|
2020-05-20 18:12:14 +02:00
|
|
|
alias_prefix = request.form.get("prefix").strip().lower()
|
2020-05-02 12:15:03 +02:00
|
|
|
signed_suffix = request.form.get("suffix")
|
2020-05-03 16:50:39 +02:00
|
|
|
mailbox_ids = request.form.getlist("mailboxes")
|
2020-02-05 11:36:06 +01:00
|
|
|
alias_note = request.form.get("note")
|
2020-01-22 00:03:25 +01:00
|
|
|
|
2020-02-10 17:19:42 +01:00
|
|
|
# check if mailbox is not tempered with
|
2020-05-03 16:50:39 +02:00
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if (
|
|
|
|
not mailbox
|
|
|
|
or mailbox.user_id != current_user.id
|
|
|
|
or not mailbox.verified
|
|
|
|
):
|
2020-02-10 17:19:42 +01:00
|
|
|
flash("Something went wrong, please retry", "warning")
|
|
|
|
return redirect(url_for("dashboard.custom_alias"))
|
2020-05-03 16:50:39 +02:00
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
flash("At least one mailbox must be selected", "error")
|
|
|
|
return redirect(url_for("dashboard.custom_alias"))
|
2020-02-10 17:19:42 +01:00
|
|
|
|
2020-05-06 10:10:47 +02:00
|
|
|
# hypothesis: user will click on the button in the 600 secs
|
2020-05-02 12:15:03 +02:00
|
|
|
try:
|
2020-05-06 10:10:47 +02:00
|
|
|
alias_suffix = signer.unsign(signed_suffix, max_age=600).decode()
|
2020-05-02 12:15:03 +02:00
|
|
|
except SignatureExpired:
|
2020-05-21 20:05:54 +02:00
|
|
|
LOG.warning("Alias creation time expired for %s", current_user)
|
2020-05-02 12:15:03 +02:00
|
|
|
flash("Alias creation time is expired, please retry", "warning")
|
|
|
|
return redirect(url_for("dashboard.custom_alias"))
|
|
|
|
except Exception:
|
2020-08-24 19:58:21 +02:00
|
|
|
LOG.warning("Alias suffix is tampered, user %s", current_user)
|
2020-05-02 12:15:03 +02:00
|
|
|
flash("Unknown error, refresh the page", "error")
|
|
|
|
return redirect(url_for("dashboard.custom_alias"))
|
|
|
|
|
2020-05-02 12:27:54 +02:00
|
|
|
if verify_prefix_suffix(current_user, alias_prefix, alias_suffix):
|
2020-01-22 00:03:25 +01:00
|
|
|
full_alias = alias_prefix + alias_suffix
|
2020-01-23 11:45:52 +01:00
|
|
|
|
2020-05-23 19:49:40 +02:00
|
|
|
if (
|
|
|
|
Alias.get_by(email=full_alias)
|
|
|
|
or DeletedAlias.get_by(email=full_alias)
|
|
|
|
or DomainDeletedAlias.get_by(email=full_alias)
|
|
|
|
):
|
2020-01-22 00:03:25 +01:00
|
|
|
LOG.d("full alias already used %s", full_alias)
|
|
|
|
flash(
|
|
|
|
f"Alias {full_alias} already exists, please choose another one",
|
|
|
|
"warning",
|
2019-08-30 22:42:06 +02:00
|
|
|
)
|
2020-01-22 00:03:25 +01:00
|
|
|
else:
|
2020-05-23 11:51:35 +02:00
|
|
|
custom_domain_id = None
|
|
|
|
# get the custom_domain_id if alias is created with a custom domain
|
|
|
|
if alias_suffix.startswith("@"):
|
|
|
|
alias_domain = alias_suffix[1:]
|
|
|
|
domain = CustomDomain.get_by(domain=alias_domain)
|
|
|
|
|
|
|
|
# check if the alias is currently in the domain trash
|
|
|
|
if domain and DomainDeletedAlias.get_by(
|
|
|
|
domain_id=domain.id, email=full_alias
|
|
|
|
):
|
|
|
|
flash(
|
|
|
|
f"Alias {full_alias} is currently in the {domain.domain} trash. "
|
|
|
|
f"Please remove it from the trash in order to re-create it.",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.custom_alias"))
|
|
|
|
|
2020-05-27 22:13:43 +02:00
|
|
|
if domain:
|
|
|
|
custom_domain_id = domain.id
|
2020-05-23 11:51:35 +02:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
alias = Alias.create(
|
2020-03-05 17:03:07 +01:00
|
|
|
user_id=current_user.id,
|
|
|
|
email=full_alias,
|
|
|
|
note=alias_note,
|
2020-05-03 16:50:39 +02:00
|
|
|
mailbox_id=mailboxes[0].id,
|
2020-05-23 11:51:35 +02:00
|
|
|
custom_domain_id=custom_domain_id,
|
2020-02-05 11:36:06 +01:00
|
|
|
)
|
2020-05-03 16:50:39 +02:00
|
|
|
db.session.flush()
|
|
|
|
|
|
|
|
for i in range(1, len(mailboxes)):
|
|
|
|
AliasMailbox.create(
|
2020-08-27 10:20:48 +02:00
|
|
|
alias_id=alias.id,
|
|
|
|
mailbox_id=mailboxes[i].id,
|
2020-05-03 16:50:39 +02:00
|
|
|
)
|
2020-01-23 11:45:52 +01:00
|
|
|
|
2019-07-06 23:25:52 +02:00
|
|
|
db.session.commit()
|
2020-01-22 00:03:25 +01:00
|
|
|
flash(f"Alias {full_alias} has been created", "success")
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
return redirect(url_for("dashboard.index", highlight_alias_id=alias.id))
|
2020-01-22 00:03:25 +01:00
|
|
|
# only happen if the request has been "hacked"
|
|
|
|
else:
|
|
|
|
flash("something went wrong", "warning")
|
2019-07-06 23:25:52 +02:00
|
|
|
|
2020-05-02 12:34:11 +02:00
|
|
|
return render_template(
|
|
|
|
"dashboard/custom_alias.html",
|
|
|
|
user_custom_domains=user_custom_domains,
|
|
|
|
suffixes=suffixes,
|
|
|
|
mailboxes=mailboxes,
|
|
|
|
)
|
2020-01-22 00:03:25 +01:00
|
|
|
|
|
|
|
|
2020-05-02 12:27:54 +02:00
|
|
|
def verify_prefix_suffix(user, alias_prefix, alias_suffix) -> bool:
|
2020-01-22 00:03:25 +01:00
|
|
|
"""verify if user could create an alias with the given prefix and suffix"""
|
2020-01-22 23:03:52 +01:00
|
|
|
if not alias_prefix or not alias_suffix: # should be caught on frontend
|
|
|
|
return False
|
|
|
|
|
2020-05-02 12:27:54 +02:00
|
|
|
user_custom_domains = [cd.domain for cd in user.verified_custom_domains()]
|
2020-01-22 00:03:25 +01:00
|
|
|
alias_prefix = alias_prefix.strip()
|
|
|
|
alias_prefix = convert_to_id(alias_prefix)
|
|
|
|
|
|
|
|
# make sure alias_suffix is either .random_word@simplelogin.co or @my-domain.com
|
|
|
|
alias_suffix = alias_suffix.strip()
|
|
|
|
if alias_suffix.startswith("@"):
|
|
|
|
alias_domain = alias_suffix[1:]
|
2020-01-22 10:22:59 +01:00
|
|
|
# alias_domain can be either custom_domain or if DISABLE_ALIAS_SUFFIX, one of the default ALIAS_DOMAINS
|
2020-01-22 00:03:25 +01:00
|
|
|
if DISABLE_ALIAS_SUFFIX:
|
2020-01-22 10:22:59 +01:00
|
|
|
if (
|
|
|
|
alias_domain not in user_custom_domains
|
|
|
|
and alias_domain not in ALIAS_DOMAINS
|
|
|
|
):
|
2020-07-17 12:59:07 +02:00
|
|
|
LOG.exception("wrong alias suffix %s, user %s", alias_suffix, user)
|
2020-01-22 00:03:25 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if alias_domain not in user_custom_domains:
|
2020-07-17 12:59:07 +02:00
|
|
|
LOG.exception("wrong alias suffix %s, user %s", alias_suffix, user)
|
2020-01-22 00:03:25 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not alias_suffix.startswith("."):
|
2020-07-17 12:59:07 +02:00
|
|
|
LOG.exception("User %s submits a wrong alias suffix %s", user, alias_suffix)
|
2020-01-22 00:03:25 +01:00
|
|
|
return False
|
2020-01-22 10:22:59 +01:00
|
|
|
|
|
|
|
full_alias = alias_prefix + alias_suffix
|
|
|
|
if not email_belongs_to_alias_domains(full_alias):
|
2020-07-17 12:59:07 +02:00
|
|
|
LOG.exception(
|
2020-01-22 10:22:59 +01:00
|
|
|
"Alias suffix should end with one of the alias domains %s",
|
2020-01-22 00:03:25 +01:00
|
|
|
user,
|
|
|
|
alias_suffix,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
random_word_part = alias_suffix[1 : alias_suffix.find("@")]
|
|
|
|
if not word_exist(random_word_part):
|
2020-07-17 12:59:07 +02:00
|
|
|
LOG.exception(
|
2020-01-22 00:03:25 +01:00
|
|
|
"alias suffix %s needs to start with a random word, user %s",
|
|
|
|
alias_suffix,
|
|
|
|
user,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|