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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add a safe tokenized FTS5 query helper for arbitrary user search input.
- Preserve unrelated tracked mirror edits across write synchronization rebases.
- Retry atomic branch-and-tag snapshot pushes after rebasing and retargeting unpublished tags.
- Update Go to 1.26.4 for current standard-library security fixes and add race and vulnerability CI gates.
Expand Down
24 changes: 24 additions & 0 deletions store/fts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"strings"
"unicode"
)

func FTS5Phrase(value string) string {
Expand Down Expand Up @@ -33,6 +34,29 @@ func FTS5Terms(value, operator string) (string, error) {
return strings.Join(quoted, separator), nil
}

// FTS5TokenQuery converts arbitrary user input into quoted FTS5 tokens joined
// by implicit AND, discarding punctuation that could be parsed as operators.
func FTS5TokenQuery(value string) string {
terms := make([]string, 0)
var token strings.Builder
flush := func() {
if token.Len() == 0 {
return
}
terms = append(terms, FTS5Phrase(token.String()))
token.Reset()
}
for _, r := range value {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
token.WriteRune(r)
continue
}
flush()
}
flush()
return strings.Join(terms, " ")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Return a valid no-match query for punctuation-only input

When the input contains no token characters, such as -- "", terms stays empty and this returns ""; passing that directly to an FTS5 MATCH ? predicate (the query shape documented in docs/cloudflare-remote-archives.md) makes SQLite raise fts5: syntax error near "" instead of returning no rows. The existing FTS5Terms helper avoids this by returning FTS5Phrase(""), so this helper should also produce a valid no-match expression or otherwise surface an error rather than handing callers an invalid FTS query for arbitrary user punctuation.

Useful? React with 👍 / 👎.

}

type contextExecer interface {
ExecContext(context.Context, string, ...any) (sql.Result, error)
}
Expand Down
6 changes: 6 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ func TestFTS5Helpers(t *testing.T) {
if _, err := FTS5Terms("hello", "NEAR"); err == nil {
t.Fatal("unsupported operator should fail")
}
if query := FTS5TokenQuery(`scope-upgrade OR café_2`); query != `"scope" "upgrade" "OR" "café_2"` {
t.Fatalf("token query = %q", query)
}
if query := FTS5TokenQuery(`-- ""`); query != "" {
t.Fatalf("punctuation-only token query = %q", query)
}

ctx := context.Background()
st, err := Open(ctx, Options{
Expand Down