Skip to content

Commit be3094a

Browse files
cathleenyIsaac
andcommitted
Rewrap a long docstring; make the probe-token test guard the nonce
Review bot follow-ups: - Rewrap the _markers_following_in docstring, where an edit had joined two lines into one of about 140 characters. - test_probe_shaped_string_value_is_not_a_probe could not fail without the nonce check, because string values are blanked during the probe pass. The test now also puts probe-shaped text in the operation itself, which reaches the probe pass unchanged, so only the nonce check keeps it from counting. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent bc590a1 commit be3094a

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎src/databricks/sql/utils.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,16 @@ def escape(value: Any) -> Any:
791791
def _markers_following_in(
792792
self, operation: str, parameters, escaped
793793
) -> Tuple[Set[int], Dict[int, Any]]:
794-
"""Return the numbers of the markers that directly follow IN and receive a sequence or a
795-
set, and the parameter key that each marker number refers to.
794+
"""Return the numbers of the markers that directly follow IN and receive a value that can
795+
render as an IN list, and the parameter key that each marker number refers to.
796796
797797
Markers are numbered as `_format_markers` numbers them. To find them, the operation is
798798
formatted once through the same %-formatting as the real substitution, so `%%` and
799799
positional markers line up exactly. Each value that can render as an IN list (see
800-
`_renders_as_in_list`) is replaced by a probe token. Numbers keep their value, so markers such as `%d` still format, and escaped strings
801-
are replaced by an empty string literal, so the text of a value cannot change the result.
802-
If the operation does not format, both results are empty.
800+
`_renders_as_in_list`) is replaced by a probe token. Numbers keep their value, so
801+
markers such as `%d` still format, and escaped strings are replaced by an empty string
802+
literal, so the text of a value cannot change the result. If the operation does not
803+
format, both results are empty.
803804
"""
804805
# The nonce stops text that only looks like a probe token from being taken for one.
805806
nonce = secrets.token_hex(8)

‎tests/unit/test_param_escaper.py‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
pe = ParamEscaper()
1717

1818

19+
def _fake_probe(index: int) -> str:
20+
"""Text shaped like render_inline's probe token for marker `index`, with a fixed nonce."""
21+
return f"\x000123456789abcdef:{index}\x00"
22+
23+
1924
class TestIndividualFormatters(object):
2025
# Test individual type escapers
2126
def test_escape_number_integer(self):
@@ -445,14 +450,31 @@ def test_without_sequences_renders_as_escape_args_without_probing(
445450
)
446451
probe.assert_not_called()
447452

448-
def test_probe_shaped_string_value_is_not_a_probe(self):
449-
"""A string value that contains a probe-like token must not make another marker render as
450-
a list."""
451-
query = "INSERT INTO t VALUES (%(note)s, %(tags)s)"
452-
params = {"note": "x IN \x000000000000000000:1\x00", "tags": ["a"]}
453-
assert pe.render_inline(query, params) == (
454-
"INSERT INTO t VALUES ('x IN \x000000000000000000:1\x00', ARRAY('a'))"
455-
)
453+
@pytest.mark.parametrize(
454+
("label", "query", "params", "expected"),
455+
(
456+
(
457+
# The operation's own text reaches the probe pass unchanged, so only the nonce
458+
# check keeps this token from marking %(tags)s (marker 0) as an IN list.
459+
"probe-shaped text in the operation",
460+
"SELECT * FROM t WHERE x IN " + _fake_probe(0) + " AND arr = %(tags)s",
461+
{"tags": ["a"]},
462+
"SELECT * FROM t WHERE x IN "
463+
+ _fake_probe(0)
464+
+ " AND arr = ARRAY('a')",
465+
),
466+
(
467+
# String values are blanked during the probe pass, so their text cannot mark
468+
# %(tags)s (marker 1) as an IN list either.
469+
"probe-shaped text in a string value",
470+
"INSERT INTO t VALUES (%(note)s, %(tags)s)",
471+
{"note": "x IN " + _fake_probe(1), "tags": ["a"]},
472+
"INSERT INTO t VALUES ('x IN " + _fake_probe(1) + "', ARRAY('a'))",
473+
),
474+
),
475+
)
476+
def test_probe_shaped_text_is_not_a_probe(self, label, query, params, expected):
477+
assert pe.render_inline(query, params) == expected
456478

457479
@pytest.mark.parametrize(
458480
("query", "params", "error", "message"),

0 commit comments

Comments
 (0)