Skip to content

Commit 83dca29

Browse files
author
majestrate
authored
Merge pull request #66 from jagerman/random-fixes
Random fixes
2 parents a8dcb45 + 900b052 commit 83dca29

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

sogs/routes/general.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_caps():
3737
return jsonify(res), res_code
3838

3939

40-
def parse_batch_req(r):
40+
def parse_batch_request(req):
4141
"""
4242
Checks a batch request dict for the required fields:
4343
@@ -56,61 +56,61 @@ def parse_batch_req(r):
5656
dict within the request for json bodies, and `body` will be the *bytes* data (i.e. decoded from
5757
base64, when using `b64`) for 'b64' or 'bytes' requests.
5858
"""
59-
if not isinstance(r, dict):
59+
if not isinstance(req, dict):
6060
app.logger.warning("Invalid batch request: batch request is not a dict")
6161
abort(http.BAD_REQUEST)
62-
if 'method' not in r:
62+
if 'method' not in req:
6363
app.logger.warning("Invalid batch request: batch request has no method")
6464
abort(http.BAD_REQUEST)
65-
if 'path' not in r:
65+
if 'path' not in req:
6666
app.logger.warning("Invalid batch request: batch request has no path")
6767
abort(http.BAD_REQUEST)
6868

69-
method, path, headers, json, body = r['method'], r['path'], {}, None, None
69+
method, path, headers, json, body = req['method'], req['path'], {}, None, None
7070

71-
if 'headers' in r:
72-
if not isinstance(r['headers'], dict):
71+
if 'headers' in req:
72+
if not isinstance(req['headers'], dict):
7373
app.logger.warning("Bad batch request: 'headers' must be a dict")
7474
abort(http.BAD_REQUEST)
75-
if any(not isinstance(k, str) or not isinstance(v, str) for k, v in r['headers'].items()):
75+
if any(not isinstance(k, str) or not isinstance(v, str) for k, v in req['headers'].items()):
7676
app.logger.warning("Bad batch request: 'headers' must contain only str/str pairs")
7777
abort(http.BAD_REQUEST)
78-
headers = r['headers']
78+
headers = req['headers']
7979

80-
has_body = r['method'] in ('POST', 'PUT')
81-
if not has_body and r['method'] not in ('GET', 'DELETE'):
82-
app.logger.warning(f"Bad batch request: invalid request method {r['method']}")
80+
has_body = method in ('POST', 'PUT')
81+
if not has_body and method not in ('GET', 'DELETE'):
82+
app.logger.warning(f"Bad batch request: invalid request method {method}")
8383
abort(http.BAD_REQUEST)
8484

8585
if not path.startswith('/'):
8686
app.logger.warning(f"Bad batch request: path must start with /, got: [{path}]")
8787
abort(http.BAD_REQUEST)
8888

89-
n_bodies = sum(k in r for k in ('b64', 'json', 'bytes'))
89+
n_bodies = sum(k in req for k in ('b64', 'json', 'bytes'))
9090
if has_body:
9191
if not n_bodies:
92-
app.logger.warning(f"Bad batch request: {r['method']} requires one of json/b64/bytes")
92+
app.logger.warning(f"Bad batch request: {method} requires one of json/b64/bytes")
9393
abort(http.BAD_REQUEST)
9494
elif n_bodies > 1:
9595
app.logger.warning(
96-
f"Bad batch request: {r['method']} cannot have more than one of json/bytes/b64"
96+
f"Bad batch request: {method} cannot have more than one of json/bytes/b64"
9797
)
9898
abort(http.BAD_REQUEST)
9999

100-
if 'b64' in r:
100+
if 'b64' in req:
101101
try:
102-
body = utils.decode_base64(r['b64'])
102+
body = utils.decode_base64(req['b64'])
103103
except Exception:
104104
app.logger.warning("Bad batch request: b64 value is not valid base64")
105-
elif 'bytes' in r:
106-
body = r['bytes']
105+
elif 'bytes' in req:
106+
body = req['bytes']
107107
if not isinstance(body, bytes):
108108
body = body.encode()
109109
else:
110-
json = r['json']
110+
json = req['json']
111111

112112
elif n_bodies:
113-
app.logger.warning(f"Bad batch request: {r['method']} cannot have a json/b64/bytes body")
113+
app.logger.warning(f"Bad batch request: {req['method']} cannot have a json/b64/bytes body")
114114
abort(http.BAD_REQUEST)
115115

116116
return method, path, headers, json, body
@@ -138,7 +138,7 @@ def batch(_sequential=False):
138138

139139
# Expand this into a list first (rather than during iteration below) so that we abort everything
140140
# if any subrequest is invalid.
141-
subreqs = [parse_batch_req(r) for r in subreqs]
141+
subreqs = [parse_batch_request(r) for r in subreqs]
142142

143143
response = []
144144
for method, path, headers, json, body in subreqs:

sogs/routes/users.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def extract_rooms_or_global(req, admin=True):
2727
to).
2828
"""
2929

30+
if not isinstance(req, dict):
31+
app.logger.warning(f"Invalid request: expected a JSON object body, not {type(req)}")
32+
abort(http.BAD_REQUEST)
33+
3034
room_tokens, global_ = req.get('rooms'), req.get('global', False)
3135

3236
if room_tokens and not isinstance(room_tokens, list):
@@ -51,7 +55,7 @@ def extract_rooms_or_global(req, admin=True):
5155

5256
try:
5357
rooms = mroom.get_rooms_with_permission(
54-
g.user, tokens=room_tokens, moderator=True, admin=admin
58+
g.user, tokens=room_tokens, moderator=True, admin=True if admin else None
5559
)
5660
except Exception as e:
5761
# This is almost certainly a bad room token passed in:

0 commit comments

Comments
 (0)