Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/answerproof/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from __future__ import annotations

import html
from typing import Any

try:
Expand Down Expand Up @@ -77,13 +78,13 @@ def _render_page(receipt: Receipt, verdict) -> str:
color = "#137333" if verdict.valid else "#c5221f"
status = "VALID" if verdict.valid else "INVALID"
rows = "".join(
f"<tr><td>{'ok' if c.passed else 'FAIL'}</td><td>{c.name}</td>"
f"<td>{c.detail or ''}</td></tr>"
f"<tr><td>{'ok' if c.passed else 'FAIL'}</td><td>{html.escape(str(c.name))}</td>"
f"<td>{html.escape(str(c.detail or ''))}</td></tr>"
for c in verdict.checks
)
return f"""<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<title>answerproof receipt {p.receipt_id}</title>
<title>answerproof receipt {html.escape(p.receipt_id)}</title>
<style>
body{{font-family:system-ui,sans-serif;max-width:760px;margin:2rem auto;padding:0 1rem}}
.badge{{display:inline-block;padding:.3rem .8rem;border-radius:6px;color:#fff}}
Expand All @@ -94,12 +95,12 @@ def _render_page(receipt: Receipt, verdict) -> str:
</style></head><body>
<h1>answerproof receipt</h1>
<p class="badge">{status}</p>
<p><strong>Receipt:</strong> <code>{p.receipt_id}</code></p>
<p><strong>Query:</strong> {p.query}</p>
<p><strong>Answer:</strong> {p.answer}</p>
<p><strong>Signer:</strong> <code>{receipt.signature.public_key}</code></p>
<p><strong>Merkle root:</strong> <code>{p.merkle_root}</code></p>
<p><strong>Grounding score:</strong> {p.grounding.grounding_score}</p>
<p><strong>Receipt:</strong> <code>{html.escape(p.receipt_id)}</code></p>
<p><strong>Query:</strong> {html.escape(p.query)}</p>
<p><strong>Answer:</strong> {html.escape(p.answer)}</p>
<p><strong>Signer:</strong> <code>{html.escape(receipt.signature.public_key)}</code></p>
<p><strong>Merkle root:</strong> <code>{html.escape(p.merkle_root)}</code></p>
<p><strong>Grounding score:</strong> {html.escape(str(p.grounding.grounding_score))}</p>
<table><thead><tr><th>result</th><th>check</th><th>detail</th></tr></thead>
<tbody>{rows}</tbody></table>
</body></html>"""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient # noqa: E402

from answerproof import ReceiptBuilder, SigningKey # noqa: E402
from answerproof.api import create_app # noqa: E402


Expand Down Expand Up @@ -45,3 +46,29 @@ def test_verify_page_renders_html(client, receipt, sources):
assert r.status_code == 200
assert "text/html" in r.headers["content-type"]
assert "VALID" in r.text


def test_verify_page_escapes_receipt_fields(client):
query = "<script>alert(1)</script>"
answer = "<img src=x onerror=alert(2)>"
receipt = (
ReceiptBuilder(SigningKey.generate())
.set_query(query)
.set_answer(answer)
.add_source("s1", content="safe source content")
.finalize(receipt_id="xss-test")
)

response = client.post(
"/verify/page",
json={
"receipt": json.loads(receipt.to_json()),
"source_contents": {"s1": "safe source content"},
},
)

assert response.status_code == 200
assert "<script>" not in response.text
assert "<img src=x" not in response.text
assert "&lt;script&gt;alert(1)&lt;/script&gt;" in response.text
assert "&lt;img src=x onerror=alert(2)&gt;" in response.text
Loading