Skip to content

Commit c3e599b

Browse files
authored
fix JDBC detector regex truncating trailing non-alphanumeric password characters (#4755)
1 parent 71c48af commit c3e599b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

pkg/detectors/jdbc/jdbc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ var _ detectors.Detector = (*Scanner)(nil)
5050
var _ detectors.CustomFalsePositiveChecker = (*Scanner)(nil)
5151

5252
var (
53-
// Matches typical JDBC connection strings amd ingores any special character at the end
54-
keyPat = regexp.MustCompile(`(?i)jdbc:[\w]{3,10}:[^\s"'<>,{}[\]]{10,511}[A-Za-z0-9]`)
53+
// Matches typical JDBC connection strings.
54+
// The terminal character class additionally excludes () and & to avoid
55+
// capturing surrounding delimiters (e.g. "(jdbc:…)" or "…&user=x&").
56+
keyPat = regexp.MustCompile(`(?i)jdbc:[\w]{3,10}:[^\s"'<>,{}[\]]{10,511}[^\s"'<>,{}[\]()&]`)
5557
)
5658

5759
// Keywords are used for efficiently pre-filtering chunks.

pkg/detectors/jdbc/jdbc_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ func TestJdbc_Pattern(t *testing.T) {
8383
"jdbc:mysql://testuser:testpassword@tcp(localhost:1521)/testdb",
8484
},
8585
},
86+
{
87+
name: "trailing non-alphanumeric characters in password",
88+
input: `jdbc:hive9://foo.example.com:10191/default;user=MyRoleUser;password=MyPa$$w0rd...`,
89+
want: []string{
90+
"jdbc:hive9://foo.example.com:10191/default;user=MyRoleUser;password=MyPa$$w0rd...",
91+
},
92+
},
8693
{
8794
name: "invalid pattern - false positives",
8895
input: `

0 commit comments

Comments
 (0)