mirror of
https://github.com/chatmail/relay.git
synced 2026-05-10 16:04:37 +00:00
fix(security): validate localpart chars and fix account creation race
- Reject localparts with chars outside [a-z0-9._-] to prevent filesystem issues from crafted usernames via IMAP/SMTP auth - Use filelock to serialize concurrent account creation for same address, preventing TOCTOU race where two threads both create an account and last writer wins
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import filelock
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import crypt_r
|
import crypt_r
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -13,6 +16,7 @@ from .dictproxy import DictProxy
|
|||||||
from .migrate_db import migrate_from_db_to_maildir
|
from .migrate_db import migrate_from_db_to_maildir
|
||||||
|
|
||||||
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
||||||
|
VALID_LOCALPART_RE = re.compile(r"^[a-z0-9._-]+$")
|
||||||
|
|
||||||
|
|
||||||
def encrypt_password(password: str):
|
def encrypt_password(password: str):
|
||||||
@@ -52,6 +56,10 @@ def is_allowed_to_create(config: Config, user, cleartext_password) -> bool:
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if not VALID_LOCALPART_RE.match(localpart):
|
||||||
|
logging.warning("localpart %r contains invalid characters", localpart)
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -140,8 +148,13 @@ class AuthDictProxy(DictProxy):
|
|||||||
if not is_allowed_to_create(self.config, addr, cleartext_password):
|
if not is_allowed_to_create(self.config, addr, cleartext_password):
|
||||||
return
|
return
|
||||||
|
|
||||||
user.set_password(encrypt_password(cleartext_password))
|
lock = filelock.FileLock(str(user.password_path) + ".lock", timeout=5)
|
||||||
print(f"Created address: {addr}", file=sys.stderr)
|
with lock:
|
||||||
|
userdata = user.get_userdb_dict()
|
||||||
|
if userdata:
|
||||||
|
return userdata
|
||||||
|
user.set_password(encrypt_password(cleartext_password))
|
||||||
|
print(f"Created address: {addr}", file=sys.stderr)
|
||||||
return user.get_userdb_dict()
|
return user.get_userdb_dict()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user