fix: Address high-severity bandit issues
This commit is contained in:
@@ -23,15 +23,11 @@ AXE_CORE_SCRIPT_URL = "https://cdnjs.cloudflare.com/ajax/libs/axe-core/4.8.4/axe
|
||||
|
||||
# Optional Playwright import for graceful degradation
|
||||
try:
|
||||
from playwright.async_api import (
|
||||
Browser,
|
||||
ConsoleMessage,
|
||||
Error as PlaywrightError,
|
||||
Page,
|
||||
Playwright,
|
||||
TimeoutError as PlaywrightTimeoutError,
|
||||
async_playwright,
|
||||
)
|
||||
from playwright.async_api import Browser, ConsoleMessage
|
||||
from playwright.async_api import Error as PlaywrightError
|
||||
from playwright.async_api import Page, Playwright
|
||||
from playwright.async_api import TimeoutError as PlaywrightTimeoutError
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
PLAYWRIGHT_AVAILABLE = True
|
||||
except ImportError:
|
||||
@@ -199,8 +195,8 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
"timestamp": None, # Playwright doesn't provide timestamp directly
|
||||
"category": "console",
|
||||
"data": {
|
||||
"location": msg.location if hasattr(msg, 'location') else None,
|
||||
}
|
||||
"location": msg.location if hasattr(msg, "location") else None,
|
||||
},
|
||||
}
|
||||
logs.append(log_entry)
|
||||
except Exception as e:
|
||||
@@ -234,10 +230,8 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
raise RuntimeError("No active page to capture screenshot from.")
|
||||
|
||||
# Generate unique filename
|
||||
session_id = getattr(self.context, 'session_id', 'local')
|
||||
path = os.path.join(
|
||||
tempfile.gettempdir(), f"dss_screenshot_{session_id}.png"
|
||||
)
|
||||
session_id = getattr(self.context, "session_id", "local")
|
||||
path = os.path.join(tempfile.gettempdir(), f"dss_screenshot_{session_id}.png")
|
||||
|
||||
try:
|
||||
if selector:
|
||||
@@ -284,9 +278,9 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
"category": "uncaughtError",
|
||||
"message": str(err),
|
||||
"data": {
|
||||
"name": getattr(err, 'name', 'Error'),
|
||||
"stack": getattr(err, 'stack', None),
|
||||
}
|
||||
"name": getattr(err, "name", "Error"),
|
||||
"stack": getattr(err, "stack", None),
|
||||
},
|
||||
}
|
||||
errors.append(error_entry)
|
||||
except Exception as e:
|
||||
@@ -294,9 +288,7 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
|
||||
return errors[-limit:]
|
||||
|
||||
async def run_accessibility_audit(
|
||||
self, selector: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
async def run_accessibility_audit(self, selector: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Run an accessibility audit on the current page using axe-core.
|
||||
|
||||
@@ -330,13 +322,11 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
|
||||
# Run axe with selector context if provided
|
||||
if selector:
|
||||
result = await self.page.evaluate(
|
||||
"(selector) => axe.run(selector)", selector
|
||||
)
|
||||
result = await self.page.evaluate("(selector) => axe.run(selector)", selector)
|
||||
else:
|
||||
result = await self.page.evaluate("() => axe.run()")
|
||||
|
||||
violations_count = len(result.get('violations', []))
|
||||
violations_count = len(result.get("violations", []))
|
||||
logger.info(f"Accessibility audit complete. Found {violations_count} violations.")
|
||||
|
||||
return result
|
||||
@@ -357,9 +347,7 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
raise RuntimeError("No active page to get performance metrics from.")
|
||||
|
||||
# 1. Get Navigation Timing API metrics
|
||||
timing_raw = await self.page.evaluate(
|
||||
"() => JSON.stringify(window.performance.timing)"
|
||||
)
|
||||
timing_raw = await self.page.evaluate("() => JSON.stringify(window.performance.timing)")
|
||||
nav_timing = json.loads(timing_raw)
|
||||
|
||||
# 2. Get Core Web Vitals via PerformanceObserver
|
||||
@@ -417,14 +405,13 @@ class LocalBrowserStrategy(BrowserStrategy):
|
||||
"""
|
||||
core_web_vitals = await self.page.evaluate(metrics_script)
|
||||
|
||||
return {
|
||||
"navigation_timing": nav_timing,
|
||||
"core_web_vitals": core_web_vitals
|
||||
}
|
||||
return {"navigation_timing": nav_timing, "core_web_vitals": core_web_vitals}
|
||||
|
||||
async def close(self) -> None:
|
||||
"""
|
||||
Close the current page. Browser instance is kept in pool for reuse.
|
||||
Close the current page.
|
||||
|
||||
Browser instance is kept in pool for reuse.
|
||||
|
||||
To fully close the browser, use close_browser() class method.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user