Files
seaweedfs/weed/filer/postgres/postgres_flat_sql_gen_test.go
T
Chris LuandClaude Opus 5.5 e0fef0bae8 filer/postgres: postgres_flat escapes the table name inside its quotes
A double quote in it is doubled, as PostgreSQL reads a quoted identifier.
Bucket names cannot hold one, but the statements no longer rely on that.
Reported by CodeQL (go/unsafe-quoting).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
2026-12-01 20:20:34 -08:00

58 lines
2.0 KiB
Go

package postgres
import (
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
)
func TestFlatPath(t *testing.T) {
for _, c := range []struct{ dir, name, want string }{
{"/", "", "/"},
{"/", "a.txt", "/a.txt"},
{"/dir/sub", "a.txt", "/dir/sub/a.txt"},
{"", "", ""},
} {
if got := flatPath(abstract_sql.EntryRowKey(c.dir, c.name)); got != c.want {
t.Errorf("flatPath(%q, %q) = %q, want %q", c.dir, c.name, got, c.want)
}
}
}
func TestFlatStatementsAddressRowsByPath(t *testing.T) {
gen := &SqlGenPostgresFlat{SqlGenPostgres{UpsertQueryTemplate: DefaultFlatUpsertQuery}}
key := abstract_sql.EntryRowKey("/dir", "a.txt")
insert, args := gen.GetSqlInsert("filemeta", key, []byte("m"))
if !strings.Contains(insert, "ON CONFLICT (path)") || args[0] != "/dir/a.txt" || args[1] != "/dir" || args[2] != "a.txt" {
t.Fatalf("insert %s %v", insert, args)
}
update, args := gen.GetSqlUpdate("filemeta", key, []byte("m"))
if !strings.Contains(update, "WHERE path=$1") || args[0] != "/dir/a.txt" {
t.Fatalf("update %s %v", update, args)
}
list, args := gen.GetSqlListExclusive("filemeta", "/dir", "", "%", 10)
if !strings.Contains(list, "WHERE directory=$1") || args[0] != "/dir" {
t.Fatalf("list %s %v", list, args)
}
gen.ForceBinaryCollation = true
if list, _ = gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10); !strings.Contains(list, `ORDER BY name COLLATE "C"`) {
t.Fatalf("list without a byte-ordered column: %s", list)
}
}
func TestFlatStatementsEscapeTheTableName(t *testing.T) {
gen := &SqlGenPostgresFlat{SqlGenPostgres{UpsertQueryTemplate: DefaultFlatUpsertQuery}}
for _, statement := range []string{
first(gen.GetSqlInsert(`a"b`, abstract_sql.EntryRowKey("/", "x"), nil)),
first(gen.GetSqlFind(`a"b`, abstract_sql.EntryRowKey("/", "x"))),
first(gen.GetSqlListExclusive(`a"b`, "/", "", "%", 1)),
} {
if !strings.Contains(statement, `"a""b"`) {
t.Fatalf("table name not escaped: %s", statement)
}
}
}
func first(s string, _ []interface{}) string { return s }