mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
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
This commit is contained in:
1 parent
2fddcae20e
commit
e0fef0bae8
2 files changed
+31
-10
No files matched your search
@@ -20,6 +20,12 @@ const DefaultFlatUpsertQuery = `INSERT INTO "%s" (path,directory,name,meta) VALU
|
||||
|
||||
var _ = abstract_sql.SqlGenerator(&SqlGenPostgresFlat{})
|
||||
|
||||
// tableIdent escapes a table name for the double quotes the statements put
|
||||
// around it.
|
||||
func tableIdent(tableName string) string {
|
||||
return strings.ReplaceAll(tableName, `"`, `""`)
|
||||
}
|
||||
|
||||
func flatPath(key abstract_sql.RowKey) string {
|
||||
switch key.Directory {
|
||||
case "":
|
||||
@@ -33,17 +39,17 @@ func flatPath(key abstract_sql.RowKey) string {
|
||||
func (gen *SqlGenPostgresFlat) GetSqlInsert(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
||||
args := []interface{}{flatPath(key), key.Directory, key.Name, meta}
|
||||
if gen.UpsertQueryTemplate != "" {
|
||||
return fmt.Sprintf(gen.UpsertQueryTemplate, tableName), args
|
||||
return fmt.Sprintf(gen.UpsertQueryTemplate, tableIdent(tableName)), args
|
||||
}
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (path,directory,name,meta) VALUES($1,$2,$3,$4)`, tableName), args
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (path,directory,name,meta) VALUES($1,$2,$3,$4)`, tableIdent(tableName)), args
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlUpdate(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
||||
return fmt.Sprintf(`UPDATE "%s" SET meta=$2 WHERE path=$1`, tableName), []interface{}{flatPath(key), meta}
|
||||
return fmt.Sprintf(`UPDATE "%s" SET meta=$2 WHERE path=$1`, tableIdent(tableName)), []interface{}{flatPath(key), meta}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlFind(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE path=$1`, tableName), []interface{}{flatPath(key)}
|
||||
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE path=$1`, tableIdent(tableName)), []interface{}{flatPath(key)}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlFindBatch(tableName string, keys []abstract_sql.RowKey) (string, []interface{}) {
|
||||
@@ -53,31 +59,31 @@ func (gen *SqlGenPostgresFlat) GetSqlFindBatch(tableName string, keys []abstract
|
||||
params[i] = fmt.Sprintf("$%d", i+1)
|
||||
args[i] = flatPath(key)
|
||||
}
|
||||
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE path IN (%s)`, tableName, strings.Join(params, ",")), args
|
||||
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE path IN (%s)`, tableIdent(tableName), strings.Join(params, ",")), args
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlDelete(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE path=$1`, tableName), []interface{}{flatPath(key)}
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE path=$1`, tableIdent(tableName)), []interface{}{flatPath(key)}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE directory=$1`, tableName), []interface{}{dir}
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE directory=$1`, tableIdent(tableName)), []interface{}{dir}
|
||||
}
|
||||
|
||||
// GetSqlScanPage pages by path; the dirhash column is a placeholder.
|
||||
func (gen *SqlGenPostgresFlat) GetSqlScanPage(tableName string, after abstract_sql.RowKey, limit int) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT 0::bigint, name, directory, meta FROM "%s" WHERE path > $1 ORDER BY path ASC LIMIT $2`, tableName),
|
||||
return fmt.Sprintf(`SELECT 0::bigint, name, directory, meta FROM "%s" WHERE path > $1 ORDER BY path ASC LIMIT $2`, tableIdent(tableName)),
|
||||
[]interface{}{flatPath(after), limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE directory=$1 AND %s>$2 AND %s like $3 ESCAPE '!' ORDER BY %s ASC LIMIT $4`, tableName, name, name, name),
|
||||
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE directory=$1 AND %s>$2 AND %s like $3 ESCAPE '!' ORDER BY %s ASC LIMIT $4`, tableIdent(tableName), name, name, name),
|
||||
[]interface{}{dir, startFileName, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgresFlat) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE directory=$1 AND %s>=$2 AND %s like $3 ESCAPE '!' ORDER BY %s ASC LIMIT $4`, tableName, name, name, name),
|
||||
return fmt.Sprintf(`SELECT name, meta FROM "%s" WHERE directory=$1 AND %s>=$2 AND %s like $3 ESCAPE '!' ORDER BY %s ASC LIMIT $4`, tableIdent(tableName), name, name, name),
|
||||
[]interface{}{dir, startFileName, namePattern, limit}
|
||||
}
|
||||
@@ -40,3 +40,18 @@ func TestFlatStatementsAddressRowsByPath(t *testing.T) {
|
||||
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 }
|
||||
Reference in new issue
Block a user