Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
177 changes: 177 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import (
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"

"github.com/1set/starlet"
"github.com/starpkg/base"
"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) {
Expand Down Expand Up @@ -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 := &registeredFunction{
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 := &registeredFunction{
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()
})
}
8 changes: 7 additions & 1 deletion function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Suggestion: The PR title includes 'order_by validation (PKG-26)', but no logic or tests related to ORDER BY clauses are found in this diff. Please check if the validation logic for PKG-26 was accidentally omitted or if it should be handled in a separate PR.

// 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ LOW RISK

Suggestion: The term 'too large' is inaccurate for negative values that fall outside the 64-bit signed integer range. Use 'out of range' to cover both directions.

Suggested change
return nil, fmt.Errorf("int value too large for SQLite: %v", v)
return nil, fmt.Errorf("int value out of range for SQLite: %v", v)

}
return i64, nil
case starlark.Float:
return float64(v), nil
Expand Down
39 changes: 38 additions & 1 deletion operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
Loading