-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6421 from influxdata/js-3883-sanitize-query
Improve query sanitization to prevent a password leak in the logs
- Loading branch information
Showing
5 changed files
with
109 additions
and
12 deletions.
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