Add <bind> bound parameters for <sql> (phase 1)

Implements phase 1 of docs/design-sql-bound-parameters.md: <bind> supplies
the value for the next ? placeholder of the enclosing <sql>, with typed
validation (int, decimal, date, datetime, bool, ident with allow list),
null="empty", and refusal to change the connection character set.

- SqlBind.*: MySQL-free placeholder scanning and binding logic
- __bind.*, __sql, __mysql: runtime wiring
- Tag.cpp, jetc/JetParser.cpp: bind is a void tag in both
- jetc/JetCodegen: emit sized ZString literals for text holding a NUL,
  which previously truncated the compiled page
- tests: unit tests, end-to-end test against MySQL (interpreted and
  jetc-compiled), and the generator for the binary test page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-20 19:55:23 -08:00
1 parent 9fae14eb96
commit a18d1358b0
18 files changed
+1103 -9

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
# Generates tests/testbind.jet and tests/testbind.expected (the .jet holds raw
# 0xBF and NUL bytes, so edit this rather than the .jet). Run from tests/:
# python3 gentestbind.py testbind.jet testbind.expected
import sys
out_jet, out_exp = sys.argv[1], sys.argv[2]
# (label, raw bytes, attribute quote)
values = [
("plain", b"hello", '"'),
("or-injection", b"x' or '1'='1", '"'),
("backslash-quote", b"\\'", '"'),
# A jet attribute can't end in a backslash (CoreUtils getTokenExclude
# reads it as escaping the closing quote), so this one is built in SQL.
("trailing-backslash", b"a\\", "concat('a', char(92))"),
("dashdash", b"-- ", '"'),
("semicolon", b"; drop table t; --", '"'),
("gbk-bf27", b"\xbf' or 1=1 -- ", '"'),
("double-quote", b'say "hi"', "'"),
("question", b"what?", '"'),
("nul", b"a\x00b", '"'),
("utf8", "héllo – ✓ 🎹".encode(), '"'),
]
J = []
E = []
w = J.append
w(b'#!../jet-2.0\n')
w(b'<jet filterblanklines="true" trimlines="true">\n')
w(b'<comment>\n'
b' End-to-end tests for <sql>/<bind> - see docs/design-sql-bound-parameters.md.\n'
b' Run by tests/runbindtests.sh against a real MySQL server; only TEMPORARY\n'
b' tables are used, so nothing outlives the connection. Compared byte-for-byte\n'
b' with tests/testbind.expected. Generated - contains raw 0xBF and NUL bytes.\n'
b'</comment>\n')
w(b'<mysql host="$[&JETTEST_DB_HOST]" database="$[&JETTEST_DB_NAME]" user="$[&JETTEST_DB_USER]" password="$[&JETTEST_DB_PASSWORD]">\n')
w(b'<sql>create temporary table bindtest (id int auto_increment primary key, s varbinary(255), n int, dec_v decimal(10,2), d date, dt datetime, b tinyint, t varchar(100) character set utf8mb4)</sql>\n')
def section(name, mode):
for label, raw, q in values:
if len(q) > 1:
w(b'<sql>select ' + q.encode() + b' as v</sql><ifrow><set name="v" value="$[.v]"></ifrow>\n')
else:
qb = q.encode()
w(b'<set name="v" value=' + qb + raw + qb + b'>\n')
w(b'<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>\n')
w(b'<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>\n')
w(('%s %s: <ifrow>$[.h]</ifrow>\n' % (mode, label)).encode())
E.append('%s %s: %s' % (mode, label, raw.hex().upper()))
w(b'<comment> Injection strings and odd bytes come back byte-for-byte. </comment>\n')
section("roundtrip", "default")
w(b'<sql>set session sql_mode = concat(@@sql_mode, \',NO_BACKSLASH_ESCAPES\')</sql>\n')
section("roundtrip", "nbe")
w(b'<sql>set session sql_mode = replace(@@sql_mode, \'NO_BACKSLASH_ESCAPES\', \'\')</sql>\n')
tail = r'''<comment> Every type. </comment>
<set name="num" value="42">
<sql>insert into bindtest (n, dec_v, d, dt, b, t) values (?, ?, ?, ?, ?, ?)
<bind value="$[num]" type="int">
<bind value="-12.50" type="decimal">
<bind value="2026-10-23" type="date">
<bind value="2026-10-23 14:05" type="datetime">
<bind value="yes" type="bool">
<bind value="" null="empty">
</sql>
<sql>select n, dec_v, d, dt, b, t is null as tnull from bindtest where id=last_insert_id()</sql>
types: <ifrow>$[.n] $[.dec_v] $[.d] $[.dt] $[.b] $[.tnull]</ifrow>
<comment> ? inside literals, identifiers and comments is left alone. </comment>
<sql>select '?' as a, "?" as `b?`, ? as c /* ? */ -- ?
<bind value="bound">
</sql>
literals: <ifrow>$[.a] $[.b?] $[.c]</ifrow>
<comment> A query with no bind runs unchanged, even with a literal ?. </comment>
<sql>select concat('what', '?') as q</sql>
nobind: <ifrow>$[.q]</ifrow>
<comment> bind inside if inside sql. </comment>
<set name="flag" value="1">
<sql>select ? as x
<if value1="$[flag]" value2="1" type="eq"><bind value="from-if"></if>
</sql>
nested: <ifrow>$[.x]</ifrow>
<comment> ident with allow, order by. </comment>
<sql>insert into bindtest (n) values (5), (3), (9)</sql>
<set name="sort" value="N">
<set name="dir" value="desc">
<sql>select n from bindtest where n in (?, ?, ?) order by ? ?
<bind value="3" type="int"><bind value="5" type="int"><bind value="9" type="int">
<bind value="$[sort]" type="ident" allow="n,s">
<bind value="$[dir]" type="ident" allow="ASC,DESC">
</sql>
ident: <whilerow name="r">$[.n] </whilerow>
<comment> Result access on a bound query: whilerow, column names and counts. </comment>
<sql>select n, s from bindtest where n > ? order by n<bind value="4" type="int"></sql>
rows: $[.#] columns: $[.?#] first column: $[.?1] second column: $[.?2]
<comment> Errors - all catchable, none echo the value. </comment>
<try><sql>select ?, ?, ?<bind value="a"><bind value="b"></sql><catch name="err">mismatch: $[err]</try>
<try><sql>select ?<bind value="1 or 1=1" type="int"></sql><catch name="err">badint: $[err]</try>
<try><sql>select ?<bind value="s3cret" type="date"></sql><catch name="err">baddate: $[err]</try>
<try><sql>select ? from bindtest<bind value="password" type="ident" allow="n,s"></sql><catch name="err">badident: $[err]</try>
<try><bind value="x"><catch name="err">outside: $[err]</try>
<try><sql>select 1<bind></sql><catch name="err">novalue: $[err]</try>
<try><sql>select ?<bind value="x" type="blob"></sql><catch name="err">badtype: $[err]</try>
<try><sql>SET NAMES latin1</sql><catch name="err">setnames: $[err]</try>
<try><sql>set character_set_client = gbk</sql><catch name="err">setcharset: $[err]</try>
<sql>select @@character_set_client as cs</sql>
charset: <ifrow>$[.cs]</ifrow>
</mysql>
</jet>
'''
w(tail.encode())
E += [
"types: 42 -12.50 2026-10-23 2026-10-23 14:05:00 1 1",
"literals: ? ? bound",
"nobind: what?",
"nested: from-if",
"ident: 9 5 3",
"rows: 3 columns: 2 first column: n second column: s",
"mismatch: sql: 3 placeholders but 2 bind values",
"badint: bind 1: value is not a valid int.",
"baddate: bind 1: value is not a valid date (YYYY-MM-DD).",
"badident: bind 1: value is not in the allow list.",
"outside: bind must be inside sql.",
"novalue: bind must have a value.",
'badtype: bind 1: unknown type "blob".',
"setnames: sql: changing the connection character set is not allowed.",
"setcharset: sql: changing the connection character set is not allowed.",
"charset: utf8mb4",
]
open(out_jet, "wb").write(b"".join(J))
open(out_exp, "w").write("\n".join(E) + "\n")
+74
View File
@@ -0,0 +1,74 @@
#!/bin/bash
# Tests for <sql>/<bind> (docs/design-sql-bound-parameters.md, phase 1):
# 1. tests/sqlbind_test.cpp - unit tests for SqlBind.cpp (no MySQL needed)
# 2. tests/testbind.jet - end to end through jet-2.0 against a real MySQL
# server (TEMPORARY tables only), compared with
# tests/testbind.expected
# 3. jetc - <bind> is void in the compiler too, and a
# compiled page runs the same <sql>/<bind> code
#
# Build jet-2.0 first (./compile). The MySQL part needs:
# JETTEST_DB_USER, JETTEST_DB_PASSWORD (required, or it is skipped)
# JETTEST_DB_HOST (default localhost), JETTEST_DB_NAME (default test)
set -u
T="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JC="$(cd "$T/.." && pwd)"
CU="$JC/../CoreUtils"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
fail=0
echo "== unit tests (SqlBind) =="
g++ -g -std=c++23 -I"$CU" -I"$JC" "$T/sqlbind_test.cpp" "$JC/SqlBind.cpp" -L"$CU" -lCoreUtils -o "$WORK/sqlbind_test" || exit 1
"$WORK/sqlbind_test" || fail=1
echo "== end to end (jet-2.0 + MySQL) =="
if [ -z "${JETTEST_DB_USER:-}" ] || [ -z "${JETTEST_DB_PASSWORD:-}" ]; then
echo "SKIPPED: set JETTEST_DB_USER and JETTEST_DB_PASSWORD"
else
export JETTEST_DB_HOST="${JETTEST_DB_HOST:-localhost}" JETTEST_DB_NAME="${JETTEST_DB_NAME:-test}"
(cd "$T" && "$JC/jet-2.0" testbind.jet) > "$WORK/testbind.out" 2>&1
if diff -u "$T/testbind.expected" "$WORK/testbind.out"; then
echo "PASS ($(wc -l < "$T/testbind.expected") lines match)"
else
echo "FAIL"; fail=1
fi
fi
echo "== jetc =="
g++ -g -std=c++23 -o "$WORK/jetc" "$JC/jetc/jetc.cpp" "$JC/jetc/JetParser.cpp" "$JC/jetc/JetCodegen.cpp" || exit 1
# A bare <bind> must not swallow the content after it looking for </bind>.
printf 'A<sql>select ?<bind value="$[x]"></sql>B<bind value="y">C\n' > "$WORK/void.jet"
"$WORK/jetc" "$WORK/void.jet" "$WORK/void.cpp" >/dev/null || fail=1
if grep -q '<sql>select ?<bind value=\\"$\[x\]\\"></sql>' "$WORK/void.cpp" &&
grep -q '"<bind value=\\"y\\">"' "$WORK/void.cpp" &&
grep -q '"C\\n"' "$WORK/void.cpp"; then
echo "PASS (bind is void; sql span intact)"
else
echo "FAIL: unexpected codegen for bind"; grep -n 'interpretSpan\|out.write' "$WORK/void.cpp"; fail=1
fi
if [ -n "${JETTEST_DB_USER:-}" ] && [ -n "${JETTEST_DB_PASSWORD:-}" ]; then
# The whole end-to-end test again, compiled by jetc instead of interpreted.
# Drop the #! line, and the newline after </jet> (the interpreter stops
# at </jet>; a compiled page would emit it as text).
tail -n +2 "$T/testbind.jet" | head -c -1 > "$WORK/compiled.jet"
"$WORK/jetc" "$WORK/compiled.jet" "$WORK/compiled.cpp" >/dev/null || fail=1
objs=""
for f in "$JC"/*.cpp; do
[ "$(basename "$f")" = "jet-2.0.cpp" ] && continue
o="$WORK/$(basename "${f%.cpp}").o"
g++ -g -c -std=c++23 -I"$CU" -I"$JC" "$f" -o "$o" &
objs="$objs $o"
done
wait
g++ -g -std=c++23 -I"$CU" -I"$JC" "$WORK/compiled.cpp" $objs -L"$CU" -lCoreUtils -lmysqlclient -lssl -lcrypto -o "$WORK/compiled" || exit 1
(cd "$T" && "$WORK/compiled") > "$WORK/compiled.out" 2>&1
if diff -u "$T/testbind.expected" "$WORK/compiled.out"; then
echo "PASS (compiled page output matches)"
else
echo "FAIL (compiled page)"; fail=1
fi
fi
[ $fail = 0 ] && echo "ALL PASSED" || echo "FAILURES"
exit $fail
+146
View File
@@ -0,0 +1,146 @@
// Unit tests for SqlBind (the MySQL-free part of <sql>/<bind>) -
// see docs/design-sql-bound-parameters.md section 10.
// Built and run by tests/runbindtests.sh.
#include "SqlBind.h"
#include "Exception.h"
#include <iostream>
#include <string>
#include <vector>
using namespace jet;
static int failures = 0, passes = 0;
#define CHECK(cond) do { if(cond) ++passes; else { ++failures; std::cout << "FAIL line " << __LINE__ << ": " #cond << std::endl; } } while(0)
// A stand-in escaper (the real one is mysql_real_escape_string_quote).
static std::string esc(const std::string &s) {
std::string o;
for(char c : s) { if(c == '\'' || c == '\\') o += '\\'; o += c; }
return o;
}
static BoundValue bv(const std::string &value, const std::string &type = "", const std::string &nullMode = "",
bool hasAllow = false, const std::string &allow = "") {
return makeBoundValue(value, type, nullMode, hasAllow, allow, 1);
}
static bool throws(const std::string &value, const std::string &type, const std::string &nullMode = "",
bool hasAllow = false, const std::string &allow = "", std::string *msg = nullptr) {
try { bv(value, type, nullMode, hasAllow, allow); }
catch(coreutils::Exception &e) { if(msg) *msg = e.text; return true; }
return false;
}
static std::string bind(const std::string &q, std::vector<BoundValue> v, bool nbe = false) {
return bindPlaceholders(q, v, nbe, esc);
}
int main() {
// --- types: valid values ---
CHECK(bv("abc").kind == BoundValue::QUOTED && bv("abc").text == "abc");
CHECK(bv("abc", "string").kind == BoundValue::QUOTED);
CHECK(bv("", "string").kind == BoundValue::QUOTED && bv("").text == "");
CHECK(bv("42", "int").kind == BoundValue::BARE && bv("42", "int").text == "42");
CHECK(bv("-7", "INT").text == "-7");
CHECK(bv("1234567890123456789", "int").text == "1234567890123456789");
CHECK(bv("3.14", "decimal").text == "3.14");
CHECK(bv("-10", "decimal").text == "-10");
for(auto t : {"1", "true", "TRUE", "yes", "Yes"}) CHECK(bv(t, "bool").text == "1");
for(auto f : {"0", "false", "no", "NO"}) CHECK(bv(f, "bool").text == "0");
CHECK(bv("2026-10-23", "date").kind == BoundValue::QUOTED);
CHECK(bv("2026-10-23 14:05", "datetime").kind == BoundValue::QUOTED);
CHECK(bv("2026-10-23 23:59:59", "datetime").text == "2026-10-23 23:59:59");
CHECK(bv("price", "ident", "", true, "name, price ,created").text == "`price`");
CHECK(bv("PRICE", "ident", "", true, "name,price").text == "`price`"); // allow list spelling wins
CHECK(bv("desc", "ident", "", true, "ASC,DESC").text == "DESC");
CHECK(bv("asc", "ident", "", true, "ASC,DESC").kind == BoundValue::BARE);
CHECK(bv("we`ird", "ident", "", true, "we`ird").text == "`we``ird`");
// --- null="empty" ---
CHECK(bv("", "string", "empty").kind == BoundValue::SQLNULL);
CHECK(bv("", "int", "empty").kind == BoundValue::SQLNULL);
CHECK(bv("5", "int", "empty").text == "5");
CHECK(bv("x", "", "EMPTY").kind == BoundValue::QUOTED);
// --- types: invalid values throw ---
for(auto v : {"", "1 or 1=1", "1.5", "abc", "12345678901234567890", "-", " 1", "0x10", "1e5"})
CHECK(throws(v, "int"));
for(auto v : {"", "1.", ".5", "1.2.3", "1,5", "1;drop"}) CHECK(throws(v, "decimal"));
for(auto v : {"", "2", "on", "y"}) CHECK(throws(v, "bool"));
for(auto v : {"", "2026-13-01", "2026-00-10", "2026-01-32", "26-01-01", "2026/01/01", "2026-01-01'"})
CHECK(throws(v, "date"));
for(auto v : {"2026-01-01", "2026-01-01 24:00", "2026-01-01 12:60", "2026-01-01T12:00", "2026-01-01 12:00:61"})
CHECK(throws(v, "datetime"));
CHECK(throws("name; drop table x", "ident", "", true, "name,price"));
CHECK(throws("price", "ident")); // ident needs allow
CHECK(throws("x", "string", "", true, "x")); // allow only with ident
CHECK(throws("x", "blob")); // unknown type
CHECK(throws("x", "string", "always")); // bad null mode
CHECK(throws("", "int")); // empty is not an int without null="empty"
// error text never includes the value (it may be a password)
{
std::string msg;
CHECK(throws("s3cret-pw", "int", "", false, "", &msg));
CHECK(msg.find("s3cret-pw") == std::string::npos);
CHECK(msg.find("bind 1") != std::string::npos);
CHECK(throws("s3cret-pw", "ident", "", true, "a,b", &msg));
CHECK(msg.find("s3cret-pw") == std::string::npos);
}
// --- placeholder scanning: replaced in order ---
CHECK(bind("select ? , ?", {bv("a"), bv("1", "int")}) == "select 'a' , 1");
CHECK(bind("x=?", {bv("", "", "empty")}) == "x=NULL");
CHECK(bind("where email=? and status='ACTIVE'", {bv("x' or '1'='1")}) ==
"where email='x\\' or \\'1\\'=\\'1' and status='ACTIVE'");
// replacement text is never rescanned
CHECK(bind("a=? and b=?", {bv("?"), bv("??")}) == "a='?' and b='??'");
// --- ? inside literals, identifiers and comments is never replaced ---
CHECK(countPlaceholders("select '?', \"?\", `?` from t", false) == 0);
CHECK(countPlaceholders("select 1 -- ?\n, ?", false) == 1);
CHECK(countPlaceholders("select 1 # ?\n, ?", false) == 1);
CHECK(countPlaceholders("select /* ? */ ?", false) == 1);
CHECK(countPlaceholders("select /* ? \n ? */ 1", false) == 0);
CHECK(countPlaceholders("select 1--?", false) == 1); // "--" not followed by space is not a comment
CHECK(countPlaceholders("select 'it''s ?' , ?", false) == 1);
CHECK(countPlaceholders("select \"say \"\"?\"\"\" , ?", false) == 1);
CHECK(countPlaceholders("select `a``?` , ?", false) == 1);
CHECK(countPlaceholders("select 'a\\'?' , ?", false) == 1); // \' escapes the quote
// with NO_BACKSLASH_ESCAPES, 'a\' is a complete literal, so both ? count
CHECK(countPlaceholders("select 'a\\', ? , ?", true) == 2);
CHECK(countPlaceholders("select 'a\\', ? , ?", false) == 0);
CHECK(countPlaceholders("select ?", false) == 1);
CHECK(countPlaceholders("", false) == 0);
// --- count mismatch throws ---
{
bool threw = false;
try { bind("select ?, ?, ?", {bv("a"), bv("b")}); }
catch(coreutils::Exception &e) { threw = true; CHECK(e.text == "sql: 3 placeholders but 2 bind values"); }
CHECK(threw);
threw = false;
try { bind("select '?'", {bv("a")}); } catch(coreutils::Exception &) { threw = true; }
CHECK(threw);
}
// --- charset changes are refused ---
CHECK(isCharsetChange("SET NAMES latin1"));
CHECK(isCharsetChange(" set names 'gbk'"));
CHECK(isCharsetChange("/* x */ SET CHARACTER SET gbk"));
CHECK(isCharsetChange("set charset gbk"));
CHECK(isCharsetChange("set @a=1, names gbk"));
CHECK(isCharsetChange("set character_set_client = gbk"));
CHECK(isCharsetChange("SET @@session.character_set_connection=gbk"));
CHECK(isCharsetChange("set collation_connection = 'gbk_bin'"));
CHECK(!isCharsetChange("select names from t"));
CHECK(!isCharsetChange("update t set names='x'"));
CHECK(!isCharsetChange("set @names = 'names'"));
CHECK(!isCharsetChange("set session sql_mode = 'NO_BACKSLASH_ESCAPES'"));
CHECK(!isCharsetChange("set @x = 'set names gbk'"));
std::cout << passes << " passed, " << failures << " failed" << std::endl;
return failures == 0 ? 0 : 1;
}
+38
View File
@@ -0,0 +1,38 @@
default plain: 68656C6C6F
default or-injection: 7827206F72202731273D2731
default backslash-quote: 5C27
default trailing-backslash: 615C
default dashdash: 2D2D20
default semicolon: 3B2064726F70207461626C6520743B202D2D
default gbk-bf27: BF27206F7220313D31202D2D20
default double-quote: 7361792022686922
default question: 776861743F
default nul: 610062
default utf8: 68C3A96C6C6F20E2809320E29C9320F09F8EB9
nbe plain: 68656C6C6F
nbe or-injection: 7827206F72202731273D2731
nbe backslash-quote: 5C27
nbe trailing-backslash: 615C
nbe dashdash: 2D2D20
nbe semicolon: 3B2064726F70207461626C6520743B202D2D
nbe gbk-bf27: BF27206F7220313D31202D2D20
nbe double-quote: 7361792022686922
nbe question: 776861743F
nbe nul: 610062
nbe utf8: 68C3A96C6C6F20E2809320E29C9320F09F8EB9
types: 42 -12.50 2026-10-23 2026-10-23 14:05:00 1 1
literals: ? ? bound
nobind: what?
nested: from-if
ident: 9 5 3
rows: 3 columns: 2 first column: n second column: s
mismatch: sql: 3 placeholders but 2 bind values
badint: bind 1: value is not a valid int.
baddate: bind 1: value is not a valid date (YYYY-MM-DD).
badident: bind 1: value is not in the allow list.
outside: bind must be inside sql.
novalue: bind must have a value.
badtype: bind 1: unknown type "blob".
setnames: sql: changing the connection character set is not allowed.
setcharset: sql: changing the connection character set is not allowed.
charset: utf8mb4
Binary file not shown.