diff --git a/README.md b/README.md index 958e533..0d02b27 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Connection object methods: - `update(table, values, where?)` — update records. - `upsert(table, values, conflict_columns)` — insert or update on conflict. - `delete(table, where?)` — delete records. -- `select(table, columns?, where?, order_by?, limit?, offset?)` — filtered/sorted read. +- `select(table, columns?, where?, order_by?, limit?, offset?)` — filtered/sorted read (`order_by` is whitelist-validated, not parameterized). - `count(table, where?)` — count matching records. - `attach(database, alias)` / `detach(alias)` — multi-database operations. - `tables()` / `table_info(table)` / `indices(table)` — schema introspection. diff --git a/docs/API.md b/docs/API.md index 75c6f80..3a01873 100644 --- a/docs/API.md +++ b/docs/API.md @@ -682,7 +682,13 @@ Selects records from a table with flexible filtering and sorting options. - `None`: No filtering - String: Simple where clause with no parameters (e.g. `"age > 18"`) - List: Where clause with parameters as `[condition, param1, param2, …]` -- `order_by` (string): Optional ORDER BY clause (e.g. `"name ASC"`, `"age DESC"`) +- `order_by` (string): Optional ORDER BY clause (e.g. `"name ASC"`, `"age DESC"`). + ORDER BY cannot be parameterized, so this value is validated against a strict + whitelist before use: a comma-separated list of column identifiers (bare, + dotted `table.col`, or double-quoted/backtick-quoted), each with an optional + `ASC`/`DESC` and optional `NULLS FIRST`/`NULLS LAST`. Anything else (raw SQL, + semicolons, parentheses, comments, other keywords) raises + `invalid order_by clause`. - `limit` (int): Optional maximum number of rows to return - `offset` (int): Optional number of rows to skip @@ -880,6 +886,11 @@ Registers a custom SQL function that can be called from SQL queries. **Raises:** Error on failure (empty name, non-callable, `num_args < -1`, duplicate registration, etc.). +> **Return values:** A function's return value is converted to a SQLite value +> the same way bind parameters are. An integer return that does not fit a +> signed 64-bit value raises `int value too large for SQLite` rather than +> being silently truncated. + **Example:** ```python diff --git a/example_test.go b/example_test.go index 1914a11..70640ca 100644 --- a/example_test.go +++ b/example_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strings" + "sync/atomic" "testing" "github.com/1set/starlet" @@ -12,6 +13,16 @@ import ( "go.starlark.net/starlark" ) +// funcNameCounter makes custom-function names unique within a process. The +// register_function registry is process-global, so reusing a name across runs +// (e.g. under -count>1) would fail with "already registered"; a per-call suffix +// keeps registration-based tests repeatable. +var funcNameCounter int64 + +func uniqueFuncName(prefix string) string { + return fmt.Sprintf("%s_%d", prefix, atomic.AddInt64(&funcNameCounter, 1)) +} + // TestStarlarkScripts runs Starlark test scripts from the test directory. // Scripts with "test-" prefix should succeed, "panic-" prefix should fail. func TestStarlarkScripts(t *testing.T) { @@ -2300,3 +2311,169 @@ main() return NewModule().LoadModule() }) } + +// ============================================================================ +// Hardening: custom-function return values (PKG-25) +// +// A custom SQL function whose return value does not fit an int64 must surface a +// clean error instead of silently truncating to a wrong value, mirroring the +// bind-parameter path (starlarkToSQLiteValue). In-range ints still round-trip. +// ============================================================================ + +func TestRegisteredFunctionOutOfRangeIntErrors(t *testing.T) { + // A function returning (1<<100)+7 cannot fit an int64; the wrapper must + // reject it with a clear error rather than returning 0 / a garbage value. + regFunc := ®isteredFunction{ + name: "TEST_BIGINT", + starlarkFunc: starlark.NewBuiltin("TEST_BIGINT", func(_ *starlark.Thread, _ *starlark.Builtin, _ starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) { + big := starlark.MakeInt(1).Lsh(100) // 1 << 100 + return big.Add(starlark.MakeInt(7)), nil // (1<<100)+7 + }), + } + wrapper := createGoFunctionWrapper(regFunc) + val, err := wrapper(nil, nil) + if err == nil { + t.Fatalf("expected out-of-range int return to error, got value %v", val) + } + if !strings.Contains(err.Error(), "int value too large for SQLite") { + t.Fatalf("unexpected error for out-of-range int: %v", err) + } + + // An in-range int return must still round-trip unchanged. + okFunc := ®isteredFunction{ + name: "TEST_INRANGE", + starlarkFunc: starlark.NewBuiltin("TEST_INRANGE", func(_ *starlark.Thread, _ *starlark.Builtin, _ starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) { + return starlark.MakeInt64(1234567890), nil + }), + } + got, err := createGoFunctionWrapper(okFunc)(nil, nil) + if err != nil { + t.Fatalf("in-range int return should not error: %v", err) + } + if got != int64(1234567890) { + t.Fatalf("expected in-range int 1234567890, got %v (%T)", got, got) + } +} + +// TestRegisteredFunctionBigIntScript exercises the same property end-to-end +// through a query that calls the registered function. Function names are made +// unique per run because the registry is process-global (so the test is +// repeatable under -count>1). +func TestRegisteredFunctionBigIntScript(t *testing.T) { + bigName := uniqueFuncName("BIG_VALUE") + bigScript := fmt.Sprintf(` +load("sqlite", "connect", "register_function") + +def main(): + register_function(%q, lambda: (1 << 100) + 7, num_args=0) + db = connect(":memory:") + db.execute("CREATE TABLE t (id INTEGER)") + db.execute("INSERT INTO t (id) VALUES (1)") + db.query("SELECT %s() FROM t") + +main() +`, bigName, bigName) + requireSQLiteScriptErrorContains(t, bigScript, func() starlet.ModuleLoader { + return NewModule().LoadModule() + }, "int value too large for SQLite") + + sqName := uniqueFuncName("SQUARE") + inRangeScript := fmt.Sprintf(` +load("sqlite", "connect", "register_function") + +def main(): + register_function(%q, lambda x: x * x, num_args=1) + db = connect(":memory:") + db.execute("CREATE TABLE t (id INTEGER)") + db.execute("INSERT INTO t (id) VALUES (9)") + rows = db.query("SELECT %s(id) AS s FROM t") + if rows[0]["s"] != 81: + fail("expected 81, got {}".format(rows[0]["s"])) + +main() +`, sqName, sqName) + requireSQLiteScript(t, inRangeScript, func() starlet.ModuleLoader { + return NewModule().LoadModule() + }) +} + +// ============================================================================ +// Hardening: order_by whitelist validation (PKG-26) +// +// order_by cannot be parameterized (SQL forbids bind params there) and is +// interpolated into the statement, so it must be validated against a strict +// whitelist: comma-separated (optionally dotted/quoted) column identifiers, +// each with an optional ASC/DESC and optional NULLS FIRST/LAST. Anything else +// (semicolons, parens, comments, arbitrary keywords) is rejected. +// ============================================================================ + +func TestSelectOrderByRejectsInjection(t *testing.T) { + payloads := []string{ + "id; DROP TABLE t", + "id) ; --", + "id; DELETE FROM t", + "1; DROP TABLE t --", + "id /* comment */", + "id UNION SELECT 1", + "(SELECT 1)", + "id, (SELECT password FROM users)", + "name COLLATE NOCASE", + "bad name", + } + for _, payload := range payloads { + payload := payload + t.Run(payload, func(t *testing.T) { + script := fmt.Sprintf(` +load("sqlite", "connect") + +def main(): + db = connect(":memory:") + db.execute("CREATE TABLE t (id INTEGER, name TEXT)") + db.execute("INSERT INTO t (id, name) VALUES (1, 'a')") + db.select("t", "*", order_by=%s) + +main() +`, quoteStarlarkString(payload)) + requireSQLiteScriptErrorContains(t, script, func() starlet.ModuleLoader { + return NewModule().LoadModule() + }, "invalid order_by clause") + }) + } +} + +func TestSelectOrderByAllowsLegitClauses(t *testing.T) { + const script = ` +load("sqlite", "connect") + +def main(): + db = connect(":memory:") + db.execute("CREATE TABLE t (id INTEGER, name TEXT, age INTEGER)") + for row in [(3, "c", 30), (1, "a", 10), (2, "b", 20)]: + db.execute("INSERT INTO t (id, name, age) VALUES (?, ?, ?)", list(row)) + + # Plain column name orders ascending. + by_name = db.select("t", ["name"], order_by="name") + if [r["name"] for r in by_name] != ["a", "b", "c"]: + fail("name ordering wrong: {}".format(by_name)) + + # Single column DESC. + by_age = db.select("t", ["age"], order_by="age DESC") + if [r["age"] for r in by_age] != [30, 20, 10]: + fail("age DESC ordering wrong: {}".format(by_age)) + + # Multiple columns with a trailing direction. + multi = db.select("t", ["id", "name"], order_by="id, name ASC") + if [r["id"] for r in multi] != [1, 2, 3]: + fail("multi ordering wrong: {}".format(multi)) + + # Dotted, quoted, and NULLS modifiers are accepted. + db.select("t", "*", order_by="t.id ASC") + db.select("t", "*", order_by='"name" DESC') + db.select("t", "*", order_by="age DESC NULLS LAST") + +main() +` + requireSQLiteScript(t, script, func() starlet.ModuleLoader { + return NewModule().LoadModule() + }) +} diff --git a/function.go b/function.go index 56ab1a9..7a2ee7b 100644 --- a/function.go +++ b/function.go @@ -209,7 +209,13 @@ func starlarkToDriverValue(val starlark.Value) (driver.Value, error) { case starlark.Bool: return bool(v), nil case starlark.Int: - i64, _ := v.Int64() + // Mirror the bind-parameter path (starlarkToSQLiteValue): an int that + // does not fit int64 must surface a clear error, never silently + // truncate to a wrong driver value. + i64, ok := v.Int64() + if !ok { + return nil, fmt.Errorf("int value too large for SQLite: %v", v) + } return i64, nil case starlark.Float: return float64(v), nil diff --git a/operations.go b/operations.go index 6f22b69..27af24a 100644 --- a/operations.go +++ b/operations.go @@ -4,12 +4,44 @@ import ( "database/sql" "errors" "fmt" + "regexp" "strings" "go.starlark.net/starlark" sqlite "modernc.org/sqlite" ) +// orderByTermPattern matches a single ORDER BY term: a column identifier +// (bare like `col`, dotted like `tbl.col`, or double-quoted/backtick-quoted), +// followed by an optional ASC/DESC direction and an optional NULLS FIRST/LAST +// modifier (all case-insensitive). Identifiers are restricted to +// letters/digits/underscore so no SQL syntax (parens, semicolons, comments, +// operators, spaces-in-names, arbitrary keywords) can slip through. +var orderByTermPattern = regexp.MustCompile( + `^(?:[A-Za-z_][A-Za-z0-9_]*|"[^"]+"|` + "`[^`]+`)" + + `(?:\.(?:[A-Za-z_][A-Za-z0-9_]*|"[^"]+"|` + "`[^`]+`))?" + + `(?i:\s+(?:ASC|DESC))?` + + `(?i:\s+NULLS\s+(?:FIRST|LAST))?$`, +) + +// validateOrderBy validates an order_by clause before it is interpolated into +// a SQL statement. ORDER BY cannot be parameterized, so the value is checked +// against a strict whitelist instead: a comma-separated list of column +// identifiers, each with an optional ASC/DESC and optional NULLS FIRST/LAST. +// Anything else is rejected to close the raw-interpolation injection surface. +func validateOrderBy(orderBy string) error { + if strings.TrimSpace(orderBy) == "" { + return nil + } + for _, term := range strings.Split(orderBy, ",") { + t := strings.TrimSpace(term) + if t == "" || !orderByTermPattern.MatchString(t) { + return fmt.Errorf("invalid order_by clause: %q", orderBy) + } + } + return nil +} + // isTableAlreadyExistsError checks if the error is specifically about a table already existing. // Uses SQLite's semantic error codes rather than string matching. func isTableAlreadyExistsError(err error) bool { @@ -653,8 +685,13 @@ func (db *database) selectRecords(_ *starlark.Thread, fn *starlark.Builtin, args query += " WHERE " + whereClause } - // Add ORDER BY clause if provided + // Add ORDER BY clause if provided. order_by is interpolated raw (SQL does + // not allow bind parameters there), so validate it against a strict + // whitelist first to prevent injection. if orderBy != "" { + if err := validateOrderBy(orderBy); err != nil { + return nil, err + } query += " ORDER BY " + orderBy }