From a71eb6b14ca6d7c7ac03e9d6ca3eaa25cb2e4fda Mon Sep 17 00:00:00 2001 From: scandalist-trash Date: Sun, 25 Oct 2026 07:06:34 -0400 Subject: [PATCH] fix(starpost): reject contract probes before database access --- starpost/api/app.py | 50 +++++++++++++++++++++++++++++++++------------ starpost/resume.md | 12 +++++++++-- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/starpost/api/app.py b/starpost/api/app.py index dbf4664..17f9780 100644 --- a/starpost/api/app.py +++ b/starpost/api/app.py @@ -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) diff --git a/starpost/resume.md b/starpost/resume.md index 7f7928a..954efb8 100644 --- a/starpost/resume.md +++ b/starpost/resume.md @@ -24,6 +24,12 @@ Before the patch, a seller could request `GET /api/contract?file=contract-