From a18d1358b0a3bdb483af09c9b6f81835a1a633e4 Mon Sep 17 00:00:00 2001 From: brad Arant Date: Fri, 20 Nov 2026 19:55:23 -0800 Subject: [PATCH] Add bound parameters for (phase 1) Implements phase 1 of docs/design-sql-bound-parameters.md: supplies the value for the next ? placeholder of the enclosing , 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 --- SqlBind.cpp | 305 ++++++++++++++++++++++++++++ SqlBind.h | 64 ++++++ Tag.cpp | 6 +- __bind.cpp | 29 +++ __bind.h | 21 ++ __mysql.cpp | 17 ++ __mysql.h | 7 + __sql.cpp | 16 +- __sql.h | 17 +- docs/design-sql-bound-parameters.md | 225 ++++++++++++++++++++ jetc/JetCodegen.cpp | 13 +- jetc/JetCodegen.h | 1 + jetc/JetParser.cpp | 5 + tests/gentestbind.py | 128 ++++++++++++ tests/runbindtests.sh | 74 +++++++ tests/sqlbind_test.cpp | 146 +++++++++++++ tests/testbind.expected | 38 ++++ tests/testbind.jet | Bin 0 -> 8405 bytes 18 files changed, 1103 insertions(+), 9 deletions(-) create mode 100644 SqlBind.cpp create mode 100644 SqlBind.h create mode 100644 __bind.cpp create mode 100644 __bind.h create mode 100644 docs/design-sql-bound-parameters.md create mode 100644 tests/gentestbind.py create mode 100755 tests/runbindtests.sh create mode 100644 tests/sqlbind_test.cpp create mode 100644 tests/testbind.expected create mode 100644 tests/testbind.jet diff --git a/SqlBind.cpp b/SqlBind.cpp new file mode 100644 index 0000000..3061186 --- /dev/null +++ b/SqlBind.cpp @@ -0,0 +1,305 @@ +#include "SqlBind.h" +#include "Exception.h" +#include + +namespace jet { + + namespace { + + std::string lower(std::string s) { + for(char &c : s) c = (char)std::tolower((unsigned char)c); + return s; + } + + std::string trim(const std::string &s) { + size_t b = 0, e = s.size(); + while(b < e && std::isspace((unsigned char)s[b])) ++b; + while(e > b && std::isspace((unsigned char)s[e - 1])) --e; + return s.substr(b, e - b); + } + + std::string where(int position) { + return "bind " + std::to_string(position) + ": "; + } + + bool allDigits(const std::string &s, size_t from, size_t to) { + if(from >= to) return false; + for(size_t i = from; i < to; ++i) + if(!std::isdigit((unsigned char)s[i])) return false; + return true; + } + + // ^-?[0-9]{1,maxDigits}$ + bool isInteger(const std::string &s, size_t maxDigits) { + size_t start = (!s.empty() && s[0] == '-') ? 1 : 0; + size_t digits = s.size() - start; + return digits >= 1 && digits <= maxDigits && allDigits(s, start, s.size()); + } + + // ^-?[0-9]{1,30}(\.[0-9]{1,30})?$ + bool isDecimal(const std::string &s) { + size_t dot = s.find('.'); + if(dot == std::string::npos) return isInteger(s, 30); + std::string frac = s.substr(dot + 1); + return isInteger(s.substr(0, dot), 30) && frac.size() >= 1 && frac.size() <= 30 && allDigits(frac, 0, frac.size()); + } + + int num(const std::string &s, size_t at, size_t len) { + return std::stoi(s.substr(at, len)); + } + + // YYYY-MM-DD with a real month and day range. + bool isDate(const std::string &s) { + if(s.size() != 10 || s[4] != '-' || s[7] != '-') return false; + if(!allDigits(s, 0, 4) || !allDigits(s, 5, 7) || !allDigits(s, 8, 10)) return false; + int month = num(s, 5, 2), day = num(s, 8, 2); + return month >= 1 && month <= 12 && day >= 1 && day <= 31; + } + + // YYYY-MM-DD HH:MM or YYYY-MM-DD HH:MM:SS + bool isDateTime(const std::string &s) { + if(s.size() != 16 && s.size() != 19) return false; + if(!isDate(s.substr(0, 10)) || s[10] != ' ' || s[13] != ':') return false; + if(!allDigits(s, 11, 13) || !allDigits(s, 14, 16)) return false; + if(num(s, 11, 2) > 23 || num(s, 14, 2) > 59) return false; + if(s.size() == 19) { + if(s[16] != ':' || !allDigits(s, 17, 19) || num(s, 17, 2) > 59) return false; + } + return true; + } + + // Walks query and calls onPlaceholder(index) for every '?' that is + // outside '...', "...", `...`, -- / # line comments and /* */ + // comments. With NO_BACKSLASH_ESCAPES off, a backslash escapes the + // next character inside '...' and "..."; with it on, it doesn't. + // A doubled quote ('', "", ``) is an escaped quote in every mode. + template + void scan(const std::string &q, bool noBackslashEscapes, F onPlaceholder) { + size_t i = 0, n = q.size(); + while(i < n) { + char c = q[i]; + if(c == '\'' || c == '"' || c == '`') { + char quote = c; + ++i; + while(i < n) { + if(quote != '`' && !noBackslashEscapes && q[i] == '\\') { i += 2; continue; } + if(q[i] == quote) { + if(i + 1 < n && q[i + 1] == quote) { i += 2; continue; } + ++i; + break; + } + ++i; + } + continue; + } + if(c == '#' || (c == '-' && i + 1 < n && q[i + 1] == '-' && + (i + 2 >= n || (unsigned char)q[i + 2] <= ' '))) { + while(i < n && q[i] != '\n') ++i; + continue; + } + if(c == '/' && i + 1 < n && q[i + 1] == '*') { + size_t end = q.find("*/", i + 2); + i = (end == std::string::npos) ? n : end + 2; + continue; + } + if(c == '?') onPlaceholder(i); + ++i; + } + } + + // Lowercased words ([a-z0-9_@.$]) outside quotes and comments. + std::vector words(const std::string &q) { + std::vector out; + std::string cur; + size_t i = 0, n = q.size(); + auto flush = [&]() { if(!cur.empty()) { out.push_back(lower(cur)); cur.clear(); } }; + while(i < n) { + char c = q[i]; + if(c == '\'' || c == '"' || c == '`') { + flush(); + char quote = c; + ++i; + while(i < n) { + if(quote != '`' && q[i] == '\\') { i += 2; continue; } + if(q[i] == quote) { + if(i + 1 < n && q[i + 1] == quote) { i += 2; continue; } + ++i; + break; + } + ++i; + } + out.push_back("'"); // a literal stands between words + continue; + } + if(c == '#' || (c == '-' && i + 1 < n && q[i + 1] == '-' && + (i + 2 >= n || (unsigned char)q[i + 2] <= ' '))) { + flush(); + while(i < n && q[i] != '\n') ++i; + continue; + } + if(c == '/' && i + 1 < n && q[i + 1] == '*') { + flush(); + size_t end = q.find("*/", i + 2); + i = (end == std::string::npos) ? n : end + 2; + continue; + } + if(std::isalnum((unsigned char)c) || c == '_' || c == '@' || c == '.' || c == '$') + cur += c; + else + flush(); + ++i; + } + flush(); + return out; + } + + } + + BoundValue makeBoundValue(const std::string &value, + const std::string &typeIn, + const std::string &nullModeIn, + bool hasAllow, + const std::string &allow, + int position) { + std::string type = typeIn.empty() ? "string" : lower(trim(typeIn)); + std::string nullMode = lower(trim(nullModeIn)); + + if(nullMode != "" && nullMode != "empty") + throw coreutils::Exception(where(position) + "null must be \"empty\"."); + if(hasAllow && type != "ident") + throw coreutils::Exception(where(position) + "allow is only valid with type=\"ident\"."); + + BoundValue b; + if(nullMode == "empty" && value.empty()) { + b.kind = BoundValue::SQLNULL; + return b; + } + + if(type == "string") { + b.kind = BoundValue::QUOTED; + b.text = value; + } else if(type == "int") { + if(!isInteger(value, 19)) + throw coreutils::Exception(where(position) + "value is not a valid int."); + b.kind = BoundValue::BARE; + b.text = value; + } else if(type == "decimal") { + if(!isDecimal(value)) + throw coreutils::Exception(where(position) + "value is not a valid decimal."); + b.kind = BoundValue::BARE; + b.text = value; + } else if(type == "bool") { + std::string v = lower(value); + b.kind = BoundValue::BARE; + if(v == "1" || v == "true" || v == "yes") + b.text = "1"; + else if(v == "0" || v == "false" || v == "no") + b.text = "0"; + else + throw coreutils::Exception(where(position) + "value is not a valid bool."); + } else if(type == "date") { + if(!isDate(value)) + throw coreutils::Exception(where(position) + "value is not a valid date (YYYY-MM-DD)."); + b.kind = BoundValue::QUOTED; + b.text = value; + } else if(type == "datetime") { + if(!isDateTime(value)) + throw coreutils::Exception(where(position) + "value is not a valid datetime (YYYY-MM-DD HH:MM[:SS])."); + b.kind = BoundValue::QUOTED; + b.text = value; + } else if(type == "ident") { + if(!hasAllow) + throw coreutils::Exception(where(position) + "type=\"ident\" requires an allow list."); + // Match case-insensitively, but emit the allow list's own spelling: + // what reaches the query always comes from the page, never the request. + std::string match; + bool found = false; + size_t start = 0; + while(start <= allow.size()) { + size_t comma = allow.find(',', start); + if(comma == std::string::npos) comma = allow.size(); + std::string entry = trim(allow.substr(start, comma - start)); + if(!entry.empty() && lower(entry) == lower(value)) { + match = entry; + found = true; + break; + } + start = comma + 1; + } + if(!found) + throw coreutils::Exception(where(position) + "value is not in the allow list."); + b.kind = BoundValue::BARE; + std::string m = lower(match); + if(m == "asc" || m == "desc") { + b.text = (m == "asc") ? "ASC" : "DESC"; + } else { + b.text = "`"; + for(char c : match) { + if(c == '`') b.text += '`'; + b.text += c; + } + b.text += "`"; + } + } else + throw coreutils::Exception(where(position) + "unknown type \"" + type + "\"."); + + return b; + } + + size_t countPlaceholders(const std::string &query, bool noBackslashEscapes) { + size_t count = 0; + scan(query, noBackslashEscapes, [&](size_t) { ++count; }); + return count; + } + + std::string bindPlaceholders(const std::string &query, + const std::vector &values, + bool noBackslashEscapes, + const std::function "eEscape) { + std::vector at; + scan(query, noBackslashEscapes, [&](size_t i) { at.push_back(i); }); + if(at.size() != values.size()) + throw coreutils::Exception("sql: " + std::to_string(at.size()) + " placeholders but " + + std::to_string(values.size()) + " bind values"); + std::string out; + out.reserve(query.size() + 32 * values.size()); + size_t last = 0; + for(size_t k = 0; k < at.size(); ++k) { + out.append(query, last, at[k] - last); + const BoundValue &v = values[k]; + if(v.kind == BoundValue::SQLNULL) + out += "NULL"; + else if(v.kind == BoundValue::BARE) + out += v.text; + else { + out += '\''; + out += quoteEscape(v.text); + out += '\''; + } + last = at[k] + 1; + } + out.append(query, last, std::string::npos); + return out; + } + + bool isCharsetChange(const std::string &query) { + std::vector w = words(query); + if(w.empty() || w[0] != "set") + return false; + for(size_t i = 1; i < w.size(); ++i) { + const std::string &x = w[i]; + if(x == "names" || x == "charset") + return true; + if(x == "character" && i + 1 < w.size() && w[i + 1] == "set") + return true; + // character_set_client, @@character_set_client, @@session.character_set_client, ... + for(const char *var : {"character_set_client", "character_set_connection", "collation_connection"}) { + std::string v(var); + if(x.size() >= v.size() && x.compare(x.size() - v.size(), v.size(), v) == 0) + return true; + } + } + return false; + } + +} diff --git a/SqlBind.h b/SqlBind.h new file mode 100644 index 0000000..0c2e9f7 --- /dev/null +++ b/SqlBind.h @@ -0,0 +1,64 @@ +#ifndef __SqlBind_h__ +#define __SqlBind_h__ + +#include +#include +#include + +// Bound parameters for - see docs/design-sql-bound-parameters.md. +// +// Everything here is plain string logic with no MySQL or Tag dependency, +// so it can be unit tested on its own (tests/sqlbind_test.cpp). __bind +// builds BoundValues with makeBoundValue(); __sql substitutes them into +// the rendered query with bindPlaceholders(), passing in the connection's +// own escaping function. + +namespace jet { + + struct BoundValue { + enum Kind { + SQLNULL, // emitted as NULL + BARE, // already validated (int/decimal/bool/ASC/DESC/`ident`) - emitted as is + QUOTED // escaped with the connection's escaper and wrapped in '...' + }; + Kind kind = QUOTED; + std::string text; + }; + + // Validates value against type and returns how it must be emitted. + // position is the 1-based bind number, used only in error messages. + // Error messages never contain the value itself (it may be a password). + // type: string (default if empty), int, decimal, bool, date, datetime, ident + // nullMode: "" (bind '' for an empty value) or "empty" (bind NULL) + // allow: comma list of permitted identifiers - required for ident, + // not allowed on any other type + BoundValue makeBoundValue(const std::string &value, + const std::string &type, + const std::string &nullMode, + bool hasAllow, + const std::string &allow, + int position); + + // Number of '?' placeholders in query that are outside string literals, + // backtick identifiers and comments. + size_t countPlaceholders(const std::string &query, bool noBackslashEscapes); + + // Replaces each placeholder (as counted above) with the matching bound + // value, in order. Throws if the placeholder and value counts differ. + // quoteEscape escapes the body of a single-quoted string literal (no + // surrounding quotes) for the actual connection. + std::string bindPlaceholders(const std::string &query, + const std::vector &values, + bool noBackslashEscapes, + const std::function "eEscape); + + // True for a SET statement that changes the connection character set + // (SET NAMES, SET CHARACTER SET, SET CHARSET, or an assignment to + // character_set_client / character_set_connection / collation_connection). + // Changing it after connect makes client-side escaping and server-side + // parsing disagree, so refuses these. + bool isCharsetChange(const std::string &query); + +} + +#endif diff --git a/Tag.cpp b/Tag.cpp index dfab4a9..b0a3b4d 100644 --- a/Tag.cpp +++ b/Tag.cpp @@ -6,6 +6,7 @@ #include "Global.h" #include "__mysql.h" #include "__sql.h" +#include "__bind.h" #include "__imap.h" #include "__imaplist.h" #include "__imapmessage.h" @@ -88,7 +89,7 @@ namespace jet { (nameLower == "cookie") || (nameLower == "header") || (nameLower == "read") || (nameLower == "write") || (nameLower == "call") || (nameLower == "system") || - (nameLower == "dump"); + (nameLower == "dump") || (nameLower == "bind"); // A CUSTOM tag (not one of jet's own built-ins above) // written bare, with no explicit close, is genuinely // ambiguous: is a real body coming (scan for @@ -494,6 +495,9 @@ namespace jet { } else if(ifTagName(in, "sql")) { __sql _sql(in, out, global, this, local); continue; + } else if(ifTagName(in, "bind")) { + __bind _bind(in, out, global, this, local); + continue; } else if(ifTagName(in, "imap")) { __imap _imap(in, out, global, this, local); continue; diff --git a/__bind.cpp b/__bind.cpp new file mode 100644 index 0000000..46866d8 --- /dev/null +++ b/__bind.cpp @@ -0,0 +1,29 @@ +#include "__bind.h" +#include "__sql.h" +#include "Exception.h" +#include "SqlBind.h" + +namespace jet { + + __bind::__bind(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) { + output = false; + + // Nearest enclosing , through any //etc. in between. + __sql *sql = NULL; + for(Tag *scope = parent; scope && !sql; scope = scope->parent) + sql = dynamic_cast<__sql *>(scope); + if(!sql) + throw coreutils::Exception("bind must be inside sql."); + if(!keywordDefined("value")) + throw coreutils::Exception("bind must have a value."); + + std::string value = resolveKeyword("value").str(); + std::string type = keywordDefined("type") ? resolveKeyword("type").str() : ""; + std::string nullMode = keywordDefined("null") ? resolveKeyword("null").str() : ""; + bool hasAllow = keywordDefined("allow"); + std::string allow = hasAllow ? resolveKeyword("allow").str() : ""; + + sql->addBind(makeBoundValue(value, type, nullMode, hasAllow, allow, (int)sql->bindCount() + 1)); + } + +} diff --git a/__bind.h b/__bind.h new file mode 100644 index 0000000..8b5aac4 --- /dev/null +++ b/__bind.h @@ -0,0 +1,21 @@ +#ifndef ____bind_h__ +#define ____bind_h__ + +#include "Tag.h" + +namespace jet { + + // - supplies + // the value for the next '?' placeholder of the enclosing . Always + // void (no closing tag) and produces no output. See + // docs/design-sql-bound-parameters.md. + class __bind : public Tag { + + public: + __bind(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local); + + }; + +} + +#endif diff --git a/__mysql.cpp b/__mysql.cpp index e458823..a765e1b 100644 --- a/__mysql.cpp +++ b/__mysql.cpp @@ -108,6 +108,10 @@ namespace jet { if(!mysql) { MYSQL *handle = mysql_init(NULL); + // Pin the connection character set so 's client-side + // escaping and the server's parsing always agree ( refuses + // SET NAMES and friends for the same reason). + mysql_options(handle, MYSQL_SET_CHARSET_NAME, "utf8mb4"); mysql = mysql_real_connect(handle, hostVal.c_str(), userVal.c_str(), @@ -207,6 +211,19 @@ namespace jet { } } + std::string __mysql::escapeString(const std::string &value) { + std::string to(value.size() * 2 + 1, '\0'); + unsigned long len = mysql_real_escape_string_quote(mysql, to.data(), value.data(), value.size(), '\''); + if(len == (unsigned long)-1) + throw coreutils::Exception("sql: bind value could not be escaped."); + to.resize(len); + return to; + } + + bool __mysql::noBackslashEscapes() { + return (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES) != 0; + } + void __mysql::nextRow() { row = mysql_fetch_row(result); fieldLength = mysql_fetch_lengths(result); diff --git a/__mysql.h b/__mysql.h index 5119365..8d3170a 100644 --- a/__mysql.h +++ b/__mysql.h @@ -21,6 +21,13 @@ namespace jet { void nextRow(); bool hasRow(); coreutils::ZString getColumnValue(coreutils::ZString column); + + // Body of a single-quoted SQL string literal for this connection + // (mysql_real_escape_string_quote: uses the connection's character + // set and honours NO_BACKSLASH_ESCAPES). Used by . + std::string escapeString(const std::string &value); + // True while the session's sql_mode includes NO_BACKSLASH_ESCAPES. + bool noBackslashEscapes(); private: // Explicitly initialized - result/row/fieldLength were otherwise diff --git a/__sql.cpp b/__sql.cpp index abaa648..2349405 100644 --- a/__sql.cpp +++ b/__sql.cpp @@ -26,7 +26,21 @@ namespace jet { if(!global.sessionExists(sessionId)) throw coreutils::Exception("sessionid does not exist."); processContainer(container); - global.getSession(sessionId)->query(out); + __mysql *session = global.getSession(sessionId); + // No inside: the query runs exactly as rendered, with no '?' + // replacement at all, so existing queries containing a literal '?' + // are unaffected. See docs/design-sql-bound-parameters.md. + if(binds.empty()) { + if(isCharsetChange(out.str())) + throw coreutils::Exception("sql: changing the connection character set is not allowed."); + session->query(out); + return; + } + std::string bound = bindPlaceholders(out.str(), binds, session->noBackslashEscapes(), + [session](const std::string &s) { return session->escapeString(s); }); + if(isCharsetChange(bound)) + throw coreutils::Exception("sql: changing the connection character set is not allowed."); + session->query(coreutils::MString(bound)); } } diff --git a/__sql.h b/__sql.h index 60374d8..22b06c8 100644 --- a/__sql.h +++ b/__sql.h @@ -2,16 +2,25 @@ #define ____sql_h__ #include "Tag.h" +#include "SqlBind.h" +#include namespace jet { - + class __sql : public Tag { - + public: __sql(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local); - + + // Called by each inside this while its body renders. + void addBind(BoundValue value) { binds.push_back(std::move(value)); } + size_t bindCount() const { return binds.size(); } + + private: + std::vector binds; + }; - + } #endif diff --git a/docs/design-sql-bound-parameters.md b/docs/design-sql-bound-parameters.md new file mode 100644 index 0000000..1f5ddfb --- /dev/null +++ b/docs/design-sql-bound-parameters.md @@ -0,0 +1,225 @@ +# Design: bound parameters for `` + +**Status:** draft for review · **Author:** Brad Arant (drafted with Claude) · **Date:** 2026-10-23 +**Applies to:** JetCore (`__sql`, `__mysql`, `Tag.cpp` tag dispatch, `jetc` parser/codegen) + +## 1. The problem + +`` builds its query by rendering its body as text, then sending the finished +string to MySQL: + +```cpp +// __sql.cpp today +processContainer(container); // every $[...] substituted as plain text +global.getSession(sessionId)->query(out); // mysql_real_query(the whole string) +``` + +So a request value lands *inside the SQL text*: + +```html +select id from users where email='$[email]' and secret='$[password]' +``` + +With `email = x' or '1'='1' -- ` that becomes a different query. There is no +escaping function in Jet, so this is the only way to write a query, and it's +unsafe everywhere a value comes from a request. Numeric positions are worse: +`where id=$[id]` has no quotes, so even escaping wouldn't help (`id = 1 or 1=1`). + +Measured on 2026-10-23 (`` blocks whose body contains a request/session +value other than `$[.column]`, `$[_login_id]` or `$[_session_id]`): + +| Site | `` blocks | with values pasted in | files | +|---|---:|---:|---:| +| barant.com | 152 | 117 | 64 | +| ClockPlanet | 94 | 83 | 35 | + +Roughly 80% of all queries. Login, signup, cart, checkout and admin pages are +all in that set. + +## 2. Goals + +1. A way to pass values to `` that **can never change the query's meaning**, + whatever they contain. +2. **No change to how results are read** - ``, ``, `$[.column]`, + `$[?N]`, `$[#]` keep working exactly as today. +3. Easy, mechanical migration of existing pages; old pages keep running until migrated. +4. A path to *enforcing* it, so new unsafe queries can't be written by accident. + +Non-goals: an ORM, query builders, or changing ``/`` connection handling. + +## 3. Proposed syntax + +Placeholders are `?` in the query; values come from `` tags inside the +``, in order: + +```html + + select id, name from users where email=? and status=? + + + +``` + +`` attributes: + +| attribute | meaning | +|---|---| +| `value` | the value (any Jet expression - `$[...]` is fine here, that's the point) | +| `type` | `string` (default), `int`, `decimal`, `bool`, `date`, `datetime`, `ident` | +| `null` | `empty` = bind SQL `NULL` when the value is empty (default: bind `''`) | +| `allow` | for `type="ident"` only: comma list of permitted identifiers | + +Type behaviour (a value that doesn't fit throws a catchable Jet exception - +``/`` works, see §6): + +- `string` - quoted and escaped (§4). +- `int` - must match `^-?[0-9]{1,19}$`; emitted bare. This fixes the unquoted + `where id=$[id]` case. +- `decimal` - must match `^-?[0-9]{1,30}(\.[0-9]{1,30})?$`; emitted bare. +- `bool` - `1/0/true/false/yes/no` → `1`/`0`. +- `date` / `datetime` - must parse as `YYYY-MM-DD` / `YYYY-MM-DD HH:MM[:SS]`; quoted. +- `ident` - for the things SQL can't parameterise (column in `ORDER BY`, a sort + direction, a table name). The value must be in `allow` or it throws; emitted + backtick-quoted (`ASC`/`DESC` emitted bare). Example: + + ```html + select * from products order by ? ? + + + + ``` + +Why `` and not ``: `param` is an HTML element (inside ``), +and `jetc`'s void-tag list treats it as HTML. A Jet-only name avoids any ambiguity. + +Why positional `?` and not named (`:email`): `?` is what MySQL and every SQL +library uses, it needs no new attribute-name syntax in the parser, and the +`` lines sit right under the query so the order is easy to read. Named +binds can be added later without breaking this (§9). + +## 4. How it works + +### Emulated binding (recommended for phase 1) + +`__sql` collects the `` values while its body renders, then replaces each +`?` that is **outside** a string literal, backtick identifier or comment with the +bound value, rendered safely - and only then calls `query()` as today. + +- Strings are escaped with `mysql_real_escape_string_quote(mysql, to, from, len, '\'')` + and wrapped in single quotes. This uses the *connection's* character set and + respects `NO_BACKSLASH_ESCAPES`, which plain `mysql_real_escape_string` doesn't. +- The connection's character set is pinned at connect time + (`mysql_options(MYSQL_SET_CHARSET_NAME, "utf8mb4")` in `__mysql`), so escaping + and server parsing always agree. `` refuses a `SET NAMES`/`SET CHARACTER SET` + statement (the classic way to make escaping and parsing disagree). +- Number of `?` must equal number of ``s, otherwise throw + `"sql: 3 placeholders but 2 bind values"`. + +This is the same approach PHP's PDO uses by default. Its big advantage here: +**the result path doesn't change at all** - `query()` still runs +`mysql_real_query` and `mysql_store_result`, so `MYSQL_ROW`, `getColumnValue()`, +``, `` and the column-index map are untouched. + +### Why not real server-side prepared statements (yet) + +`mysql_stmt_*` returns rows through typed bind buffers, not `MYSQL_ROW`, so +`getColumnValue()`, `$[?N]`, `$[#]` and the field-index map would all need a +second implementation, and some statements (multi-statements, some DDL) can't be +prepared. That's a bigger change for no extra safety over correct emulation. +It stays open as a later option (§9), behind the same `` syntax, so pages +written now won't need touching again. + +### Code shape + +- **New** `__bind.h/.cpp`: on construction, find the nearest enclosing `__sql` + (walk `parent` with `dynamic_cast<__sql*>`; throw `"bind must be inside sql"` + if none), resolve `value`/`type`/`null`/`allow`, validate per type, and call + `sql->addBind(...)`. Produces no output. +- **`__sql`**: a `std::vector` member and `addBind()`. After + `processContainer(container)`, run `bindPlaceholders(out)` - a small scanner + that tracks `'...'` (with `''` and `\'`), `"..."`, `` `...` ``, `-- `, `#` and + `/* */` so `?` inside them is left alone - then `query(result)`. +- **`__mysql`**: `escapeString(const MString&)` wrapper around + `mysql_real_escape_string_quote`; pin `utf8mb4` at connect. +- **`Tag.cpp`**: add `ifTagName(in, "bind")` next to `sql` (line ~494). +- **`jetc` `JetParser.cpp`**: add `"bind"` to the Jet void-tag list (it has no + closing tag); **`JetCodegen.cpp`**: emit `__bind` like the other tags. + +Estimated size: ~200 lines plus tests. + +## 5. Existing pages: nothing breaks + +A `` with no `` runs exactly as today - no `?` replacement at all, +so existing queries containing a literal `?` are unaffected. Migration is +page by page: + +```html + +select * from users where email="$[email]" and status="CONFIRM" + +select * from users where email=? and status="CONFIRM" + + + + +delete from cart_item where session_uuid='$[_session_id]' and sku='$[sku]' + +delete from cart_item where session_uuid=? and sku=? + + + +``` + +## 6. Errors + +- Bad `` value (wrong type, ident not allowed), placeholder/bind count + mismatch, `` outside ``: throw `coreutils::Exception` with a clear + message, caught by ``/`` like the MySQL errors fixed on 2026-09-13. +- The exception text never includes the bound value itself (it may be a password). + +## 7. Enforcement + +Once pages are migrated, make the unsafe form an error instead of a habit: + +1. **`jetc --lint-sql`** - lists every `` whose body contains `$[...]` + other than `$[.column]` (row values from an outer query), with file:line. + This is the migration worklist (§8) and a CI check. +2. **Per-site strict mode** - an APIServer site setting + `"sqlInterpolation": "warn" | "error"`. In `error` mode `` refuses to run + if its body substituted any `$[...]` except `$[.column]`; `warn` logs it. + Default `warn` for existing sites, `error` for new ones. +3. **Escape hatch** - `` for the rare trusted dynamic SQL + (generated by the site itself, never from input), so it's visible in review + and greppable. + +## 8. Migration plan + +| Phase | What | Notes | +|---|---|---| +| 1 | Implement ``, escaping, utf8mb4 pinning, tests | JetCore only; no page changes | +| 2 | `jetc --lint-sql`; run on both sites | produces the worklist (≈200 queries) | +| 3 | Convert the high-risk pages first | login/signup/password reset, cart/checkout/payment (`pp.jet`), admin procs, ClockPlanet `mobile_api.jet` and auth | +| 4 | Convert the rest; turn on `warn` | logs show anything missed | +| 5 | Switch both sites to `error` | unsafe queries can no longer ship | + +Phase 3 is the one that matters most for security and is a few days' work; +each conversion is mechanical and testable page by page. + +## 9. Later + +- Named binds: `where email=:email` + ``. +- Real prepared statements (`mysql_stmt_*`) behind the same `` syntax. +- The companion fix for output: HTML-escape `$[...]` by default with an explicit + raw form - the same "safe unless you say otherwise" rule for the other half of + the injection problem (cross-site scripting). + +## 10. Tests + +- Every type: valid values bind correctly; invalid values throw. +- Injection strings (`' or '1'='1`, `\'`, multibyte sequences such as `0xbf27`, + `--`, `;`, NUL bytes) are stored and read back byte-for-byte unchanged. +- `?` inside `'...'`, `"..."`, `` `...` ``, `--`, `#` and `/* */` is never replaced. +- Placeholder/bind count mismatch throws; `` outside `` throws. +- A `` with no binds produces exactly the same query text as before. +- ``/``/`$[.col]`/`$[?N]`/`$[#]` behave identically on bound queries. +- `NO_BACKSLASH_ESCAPES` on and off. diff --git a/jetc/JetCodegen.cpp b/jetc/JetCodegen.cpp index ba9fafa..07fe49b 100644 --- a/jetc/JetCodegen.cpp +++ b/jetc/JetCodegen.cpp @@ -32,6 +32,13 @@ namespace jetc { return r; } + std::string Codegen::zstr(const std::string &raw) { + // A plain C string literal stops at its first NUL, so text holding + // one (raw bytes in a page) has to carry its length explicitly. + if (raw.find('\0') == std::string::npos) return cstr(raw); + return "coreutils::ZString(" + cstr(raw) + ", " + std::to_string(raw.size()) + ")"; + } + const char *Codegen::staticTypeOp(const std::string &type, const char *rc) { // type is known at compile time (a literal attribute value, not a // runtime expression) so the comparison operator is picked once here @@ -59,7 +66,7 @@ namespace jetc { case NodeKind::TEXT: { if (n->text.empty()) break; - target << I << "out << " << cstr(n->text) << ";\n"; + target << I << "out << " << zstr(n->text) << ";\n"; break; } @@ -166,7 +173,7 @@ namespace jetc { } case NodeKind::FALLBACK: { - target << I << "{ coreutils::ZString span(" << cstr(n->rawSpan) << "); scope.interpretSpan(span, out); }\n"; + target << I << "{ coreutils::ZString span(" << zstr(n->rawSpan) << "); scope.interpretSpan(span, out); }\n"; break; } @@ -213,7 +220,7 @@ namespace jetc { // correctness fallback: any invocation this compiler doesn't // itself recognize - e.g. reached through content this pass // never compiled - still resolves it exactly as it always has). - target << I << "{ coreutils::ZString span(" << cstr(n->rawSpan) << "); scope.interpretSpan(span, out); }\n"; + target << I << "{ coreutils::ZString span(" << zstr(n->rawSpan) << "); scope.interpretSpan(span, out); }\n"; if (compiledOk) { // The registration into compiledTags right after is the only // new behavior beyond the raw registration above - a pure diff --git a/jetc/JetCodegen.h b/jetc/JetCodegen.h index e1d3149..60e3715 100644 --- a/jetc/JetCodegen.h +++ b/jetc/JetCodegen.h @@ -86,6 +86,7 @@ namespace jetc { void emitNode(const NodePtr &n, int indent, std::ostringstream &target); static std::string ind(int indent); static std::string cstr(const std::string &raw); // C++ string-literal escape + static std::string zstr(const std::string &raw); // same, as a sized ZString if it holds a NUL static const char *staticTypeOp(const std::string &type, const char *rcVar); }; diff --git a/jetc/JetParser.cpp b/jetc/JetParser.cpp index d4c792b..d069fea 100755 --- a/jetc/JetParser.cpp +++ b/jetc/JetParser.cpp @@ -83,6 +83,11 @@ namespace jetc { // otherwise), it was previously miscategorized as void. "set", "include", "cookie", "header", "read", "write", "call", "system", "dump", + // (see __bind.cpp) supplies one placeholder value + // and never has a closing tag - without this a bare + // outside a span would scan forward + // for a "" that doesn't exist. + "bind", // (see parseContainerSlot/Tag::renderContainer) - // real content always writes it as a bare marker with no // separate closing tag, even when it carries its own diff --git a/tests/gentestbind.py b/tests/gentestbind.py new file mode 100644 index 0000000..094bf9b --- /dev/null +++ b/tests/gentestbind.py @@ -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'\n') +w(b'\n' + b' End-to-end tests for / - 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'\n') +w(b'\n') +w(b'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)\n') + +def section(name, mode): + for label, raw, q in values: + if len(q) > 1: + w(b'select ' + q.encode() + b' as v\n') + else: + qb = q.encode() + w(b'\n') + w(b'insert into bindtest (s) values (?)\n') + w(b'select hex(s) as h from bindtest where id=last_insert_id()\n') + w(('%s %s: $[.h]\n' % (mode, label)).encode()) + E.append('%s %s: %s' % (mode, label, raw.hex().upper())) + +w(b' Injection strings and odd bytes come back byte-for-byte. \n') +section("roundtrip", "default") +w(b'set session sql_mode = concat(@@sql_mode, \',NO_BACKSLASH_ESCAPES\')\n') +section("roundtrip", "nbe") +w(b'set session sql_mode = replace(@@sql_mode, \'NO_BACKSLASH_ESCAPES\', \'\')\n') + +tail = r''' Every type. + +insert into bindtest (n, dec_v, d, dt, b, t) values (?, ?, ?, ?, ?, ?) + + + + + + + +select n, dec_v, d, dt, b, t is null as tnull from bindtest where id=last_insert_id() +types: $[.n] $[.dec_v] $[.d] $[.dt] $[.b] $[.tnull] + ? inside literals, identifiers and comments is left alone. +select '?' as a, "?" as `b?`, ? as c /* ? */ -- ? + + +literals: $[.a] $[.b?] $[.c] + A query with no bind runs unchanged, even with a literal ?. +select concat('what', '?') as q +nobind: $[.q] + bind inside if inside sql. + +select ? as x + + +nested: $[.x] + ident with allow, order by. +insert into bindtest (n) values (5), (3), (9) + + +select n from bindtest where n in (?, ?, ?) order by ? ? + + + + +ident: $[.n] + Result access on a bound query: whilerow, column names and counts. +select n, s from bindtest where n > ? order by n +rows: $[.#] columns: $[.?#] first column: $[.?1] second column: $[.?2] + Errors - all catchable, none echo the value. +select ?, ?, ?mismatch: $[err] +select ?badint: $[err] +select ?baddate: $[err] +select ? from bindtestbadident: $[err] +outside: $[err] +select 1novalue: $[err] +select ?badtype: $[err] +SET NAMES latin1setnames: $[err] +set character_set_client = gbksetcharset: $[err] +select @@character_set_client as cs +charset: $[.cs] + + +''' +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") diff --git a/tests/runbindtests.sh b/tests/runbindtests.sh new file mode 100755 index 0000000..a03574b --- /dev/null +++ b/tests/runbindtests.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# Tests for / (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 - is void in the compiler too, and a +# compiled page runs the same / 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 must not swallow the content after it looking for . +printf 'Aselect ?BC\n' > "$WORK/void.jet" +"$WORK/jetc" "$WORK/void.jet" "$WORK/void.cpp" >/dev/null || fail=1 +if grep -q 'select ?' "$WORK/void.cpp" && + grep -q '""' "$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 (the interpreter stops + # at ; 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 diff --git a/tests/sqlbind_test.cpp b/tests/sqlbind_test.cpp new file mode 100644 index 0000000..2336c32 --- /dev/null +++ b/tests/sqlbind_test.cpp @@ -0,0 +1,146 @@ +// Unit tests for SqlBind (the MySQL-free part of /) - +// see docs/design-sql-bound-parameters.md section 10. +// Built and run by tests/runbindtests.sh. + +#include "SqlBind.h" +#include "Exception.h" +#include +#include +#include + +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 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; +} diff --git a/tests/testbind.expected b/tests/testbind.expected new file mode 100644 index 0000000..cca49d4 --- /dev/null +++ b/tests/testbind.expected @@ -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 diff --git a/tests/testbind.jet b/tests/testbind.jet new file mode 100644 index 0000000000000000000000000000000000000000..dc3509736141500b9e5909eeac5c03a911d52311 GIT binary patch literal 8405 zcmds7%W~Vu70sGoaR;LsQd0ye(smNtq-fdZWRmz5QB$duqT)1xrr2YkA)yorTAIFR(@c6~Qx&h?IIi`AB#x!F$2A)2(6@;%bx2kkYiOFJ)HB!7al1EB zIy@$yjFc26foX@*s7vi*)So07R$hrz#1f~PRvfl!bed_J%=4YvX{LDxw{6SJNL&h~ zjU_>;6cN3hkN)%qd#5)teNKsv=5#iE`{DiR$?0EeWW^+shD0hUGcs(FNk}Jlrt~Ex znT^y9M(s>ekZ3Ifs}kL!S4j+sWC$6o^nt|ZiyGZ3J6j0F(y&x5d3S|nGHlWBq?W0` zC7@zA%TiJ*Zt3XmJt=BGf*Nf-S1kDm-``*F%SAQ6uoef84A3`^qMx(lmf{FsncdhQlk-YuAk6bp_HiLV3{ox;?XGCTck zJb8TBYqRi6N2K5DA95>_fxqfYm>!Bx!rYbyhKYen1RvLJ+oDy2@Ch0Y+4pWdND*bY zn#m|i3T?r%GPMjYSy7QW%qJUa3=W;w7mE+hZ=mTySQgpH2y)U)-ZijH7@E;EP2%Nl zw=)T!Rbf98#zNzK;8=x?0&*y)B8y;w2!8gAdJ4;Lj}OjUvx{Ck{{hQZ0J*CZLbKkf z_v@V<#Dic{g$*A9>#y}41jCMIV8EGwoh4Ro#j|t>B#uDXMqSjK&gC0VyNAv{ZgTHg z*tycOFn;c4rTMY%4!NpLg~9{m(l6DrRUs9A>-)5W77k%t{(A_L=M<*N)gr^%=j8i4 z2+YV>1xb`VD7XB}<(5vr15CSUt;@+re=_a<<^k5K8!;zuro8&%Nx4G-6($)T(x1sK z6`^!H6LzqJK6jm=szL?!0P}d_SCbt?qO<5BAZGvm2dWGD-;Rf)S0^8a zqk2)>R6gBytpL*#xi`fZW7&(si-DBY#b*-ZeivP=iG3=#7|-4nTMTNi3M~e-CqP|%Tc}H`0&Mq9(cSa*%dadNrTmYhczPFAjohW_(~S2?}1mtd7E?mPPg^ssF<^0 z6D#I*kGjA1J4b%^Q8^bx<0mIl=kc?nC#%z$j;vTRm!_CCNs_2yhQ}@%^*s=&c8>81 z(yvlmq>Nk1T%hGG9>@o>=FDn!t}h6LkfUDntNhctuZjC~=d93$rFRTKZ5oB@JyKv? zMP!;_BEk2nsRBpuDAmPW!*WM*3T7Bw!KO1UORV~!&d5ZQyn)A8-%keL!zcJMp!V0e z`nt`m!Qh_VU5j9;$wH)6tRe?Ba9=?cq7%-4@OVez>zuVEbQTO{8q~7BsAmOzp|7!u z<*o{g^O1!D@jiRo#7jj8#(py200Sjf(O~LrYj?hTNU8#^E7o>3J$-#?~!F4`wv6&GM&H;7Hij&c+=4-rIF9XL}2R6K&sYs$(uxN3l9(N6pEqd)@1llZ9 z!KpOdW)MMuH+!_#&~|eHna9&JD%OCmPNFQz}}0AVq*_LqiKWj$|-PT+h(O%sM`s&W~L~tq926q1GUXjhF+0wIKK1NejEk6qbpV ziTQLE95)bjd_b*t@0hg%I#3>gg{@Fk@7@s;5yBxKc7S<=1|YmMe_V4PZ#x9+&l5g- zz`84};VHV9#evR~*JERw0vR1gQswchiSG^w?&$^{Vtx!(E?(*4b)2+Yji?jnD4*M9 zu!i}r06U42PdEthu@ScJJsO@7w~0p-3A9!_+w_F&$sJD@Hd`DGK~(f$$Cy42BE>D> wKELpNVq0E>>6;R+sPDy#%8j`)F!{!{a+77BGcYTUptkFw^YaS-gW1N*8l(j literal 0 HcmV?d00001