From 74cace456143fc19e13357c4c775c36caf30cad0 Mon Sep 17 00:00:00 2001 From: yato Date: Sun, 29 Nov 2026 15:15:38 +0300 Subject: [PATCH] Handle malformed braindance JSON safely --- braindance/backend/app.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/braindance/backend/app.py b/braindance/backend/app.py index 854ba18..3cf0241 100755 --- a/braindance/backend/app.py +++ b/braindance/backend/app.py @@ -101,6 +101,10 @@ def track_changes(response): def capacity_error(): return jsonify(error='storage limit reached; retry after old data expires'), 507 +def json_object(): + data = request.get_json(force=True, silent=True) + return data if isinstance(data, dict) else None + def record_public(rec): return {n: b64(rec['layers'][n]) for n in rec['public'] if n in rec['layers']} @@ -373,7 +377,9 @@ def stats(): @app.route('/register', methods=['POST']) def register(): - data = request.get_json(force=True, silent=True) or {} + data = json_object() + if data is None: + return (jsonify(error='bad json'), 400) name = str(data.get('username', '')).strip() bio = str(data.get('bio', ''))[:140] if not name or len(name) > 64 or '/' in name: @@ -570,7 +576,9 @@ def comments(rid): user = auth() if not user: return (jsonify(error='unauthorized'), 401) - data = request.get_json(force=True, silent=True) or {} + data = json_object() + if data is None: + return (jsonify(error='bad json'), 400) text = str(data.get('text', ''))[:200] if len(COMMENTS.get(rid, ())) >= 100 or COMMENT_COUNT >= 20000: return capacity_error() @@ -587,7 +595,9 @@ def collections(): with LOCK: mine = [{'id': cid, 'name': c['name'], 'items': len(c['items'])} for cid, c in COLLECTIONS.items() if c['owner'] == user] return jsonify(collections=mine) - data = request.get_json(force=True, silent=True) or {} + data = json_object() + if data is None: + return (jsonify(error='bad json'), 400) name = str(data.get('name', 'untitled'))[:60] with LOCK: if len(COLLECTIONS) >= MAX_OBJECTS or len(OWNER_COLLECTIONS.get(user, ())) >= 32: @@ -602,7 +612,9 @@ def collection_add(cid): user = auth() if not user: return (jsonify(error='unauthorized'), 401) - data = request.get_json(force=True, silent=True) or {} + data = json_object() + if data is None: + return (jsonify(error='bad json'), 400) rid = str(data.get('record_id', '')) with LOCK: col = COLLECTIONS.get(cid) @@ -664,7 +676,9 @@ def trailer(rid): @app.route('/preview', methods=['POST']) def preview(): - data = request.get_json(force=True, silent=True) or {} + data = json_object() + if data is None: + return (jsonify(error='bad json'), 400) tpl = str(data.get('template', ''))[:400] title = str(data.get('title', 'untitled'))[:60] mood = str(data.get('mood', 'neutral'))[:30]