Skip to content

Commit 44b9767

Browse files
committed
fix: validate
1 parent e7b0686 commit 44b9767

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

backend/api.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/gin-gonic/gin"
56
"gorm.io/gorm"
67
"net/http"
@@ -34,7 +35,16 @@ func ListFloorsInASpecialHole(c *gin.Context) {
3435
c.String(http.StatusInternalServerError, "failed to make query set: %v", err)
3536
return
3637
}
37-
result := querySet.Order(query.OrderBy + " " + query.Sort).
38+
39+
validSorts := map[string]bool{"asc": true, "desc": true, "": true}
40+
validOrderBy := map[string]bool{"id": true, "like": true, "": true}
41+
42+
if !validSorts[query.Sort] || !validOrderBy[query.OrderBy] {
43+
c.String(http.StatusBadRequest, "invalid sort or order_by")
44+
return
45+
}
46+
47+
result := querySet.Order(fmt.Sprintf("`%s` %s", query.OrderBy, query.Sort)).
3848
Find(&floors)
3949
if result.Error != nil {
4050
c.String(http.StatusInternalServerError, "failed to query floors: %v", result.Error)

0 commit comments

Comments
 (0)