-
Notifications
You must be signed in to change notification settings - Fork 87
fix: database query built from user-controlled sources #290
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import ( | |
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "regexp" | ||
| "github.com/gin-gonic/gin" | ||
|
|
||
| "github.com/karmada-io/dashboard/cmd/metrics-scraper/app/scrape" | ||
|
|
@@ -74,6 +74,11 @@ func QueryMetrics(c *gin.Context) { | |
| } | ||
|
|
||
| func queryMetricNames(c *gin.Context, tx *sql.Tx, sanitizedPodName string) { | ||
| if !isValidTableName(sanitizedPodName) { | ||
| log.Printf("Invalid table name: %v", sanitizedPodName) | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid table name"}) | ||
| return | ||
| } | ||
| rows, err := tx.Query(fmt.Sprintf("SELECT DISTINCT name FROM %s", sanitizedPodName)) | ||
| if err != nil { | ||
| log.Printf("Error querying metric names: %v, SQL Error: %v", err, err) | ||
|
|
@@ -96,6 +101,14 @@ func queryMetricNames(c *gin.Context, tx *sql.Tx, sanitizedPodName string) { | |
| c.JSON(http.StatusOK, gin.H{"metricNames": metricNames}) | ||
| } | ||
|
|
||
| // isValidTableName checks if the given table name is a valid identifier. | ||
| func isValidTableName(name string) bool { | ||
| // Only allow table names that start with a letter or underscore, then letters, numbers, underscores, up to 64 chars | ||
| // Adjust length as per your table name limits (e.g., SQLite, MySQL usually 64) | ||
| validTableName := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For example: var (
validTableName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]{0,63}$`)
)
// ... in isValidTableName ...
return validTableName.MatchString(name)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes sense. @odaysec what do you think?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| return validTableName.MatchString(name) | ||
| } | ||
|
|
||
| func queryMetricDetailsByName(c *gin.Context, tx *sql.Tx, sanitizedPodName, metricName string) { | ||
| if metricName == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Metric name required for details"}) | ||
|
|
||
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.
Right, this is a typical SQL injection issue, like an attacker could exploit this by providing a malicious table name (e.g.,
podName; DROP TABLE users; --), which could result in data deletion...