Skip to content

Commit 8e8104b

Browse files
committed
fix some gosec issues
1 parent 939fafc commit 8e8104b

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

examples/gin-server/main.go

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

33
import (
4+
"log"
45
"net/http"
56

67
"github.com/gin-gonic/gin"
@@ -38,5 +39,8 @@ func main() {
3839

3940
r := setupRouter()
4041
// Listen and Server in 0.0.0.0:8080
41-
r.Run(":8080")
42+
err := r.Run(":8080")
43+
if err != nil {
44+
log.Fatalf("gin server failed with error: %v", err)
45+
}
4246
}

examples/http-server/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func main() {
9090
))
9191
// Uncomment to enable TLS server
9292
// log.Fatal(http.ListenAndServeTLS(":8081", certFile, keyFile, r))
93+
// G114 (CWE-676): Use of net/http serve function that has no support for setting timeouts (Confidence: HIGH, Severity: MEDIUM)
94+
// #nosec G114
9395
log.Fatal(http.ListenAndServe(":8081", r))
9496
}
9597

@@ -135,7 +137,7 @@ func fooHandler(w http.ResponseWriter, r *http.Request) {
135137
w.Header().Set("Content-Type", "application/json")
136138
w.WriteHeader(http.StatusOK)
137139

138-
w.Write([]byte(fmt.Sprintf("{\"param0\": \"Hello %s\", \"authorization\": \"Bearer some-jwt-token\", \"param2\":{\"param3\":\"param4\",\"value\":\"00000\"}}", p.Name)))
140+
_, _ = w.Write([]byte(fmt.Sprintf("{\"param0\": \"Hello %s\", \"authorization\": \"Bearer some-jwt-token\", \"param2\":{\"param3\":\"param4\",\"value\":\"00000\"}}", p.Name)))
139141
}
140142

141143
var responseBody string = `{"serviceCount":4,"personalDetailChildren":[{"id":"41d29bf9-3d7b-4e7b-90f1-e405693b592f","firstName":"Bruce","lastName":"Meyer","dateOfBirth":"1951-4-20",` +
@@ -192,7 +194,7 @@ func payloadHandler(w http.ResponseWriter, r *http.Request) {
192194

193195
w.Header().Set("Content-Type", "application/json")
194196
w.WriteHeader(http.StatusOK)
195-
w.Write([]byte(responseBody))
197+
_, _ = w.Write([]byte(responseBody))
196198
}
197199

198200
// Should be a GET request
@@ -215,7 +217,7 @@ func barHandler(w http.ResponseWriter, r *http.Request) {
215217
}
216218

217219
w.WriteHeader(http.StatusOK)
218-
w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", author)))
220+
_, _ = w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", author)))
219221
}
220222

221223
// Should be a POST request.
@@ -234,7 +236,7 @@ func barBodyHandler(w http.ResponseWriter, r *http.Request) {
234236
w.Header().Add("Set-Cookie", "timothercookie789=cval1;secure")
235237
w.Header().Add("Set-Cookie", "timothercookie=cval2;secure")
236238
w.WriteHeader(http.StatusOK)
237-
w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", author)))
239+
_, _ = w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", author)))
238240
}
239241

240242
const multipartMaxSize int64 = 1024 * 1024
@@ -261,7 +263,7 @@ func multipartFormHandler(w http.ResponseWriter, r *http.Request) {
261263
w.WriteHeader(http.StatusInternalServerError)
262264
return
263265
}
264-
err = os.WriteFile("/tmp/gadat1.png", buf, 0644)
266+
err = os.WriteFile("/tmp/gadat1.png", buf, 0600)
265267
if err != nil {
266268
fmt.Printf("error while writing file: %v\n", err)
267269
w.WriteHeader(http.StatusInternalServerError)
@@ -275,7 +277,7 @@ func multipartFormHandler(w http.ResponseWriter, r *http.Request) {
275277
w.Header().Add("Set-Cookie", "othercookie789=cval1;secure")
276278
w.Header().Add("Set-Cookie", "othercookie=cval2;secure")
277279
w.WriteHeader(http.StatusOK)
278-
w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", author)))
280+
_, _ = w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", author)))
279281
}
280282

281283
func outgoingCallHandler(w http.ResponseWriter, r *http.Request) {
@@ -330,7 +332,7 @@ func echoUpperCaseHandler(w http.ResponseWriter, r *http.Request) {
330332

331333
w.Header().Set("Content-Type", "application/json")
332334
w.WriteHeader(http.StatusOK)
333-
w.Write([]byte(strings.ToUpper(string(sBody))))
335+
_, _ = w.Write([]byte(strings.ToUpper(string(sBody))))
334336
}
335337

336338
func echoHandler(w http.ResponseWriter, r *http.Request) {
@@ -350,7 +352,7 @@ func echoHandler(w http.ResponseWriter, r *http.Request) {
350352
}
351353
w.Header().Set("Access-Control-Allow-Credentials", "true")
352354
w.WriteHeader(http.StatusOK)
353-
w.Write([]byte(string(sBody)))
355+
_, _ = w.Write([]byte(string(sBody)))
354356
}
355357

356358
func getXmlHandler(w http.ResponseWriter, r *http.Request) {
@@ -372,5 +374,5 @@ func getXmlHandler(w http.ResponseWriter, r *http.Request) {
372374

373375
w.Header().Set("Content-Type", "application/xml")
374376
w.WriteHeader(http.StatusOK)
375-
w.Write([]byte(xmlSampleBody))
377+
_, _ = w.Write([]byte(xmlSampleBody))
376378
}

examples/mux-server/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func main() {
2828
r := mux.NewRouter()
2929
r.Use(hypermux.NewMiddleware(&sdkhttp.Options{})) // here we use the mux middleware
3030
r.HandleFunc("/foo", http.HandlerFunc(fooHandler))
31+
// G114 (CWE-676): Use of net/http serve function that has no support for setting timeouts (Confidence: HIGH, Severity: MEDIUM)
32+
// #nosec G114
3133
log.Fatal(http.ListenAndServe(":8081", r))
3234
}
3335

@@ -54,5 +56,5 @@ func fooHandler(w http.ResponseWriter, r *http.Request) {
5456

5557
w.Header().Set("Content-Type", "application/json")
5658
w.WriteHeader(http.StatusOK)
57-
w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", p.Name)))
59+
_, _ = w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", p.Name)))
5860
}

examples/sql-query/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func main() {
4040
http.HandlerFunc(fooHandlerFunc),
4141
"/foo",
4242
))
43+
// G114 (CWE-676): Use of net/http serve function that has no support for setting timeouts (Confidence: HIGH, Severity: MEDIUM)
44+
// #nosec G114
4345
log.Fatal(http.ListenAndServe(":8081", r))
4446
}
4547

@@ -68,7 +70,7 @@ func fooHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
6870

6971
w.Header().Set("Content-Type", "application/json")
7072
w.WriteHeader(http.StatusOK)
71-
w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", p.Name)))
73+
_, _ = w.Write([]byte(fmt.Sprintf("{\"message\": \"Hello %s\"}", p.Name)))
7274
}
7375

7476
func dbConn() (db *sql.DB) {

0 commit comments

Comments
 (0)