Skip to content

Commit

Permalink
feat: safely quote terms
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryvchun committed Apr 11, 2023
1 parent 242d3f6 commit b400eb6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
11 changes: 10 additions & 1 deletion sonic/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ func (s searchChannel) Query(collection, bucket, term string, limit, offset int,
}
defer d.close()

err = d.write(fmt.Sprintf("%s %s %s \"%s\" LIMIT(%d) OFFSET(%d)"+langFormat(lang), query, collection, bucket, term, limit, offset, lang))
err = d.write(fmt.Sprintf(
"%s %s %s %q LIMIT(%d) OFFSET(%d)"+langFormat(lang),
query,
collection,
bucket,
term,
limit,
offset,
lang,
))
if err != nil {
return nil, err
}
Expand Down
45 changes: 45 additions & 0 deletions sonic/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ func TestSearch(t *testing.T) {
}
})

t.Run("Query_quote", func(t *testing.T) {
t.Parallel()

_, err := srch.Query(col, bucket, `'quote' "hello"`, 1, 0, sonic.LangAutoDetect)
if err != nil {
t.Fatal("Query", err)
}
})

t.Run("Query_escape", func(t *testing.T) {
t.Parallel()

_, err := srch.Query(col, bucket, `escape symbol \`, 1, 0, sonic.LangAutoDetect)
if err != nil {
t.Fatal("Query", err)
}
})

t.Run("Query_tab", func(t *testing.T) {
t.Parallel()

_, err := srch.Query(col, bucket, "\t", 1, 0, sonic.LangAutoDetect)
if err != nil {
t.Fatal("Query", err)
}
})

t.Run("Query_space", func(t *testing.T) {
t.Parallel()

_, err := srch.Query(col, bucket, " ", 1, 0, sonic.LangAutoDetect)
if err == nil {
t.Fatal("Expected error, but got nil")
}
})

t.Run("Query_empty", func(t *testing.T) {
t.Parallel()

_, err := srch.Query(col, bucket, "", 1, 0, sonic.LangAutoDetect)
if err == nil {
t.Fatal("Expected error, but got nil")
}
})

t.Run("Query_empty", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit b400eb6

Please sign in to comment.