fix: avoid leaking raw postgres error details in duplicate key response (#2538)

This commit is contained in:
Adithya Vardhan 2026-08-11 18:45:59 +05:30 committed by GitHub
parent 459b825cfb
commit 0c24ab84c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,10 +6,12 @@ import (
"github.com/getAlby/hub/constants" "github.com/getAlby/hub/constants"
"github.com/getAlby/hub/nip47/models" "github.com/getAlby/hub/nip47/models"
"github.com/getAlby/hub/transactions" "github.com/getAlby/hub/transactions"
"gorm.io/gorm"
) )
func mapNip47Error(err error) *models.Error { func mapNip47Error(err error) *models.Error {
code := constants.ERROR_INTERNAL code := constants.ERROR_INTERNAL
message := err.Error()
if errors.Is(err, transactions.NewNotFoundError()) { if errors.Is(err, transactions.NewNotFoundError()) {
code = constants.ERROR_NOT_FOUND code = constants.ERROR_NOT_FOUND
} }
@ -19,9 +21,13 @@ func mapNip47Error(err error) *models.Error {
if errors.Is(err, transactions.NewQuotaExceededError()) { if errors.Is(err, transactions.NewQuotaExceededError()) {
code = constants.ERROR_QUOTA_EXCEEDED code = constants.ERROR_QUOTA_EXCEEDED
} }
if errors.Is(err, gorm.ErrDuplicatedKey) {
// avoid leaking raw driver/SQL error details (e.g. constraint names) to NWC apps
message = gorm.ErrDuplicatedKey.Error()
}
return &models.Error{ return &models.Error{
Code: code, Code: code,
Message: err.Error(), Message: message,
} }
} }