Request log: a hook's secret is masked in uvicorn's lines and the tunnel connector's failure line
Claude-Session: https://claude.ai/code/session_01SSi5AvZ7pFmySK8zy3NPMh
This commit is contained in:
+17
-8
@@ -66,26 +66,35 @@ logger = logging.getLogger(__name__)
|
||||
#: worker's `?token=`, the pairing poll's `?secret=`.
|
||||
_CREDENTIAL_IN_QUERY = re.compile(r"([?&](?:[\w-]*token|[\w-]*secret|ticket)=)[^&\s]+")
|
||||
|
||||
#: A hook's secret, the last segment of `/hooks/<flow>/<path>/<secret>`. A hook
|
||||
#: without one loses its last path segment from the log too, since the log
|
||||
#: cannot tell which hooks carry a secret.
|
||||
_SECRET_IN_HOOK_PATH = re.compile(r"(/hooks/[^/\s?]+/(?:[^/\s?]*/)*)[^/\s?]+")
|
||||
|
||||
|
||||
def _redact(text: str) -> str:
|
||||
return _SECRET_IN_HOOK_PATH.sub(r"\1***", _CREDENTIAL_IN_QUERY.sub(r"\1***", text))
|
||||
|
||||
|
||||
class _RedactTokens(logging.Filter):
|
||||
"""Keep query-string credentials out of uvicorn's request lines.
|
||||
"""Keep credentials out of request lines, from the query string or the path.
|
||||
|
||||
uvicorn logs each request and websocket handshake with its query string,
|
||||
and the live socket, a worker's attach and a device polling for its
|
||||
pairing carry their credential there.
|
||||
uvicorn logs each request and websocket handshake with its URL, and the
|
||||
live socket, a worker's attach and a device polling for its pairing carry
|
||||
their credential in the query, a webhook or served socket in the path.
|
||||
"""
|
||||
|
||||
def filter(self, record: logging.LogRecord) -> bool:
|
||||
if isinstance(record.args, tuple):
|
||||
record.args = tuple(
|
||||
_CREDENTIAL_IN_QUERY.sub(r"\1***", arg) if isinstance(arg, str) else arg
|
||||
for arg in record.args
|
||||
_redact(arg) if isinstance(arg, str) else arg for arg in record.args
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
# The handshake lines go to `uvicorn.error`, the request lines to `uvicorn.access`.
|
||||
for _name in ("uvicorn.access", "uvicorn.error"):
|
||||
# The handshake lines go to `uvicorn.error`, the request lines to `uvicorn.access`,
|
||||
# and the tunnel connector names the path of a call it could not proxy.
|
||||
for _name in ("uvicorn.access", "uvicorn.error", "fluksio.cloud.connector"):
|
||||
logging.getLogger(_name).addFilter(_RedactTokens())
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user