diff --git a/test/s3vectors/s3vectors_test.py b/test/s3vectors/s3vectors_test.py
index f0640c40b9..95b4f4432a 100644
--- a/test/s3vectors/s3vectors_test.py
+++ b/test/s3vectors/s3vectors_test.py
@@ -266,6 +266,25 @@ def test_filter_arrays(vectors, index):
assert matches({"$nin": [1, "2.5"]}) == ["flag", "scalar", "space"]
+def test_filter_control_characters(vectors, index):
+ chars = [chr(c) for c in [*range(0x20), *range(0x7F, 0xA0)]] + ['"', "\\"]
+ rows = {f"c{ord(ch):02x}": f"a{ch}b" for ch in chars}
+ vectors.put_vectors(**index, vectors=[{"key": key, "data": {"float32": [1, 0, 0, 0]}, "metadata": {"s": value}}
+ for key, value in rows.items()])
+
+ def matches(condition):
+ hits = vectors.query_vectors(**index, queryVector={"float32": [1, 0, 0, 0]}, topK=100,
+ filter={"s": condition})["vectors"]
+ return sorted(hit["key"] for hit in hits)
+
+ for key, value in rows.items():
+ others = sorted(k for k in rows if k != key)
+ assert matches({"$eq": value}) == [key], repr(value)
+ assert matches({"$in": [value, "other"]}) == [key], repr(value)
+ assert matches({"$ne": value}) == others, repr(value)
+ assert matches({"$nin": [value, "other"]}) == others, repr(value)
+
+
def test_euclidean_distance(vectors, index):
vectors.put_vectors(**index, vectors=[{"key": "p", "data": {"float32": [3, 4, 0, 0]}}])
result = vectors.query_vectors(**index, queryVector={"float32": [0, 0, 0, 0]}, topK=1, returnDistance=True)
diff --git a/weed/s3api/s3vectors/handlers.go b/weed/s3api/s3vectors/handlers.go
index 933ce6d337..c32da84f0b 100644
--- a/weed/s3api/s3vectors/handlers.go
+++ b/weed/s3api/s3vectors/handlers.go
@@ -2111,7 +2111,7 @@ func elementMatch(field string, operand interface{}) (string, error) {
}
// jsonbString renders s as the JSON text json_extract returns for a stored
-// string: quoted, with the same escapes the jsonb serializer emits.
+// string: quoted, with the escapes serde_json emits.
func jsonbString(s string) string {
var b strings.Builder
b.WriteByte('"')
@@ -2127,11 +2127,13 @@ func jsonbString(s string) string {
b.WriteString(`\r`)
case '\t':
b.WriteString(`\t`)
- case 0:
- b.WriteString(`\0`)
+ case '\b':
+ b.WriteString(`\b`)
+ case '\f':
+ b.WriteString(`\f`)
default:
- if r < 0x20 || (r >= 0x7f && r <= 0x9f) {
- fmt.Fprintf(&b, `\u{%x}`, r)
+ if r < 0x20 {
+ fmt.Fprintf(&b, `\u%04x`, r)
} else {
b.WriteRune(r)
}
diff --git a/weed/s3api/s3vectors/handlers_test.go b/weed/s3api/s3vectors/handlers_test.go
index 07436c468c..bc13c8f86f 100644
--- a/weed/s3api/s3vectors/handlers_test.go
+++ b/weed/s3api/s3vectors/handlers_test.go
@@ -399,6 +399,9 @@ func TestTranslateFilter(t *testing.T) {
"(COALESCE(json_extract(`metadata`, '$[\"a''b\"]') = '\"v''1\"', false) OR COALESCE(json_array_contains(`metadata`, '$[\"a''b\"]', '\"v''1\"'), false))"},
{"array value escapes", map[string]interface{}{"k": ``},
"(COALESCE(" + str + ` = '""', false) OR ` + elem(`""`) + ")"},
+ {"control characters", map[string]interface{}{"k": map[string]interface{}{"$ne": "\x00\x01\b\f\x1f\x7f\u0085\u009f"}},
+ "NOT (COALESCE(" + str + " = '\"\\u0000\\u0001\\b\\f\\u001f\x7f\u0085\u009f\"', false) OR " +
+ elem(`"\u0000\u0001\b\f\u001f`+"\x7f\u0085\u009f\"") + ")"},
}
for _, tc := range cases {
got, err := translateFilter(tc.filter)
@@ -434,6 +437,24 @@ func TestTranslateFilter(t *testing.T) {
}
}
+// json_extract serializes a stored string with serde_json, which escapes the
+// same characters as encoding/json below U+00A0.
+func TestJsonbStringControlCharacters(t *testing.T) {
+ for r := rune(0); r < 0xa0; r++ {
+ s := "a" + string(r) + "b"
+ var b strings.Builder
+ enc := json.NewEncoder(&b)
+ enc.SetEscapeHTML(false)
+ if err := enc.Encode(s); err != nil {
+ t.Fatal(err)
+ }
+ want := strings.TrimSuffix(b.String(), "\n")
+ if got := jsonbString(s); got != want {
+ t.Errorf("U+%04X: got %q, want %q", r, got, want)
+ }
+ }
+}
+
// The catalog stores nonFilterableMetadataKeys as a JSON string; an older
// record may still carry the array form. Both must round-trip into the index.
func TestIndexFromRecordNonFilterableKeys(t *testing.T) {