Skip to content

Commit

Permalink
fix(search): check column name before searching
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Mishra <[email protected]>
  • Loading branch information
GMishx committed May 20, 2024
1 parent 66b83ac commit 2681632
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,19 @@ func SearchInLicense(c *gin.Context) {
query := db.DB.Model(&license)

if input.Search == "fuzzy" {
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field), fmt.Sprintf("%%%s%%", input.SearchTerm))
if !db.DB.Migrator().HasColumn(&models.LicenseDB{}, input.Field) {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("invalid field name '%s'", input.Field),
Error: "field does not exist in the database",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return
}
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field),

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
fmt.Sprintf("%%%s%%", input.SearchTerm))
} else if input.Search == "" || input.Search == "full_text_search" {
query = query.Where(input.Field+" @@ plainto_tsquery(?)", input.SearchTerm)
} else {
Expand Down

0 comments on commit 2681632

Please sign in to comment.