|
@@ -4,6 +4,7 @@
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import argparse
|
|
import argparse
|
|
|
|
|
+import concurrent.futures
|
|
|
import hashlib
|
|
import hashlib
|
|
|
import json
|
|
import json
|
|
|
import os
|
|
import os
|
|
@@ -302,9 +303,8 @@ def fetch_live(url: str, timeout: int) -> tuple[int, str, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
def scan_live(manifest: dict[str, Any], timeout: int) -> dict[str, Any]:
|
|
def scan_live(manifest: dict[str, Any], timeout: int) -> dict[str, Any]:
|
|
|
- records: list[dict[str, Any]] = []
|
|
|
|
|
- violations: list[dict[str, Any]] = []
|
|
|
|
|
- for site in manifest.get("websites", []):
|
|
|
|
|
|
|
+ def scan_site(site: dict[str, Any]) -> tuple[dict[str, Any], list[dict[str, Any]]]:
|
|
|
|
|
+ site_violations: list[dict[str, Any]] = []
|
|
|
record: dict[str, Any] = {"name": site["name"], "url": site["url"]}
|
|
record: dict[str, Any] = {"name": site["name"], "url": site["url"]}
|
|
|
try:
|
|
try:
|
|
|
status, final_url, body = fetch_live(site["url"], timeout)
|
|
status, final_url, body = fetch_live(site["url"], timeout)
|
|
@@ -319,59 +319,78 @@ def scan_live(manifest: dict[str, Any], timeout: int) -> dict[str, Any]:
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
if bad_domains:
|
|
if bad_domains:
|
|
|
- violations.append({"type": "banned-domain-live", "site": site["name"], "domains": bad_domains})
|
|
|
|
|
|
|
+ site_violations.append(
|
|
|
|
|
+ {"type": "banned-domain-live", "site": site["name"], "domains": bad_domains}
|
|
|
|
|
+ )
|
|
|
for script in scripts:
|
|
for script in scripts:
|
|
|
if not script["isExternal"]:
|
|
if not script["isExternal"]:
|
|
|
continue
|
|
continue
|
|
|
exception = exception_for(script["host"], script["path"], manifest)
|
|
exception = exception_for(script["host"], script["path"], manifest)
|
|
|
if exception is None:
|
|
if exception is None:
|
|
|
- violations.append(
|
|
|
|
|
|
|
+ site_violations.append(
|
|
|
{
|
|
{
|
|
|
"type": "unapproved-external-script-live",
|
|
"type": "unapproved-external-script-live",
|
|
|
"site": site["name"],
|
|
"site": site["name"],
|
|
|
"url": script["url"],
|
|
"url": script["url"],
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
- same_origin_assets: list[dict[str, Any]] = []
|
|
|
|
|
seen_scripts: set[str] = set()
|
|
seen_scripts: set[str] = set()
|
|
|
|
|
+ same_origin_urls: list[str] = []
|
|
|
for script in scripts:
|
|
for script in scripts:
|
|
|
if script["isExternal"] or script["url"] in seen_scripts:
|
|
if script["isExternal"] or script["url"] in seen_scripts:
|
|
|
continue
|
|
continue
|
|
|
seen_scripts.add(script["url"])
|
|
seen_scripts.add(script["url"])
|
|
|
|
|
+ same_origin_urls.append(script["url"])
|
|
|
|
|
+
|
|
|
|
|
+ def fetch_script(script_url: str) -> dict[str, Any]:
|
|
|
try:
|
|
try:
|
|
|
- _, _, script_body = fetch_live(script["url"], timeout)
|
|
|
|
|
|
|
+ _, _, script_body = fetch_live(script_url, timeout)
|
|
|
except Exception as exc:
|
|
except Exception as exc:
|
|
|
- same_origin_assets.append(
|
|
|
|
|
- {
|
|
|
|
|
- "url": script["url"],
|
|
|
|
|
- "status": 0,
|
|
|
|
|
- "error": f"{type(exc).__name__}: {exc}",
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
- continue
|
|
|
|
|
|
|
+ return {
|
|
|
|
|
+ "url": script_url,
|
|
|
|
|
+ "status": 0,
|
|
|
|
|
+ "error": f"{type(exc).__name__}: {exc}",
|
|
|
|
|
+ }
|
|
|
script_bad_domains = [
|
|
script_bad_domains = [
|
|
|
domain for domain in manifest.get("bannedDomains", []) if domain.lower() in script_body.lower()
|
|
domain for domain in manifest.get("bannedDomains", []) if domain.lower() in script_body.lower()
|
|
|
]
|
|
]
|
|
|
- asset = {
|
|
|
|
|
- "url": script["url"],
|
|
|
|
|
|
|
+ return {
|
|
|
|
|
+ "url": script_url,
|
|
|
"status": 200,
|
|
"status": 200,
|
|
|
"sha256": hashlib.sha256(script_body.encode("utf-8")).hexdigest(),
|
|
"sha256": hashlib.sha256(script_body.encode("utf-8")).hexdigest(),
|
|
|
"bannedDomains": script_bad_domains,
|
|
"bannedDomains": script_bad_domains,
|
|
|
}
|
|
}
|
|
|
- same_origin_assets.append(asset)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ same_origin_assets: list[dict[str, Any]] = []
|
|
|
|
|
+ if same_origin_urls:
|
|
|
|
|
+ with concurrent.futures.ThreadPoolExecutor(max_workers=min(6, len(same_origin_urls))) as executor:
|
|
|
|
|
+ same_origin_assets = list(executor.map(fetch_script, same_origin_urls))
|
|
|
|
|
+ for asset in same_origin_assets:
|
|
|
|
|
+ script_bad_domains = asset.get("bannedDomains", [])
|
|
|
if script_bad_domains:
|
|
if script_bad_domains:
|
|
|
- violations.append(
|
|
|
|
|
|
|
+ site_violations.append(
|
|
|
{
|
|
{
|
|
|
"type": "banned-domain-live-script",
|
|
"type": "banned-domain-live-script",
|
|
|
"site": site["name"],
|
|
"site": site["name"],
|
|
|
- "url": script["url"],
|
|
|
|
|
|
|
+ "url": asset["url"],
|
|
|
"domains": script_bad_domains,
|
|
"domains": script_bad_domains,
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
record["sameOriginScripts"] = same_origin_assets
|
|
record["sameOriginScripts"] = same_origin_assets
|
|
|
except Exception as exc: # network failures are reported, not hidden
|
|
except Exception as exc: # network failures are reported, not hidden
|
|
|
record.update({"status": 0, "error": f"{type(exc).__name__}: {exc}"})
|
|
record.update({"status": 0, "error": f"{type(exc).__name__}: {exc}"})
|
|
|
|
|
+ return record, site_violations
|
|
|
|
|
+
|
|
|
|
|
+ sites = manifest.get("websites", [])
|
|
|
|
|
+ if not sites:
|
|
|
|
|
+ return {"records": [], "violations": []}
|
|
|
|
|
+ with concurrent.futures.ThreadPoolExecutor(max_workers=min(6, len(sites))) as executor:
|
|
|
|
|
+ results = list(executor.map(scan_site, sites))
|
|
|
|
|
+ records: list[dict[str, Any]] = []
|
|
|
|
|
+ violations: list[dict[str, Any]] = []
|
|
|
|
|
+ for record, site_violations in results:
|
|
|
records.append(record)
|
|
records.append(record)
|
|
|
|
|
+ violations.extend(site_violations)
|
|
|
return {"records": records, "violations": violations}
|
|
return {"records": records, "violations": violations}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -414,7 +433,11 @@ def main() -> int:
|
|
|
warnings = static["warnings"] + db["warnings"]
|
|
warnings = static["warnings"] + db["warnings"]
|
|
|
report = {
|
|
report = {
|
|
|
"generatedAt": datetime.now(timezone.utc).isoformat(),
|
|
"generatedAt": datetime.now(timezone.utc).isoformat(),
|
|
|
- "manifest": args.manifest.relative_to(ROOT).as_posix(),
|
|
|
|
|
|
|
+ "manifest": (
|
|
|
|
|
+ args.manifest.relative_to(ROOT).as_posix()
|
|
|
|
|
+ if args.manifest.is_relative_to(ROOT)
|
|
|
|
|
+ else str(args.manifest)
|
|
|
|
|
+ ),
|
|
|
"summary": {
|
|
"summary": {
|
|
|
"websites": len(manifest.get("websites", [])),
|
|
"websites": len(manifest.get("websites", [])),
|
|
|
"filesWithSignals": len(static["files"]),
|
|
"filesWithSignals": len(static["files"]),
|