Skip to content

Commit

Permalink
Fix gosec and detected issues
Browse files Browse the repository at this point in the history
  • Loading branch information
epapbak committed Sep 30, 2024
1 parent cbee36f commit c3cd761
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion goerrcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ echo -e "${BLUE}Finding all unchecked errors${NC}"
if ! [ -x "$(command -v errcheck)" ]
then
echo -e "${BLUE}Installing errcheck ${NC}"
GO111MODULE=off go get github.com/kisielk/errcheck
GO111MODULE=on go install github.com/kisielk/errcheck@latest
fi


Expand Down
45 changes: 35 additions & 10 deletions http/router_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,15 @@ func ReadOrganizationID(writer http.ResponseWriter, request *http.Request, auth
return 0, false
}

successful := CheckPermissions(writer, request, ctypes.OrgID(organizationID), auth)
orgID, err := types.Uint64ToUint32(organizationID)
if err != nil {
HandleOrgIDError(writer, err)
return 0, false
}

return ctypes.OrgID(organizationID), successful
successful := CheckPermissions(writer, request, ctypes.OrgID(orgID), auth)

return ctypes.OrgID(orgID), successful
}

// ReadClusterNames does the same as `readClusterName`, except for multiple clusters.
Expand Down Expand Up @@ -223,6 +229,30 @@ func ReadClusterNames(writer http.ResponseWriter, request *http.Request) ([]ctyp
return clusterNamesConverted, true
}

// parseAndValidateOrgID parses and validates a single organization ID string.
func parseAndValidateOrgID(writer http.ResponseWriter, orgStr string) (ctypes.OrgID, bool) {
v, err := strconv.ParseUint(orgStr, 10, 64)
if err != nil {
handleOrgIDParsingError(writer, orgStr, "integer array expected")
return 0, false
}
orgInt, err := types.Uint64ToUint32(v)
if err != nil {
handleOrgIDParsingError(writer, orgStr, "integer array expected")
return 0, false
}
return ctypes.OrgID(orgInt), true
}

// handleOrgIDParsingError handles the error for parsing organization IDs.
func handleOrgIDParsingError(writer http.ResponseWriter, orgStr, errString string) {
types.HandleServerError(writer, &types.RouterParsingError{
ParamName: "organizations",
ParamValue: orgStr,
ErrString: errString,
})
}

// ReadOrganizationIDs does the same as `readOrganizationID`, except for multiple organizations.
func ReadOrganizationIDs(writer http.ResponseWriter, request *http.Request) ([]ctypes.OrgID, bool) {
organizationsParam, err := GetRouterParam(request, "organizations")
Expand All @@ -233,16 +263,11 @@ func ReadOrganizationIDs(writer http.ResponseWriter, request *http.Request) ([]c

organizationsConverted := make([]ctypes.OrgID, 0)
for _, orgStr := range SplitRequestParamArray(organizationsParam) {
orgInt, err := strconv.ParseUint(orgStr, 10, 64)
if err != nil {
types.HandleServerError(writer, &types.RouterParsingError{
ParamName: "organizations",
ParamValue: orgStr,
ErrString: "integer array expected",
})
orgID, ok := parseAndValidateOrgID(writer, orgStr)
if !ok {
return []ctypes.OrgID{}, false
}
organizationsConverted = append(organizationsConverted, ctypes.OrgID(orgInt))
organizationsConverted = append(organizationsConverted, orgID)
}

return organizationsConverted, true
Expand Down
2 changes: 1 addition & 1 deletion migrations/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func regexGetNthMatch(regexStr string, nMatch uint, str string) (string, error)
}

matches := regex.FindStringSubmatch(str)
if len(matches) < int(nMatch+1) {
if uint(len(matches)) < nMatch+1 {
return "", errors.New("regexGetNthMatch unable to find match")
}

Expand Down

0 comments on commit c3cd761

Please sign in to comment.