Fix pylint warnings: logging, globals, exceptions, imports, duplicates
Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/576427d4-4f6a-46d2-b75f-6862ecbcf526 Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -248,9 +248,13 @@ async def get_domain_dns_records(domain_id: str = Path(..., title="The domain ID
|
||||
|
||||
# For Milestone 1, return mock DNS record data
|
||||
# In a future milestone, this will be replaced with actual DNS lookups
|
||||
mock_dmarc_record = (
|
||||
"v=DMARC1; p=none; rua=mailto:dmarc@example.com;"
|
||||
" ruf=mailto:forensic@example.com; pct=100"
|
||||
)
|
||||
return DNSRecordResponse(
|
||||
dmarc=True,
|
||||
dmarcRecord="v=DMARC1; p=none; rua=mailto:dmarc@example.com; ruf=mailto:forensic@example.com; pct=100",
|
||||
dmarcRecord=mock_dmarc_record,
|
||||
spf=True,
|
||||
spfRecord="v=spf1 include:_spf.google.com include:spf.protection.outlook.com -all",
|
||||
dkim=True,
|
||||
@@ -299,7 +303,7 @@ async def get_domain_reports(
|
||||
date = datetime.now() - timedelta(days=i)
|
||||
date_str = date.strftime("%Y-%m-%d")
|
||||
|
||||
# TODO: Replace with actual historical data in future milestone
|
||||
# TODO: Replace with actual historical data in future milestone # pylint: disable=fixme
|
||||
# For now, generate mock data with variation for demonstration purposes
|
||||
compliance_rate = random.uniform(80, 100) # nosec B311 - Mock data only
|
||||
|
||||
|
||||
@@ -47,16 +47,16 @@ def _validate_mime_type(file_content: bytes) -> None:
|
||||
try:
|
||||
mime_type = magic.from_buffer(file_content, mime=True)
|
||||
if mime_type not in ALLOWED_MIME_TYPES:
|
||||
logger.warning(f"Rejected file with MIME type: {mime_type}")
|
||||
logger.warning("Rejected file with MIME type: %s", mime_type)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Invalid file type. File must be XML, ZIP, or GZIP format.",
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
# If magic fails, log but continue (fallback to extension check)
|
||||
logger.warning(f"MIME type detection failed: {str(e)}")
|
||||
logger.warning("MIME type detection failed: %s", str(e))
|
||||
|
||||
|
||||
def _validate_upload_file(file: UploadFile, file_content: bytes) -> None:
|
||||
@@ -89,15 +89,14 @@ def _handle_upload_value_error(filename: str, error_message: str) -> None:
|
||||
|
||||
Always raises — never returns.
|
||||
"""
|
||||
logger.error(f"ValueError processing report {filename}: {error_message}")
|
||||
logger.error("ValueError processing report %s: %s", filename, error_message)
|
||||
if "too large" in error_message.lower():
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="File too large"
|
||||
)
|
||||
elif "zip bomb" in error_message.lower():
|
||||
if "zip bomb" in error_message.lower():
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid archive file")
|
||||
else:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid report format")
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid report format")
|
||||
|
||||
|
||||
class UploadResponse(BaseModel):
|
||||
@@ -196,13 +195,13 @@ async def upload_report(file: UploadFile = File(...)):
|
||||
except ValueError as e:
|
||||
# Security: Sanitize error messages from parser
|
||||
_handle_upload_value_error(file.filename, str(e))
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
# Security: Don't expose internal errors to client
|
||||
logger.error(f"Unexpected error processing report {file.filename}: {str(e)}")
|
||||
logger.error("Unexpected error processing report %s: %s", file.filename, str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Error processing report. Please contact support if this persists.",
|
||||
)
|
||||
) from e
|
||||
|
||||
|
||||
@router.get("/domains", response_model=List[str])
|
||||
@@ -300,10 +299,11 @@ async def get_domain_reports_paginated(
|
||||
|
||||
if sort_field == "total_count":
|
||||
all_reports.sort(
|
||||
key=lambda r: r.get("summary", {}).get("total_count", 0), reverse=(sort_order == "desc")
|
||||
key=lambda r: r.get("summary", {}).get("total_count", 0),
|
||||
reverse=sort_order == "desc",
|
||||
)
|
||||
else:
|
||||
all_reports.sort(key=lambda r: r.get(sort_field, ""), reverse=(sort_order == "desc"))
|
||||
all_reports.sort(key=lambda r: r.get(sort_field, ""), reverse=sort_order == "desc")
|
||||
|
||||
# Apply pagination
|
||||
total = len(all_reports)
|
||||
|
||||
Reference in New Issue
Block a user