fix(starpost): reject contract probes before database access
This commit is contained in:
1 parent
2870f3e635
commit
a71eb6b14c
2 files changed
+47
-15
No files matched your search
+37
-13
@@ -3,9 +3,10 @@ import hashlib
|
||||
import hmac
|
||||
import json as _json
|
||||
import os
|
||||
import re
|
||||
import secrets
|
||||
from datetime import datetime, timezone
|
||||
from functools import wraps
|
||||
from functools import lru_cache, wraps
|
||||
|
||||
import psycopg
|
||||
from flask import Flask, jsonify, request, send_file, session
|
||||
@@ -27,6 +28,8 @@ app.config.update(
|
||||
MAX_CONTENT_LENGTH=32 * 1024,
|
||||
)
|
||||
|
||||
CONTRACT_FILENAME_RE = re.compile(r"contract-([0-9]+)-[0-9a-f]{16}\.pdf")
|
||||
|
||||
|
||||
migrate()
|
||||
|
||||
@@ -50,6 +53,21 @@ def text(value, field, minimum=1, maximum=200):
|
||||
return value.strip()
|
||||
|
||||
|
||||
def own_contract_filename(filename, user_id):
|
||||
match = CONTRACT_FILENAME_RE.fullmatch(filename)
|
||||
return match is not None and match.group(1) == str(user_id)
|
||||
|
||||
|
||||
@lru_cache(maxsize=4096)
|
||||
def contract_owner(user_id):
|
||||
with db() as conn, conn.cursor() as cur:
|
||||
cur.execute("SELECT id,contract_file FROM users WHERE id=%s", (user_id,))
|
||||
owner = cur.fetchone()
|
||||
if not owner:
|
||||
return None
|
||||
return owner["id"], owner["contract_file"]
|
||||
|
||||
|
||||
def auth(role=None):
|
||||
def decorator(handler):
|
||||
@wraps(handler)
|
||||
@@ -700,23 +718,31 @@ def seller_reviews():
|
||||
@app.route("/api/contract", methods=["GET", "POST"])
|
||||
@auth("seller")
|
||||
def get_contract():
|
||||
with db() as conn, conn.cursor() as cur:
|
||||
cur.execute("SELECT id,contract_file FROM users WHERE id=%s", (session["user_id"],))
|
||||
owner = cur.fetchone()
|
||||
if not owner or not owner["contract_file"]:
|
||||
requested = request.args.get("file")
|
||||
if request.method == "GET" and requested:
|
||||
requested = requested.strip()
|
||||
own_alias = f"contract-{session['user_id']}"
|
||||
if requested != own_alias and not own_contract_filename(requested, session["user_id"]):
|
||||
return error("contract not found", 404)
|
||||
contract_id = None
|
||||
if request.method == "POST":
|
||||
contract_id = request.headers.get("X-Contract-Id", "").strip()
|
||||
if not re.fullmatch(r"\d+", contract_id) or contract_id != str(session["user_id"]):
|
||||
return error("invalid contract id", 400)
|
||||
|
||||
owner = contract_owner(session["user_id"])
|
||||
if not owner or not owner[1]:
|
||||
return error("contract not found", 404)
|
||||
|
||||
filename = owner["contract_file"]
|
||||
owner_id, filename = owner
|
||||
if not own_contract_filename(filename, owner_id):
|
||||
return error("contract not found", 404)
|
||||
contract_dir = CONTRACT_DIR.resolve()
|
||||
path = (CONTRACT_DIR / filename).resolve()
|
||||
if path.parent != contract_dir:
|
||||
return error("contract not found", 404)
|
||||
|
||||
if request.method == "POST":
|
||||
import re
|
||||
contract_id = request.headers.get("X-Contract-Id", "").strip()
|
||||
if not re.fullmatch(r"\d+", contract_id) or contract_id != str(owner["id"]):
|
||||
return error("invalid contract id", 400)
|
||||
try:
|
||||
path.unlink()
|
||||
except FileNotFoundError:
|
||||
@@ -725,10 +751,8 @@ def get_contract():
|
||||
return error("contract deletion failed", 500)
|
||||
return jsonify(status="deleted", deleted=1)
|
||||
|
||||
requested = request.args.get("file")
|
||||
if requested:
|
||||
raw = requested.strip()
|
||||
if raw not in {filename, f"contract-{owner['id']}"}:
|
||||
if requested not in {filename, f"contract-{owner_id}"}:
|
||||
return error("contract not found", 404)
|
||||
if not path.is_file():
|
||||
return error("contract not found", 404)
|
||||
|
||||
Reference in new issue
Block a user