refatoração de segurança e logs
This commit is contained in:
parent
6873b87a85
commit
663a8d5bf2
12 changed files with 362 additions and 37 deletions
|
|
@ -13,6 +13,22 @@ import (
|
|||
|
||||
var produtoRe = regexp.MustCompile(`^[a-z_][a-z0-9_]*$`)
|
||||
|
||||
// TableNameValido valida se o nome de tabela está no formato esperado do projeto:
|
||||
// "nps_" + produto normalizado.
|
||||
//
|
||||
// Motivação (segurança): identificadores não podem ser parametrizados em SQL.
|
||||
// Então, sempre que precisarmos interpolar um nome de tabela, validamos aqui
|
||||
// para evitar SQL injection via identificador.
|
||||
func TableNameValido(tableName string) bool {
|
||||
if !strings.HasPrefix(tableName, "nps_") {
|
||||
return false
|
||||
}
|
||||
produto := strings.TrimPrefix(tableName, "nps_")
|
||||
// produtoRe já garante: ^[a-z_][a-z0-9_]*$
|
||||
// (e a normalização limita tamanho em NormalizeProduto).
|
||||
return produtoRe.MatchString(produto)
|
||||
}
|
||||
|
||||
// NormalizeProduto normaliza e valida um nome de produto para uso em:
|
||||
// - nomes de tabela no Postgres (prefixo nps_)
|
||||
// - rotas/URLs (parâmetro {produto})
|
||||
|
|
@ -80,11 +96,14 @@ func EnsurePgcrypto(ctx context.Context, pool *pgxpool.Pool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// EnsureNPSTable creates the per-product table + indexes if they do not exist.
|
||||
// IMPORTANT: tableName must be created from a sanitized product name.
|
||||
// EnsureNPSTable cria a tabela por produto + índices se não existirem.
|
||||
// Importante: tableName deve ser criada a partir de um produto normalizado.
|
||||
func EnsureNPSTable(ctx context.Context, pool *pgxpool.Pool, tableName string) error {
|
||||
// Identifiers cannot be passed as $1 parameters, so we must interpolate.
|
||||
// Safety: tableName is strictly derived from NormalizeProduto + prefix.
|
||||
// Segurança: tableName deve ser estritamente derivada de NormalizeProduto + prefix.
|
||||
if !TableNameValido(tableName) {
|
||||
return fmt.Errorf("nome de tabela invalido")
|
||||
}
|
||||
q := fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue