test: cover snapshot integrity, disconnects and timeout isolation

This commit is contained in:
cflmy committed 2026-11-24 14:41:40 +08:00
1 parent 5740362880
commit bbdfec2174
1 file changed
+37 -4
+37 -4
View File
@@ -9,8 +9,11 @@ import os
from pathlib import Path, PurePosixPath from pathlib import Path, PurePosixPath
import re import re
import shutil import shutil
import socket
import struct
import subprocess import subprocess
import tempfile import tempfile
import time
import urllib.error import urllib.error
import urllib.request import urllib.request
import zipfile import zipfile
@@ -179,6 +182,30 @@ def check(root):
assert headers.get_content_type() == "application/zip" and "attachment" in headers["Content-Disposition"] assert headers.get_content_type() == "application/zip" and "attachment" in headers["Content-Disposition"]
assert headers["Cache-Control"] == "no-store" and headers["X-Content-Type-Options"] == "nosniff" assert headers["Cache-Control"] == "no-store" and headers["X-Content-Type-Options"] == "nosniff"
verify_zip(payload, os.environ.get("SNAPSHOT_ZIP"), headers["X-Marqdo-Manifest-SHA256"]) verify_zip(payload, os.environ.get("SNAPSHOT_ZIP"), headers["X-Marqdo-Manifest-SHA256"])
manifest_digest = headers["X-Marqdo-Manifest-SHA256"]
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
manifest = json.loads(archive.read("manifest.json"))
assert sha(archive.read("manifest.json") + b" ") != manifest_digest
for entry in manifest["files"] + manifest["assets"]:
assert sha(archive.read(entry["path"]) + b" ") != entry["sha256"]
post(browser, base, "/api/exports", token, {**selection, "unknown": "x" * 8192}, 429)
auth = next(handler.cookiejar for handler in browser.handlers if hasattr(handler, "cookiejar"))
cookies = "; ".join(f"{cookie.name}={cookie.value}" for cookie in auth)
body = json.dumps(selection).encode()
host, port = base.removeprefix("http://").rsplit(":", 1)
request = (f"POST /api/exports HTTP/1.1\r\nHost: {host}:{port}\r\n"
f"Origin: {base}\r\nX-View-CSRF: {token}\r\nCookie: {cookies}\r\n"
f"Content-Type: application/json\r\nContent-Length: {len(body)}\r\n\r\n").encode()
with socket.create_connection((host, int(port)), timeout=10) as abandoned:
abandoned.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4096)
abandoned.sendall(request + body)
assert abandoned.recv(16).startswith(b"HTTP/1.1 200")
abandoned.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
deadline = time.monotonic() + 5
while list(scratch.iterdir()) and time.monotonic() < deadline:
time.sleep(.01)
assert not list(scratch.iterdir()), list(scratch.iterdir())
subprocess.run([*args, "export", str(project), "--out", str(root / "pure")], check=True, env=env) subprocess.run([*args, "export", str(project), "--out", str(root / "pure")], check=True, env=env)
assert "本次执行快照" not in (root / "pure/index.html").read_text() assert "本次执行快照" not in (root / "pure/index.html").read_text()
assert count.read_text().splitlines() == ["call"] assert count.read_text().splitlines() == ["call"]
@@ -238,7 +265,10 @@ def check(root):
html = archive.read("index.html").decode() html = archive.read("index.html").decode()
assert "<script>" not in html and "&lt;script&gt;bad()" in html assert "<script>" not in html and "&lt;script&gt;bad()" in html
assert json.loads(archive.read("manifest.json"))["execution"]["trace_status"] == "unavailable" assert json.loads(archive.read("manifest.json"))["execution"]["trace_status"] == "unavailable"
first, headers = post(browser, base, "/api/exports", token, selection)
verify_zip(first, None, headers["X-Marqdo-Manifest-SHA256"])
print("active/cancelled export denied, verified failure gated, stdout escaped, missing Trace separate") print("active/cancelled export denied, verified failure gated, stdout escaped, missing Trace separate")
print("selected IDs remain isolated, tampering detected, oversized request denied, aborted ZIP cleaned")
calls = count.read_text().splitlines() calls = count.read_text().splitlines()
assert calls == ["call"] * 4, calls assert calls == ["call"] * 4, calls
print("run/call count matches four explicit submissions:", calls) print("run/call count matches four explicit submissions:", calls)
@@ -246,18 +276,21 @@ def check(root):
server.terminate() server.terminate()
server.wait(timeout=5) server.wait(timeout=5)
with subprocess.Popen( with subprocess.Popen(
[*args, str(project), "--port", "0", "--no-open"], [*args, str(project), "--port", "0", "--no-open", "--run-timeout", "1"],
stderr=subprocess.PIPE, text=True, env=env, stderr=subprocess.PIPE, text=True, env=env,
) as restarted: ) as restarted:
try: try:
base = restarted.stderr.readline().split()[-1].rstrip("/") base = restarted.stderr.readline().split()[-1].rstrip("/")
fresh, fresh_token, _, _ = session(base) browser, token, digest, _ = session(base)
post(fresh, base, "/api/exports", fresh_token, selection, 404) post(browser, base, "/api/exports", token, selection, 404)
timed_id, timed = observe(submit("scan", "timeout"))
assert timed["state"] == "timed_out", timed
post(browser, base, "/api/exports", token, {**selection, "run_id": timed_id}, 409)
finally: finally:
restarted.terminate() restarted.terminate()
restarted.wait(timeout=5) restarted.wait(timeout=5)
assert not list(scratch.iterdir()), list(scratch.iterdir()) assert not list(scratch.iterdir()), list(scratch.iterdir())
print("restarted View cannot recover the previous job; private scratch directories are empty") print("restarted View cannot recover the previous job; timed-out execution is not exportable; private scratch directories are empty")
if __name__ == "__main__": if __name__ == "__main__":