TASK-002 Enforce authorization and media boundaries

This commit is contained in:
rob
2026-11-25 17:24:12 +00:00
parent cd6d6b3a83
commit 549f11d9f4
14 changed files with 374 additions and 92 deletions
+11 -38
View File
@@ -1,49 +1,22 @@
from settings import app, root_path
from flask import request, send_from_directory
import os
import mime
"""Media routes. Legacy client-directed downloads are deliberately disabled."""
from settings import app
from auth import token_auth
from errors import error_response
from media import media_root, file_tree, MediaDenied
@app.route("/api/file/root")
@token_auth.login_required
def get_root_path() -> list:
user = token_auth.current_user()
if not user.is_admin:
def get_root_path():
if not token_auth.current_user().is_admin:
return error_response(403)
file_list = []
for root, dirs, files in os.walk(root_path, topdown=False):
files = list(
map(
lambda file: {
"name": file,
"size": os.path.getsize(root + "/" + file),
},
files,
)
)
file_list.append(
{
"root": "/" + "/".join(root.split("/")[1:]),
"dirs": dirs,
"files": files,
}
)
return file_list[::-1]
try:
return file_tree(media_root())
except MediaDenied:
return error_response(503, "Media unavailable")
@app.route("/api/file", methods=["POST"])
@token_auth.login_required
def get_file() -> dict:
req = request.get_json()
path = req["path"]
mimetype = str(mime.Types.of(path + "/" + req["filename"])[0])
if not os.path.exists(path):
return {"error": "file not found"}
try:
return send_from_directory(
path, req["filename"], as_attachment=True, mimetype=mimetype
)
except FileNotFoundError:
return error_response(404, f"File not found: {path}/{req["filename"]}")
def get_file():
return error_response(410, "Downloads unavailable pending secure download grants")