Merge pull request 'fix(starpost): bound authentication memory' (#20) from fix/starpost-auth-memory into main

Reviewed-on: #20
This commit was merged in pull request #20.
This commit is contained in:
scandal committed 2026-10-25 17:23:18 +03:00
commit 7ed468e5d6
4 files changed
+50 -4

No files matched your search

+1 -1
View File
@@ -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"]
+3 -3
View File
@@ -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"])
+16
View File
@@ -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)
@@ -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()