Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions airflow/utils/log/secrets_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from __future__ import annotations

import collections.abc
import contextlib
import logging
import sys
from enum import Enum
Expand Down Expand Up @@ -184,17 +185,38 @@ def _record_attrs_to_ignore(self) -> Iterable[str]:
)
return frozenset(record.__dict__).difference({"msg", "args"})

def _redact_exception_with_context(self, exception):
def _redact_exception_with_context_or_cause(self, exception, visited=None):
# Exception class may not be modifiable (e.g. declared by an
# extension module such as JDBC).
try:
exception.args = (self.redact(v) for v in exception.args)
except AttributeError:
pass
if exception.__context__:
self._redact_exception_with_context(exception.__context__)
if exception.__cause__ and exception.__cause__ is not exception.__context__:
self._redact_exception_with_context(exception.__cause__)
with contextlib.suppress(AttributeError):
if visited is None:
visited = set()

if id(exception) in visited:
# already visited - it was redacted earlier
return exception

# Check depth before adding to visited to ensure we skip exceptions beyond the limit
if len(visited) >= self.MAX_RECURSION_DEPTH:
return RuntimeError(
f"Stack trace redaction hit recursion limit of {self.MAX_RECURSION_DEPTH} "
f"when processing exception of type {type(exception).__name__}. "
f"The remaining exceptions will be skipped to avoid "
f"infinite recursion and protect against revealing sensitive information."
)

visited.add(id(exception))

exception.args = tuple(self.redact(v) for v in exception.args)
if exception.__context__:
exception.__context__ = self._redact_exception_with_context_or_cause(
exception.__context__, visited
)
if exception.__cause__ and exception.__cause__ is not exception.__context__:
exception.__cause__ = self._redact_exception_with_context_or_cause(
exception.__cause__, visited
)
return exception

def filter(self, record) -> bool:
if settings.MASK_SECRETS_IN_LOGS is not True:
Expand All @@ -211,7 +233,7 @@ def filter(self, record) -> bool:
record.__dict__[k] = self.redact(v)
if record.exc_info and record.exc_info[1] is not None:
exc = record.exc_info[1]
self._redact_exception_with_context(exc)
self._redact_exception_with_context_or_cause(exc)
record.__dict__[self.ALREADY_FILTERED_FLAG] = True

return True
Expand Down
Loading
Loading