-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
25 lines (21 loc) · 903 Bytes
/
utils.py
File metadata and controls
25 lines (21 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from flask import Request
def request_to_sanitized_json(req: Request) -> dict:
"""
Convert a Flask request into a safe JSON-serializable dict (should not contain any sensitive data).
"""
return {
"method": req.method,
"path": req.path,
"query": req.args.to_dict(flat=True), # type: ignore
"form": req.form.to_dict(flat=True) if req.form else None, # type: ignore
"json": req.get_json(silent=True) if req.is_json else None,
"ip": req.headers.get("X-Forwarded-For", req.remote_addr),
"user_agent": req.user_agent.string,
}
def request_to_dirty_json(req: Request) -> dict:
"""
Convert a Flask request into a JSON-serializable dict that MAY CONTAIN SENSITIVE DATA like the X-Signature.
"""
return request_to_sanitized_json(req) | {
"headers": {key: value for key, value in req.headers.items()},
}