Repository navigation
[fix] sqlite: checked function-return ints + order_by validation (PKG-25/26) - #16
Conversation
A custom scalar function registered via register_function returns its Starlark result through starlarkToDriverValue. For starlark.Int it called v.Int64() and ignored the ok flag, so a return value that does not fit an int64 (e.g. (1<<100)+7) was silently truncated to 0 / a wrapped value with no error. Mirror the bind-parameter path (starlarkToSQLiteValue in utils.go): when Int64() reports the value does not fit, return a clear "int value too large for SQLite" error. In-range ints are unchanged. Found by defensive fuzz testing (F5, 2026-06-16). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
select(...)'s order_by argument was interpolated raw into the SQL string (unlike where/values, which are parameterized), so a script could inject arbitrary SQL via order_by (e.g. "id; DROP TABLE t"). ORDER BY cannot use bind parameters, so validate instead: validateOrderBy accepts only 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/LAST. Anything else (semicolons, parens, comments, subqueries, other keywords, spaces-in-identifiers) is rejected with "invalid order_by clause". Legitimate clauses (plain column, "col DESC", "a, b ASC", dotted/quoted columns, NULLS modifiers) still pass. Documented the validation in README.md and docs/API.md. Custom-function script tests now use process-unique names so they repeat under -count>1 (the register_function registry is process-global). Found by defensive fuzz testing (F6, 2026-06-16). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 5 |
| Duplication | 0 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
While this PR successfully implements integer range checking for custom SQLite functions (PKG-25), it fails to include any of the promised logic or tests for 'order_by' validation (PKG-26). Although Codacy reports that the current changes are 'up to standards', the missing security validation for SQL injection represents a significant gap relative to the PR's stated goals. The implementation of PKG-25 is mostly sound, though the error messaging should be updated to more accurately reflect negative integer range errors.
About this PR
- Significant implementation gap: The PR description and title claim to fix PKG-26 (SQL injection in order_by) and mention changes to operations.go, but these changes and the corresponding tests (TestSelectOrderByRejectsInjection, TestSelectOrderByAllowsLegitClauses) are entirely missing from the provided diff.
Test suggestions
- Custom function returning an out-of-range integer (e.g., 1 << 100) returns a descriptive error.
- Custom function returning a valid int64 (e.g., 1234567890) round-trips successfully.
- Selection query with 'order_by' injection payload (e.g., 'id; DROP TABLE t') is rejected.
- Selection query with valid complex 'order_by' clause (e.g., '"name", age DESC') is accepted.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Selection query with 'order_by' injection payload (e.g., 'id; DROP TABLE t') is rejected.
2. Selection query with valid complex 'order_by' clause (e.g., '"name", age DESC') is accepted.
TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback
| return bool(v), nil | ||
| case starlark.Int: | ||
| i64, _ := v.Int64() | ||
| // Mirror the bind-parameter path (starlarkToSQLiteValue): an int that |
There was a problem hiding this comment.
🟡 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.
| // truncate to a wrong driver value. | ||
| i64, ok := v.Int64() | ||
| if !ok { | ||
| return nil, fmt.Errorf("int value too large for SQLite: %v", v) |
There was a problem hiding this comment.
⚪ 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.
| 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) |
Two correctness/security fixes surfaced by defensive fuzz testing (2026-06-16), test-first, in two commits.
PKG-25 (F5) — custom-function return value: big-int silent truncation
function.go:starlarkToDriverValueconverted astarlark.Intviav.Int64()and ignored the ok flag, so a custom SQL function returning e.g.(1<<100)+7silently yielded0with no error. The bind-parameter path (utils.go:starlarkToSQLiteValue) already did this correctly.Fix: when
Int64()reports the value does not fit int64, returnint value too large for SQLite: …instead of a silently-truncated value. In-range ints are unchanged.PKG-26 (F6) — order_by raw SQL interpolation
operations.go:selectRecordsinterpolated theorder_byargument raw into the SQL string (unlikewhere/values, which are parameterized) — a SQL-injection surface (e.g.order_by="id; DROP TABLE t").ORDER BY cannot use bind parameters, so this validates instead.
validateOrderByaccepts only a comma-separated list of column identifiers (bare, dottedtable.col, or double-quoted/backtick-quoted), each with an optionalASC/DESCand optionalNULLS FIRST/LAST. Anything else (semicolons, parentheses, comments, subqueries, other keywords, spaces-in-identifiers) is rejected withinvalid order_by clause: …. Legitimate clauses ("name","age DESC","a, b ASC", dotted/quoted columns, NULLS modifiers) still pass.Tests (test-first)
New sections in
example_test.go(the repo's thematic hardening home — no new*_test.go):TestRegisteredFunctionOutOfRangeIntErrors/TestRegisteredFunctionBigIntScript— out-of-range function return errors cleanly (unit + end-to-end query); in-range int still round-trips.TestSelectOrderByRejectsInjection(10 payloads) /TestSelectOrderByAllowsLegitClauses— injection payloads error; legit clauses order correctly.Verification
go test -race ./...green,gofmt -l .empty,go vet ./...clean,go build ./...ok,doccovexits 0. Docs updated inREADME.md+docs/API.md.Note: the repo's authoritative bar is
-count=1(make test/make ci); the process-globalregister_functionregistry makes some pre-existing function tests (TestExamples/CustomSQLFunctions+ the private-repo integration.starscripts) inherently single-run — these fail identically onmasterand are out of scope. New tests here are-count>1-safe.🤖 Generated with Claude Code