Skip to content

[fix] sqlite: checked function-return ints + order_by validation (PKG-25/26) - #16

Merged
vt128 merged 2 commits into
masterfrom
fix/function-int-and-orderby
Jun 16, 2026
Merged

vt128 merged 2 commits into
masterfrom
fix/function-int-and-orderby

Conversation

@vt128

@vt128 vt128 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

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:starlarkToDriverValue converted a starlark.Int via v.Int64() and ignored the ok flag, so a custom SQL function returning e.g. (1<<100)+7 silently yielded 0 with no error. The bind-parameter path (utils.go:starlarkToSQLiteValue) already did this correctly.

Fix: when Int64() reports the value does not fit int64, return int 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:selectRecords interpolated the order_by argument raw into the SQL string (unlike where/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. 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, parentheses, comments, subqueries, other keywords, spaces-in-identifiers) is rejected with invalid 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, doccov exits 0. Docs updated in README.md + docs/API.md.

Note: the repo's authoritative bar is -count=1 (make test/make ci); the process-global register_function registry makes some pre-existing function tests (TestExamples/CustomSQLFunctions + the private-repo integration .star scripts) inherently single-run — these fail identically on master and are out of scope. New tests here are -count>1-safe.

🤖 Generated with Claude Code

vt128 and others added 2 commits June 16, 2026 13:37
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>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 5 complexity · 0 duplication

Metric Results
Complexity 5
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread function.go
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.

Comment thread function.go
// 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)

@vt128
vt128 merged commit 719a7fa into master Jun 16, 2026
10 checks passed
@vt128
vt128 deleted the fix/function-int-and-orderby branch June 16, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant