-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve query sanitization to prevent a password leak in the logs #6421
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package influxql | ||
|
||
import ( | ||
"bytes" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
sanitizeSetPassword = regexp.MustCompile(`(?i)password\s+for[^=]*=\s+(["']?[^\s"]+["']?)`) | ||
|
||
sanitizeCreatePassword = regexp.MustCompile(`(?i)with\s+password\s+(["']?[^\s"]+["']?)`) | ||
) | ||
|
||
// Sanitize attempts to sanitize passwords out of a raw query. | ||
// It looks for patterns that may be related to the SET PASSWORD and CREATE USER | ||
// statements and will redact the password that should be there. It will attempt | ||
// to redact information from common invalid queries too, but it's not guaranteed | ||
// to succeed on improper queries. | ||
// | ||
// This function works on the raw query and attempts to retain the original input | ||
// as much as possible. | ||
func Sanitize(query string) string { | ||
if matches := sanitizeSetPassword.FindAllStringSubmatchIndex(query, -1); matches != nil { | ||
var buf bytes.Buffer | ||
i := 0 | ||
for _, match := range matches { | ||
buf.WriteString(query[i:match[2]]) | ||
buf.WriteString("[REDACTED]") | ||
i = match[3] | ||
} | ||
buf.WriteString(query[i:]) | ||
query = buf.String() | ||
} | ||
|
||
if matches := sanitizeCreatePassword.FindAllStringSubmatchIndex(query, -1); matches != nil { | ||
var buf bytes.Buffer | ||
i := 0 | ||
for _, match := range matches { | ||
buf.WriteString(query[i:match[2]]) | ||
buf.WriteString("[REDACTED]") | ||
i = match[3] | ||
} | ||
buf.WriteString(query[i:]) | ||
query = buf.String() | ||
} | ||
return query | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package influxql_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/influxdata/influxdb/influxql" | ||
) | ||
|
||
func TestSanitize(t *testing.T) { | ||
var tests = []struct { | ||
s string | ||
stmt string | ||
}{ | ||
// Proper statements that should be redacted. | ||
{ | ||
s: `create user "admin" with password 'admin'`, | ||
stmt: `create user "admin" with password [REDACTED]`, | ||
}, | ||
{ | ||
s: `set password for "admin" = 'admin'`, | ||
stmt: `set password for "admin" = [REDACTED]`, | ||
}, | ||
|
||
// Common invalid statements that should still be redacted. | ||
{ | ||
s: `create user "admin" with password "admin"`, | ||
stmt: `create user "admin" with password [REDACTED]`, | ||
}, | ||
{ | ||
s: `set password for "admin" = "admin"`, | ||
stmt: `set password for "admin" = [REDACTED]`, | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
stmt := influxql.Sanitize(tt.s) | ||
if tt.stmt != stmt { | ||
t.Errorf("%d. %q\n\nsanitize mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkSanitize(b *testing.B) { | ||
b.ReportAllocs() | ||
q := `create user "admin" with password 'admin'; set password for "admin" = 'admin'` | ||
for i := 0; i < b.N; i++ { | ||
influxql.Sanitize(q) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
query.Statements
is no longer used, I thinksanitize
can be called in the response logger.https://github.com/influxdata/influxdb/blob/master/services/httpd/response_logger.go#L94
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to run the sanitizer for non-query requests. The response logger doesn't seem to be limited to just
serveQuery
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The response logger feels like a more natural fit for this code than the query handler. Other requests won't have a
q
query parameter but it could also be skipped with anif r.URL.EscapedPath() == "/query"
check.