Skip to content

Commit

Permalink
Correctly escape output of ?_trace, refs #1360
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 5, 2021
1 parent af5a7f1 commit 26fc539
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion datasette/tracer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from contextlib import contextmanager
from markupsafe import escape
import time
import json
import traceback
Expand Down Expand Up @@ -123,7 +124,7 @@ async def wrapped_send(message):
except IndexError:
content_type = ""
if "text/html" in content_type and b"</body>" in accumulated_body:
extra = json.dumps(trace_info, indent=2)
extra = escape(json.dumps(trace_info, indent=2))
extra_html = f"<pre>{extra}</pre></body>".encode("utf8")
accumulated_body = accumulated_body.replace(b"</body>", extra_html)
elif "json" in content_type and accumulated_body.startswith(b"{"):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1612,3 +1612,9 @@ def test_navigation_menu_links(
assert (
details.find("a", {"href": link}) is None
), f"{link} found but should not have been in nav menu"


def test_trace_correctly_escaped(app_client):
response = app_client.get("/fixtures?sql=select+'<h1>Hello'&_trace=1")
assert "select '<h1>Hello" not in response.text
assert "select &#39;&lt;h1&gt;Hello" in response.text

0 comments on commit 26fc539

Please # to comment.