Skip to content

Commit 91b69b8

Browse files
JuliaEdomclaude
andcommitted
fix(security): close AST-guard SELECT-INTO gap, ban session-write funcs, escape Sentence HTML
Guard was a self-standing defence layer but three vectors slipped past it: - `SELECT ... INTO t` parses as a top-level Select (passes _check_select_only) yet CREATEs a table on Postgres — now denied via the `into` arg. - pg_sleep_for / pg_sleep_until (DoS) and set_config / setval (disable the read-only GUC or bump a sequence from inside one SELECT) added to the ban list. - Streamlit Sentence renderer interpolated model/parser text into unsafe_allow_html markup without escaping — html.escape() closes the XSS class. Adversarial tests pin every new vector; retire the documented no-op guard seam. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 30373dd commit 91b69b8

5 files changed

Lines changed: 167 additions & 8 deletions

File tree

‎app/components/output.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import html
6+
57
import pandas as pd
68
import plotly.express as px
79
import streamlit as st
@@ -63,10 +65,12 @@ def render_output(output: OutputFormat | None, *, caption: str) -> None:
6365
if isinstance(output, Scalar):
6466
st.metric(scalar_metric_label(output.column), str(output.value))
6567
elif isinstance(output, Sentence):
68+
# Sentence.text can carry model-generated SQL / parser messages, so it is
69+
# untrusted; escape before it lands inside unsafe_allow_html markup.
6670
st.markdown(
6771
f'<div style="font-size:1.2rem; line-height:1.5; '
6872
f"color:var(--text); font-weight:500; "
69-
f'margin:0.4rem 0 0.6rem;">{output.text}</div>',
73+
f'margin:0.4rem 0 0.6rem;">{html.escape(output.text)}</div>',
7074
unsafe_allow_html=True,
7175
)
7276
if output.fields:

‎src/nl_sql/execution/guards.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@
2929
"postgresql": "postgres",
3030
}
3131

32-
# Functions banned outright. Mostly resource-abuse vectors.
32+
# Functions banned outright. Resource-abuse vectors (pg_sleep* / file reads /
33+
# dblink) plus session-state writes that a read-only guard must stop on its own:
34+
# set_config / setval mutate GUCs or sequences within a session, so they would
35+
# slip past a role that only blocks default_transaction_read_only.
3336
BANNED_FUNCTIONS: Final[frozenset[str]] = frozenset(
3437
{
3538
"pg_sleep",
39+
"pg_sleep_for",
40+
"pg_sleep_until",
3641
"pg_read_file",
3742
"pg_read_binary_file",
3843
"pg_ls_dir",
@@ -41,6 +46,8 @@
4146
"dblink",
4247
"load_extension",
4348
"readfile",
49+
"set_config",
50+
"setval",
4451
}
4552
)
4653

@@ -99,11 +106,11 @@ def validate_sql(sql: str, dialect: Dialect = "sqlite") -> ValidationReport:
99106
report.parsed = parsed
100107

101108
_check_select_only(parsed, report)
109+
_check_no_select_into(parsed, report)
102110
_check_no_dml_anywhere(parsed, report)
103111
_check_function_allowlist(parsed, report)
104112
_check_generate_series_bounds(parsed, report)
105113
_check_table_denylist(parsed, report)
106-
_check_no_attach_or_pragma(parsed, report)
107114

108115
return report
109116

@@ -207,8 +214,11 @@ def _check_table_denylist(parsed: exp.Expression, report: ValidationReport) -> N
207214
report.add("denied_table", f"table {name!r} is on the denylist")
208215

209216

210-
def _check_no_attach_or_pragma(parsed: exp.Expression, report: ValidationReport) -> None:
211-
# Already covered by _check_no_dml_anywhere via exp.Attach / exp.Pragma nodes;
212-
# kept as a no-op seam so future dialects (e.g. DuckDB ATTACH variants) can plug
213-
# additional textual heuristics without adding a new top-level check.
214-
return
217+
def _check_no_select_into(parsed: exp.Expression, report: ValidationReport) -> None:
218+
# `SELECT ... INTO new_table FROM ...` parses as a top-level exp.Select, so
219+
# _check_select_only lets it through — but on Postgres it CREATEs a table.
220+
# The write-creating clause lives in the `into` arg, not the node type.
221+
for node in parsed.walk():
222+
if isinstance(node, exp.Select) and node.args.get("into") is not None:
223+
report.add("select_into", "SELECT ... INTO creates a table and is denied")
224+
return

‎tests/app/__init__.py‎

Whitespace-only changes.

