harden calculation contracts and local API boundaries

This commit is contained in:
732642856
2026-07-11 23:24:54 +08:00
parent 4a268c75f2
commit a8f6a57ccc
17 changed files with 1075 additions and 157 deletions
+21 -2
View File
@@ -29,6 +29,7 @@ import sys
import re
import glob
import argparse
from urllib.parse import urlparse
try:
import markdown
@@ -352,6 +353,13 @@ def build_section(num, title, md_text):
</div>"""
def is_allowed_report_resource_url(url, *, report_url):
if url == report_url:
return True
parsed = urlparse(url)
return parsed.scheme in {'data', 'about', 'blob'}
def _html_to_pdf(html_path, pdf_path):
"""Convert HTML to PDF using Playwright headless Chromium."""
try:
@@ -364,14 +372,25 @@ def _html_to_pdf(html_path, pdf_path):
print(" Launching headless Chromium...")
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto(f"file://{os.path.abspath(html_path)}", wait_until="networkidle")
context = browser.new_context(java_script_enabled=False)
page = context.new_page()
report_url = f"file://{os.path.abspath(html_path)}"
page.route(
"**/*",
lambda route: (
route.continue_()
if is_allowed_report_resource_url(route.request.url, report_url=report_url)
else route.abort()
),
)
page.goto(report_url, wait_until="networkidle")
page.pdf(
path=pdf_path,
format="A4",
print_background=True,
margin={"top": "22mm", "bottom": "24mm", "left": "20mm", "right": "20mm"},
)
context.close()
browser.close()
size_kb = os.path.getsize(pdf_path) / 1024