-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[INS-170] Unify JDBC URL parsing across detectors and analyzers #4574
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
Merged
mustansir14
merged 7 commits into
trufflesecurity:main
from
mustansir14:INS-170-Unify-JDBC-URL-parsing-across-detectors-and-analyzers
Dec 12, 2025
+138
−93
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
542edcc
publicize jdbc url parsing methods for mysql
mustansir14 c675f7b
publicize jdbc url parsing methods for postgresql
mustansir14 3f1f28f
publicize jdbc url parsing methods for sqlserver
mustansir14 380197e
make postgres consistent with others
mustansir14 3ec6860
keep default user as postgres
mustansir14 df825ff
Merge branch 'main' into INS-170-Unify-JDBC-URL-parsing-across-detect…
mustansir14 ea004c1
use same connectioninfo struct for all handlers
mustansir14 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,20 +12,23 @@ import ( | |||||||||||||||||
| "github.com/lib/pq" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| type postgresJDBC struct { | ||||||||||||||||||
| conn string | ||||||||||||||||||
| params map[string]string | ||||||||||||||||||
| type PostgresJDBC struct { | ||||||||||||||||||
| Host string | ||||||||||||||||||
| User string | ||||||||||||||||||
| Password string | ||||||||||||||||||
| Database string | ||||||||||||||||||
| Params map[string]string | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func (s *postgresJDBC) ping(ctx context.Context) pingResult { | ||||||||||||||||||
| func (s *PostgresJDBC) ping(ctx context.Context) pingResult { | ||||||||||||||||||
| // It is crucial that we try to build a connection string ourselves before using the one we found. This is because | ||||||||||||||||||
| // if the found connection string doesn't include a username, the driver will attempt to connect using the current | ||||||||||||||||||
| // user's name, which will fail in a way that looks like a determinate failure, thus terminating the waterfall. In | ||||||||||||||||||
| // contrast, when we build a connection string ourselves, if there's no username, we try 'postgres' instead, which | ||||||||||||||||||
| // actually has a chance of working. | ||||||||||||||||||
| return ping(ctx, "postgres", isPostgresErrorDeterminate, | ||||||||||||||||||
| buildPostgresConnectionString(s.params, true), | ||||||||||||||||||
| buildPostgresConnectionString(s.params, false), | ||||||||||||||||||
| BuildPostgresConnectionString(s.Host, s.User, s.Password, "postgres", s.Params, true), | ||||||||||||||||||
| BuildPostgresConnectionString(s.Host, s.User, s.Password, "postgres", s.Params, false), | ||||||||||||||||||
| ) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -59,7 +62,7 @@ func joinKeyValues(m map[string]string, sep string) string { | |||||||||||||||||
| return strings.Join(data, sep) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func parsePostgres(ctx logContext.Context, subname string) (jdbc, error) { | ||||||||||||||||||
| func ParsePostgres(ctx logContext.Context, subname string) (jdbc, error) { | ||||||||||||||||||
| // expected form: [subprotocol:]//[user:password@]HOST[/DB][?key=val[&key=val]] | ||||||||||||||||||
|
|
||||||||||||||||||
| if !strings.HasPrefix(subname, "//") { | ||||||||||||||||||
|
|
@@ -77,16 +80,20 @@ func parsePostgres(ctx logContext.Context, subname string) (jdbc, error) { | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| params := map[string]string{ | ||||||||||||||||||
| "host": u.Host, | ||||||||||||||||||
| "dbname": dbName, | ||||||||||||||||||
| "connect_timeout": "5", | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| postgresJDBC := &PostgresJDBC{ | ||||||||||||||||||
| Host: u.Host, | ||||||||||||||||||
| Database: dbName, | ||||||||||||||||||
| Params: params, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if u.User != nil { | ||||||||||||||||||
| params["user"] = u.User.Username() | ||||||||||||||||||
| postgresJDBC.User = u.User.Username() | ||||||||||||||||||
| pass, set := u.User.Password() | ||||||||||||||||||
| if set { | ||||||||||||||||||
| params["password"] = pass | ||||||||||||||||||
| postgresJDBC.Password = pass | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -95,46 +102,51 @@ func parsePostgres(ctx logContext.Context, subname string) (jdbc, error) { | |||||||||||||||||
| // https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-PROTECTION | ||||||||||||||||||
| case "disable", "allow", "prefer", | ||||||||||||||||||
| "require", "verify-ca", "verify-full": | ||||||||||||||||||
| params["sslmode"] = v[0] | ||||||||||||||||||
| postgresJDBC.Params["sslmode"] = v[0] | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if v := u.Query().Get("user"); v != "" { | ||||||||||||||||||
| params["user"] = v | ||||||||||||||||||
| postgresJDBC.User = v | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if v := u.Query().Get("password"); v != "" { | ||||||||||||||||||
| params["password"] = v | ||||||||||||||||||
| postgresJDBC.Password = v | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if params["host"] == "" || params["password"] == "" { | ||||||||||||||||||
| if postgresJDBC.Host == "" || postgresJDBC.Password == "" { | ||||||||||||||||||
| ctx.Logger().WithName("jdbc"). | ||||||||||||||||||
| V(2). | ||||||||||||||||||
| Info("Skipping invalid Postgres URL - no password or host found") | ||||||||||||||||||
| return nil, fmt.Errorf("missing host or password in connection string") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return &postgresJDBC{subname[2:], params}, nil | ||||||||||||||||||
| return postgresJDBC, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func buildPostgresConnectionString(params map[string]string, includeDbName bool) string { | ||||||||||||||||||
| func BuildPostgresConnectionString(host string, user string, password string, dbName string, params map[string]string, includeDbName bool) string { | ||||||||||||||||||
| data := map[string]string{ | ||||||||||||||||||
| // default user | ||||||||||||||||||
| "user": "postgres", | ||||||||||||||||||
| "user": "postgres", | ||||||||||||||||||
| "password": password, | ||||||||||||||||||
| "host": host, | ||||||||||||||||||
| } | ||||||||||||||||||
| if user != "" { | ||||||||||||||||||
| data["user"] = user | ||||||||||||||||||
| } | ||||||||||||||||||
| if h, p, found := strings.Cut(host, ":"); found { | ||||||||||||||||||
| data["host"] = h | ||||||||||||||||||
| data["port"] = p | ||||||||||||||||||
| } | ||||||||||||||||||
|
||||||||||||||||||
| if h, p, found := strings.Cut(host, ":"); found { | |
| data["host"] = h | |
| data["port"] = p | |
| } | |
| if h, p, ok := strings.Cut(host, ":"); ok { | |
| data["host"] = h | |
| data["port"] = p | |
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
We can use a single
ConnectionInfostruct across all handlers, and later clean it up from jdbc analyzer models.go.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.
Yes, that is a great idea. I'll unify these as well.