mirror of
https://github.com/aclist/dztui.git
synced 2026-08-30 03:37:00 +02:00
Compare commits
3 Commits
d75f87768e
...
78151b3bb3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78151b3bb3 | ||
|
|
2418d8be68 | ||
|
|
9fa7172056 |
@ -3,7 +3,8 @@ import re
|
|||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
api_filter = r"(.*&key=)([^&]*)(.*)"
|
api_filter = r"(.*&key=)([^&]*)(.*)"
|
||||||
home_filter = r"(/home/)([^/]*)(.*)"
|
# FIXME: handle whitespace after directory name ($HOME root)
|
||||||
|
home_filter = r"(/home/)([^\s'\/]*)(.*)"
|
||||||
REDACTED = r"\1REDACTED\3"
|
REDACTED = r"\1REDACTED\3"
|
||||||
REDACTION_PATTERNS = [api_filter, home_filter]
|
REDACTION_PATTERNS = [api_filter, home_filter]
|
||||||
|
|
||||||
|
|||||||
@ -110,6 +110,7 @@ markers = [
|
|||||||
"mods: tests mod metadata/link creation",
|
"mods: tests mod metadata/link creation",
|
||||||
"pefile: validate PE files",
|
"pefile: validate PE files",
|
||||||
"post_install: requires a completed installation",
|
"post_install: requires a completed installation",
|
||||||
|
"redact: log redaction mechanisms",
|
||||||
"slow: long-running tests",
|
"slow: long-running tests",
|
||||||
"webtest: checks remote endpoints"
|
"webtest: checks remote endpoints"
|
||||||
]
|
]
|
||||||
|
|||||||
54
tests/test_log_redaction.py
Normal file
54
tests/test_log_redaction.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import logging
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from dzgui.util.redact import RedactionFilter, REDACTION_PATTERNS
|
||||||
|
|
||||||
|
|
||||||
|
class RecordsListHandler(logging.Handler):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.records_list = []
|
||||||
|
|
||||||
|
def emit(self, record: logging.LogRecord) -> None:
|
||||||
|
self.records_list.append(record)
|
||||||
|
|
||||||
|
def pop(self) -> None:
|
||||||
|
return self.records_list[-1].msg
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.redact
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"log_error, expect",
|
||||||
|
[
|
||||||
|
("/home/SENSITIVE_USERNAME/subdir", "/home/REDACTED/subdir"),
|
||||||
|
(
|
||||||
|
"https://url.com/?api&key=SENSITIVE_KEY&results=10",
|
||||||
|
"https://url.com/?api&key=REDACTED&results=10",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"https://url.com/?api&key=SENSITIVE_KEY",
|
||||||
|
"https://url.com/?api&key=REDACTED",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Error in directory: '/home/SENSITIVE_USERNAME/'",
|
||||||
|
"Error in directory: '/home/REDACTED/'",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Error in directory: '/home/SENSITIVE_USERNAME'",
|
||||||
|
"Error in directory: '/home/REDACTED'",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_log_redaction(log_error: str, expect: str) -> None:
|
||||||
|
logger = logging.getLogger("TEST")
|
||||||
|
|
||||||
|
handler = RecordsListHandler()
|
||||||
|
logger.addHandler(handler)
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
_filter = RedactionFilter(patterns=REDACTION_PATTERNS)
|
||||||
|
logger.addFilter(_filter)
|
||||||
|
|
||||||
|
logger.critical(log_error)
|
||||||
|
redacted = handler.pop()
|
||||||
|
assert expect == redacted
|
||||||
Loading…
Reference in New Issue
Block a user