File size: 1,665 Bytes
af78cff | 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | from __future__ import annotations
import logging
import os
SCANNER_PATH_PREFIXES = (
"/.env",
"/.git",
"/.streamlit",
"/_debug",
"/_profiler",
"/_stcore",
"/actuator",
"/admin",
"/api/config",
"/api/credentials",
"/api/env",
"/api/keys",
"/api/predict",
"/api/queue/status",
"/api/secrets",
"/api/settings",
"/api/v1/",
"/api-docs",
"/app.py",
"/backup/",
"/config",
"/debug",
"/docs",
"/elmah",
"/file",
"/graphql",
"/horizon",
"/info",
"/internal",
"/main.py",
"/manifest.json",
"/metrics",
"/openapi.json",
"/phpinfo",
"/proc/",
"/prometheus",
"/redoc",
"/run/predict",
"/server-",
"/settings",
"/swagger",
"/telescope",
"/trace",
"/upload?upload_id=",
"/wp-config",
"/__phpinfo",
)
class ScannerNoiseFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
try:
path = str(record.args[2]).lower()
status_code = int(record.args[4])
except (IndexError, TypeError, ValueError):
return True
if status_code not in {404, 405}:
return True
return not path.startswith(SCANNER_PATH_PREFIXES)
def install_access_log_filter() -> None:
enabled = os.getenv("SUPPRESS_SCANNER_ACCESS_LOGS", "true").lower() in {
"1",
"true",
"yes",
}
if not enabled:
return
logger = logging.getLogger("uvicorn.access")
if not any(isinstance(item, ScannerNoiseFilter) for item in logger.filters):
logger.addFilter(ScannerNoiseFilter())
|