Skip to content

Commit fc0eba7

Browse files
minrkblink1073
authored andcommitted
Backport PR jupyter-server#1212: Redact tokens, etc. in url parameters from request logs
replaces `?token=abc123` with `?token=[secret]` in logs (cherry picked from commit 968c56c)
1 parent ef9663e commit fc0eba7

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

jupyter_server/log.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,38 @@
55
# the file COPYING, distributed as part of this software.
66
# -----------------------------------------------------------------------------
77
import json
8+
from urllib.parse import urlparse, urlunparse
89

910
from tornado.log import access_log
1011

1112
from .prometheus.log_functions import prometheus_log_method
1213

14+
# url params to be scrubbed if seen
15+
# any url param that *contains* one of these
16+
# will be scrubbed from logs
17+
_SCRUB_PARAM_KEYS = {"token", "auth", "key", "code", "state", "xsrf"}
18+
19+
20+
def _scrub_uri(uri: str) -> str:
21+
"""scrub auth info from uri"""
22+
parsed = urlparse(uri)
23+
if parsed.query:
24+
# check for potentially sensitive url params
25+
# use manual list + split rather than parsing
26+
# to minimally perturb original
27+
parts = parsed.query.split("&")
28+
changed = False
29+
for i, s in enumerate(parts):
30+
key, sep, value = s.partition("=")
31+
for substring in _SCRUB_PARAM_KEYS:
32+
if substring in key:
33+
parts[i] = f"{key}{sep}[secret]"
34+
changed = True
35+
if changed:
36+
parsed = parsed._replace(query="&".join(parts))
37+
return urlunparse(parsed)
38+
return uri
39+
1340

1441
def log_request(handler):
1542
"""log a bit more information about each request than tornado's default
@@ -41,13 +68,13 @@ def log_request(handler):
4168
status=status,
4269
method=request.method,
4370
ip=request.remote_ip,
44-
uri=request.uri,
71+
uri=_scrub_uri(request.uri),
4572
request_time=request_time,
4673
)
4774
msg = "{status} {method} {uri} ({ip}) {request_time:.2f}ms"
4875
if status >= 400:
4976
# log bad referers
50-
ns["referer"] = request.headers.get("Referer", "None")
77+
ns["referer"] = _scrub_uri(request.headers.get("Referer", "None"))
5178
msg = msg + " referer={referer}"
5279
if status >= 500 and status != 502:
5380
# Log a subset of the headers if it caused an error.

0 commit comments

Comments
 (0)