‎tests/app/test_output_escaping.py‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""Security test: the Sentence renderer must escape untrusted text.
2+
3+
`app/components/output.py` renders `Sentence.text` inside `st.markdown(...,
4+
unsafe_allow_html=True)`. Sentence text can carry model-generated SQL or parser
5+
error strings, so a `<script>`/`<img onerror>` payload must be HTML-escaped
6+
before it reaches the markup — otherwise it is a stored-XSS vector in the demo.
7+
8+
The app package imports `streamlit`, `i18n`, and `theme` at module load; none are
9+
available (or wanted) in the unit-test process, so we stub them in sys.modules
10+
and put `app/` on the path before importing the component.
11+
"""
12+
13+
from __future__ import annotations
14+
15+
import sys
16+
import types
17+
from pathlib import Path
18+
from typing import Any
19+
20+
import pytest
21+
22+
APP_DIR = Path(__file__).resolve().parents[2] / "app"
23+
24+
25+
class _RecordingStreamlit(types.ModuleType):
26+
"""Minimal `streamlit` stand-in that records markdown payloads."""
27+
28+
def __init__(self) -> None:
29+
super().__init__("streamlit")
30+
self.markdown_calls: list[str] = []
31+
32+
def markdown(self, body: str, **_: Any) -> None:
33+
self.markdown_calls.append(body)
34+
35+
def json(self, *_: Any, **__: Any) -> None:
36+
pass
37+
38+
def caption(self, *_: Any, **__: Any) -> None:
39+
pass
40+
41+
def metric(self, *_: Any, **__: Any) -> None:
42+
pass
43+
44+
def warning(self, *_: Any, **__: Any) -> None:
45+
pass
46+
47+
48+
@pytest.fixture
49+
def output_module(monkeypatch: pytest.MonkeyPatch) -> Any:
50+
fake_st = _RecordingStreamlit()
51+
fake_i18n = types.ModuleType("i18n")
52+
fake_i18n.t = lambda key: key # type: ignore[attr-defined]
53+
fake_theme = types.ModuleType("theme")
54+
fake_theme.style_fig = lambda fig: fig # type: ignore[attr-defined]
55+
56+
monkeypatch.setitem(sys.modules, "streamlit", fake_st)
57+
monkeypatch.setitem(sys.modules, "i18n", fake_i18n)
58+
monkeypatch.setitem(sys.modules, "theme", fake_theme)
59+
monkeypatch.syspath_prepend(str(APP_DIR))
60+
# Drop the whole `components` namespace so the module re-imports and rebinds
61+
# the freshly stubbed `streamlit` (a cached submodule would keep the old st).
62+
for name in [m for m in sys.modules if m == "components" or m.startswith("components.")]:
63+
monkeypatch.delitem(sys.modules, name, raising=False)
64+
65+
from components import output as output_mod # type: ignore[import-not-found]
66+
67+
return output_mod, fake_st
68+
69+
70+
def test_sentence_html_is_escaped(output_module: Any) -> None:
71+
output_mod, fake_st = output_module
72+
from nl_sql.render.formats import Sentence
73+
74+
payload = "<script>alert('xss')</script> & <img src=x onerror=alert(1)>"
75+
output_mod.render_output(Sentence(text=payload), caption="")
76+
77+
rendered = "\n".join(fake_st.markdown_calls)
78+
assert "<script>" not in rendered
79+
assert "onerror=alert(1)>" not in rendered
80+
# The escaped form must be present instead.
81+
assert "&lt;script&gt;" in rendered
82+
assert "&amp;" in rendered
83+
84+
85+
def test_benign_sentence_text_survives(output_module: Any) -> None:
86+
output_mod, fake_st = output_module
87+
from nl_sql.render.formats import Sentence
88+
89+
output_mod.render_output(Sentence(text="There are 4 albums."), caption="")
90+
91+
rendered = "\n".join(fake_st.markdown_calls)
92+
assert "There are 4 albums." in rendered

‎tests/test_execution_guards.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,58 @@ def test_load_extension_is_rejected() -> None:
112112
assert not report.ok
113113

114114

115+
def test_pg_sleep_for_is_rejected() -> None:
116+
report = validate_sql("SELECT pg_sleep_for('5 minutes')", dialect="postgresql")
117+
assert not report.ok
118+
assert any(v.code == "banned_function" for v in report.violations)
119+
120+
121+
def test_pg_sleep_until_is_rejected() -> None:
122+
report = validate_sql("SELECT pg_sleep_until('tomorrow')", dialect="postgresql")
123+
assert not report.ok
124+
assert any(v.code == "banned_function" for v in report.violations)
125+
126+
127+
def test_set_config_is_rejected() -> None:
128+
# set_config('default_transaction_read_only','off',false) would disable the
129+
# session read-only guard from inside a single SELECT — must be denied here.
130+
report = validate_sql(
131+
"SELECT set_config('default_transaction_read_only', 'off', false)",
132+
dialect="postgresql",
133+
)
134+
assert not report.ok
135+
assert any(v.code == "banned_function" for v in report.violations)
136+
137+
138+
def test_setval_is_rejected() -> None:
139+
report = validate_sql("SELECT setval('some_seq', 1)", dialect="postgresql")
140+
assert not report.ok
141+
assert any(v.code == "banned_function" for v in report.violations)
142+
143+
144+
# ---------- SELECT ... INTO (table creation) ----------
145+
146+
147+
def test_select_into_is_rejected_postgres() -> None:
148+
# Parses as a top-level Select but CREATEs a table on Postgres.
149+
report = validate_sql("SELECT * INTO new_table FROM Artists", dialect="postgresql")
150+
assert not report.ok
151+
assert any(v.code == "select_into" for v in report.violations)
152+
153+
154+
def test_select_into_is_rejected_sqlite() -> None:
155+
report = validate_sql("SELECT id INTO backup FROM Artists", dialect="sqlite")
156+
assert not report.ok
157+
assert any(v.code == "select_into" for v in report.violations)
158+
159+
160+
def test_select_into_in_subquery_is_rejected() -> None:
161+
sql = "SELECT * FROM (SELECT * INTO leak FROM Artists) AS t"
162+
report = validate_sql(sql, dialect="postgresql")
163+
assert not report.ok
164+
assert any(v.code == "select_into" for v in report.violations)
165+
166+
115167
# ---------- generate_series cap ----------
116168

117169

@@ -173,6 +225,7 @@ def test_garbage_input_is_rejected() -> None:
173225
"banned_function",
174226
"denied_table",
175227
"generate_series_too_large",
228+
"select_into",
176229
],
177230
)
178231
def test_violation_codes_are_documented(code: str) -> None:

0 commit comments

Comments
 (0)