Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions dialect/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ import (
)

func AppendError(b []byte, err error) []byte {
b = append(b, "?!("...)
b = append(b, err.Error()...)
b = append(b, ')')
b = append(b, "?!('"...)
b = appendSanitizedError(b, err.Error())
b = append(b, "')"...)
Comment on lines +11 to +13
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
b = append(b, "?!('"...)
b = appendSanitizedError(b, err.Error())
b = append(b, "')"...)
b = append(b, "?!("...)
b = gen.Dialect().AppendString(b, err.Error())
b = append(b, ")"...)

AppendError should probably accept a QueryGen as dependency and use it's AppendString method, which already handles escaping exhaustively.

return b
}

// appendSanitizedError escapes single quotes in error messages to prevent
// SQL injection when driver.Valuer returns an error that gets embedded in
// the query string.
func appendSanitizedError(b []byte, s string) []byte {
for i := 0; i < len(s); i++ {
if s[i] == '\'' {
b = append(b, '\'', '\'')
} else {
b = append(b, s[i])
}
}
return b
}

Expand Down
Loading