Handle malformed braindance JSON safely
This commit is contained in:
1 parent
c49ccab44d
commit
74cace4561
1 file changed
+19
-5
@@ -101,6 +101,10 @@ def track_changes(response):
|
|||||||
def capacity_error():
|
def capacity_error():
|
||||||
return jsonify(error='storage limit reached; retry after old data expires'), 507
|
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):
|
def record_public(rec):
|
||||||
return {n: b64(rec['layers'][n]) for n in rec['public'] if n in rec['layers']}
|
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'])
|
@app.route('/register', methods=['POST'])
|
||||||
def register():
|
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()
|
name = str(data.get('username', '')).strip()
|
||||||
bio = str(data.get('bio', ''))[:140]
|
bio = str(data.get('bio', ''))[:140]
|
||||||
if not name or len(name) > 64 or '/' in name:
|
if not name or len(name) > 64 or '/' in name:
|
||||||
@@ -570,7 +576,9 @@ def comments(rid):
|
|||||||
user = auth()
|
user = auth()
|
||||||
if not user:
|
if not user:
|
||||||
return (jsonify(error='unauthorized'), 401)
|
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]
|
text = str(data.get('text', ''))[:200]
|
||||||
if len(COMMENTS.get(rid, ())) >= 100 or COMMENT_COUNT >= 20000:
|
if len(COMMENTS.get(rid, ())) >= 100 or COMMENT_COUNT >= 20000:
|
||||||
return capacity_error()
|
return capacity_error()
|
||||||
@@ -587,7 +595,9 @@ def collections():
|
|||||||
with LOCK:
|
with LOCK:
|
||||||
mine = [{'id': cid, 'name': c['name'], 'items': len(c['items'])} for cid, c in COLLECTIONS.items() if c['owner'] == user]
|
mine = [{'id': cid, 'name': c['name'], 'items': len(c['items'])} for cid, c in COLLECTIONS.items() if c['owner'] == user]
|
||||||
return jsonify(collections=mine)
|
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]
|
name = str(data.get('name', 'untitled'))[:60]
|
||||||
with LOCK:
|
with LOCK:
|
||||||
if len(COLLECTIONS) >= MAX_OBJECTS or len(OWNER_COLLECTIONS.get(user, ())) >= 32:
|
if len(COLLECTIONS) >= MAX_OBJECTS or len(OWNER_COLLECTIONS.get(user, ())) >= 32:
|
||||||
@@ -602,7 +612,9 @@ def collection_add(cid):
|
|||||||
user = auth()
|
user = auth()
|
||||||
if not user:
|
if not user:
|
||||||
return (jsonify(error='unauthorized'), 401)
|
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', ''))
|
rid = str(data.get('record_id', ''))
|
||||||
with LOCK:
|
with LOCK:
|
||||||
col = COLLECTIONS.get(cid)
|
col = COLLECTIONS.get(cid)
|
||||||
@@ -664,7 +676,9 @@ def trailer(rid):
|
|||||||
|
|
||||||
@app.route('/preview', methods=['POST'])
|
@app.route('/preview', methods=['POST'])
|
||||||
def preview():
|
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]
|
tpl = str(data.get('template', ''))[:400]
|
||||||
title = str(data.get('title', 'untitled'))[:60]
|
title = str(data.get('title', 'untitled'))[:60]
|
||||||
mood = str(data.get('mood', 'neutral'))[:30]
|
mood = str(data.get('mood', 'neutral'))[:30]
|
||||||
|
|||||||
Reference in new issue
Block a user