fix(starpost): bound authentication memory #20
No files matched your search
@@ -5,4 +5,4 @@ COPY requirements.txt /app/requirements.txt
|
|||||||
RUN pip install --no-cache-dir -r /app/requirements.txt
|
RUN pip install --no-cache-dir -r /app/requirements.txt
|
||||||
COPY *.py /app/
|
COPY *.py /app/
|
||||||
WORKDIR /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
@@ -10,7 +10,6 @@ from functools import lru_cache, wraps
|
|||||||
|
|
||||||
import psycopg
|
import psycopg
|
||||||
from flask import Flask, jsonify, request, send_file, session
|
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 contracts import CONTRACT_DIR, create_contract
|
||||||
from database import (
|
from database import (
|
||||||
@@ -22,6 +21,7 @@ from database import (
|
|||||||
from warehouse_client import CALLBACK_TOKEN as WAREHOUSE_CALLBACK_TOKEN
|
from warehouse_client import CALLBACK_TOKEN as WAREHOUSE_CALLBACK_TOKEN
|
||||||
from warehouse_client import get_health as get_warehouse_health
|
from warehouse_client import get_health as get_warehouse_health
|
||||||
from warehouse_client import submit_jobs as submit_warehouse_jobs
|
from warehouse_client import submit_jobs as submit_warehouse_jobs
|
||||||
|
from passwords import hash_password, password_matches
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -175,7 +175,7 @@ def register():
|
|||||||
return error(str(exc))
|
return error(str(exc))
|
||||||
try:
|
try:
|
||||||
with db() as conn, conn.cursor() as cur:
|
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"]
|
user_id = cur.fetchone()["id"]
|
||||||
if role == "seller":
|
if role == "seller":
|
||||||
cur.execute("UPDATE users SET contract_file=%s WHERE id=%s", (create_contract(user_id, company, tax_id, signature), user_id))
|
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:
|
with db() as conn, conn.cursor() as cur:
|
||||||
cur.execute("SELECT * FROM users WHERE username=%s", (data.get("username"),))
|
cur.execute("SELECT * FROM users WHERE username=%s", (data.get("username"),))
|
||||||
user = cur.fetchone()
|
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)
|
return error("invalid credentials", 401)
|
||||||
session.clear()
|
session.clear()
|
||||||
session.update(user_id=user["id"], username=user["username"], role=user["role"])
|
session.update(user_id=user["id"], username=user["username"], role=user["role"])
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in new issue
Block a user