Potential fix for code scanning alert no. 120: Log Injection
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8d5b713bf1
commit
a77a89f475
@@ -15,6 +15,22 @@ from typing import List, Optional, Tuple
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _sanitize_for_log(value: Optional[str]) -> str:
|
||||||
|
"""
|
||||||
|
Return a log-safe representation of *value* by removing newline and
|
||||||
|
carriage-return characters that could be used for log injection.
|
||||||
|
|
||||||
|
This is only used for logging; the original value should still be used
|
||||||
|
for functional behavior such as DNS lookups.
|
||||||
|
"""
|
||||||
|
if value is None:
|
||||||
|
return ""
|
||||||
|
# Ensure we are working with a string, then strip CR/LF characters
|
||||||
|
text = str(value)
|
||||||
|
return text.replace("\r", "").replace("\n", "")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _sanitize_for_log(value: str) -> str:
|
def _sanitize_for_log(value: str) -> str:
|
||||||
"""
|
"""
|
||||||
Remove characters that could be used for log injection from a string.
|
Remove characters that could be used for log injection from a string.
|
||||||
@@ -108,8 +124,13 @@ class BaseDNSProvider(ABC):
|
|||||||
safe_domain = _sanitize_for_log(domain)
|
safe_domain = _sanitize_for_log(domain)
|
||||||
logger.debug("SPF lookup failed for %s: %s", safe_domain, exc)
|
logger.debug("SPF lookup failed for %s: %s", safe_domain, exc)
|
||||||
async def check_spf(self, domain: str) -> Tuple[bool, Optional[str]]:
|
async def check_spf(self, domain: str) -> Tuple[bool, Optional[str]]:
|
||||||
|
safe_selector = _sanitize_for_log(selector)
|
||||||
|
safe_domain = _sanitize_for_log(domain)
|
||||||
"""Return *(found, record_string)* for the domain's SPF TXT record."""
|
"""Return *(found, record_string)* for the domain's SPF TXT record."""
|
||||||
try:
|
"DKIM lookup failed for selector=%s domain=%s: %s",
|
||||||
|
safe_selector,
|
||||||
|
safe_domain,
|
||||||
|
exc,
|
||||||
records = await self.lookup_txt(domain)
|
records = await self.lookup_txt(domain)
|
||||||
for record in records:
|
for record in records:
|
||||||
if record.lower().startswith("v=spf1"):
|
if record.lower().startswith("v=spf1"):
|
||||||
|
|||||||
Reference in New Issue
Block a user