From b4870b401a913ad6941b6ecc58b88ba46a237ea5 Mon Sep 17 00:00:00 2001 From: scandalist-trash Date: Sun, 25 Oct 2026 10:21:50 -0400 Subject: [PATCH] fix(starpost): bound authentication memory --- starpost/api/Dockerfile | 2 +- starpost/api/app.py | 6 ++--- starpost/api/passwords.py | 16 ++++++++++++ starpost/tests/password_hash_regression.py | 30 ++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 starpost/api/passwords.py create mode 100644 starpost/tests/password_hash_regression.py diff --git a/starpost/api/Dockerfile b/starpost/api/Dockerfile index dc0cd4b..b3c878a 100644 --- a/starpost/api/Dockerfile +++ b/starpost/api/Dockerfile @@ -5,4 +5,4 @@ COPY requirements.txt /app/requirements.txt RUN pip install --no-cache-dir -r /app/requirements.txt COPY *.py /app/ WORKDIR /app -CMD ["gunicorn", "--preload", "--workers", "2", "--threads", "4", "--bind", "0.0.0.0:5000", "app:app"] +CMD ["gunicorn", "--preload", "--workers", "2", "--threads", "2", "--bind", "0.0.0.0:5000", "app:app"] diff --git a/starpost/api/app.py b/starpost/api/app.py index aa5b2c2..141b35e 100644 --- a/starpost/api/app.py +++ b/starpost/api/app.py @@ -10,7 +10,6 @@ from functools import lru_cache, wraps import psycopg from flask import Flask, jsonify, request, send_file, session -from werkzeug.security import check_password_hash, generate_password_hash from contracts import CONTRACT_DIR, create_contract from database import ( @@ -22,6 +21,7 @@ from database import ( from warehouse_client import CALLBACK_TOKEN as WAREHOUSE_CALLBACK_TOKEN from warehouse_client import get_health as get_warehouse_health from warehouse_client import submit_jobs as submit_warehouse_jobs +from passwords import hash_password, password_matches app = Flask(__name__) @@ -175,7 +175,7 @@ def register(): return error(str(exc)) try: with db() as conn, conn.cursor() as cur: - cur.execute("INSERT INTO users(username,password_hash,role,company_name,tax_id) VALUES(%s,%s,%s,%s,%s) RETURNING id", (username, generate_password_hash(password), role, company, tax_id)) + cur.execute("INSERT INTO users(username,password_hash,role,company_name,tax_id) VALUES(%s,%s,%s,%s,%s) RETURNING id", (username, hash_password(password), role, company, tax_id)) user_id = cur.fetchone()["id"] if role == "seller": cur.execute("UPDATE users SET contract_file=%s WHERE id=%s", (create_contract(user_id, company, tax_id, signature), user_id)) @@ -197,7 +197,7 @@ def login(): with db() as conn, conn.cursor() as cur: cur.execute("SELECT * FROM users WHERE username=%s", (data.get("username"),)) user = cur.fetchone() - if not user or not isinstance(data.get("password"), str) or not check_password_hash(user["password_hash"], data["password"]): + if not user or not isinstance(data.get("password"), str) or not password_matches(user["password_hash"], data["password"]): return error("invalid credentials", 401) session.clear() session.update(user_id=user["id"], username=user["username"], role=user["role"]) diff --git a/starpost/api/passwords.py b/starpost/api/passwords.py new file mode 100644 index 0000000..409cda5 --- /dev/null +++ b/starpost/api/passwords.py @@ -0,0 +1,16 @@ +from werkzeug.security import check_password_hash, generate_password_hash + + +# Werkzeug's default scrypt hash needs about 32 MiB of working memory per +# concurrent request. The API serves up to four authentication requests at a +# time, so use a CPU-bound hash for new accounts while retaining verification +# support for existing scrypt hashes. +PASSWORD_HASH_METHOD = "pbkdf2:sha256:600000" + + +def hash_password(password): + return generate_password_hash(password, method=PASSWORD_HASH_METHOD) + + +def password_matches(password_hash, password): + return check_password_hash(password_hash, password) diff --git a/starpost/tests/password_hash_regression.py b/starpost/tests/password_hash_regression.py new file mode 100644 index 0000000..ed6258a --- /dev/null +++ b/starpost/tests/password_hash_regression.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +import sys +from pathlib import Path + +from werkzeug.security import generate_password_hash + + +API_DIR = Path(__file__).resolve().parents[1] / "api" +sys.path.insert(0, str(API_DIR)) + +from passwords import hash_password, password_matches # noqa: E402 + + +def main(): + password = "correct horse battery staple" + + current_hash = hash_password(password) + assert current_hash.startswith("pbkdf2:sha256:600000$") + assert password_matches(current_hash, password) + assert not password_matches(current_hash, password + "!") + + legacy_hash = generate_password_hash(password, method="scrypt:32768:8:1") + assert password_matches(legacy_hash, password) + assert not password_matches(legacy_hash, password + "!") + + print("password hash compatibility: ok") + + +if __name__ == "__main__": + main() -- 2.43.0