Skip to content

fix: prevent SQL injection via driver.Valuer error messages#1369

Open
Yanhu007 wants to merge 1 commit into
uptrace:masterfrom
Yanhu007:fix/sql-injection-append-error
Open

fix: prevent SQL injection via driver.Valuer error messages#1369
Yanhu007 wants to merge 1 commit into
uptrace:masterfrom
Yanhu007:fix/sql-injection-append-error

Conversation

@Yanhu007
Copy link
Copy Markdown

Security Fix

Fixes #1307

Problem

AppendError embeds error messages directly into the SQL query string without any escaping. A driver.Valuer implementation can return an error with a crafted message that closes the ?!() wrapper and injects arbitrary SQL.

Fix

Wrap the error message in single quotes and escape embedded single quotes within the message. This ensures the error message is always treated as a string literal, preventing SQL injection regardless of the error content.

Before: ?!(crafted SQL injection here)
After:  ?!('crafted SQL injection here')

AppendError embeds error messages directly into the SQL query string
without any escaping. A malicious driver.Valuer can return an error
whose message closes the ?!() wrapper and injects arbitrary SQL.

Wrap the error message in single quotes and escape any single quotes
within the message to prevent SQL injection.

Before: ?!(error message here)
After:  ?!('error message here')

Fixes uptrace#1307
Comment thread dialect/append.go
Comment on lines +11 to +13
b = append(b, "?!('"...)
b = appendSanitizedError(b, err.Error())
b = append(b, "')"...)
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible SQL Injection via driver.Valuer Error Messages

2 participants