From 03839c65a06e4d17bd5cc1e4e24313d5fcb5961f Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:31:55 +0200 Subject: [PATCH 1/5] style check --- .github/workflows/ci.yml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eacc966..6905485 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,20 @@ jobs: go-version: "1.23.0" - name: Tests - run: go test ./... -cover \ No newline at end of file + run: go test ./... -cover + + style: + name: Style + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Style + run: test -z $(go fmt ./...) \ No newline at end of file From 6ab3c33a1cef4a5bd51656245be83bc5648f9be6 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:36:53 +0200 Subject: [PATCH 2/5] linting (with failing code) --- .github/workflows/ci.yml | 8 ++++++++ main.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6905485..6184eab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,5 +34,13 @@ jobs: with: go-version: "1.23.0" + - name: Install staticcheck + uses: dominikh/staticcheck-action@v1 + with: + version: "latest" + - name: Style + run: test -z $(go fmt ./...) + + - name: Linting run: test -z $(go fmt ./...) \ No newline at end of file diff --git a/main.go b/main.go index 19d7366..a621713 100644 --- a/main.go +++ b/main.go @@ -96,3 +96,8 @@ func main() { log.Printf("Serving on port: %s\n", port) log.Fatal(srv.ListenAndServe()) } + +func unused() { + // this function does nothing + // and is called nowhere +} From 222097af1ca4382891557b5d9c04423bacd48587 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:38:14 +0200 Subject: [PATCH 3/5] remove unused function --- main.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/main.go b/main.go index a621713..19d7366 100644 --- a/main.go +++ b/main.go @@ -96,8 +96,3 @@ func main() { log.Printf("Serving on port: %s\n", port) log.Fatal(srv.ListenAndServe()) } - -func unused() { - // this function does nothing - // and is called nowhere -} From 7603f9395afd24a2ddc0acee49862a6a2a35a737 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:42:59 +0200 Subject: [PATCH 4/5] security checks --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6184eab..73a0e7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,9 +18,15 @@ jobs: with: go-version: "1.23.0" + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + - name: Tests run: go test ./... -cover + - name: Security Checks + run: gosec ./... + style: name: Style runs-on: ubuntu-latest From 837c5863fd9c3da95a19ee3f6f9827b7519a1143 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:47:40 +0200 Subject: [PATCH 5/5] fix security issues --- json.go | 7 ++++++- main.go | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/json.go b/json.go index 1e6e798..7ee5351 100644 --- a/json.go +++ b/json.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "log" "net/http" ) @@ -30,5 +31,9 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { return } w.WriteHeader(code) - w.Write(dat) + _, err = w.Write(dat) + if err != nil { + fmt.Printf("Error wiring response: %s", err.Error()) + return + } } diff --git a/main.go b/main.go index 19d7366..7f48a6c 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -89,8 +90,9 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: time.Second * 30, } log.Printf("Serving on port: %s\n", port)