Skip to content

Commit

Permalink
Account for CR and other whitespace in prepared statements, add tests (
Browse files Browse the repository at this point in the history
…#1322)

* Account for CR in prepared statements tail, add tests
* Add additional whitespace characters
* Add \r\n into whitespace test
  • Loading branch information
KianNH authored Jul 4, 2024
1 parent c4d67b9 commit de0c66d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/workerd/api/sql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ async function test(storage) {
'Error: Wrong number of parameter bindings for SQL query.'
)

// Prepared statement with whitespace
const whitespace = [' ', '\t', '\n', '\r', '\v', '\f', '\r\n']

for (const char of whitespace) {
const prepared = sql.prepare(`SELECT 1;${char}`);
const result = [...prepared()]

assert.equal(result.length, 1)
}

// Prepared statement with multiple statements
assert.throws(() => {
sql.prepare('SELECT 1; SELECT 2;');
}, /A prepared SQL statement must contain only one statement./)

// Accessing a hidden _cf_ table
assert.throws(
() => sql.exec('CREATE TABLE _cf_invalid (name TEXT)'),
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/util/sqlite.c++
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ kj::Own<sqlite3_stmt> SqliteDatabase::prepareSql(
SQLITE_REQUIRE(result != nullptr, "SQL code did not contain a statement.", sqlCode);
auto ownResult = ownSqlite(result);

while (*tail == ' ' || *tail == '\n') ++tail;
while (*tail == ' ' || *tail == '\t' || *tail == '\n' || *tail == '\r' || *tail == '\v' || *tail == '\f') ++tail;

switch (multi) {
case SINGLE:
Expand Down

0 comments on commit de0c66d

Please sign in to comment.