mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
filer/postgres: escape the table name inside its quotes
A double quote in it is doubled, as PostgreSQL reads a quoted identifier. Bucket names cannot hold one; the statements no longer rely on that. Reported by CodeQL (go/unsafe-quoting) on the lines this branch changed. 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
737b633ec3
commit
102dae663f
2 files changed
+30
-12
No files matched your search
@@ -47,17 +47,17 @@ func ResolveCreateTableQuery(createTable string) string {
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlInsert(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
||||
if gen.UpsertQueryTemplate != "" {
|
||||
return fmt.Sprintf(gen.UpsertQueryTemplate, tableName), key.Args(meta)
|
||||
return fmt.Sprintf(gen.UpsertQueryTemplate, tableIdent(tableName)), key.Args(meta)
|
||||
}
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)`, tableName), key.Args(meta)
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)`, tableIdent(tableName)), key.Args(meta)
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlUpdate(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
||||
return fmt.Sprintf(`UPDATE "%s" SET meta=$4 WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args(meta)
|
||||
return fmt.Sprintf(`UPDATE "%s" SET meta=$4 WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableIdent(tableName)), key.Args(meta)
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlFind(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args()
|
||||
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableIdent(tableName)), key.Args()
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlFindBatch(tableName string, keys []abstract_sql.RowKey) (string, []interface{}) {
|
||||
@@ -67,18 +67,24 @@ func (gen *SqlGenPostgres) GetSqlFindBatch(tableName string, keys []abstract_sql
|
||||
rows[i] = fmt.Sprintf("($%d,$%d,$%d)", 3*i+1, 3*i+2, 3*i+3)
|
||||
args = append(args, key.Args()...)
|
||||
}
|
||||
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE (dirhash, name, directory) IN (%s)`, tableName, strings.Join(rows, ",")), args
|
||||
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE (dirhash, name, directory) IN (%s)`, tableIdent(tableName), strings.Join(rows, ",")), args
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDelete(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args()
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableIdent(tableName)), key.Args()
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableName), []interface{}{util.HashStringToLong(dir), dir}
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableIdent(tableName)), []interface{}{util.HashStringToLong(dir), dir}
|
||||
}
|
||||
|
||||
// nameExpr forces byte ordering on a locale-aware column via COLLATE "C".
|
||||
// tableIdent escapes a table name for the double quotes the statements put
|
||||
// around it.
|
||||
func tableIdent(tableName string) string {
|
||||
return strings.ReplaceAll(tableName, `"`, `""`)
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) nameExpr() string {
|
||||
if gen.ForceBinaryCollation {
|
||||
return `name COLLATE "C"`
|
||||
@@ -90,19 +96,19 @@ func (gen *SqlGenPostgres) nameExpr() string {
|
||||
// PostgreSQL. The name keeps the column's own collation (COLLATE "C" would
|
||||
// sort every page); the scan promises no lexical order.
|
||||
func (gen *SqlGenPostgres) GetSqlScanPage(tableName string, after abstract_sql.RowKey, limit int) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT dirhash, name, directory, meta FROM "%s" WHERE (dirhash, name) > ($1, $2) ORDER BY dirhash ASC, name ASC LIMIT $3`, tableName),
|
||||
return fmt.Sprintf(`SELECT dirhash, name, directory, meta FROM "%s" WHERE (dirhash, name) > ($1, $2) ORDER BY dirhash ASC, name ASC LIMIT $3`, tableIdent(tableName)),
|
||||
[]interface{}{after.DirHash, after.Name, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name),
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableIdent(tableName), name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>=$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name),
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>=$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableIdent(tableName), name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
@@ -110,9 +116,9 @@ func (gen *SqlGenPostgres) GetSqlCreateTable(tableName string) string {
|
||||
if gen.CreateTableSqlTemplate == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(gen.CreateTableSqlTemplate, tableName)
|
||||
return fmt.Sprintf(gen.CreateTableSqlTemplate, tableIdent(tableName))
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDropTable(tableName string) string {
|
||||
return fmt.Sprintf(gen.DropTableSqlTemplate, tableName)
|
||||
return fmt.Sprintf(gen.DropTableSqlTemplate, tableIdent(tableName))
|
||||
}
|
||||
@@ -166,3 +166,15 @@ func TestStatementArguments(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatementsEscapeTheTableName(t *testing.T) {
|
||||
gen := &SqlGenPostgres{UpsertQueryTemplate: DefaultUpsertQuery, CreateTableSqlTemplate: DefaultCreateTableQuery}
|
||||
key := abstract_sql.EntryRowKey("/", "x")
|
||||
insert, _ := gen.GetSqlInsert(`a"b`, key, nil)
|
||||
find, _ := gen.GetSqlFind(`a"b`, key)
|
||||
for _, statement := range []string{insert, find, gen.GetSqlCreateTable(`a"b`)} {
|
||||
if !strings.Contains(statement, `"a""b"`) {
|
||||
t.Fatalf("table name not escaped: %s", statement)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user