4
4
"log"
5
5
"net/http"
6
6
"os"
7
+ "strconv"
7
8
8
9
"github.com/gin-contrib/cors"
9
10
"github.com/gin-gonic/gin"
@@ -101,7 +102,14 @@ func getOrder(c *gin.Context) {
101
102
return
102
103
}
103
104
104
- order , err := client .repo .GetOrder (c .Param ("id" ))
105
+ id , err := sanitizeOrderId (c .Param ("id" ))
106
+ if err != nil {
107
+ log .Printf ("Failed to sanitize order id: %s" , err )
108
+ c .AbortWithStatus (http .StatusBadRequest )
109
+ return
110
+ }
111
+
112
+ order , err := client .repo .GetOrder (id )
105
113
if err != nil {
106
114
log .Printf ("Failed to get order from database: %s" , err )
107
115
c .AbortWithStatus (http .StatusInternalServerError )
@@ -128,7 +136,15 @@ func updateOrder(c *gin.Context) {
128
136
return
129
137
}
130
138
131
- err := client .repo .UpdateOrder (order )
139
+ // check if the order id is valid
140
+ _ , err := sanitizeOrderId (order .OrderID )
141
+ if err != nil {
142
+ log .Printf ("Failed to sanitize order id: %s" , err )
143
+ c .AbortWithStatus (http .StatusBadRequest )
144
+ return
145
+ }
146
+
147
+ err = client .repo .UpdateOrder (order )
132
148
if err != nil {
133
149
log .Printf ("Failed to update order status: %s" , err )
134
150
c .AbortWithStatus (http .StatusInternalServerError )
@@ -175,3 +191,17 @@ func initDatabase(apiType string) (*OrderService, error) {
175
191
return NewOrderService (mongoRepo ), nil
176
192
}
177
193
}
194
+
195
+ func sanitizeOrderId (id string ) (string , error ) {
196
+ // sanitize the id to prevent NoSQL injection
197
+ var sanitizedId string
198
+ // ensure id is a valid orderId that can be converted to int
199
+ _ , err := strconv .Atoi (id )
200
+ if err != nil {
201
+ log .Printf ("Invalid order id: %s" , err )
202
+ return sanitizedId , err
203
+ } else {
204
+ sanitizedId = id
205
+ }
206
+ return sanitizedId , nil
207
+ }
0 commit comments