TykTechnologies / tyk

Tyk Open Source API Gateway written in Go, supporting REST, GraphQL, TCP and gRPC protocols
Other
9.7k stars 1.09k forks source link

[TT-11762] fix nil err return #6365

Closed jeffy-mathew closed 4 months ago

jeffy-mathew commented 4 months ago

User description

Description

Fix sonarcloud reported issue where err is returned when it is nil

Related Issue

https://tyktech.atlassian.net/browse/TT-11762

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

Checklist


PR Type

Bug fix


Description


Changes walkthrough ๐Ÿ“

Relevant files
Bug fix
mw_auth_key.go
Fix nil error check in `validateSignature` method               

gateway/mw_auth_key.go
  • Fixed the condition to correctly handle non-nil errors in
    validateSignature.
  • +1/-1     

    ๐Ÿ’ก PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    github-actions[bot] commented 4 months ago

    PR Reviewer Guide ๐Ÿ”

    โฑ๏ธ Estimated effort to review [1-5] 1
    ๐Ÿงช Relevant tests No
    ๐Ÿ”’ Security concerns No
    โšก Key issues to review None
    github-actions[bot] commented 4 months ago

    API Changes

    no api changes detected
    github-actions[bot] commented 4 months ago

    PR Code Suggestions โœจ

    CategorySuggestion                                                                                                                                    Score
    Error handling
    Handle potential errors from storage.TokenID to prevent undefined behavior ___ **Consider handling the case where err is not nil after the call to storage.TokenID(key).
    This would improve error handling and avoid proceeding with an undefined keyID.** [gateway/mw_auth_key.go [175-177]](https://github.com/TykTechnologies/tyk/pull/6365/files#diff-aeba053023a54c723dd9f83837e29ca0b2d9a212bc98fa6ad4bbb062669a1cf0R175-R177) ```diff keyID, err := storage.TokenID(key) -if err == nil { - err, statusCode := k.validateSignature(r, keyID) +if err != nil { + return err, http.StatusBadRequest // or another appropriate error code +} +err, statusCode := k.validateSignature(r, keyID) ```
    Suggestion importance[1-10]: 10 Why: This suggestion improves error handling by ensuring that the function does not proceed with an undefined `keyID`, which could lead to unpredictable behavior. This is a crucial fix for stability.
    10
    Possible bug
    Ensure a meaningful status code is returned when an error occurs ___ **The condition if err != nil should be followed by a return statement that includes a
    default or meaningful status code if statusCode is not set, to avoid returning an
    uninitialized statusCode.** [gateway/mw_auth_key.go [178-179]](https://github.com/TykTechnologies/tyk/pull/6365/files#diff-aeba053023a54c723dd9f83837e29ca0b2d9a212bc98fa6ad4bbb062669a1cf0R178-R179) ```diff if err != nil { + if statusCode == 0 { + statusCode = http.StatusInternalServerError // or another appropriate default code + } return err, statusCode } ```
    Suggestion importance[1-10]: 9 Why: This suggestion addresses a potential bug where an uninitialized status code might be returned, ensuring that a meaningful status code is always provided. This is important for robust error handling.
    9
    Best practice
    Adjust the order of return values from validateSignature to follow Go's convention ___ **The function validateSignature should return values in the order (statusCode, err) to
    follow Go's convention of returning the error as the last value. This will also require
    updating the handling of the returned values.** [gateway/mw_auth_key.go [177]](https://github.com/TykTechnologies/tyk/pull/6365/files#diff-aeba053023a54c723dd9f83837e29ca0b2d9a212bc98fa6ad4bbb062669a1cf0R177-R177) ```diff -err, statusCode := k.validateSignature(r, keyID) +statusCode, err := k.validateSignature(r, keyID) ```
    Suggestion importance[1-10]: 8 Why: This suggestion follows Go's convention of returning the error as the last value, which improves code readability and consistency. It is a best practice but not a critical fix.
    8
    Maintainability
    Refactor the nested conditionals to improve code readability and maintainability ___ **Refactor the nested conditionals for better readability and maintainability. Flatten the
    structure by handling error cases first and returning early.** [gateway/mw_auth_key.go [176-180]](https://github.com/TykTechnologies/tyk/pull/6365/files#diff-aeba053023a54c723dd9f83837e29ca0b2d9a212bc98fa6ad4bbb062669a1cf0R176-R180) ```diff -if err == nil { - err, statusCode := k.validateSignature(r, keyID) - if err != nil { - return err, statusCode - } +if err != nil { + return nil, http.StatusOK // Assuming no error means success +} +statusCode, err := k.validateSignature(r, keyID) +if err != nil { + return err, statusCode } ```
    Suggestion importance[1-10]: 7 Why: This suggestion improves code readability and maintainability by flattening nested conditionals. While it enhances the code structure, it is not as critical as fixing functional issues.
    7
    github-actions[bot] commented 4 months ago

    :boom: CI tests failed :see_no_evil:

    git-state

    all ok

    Please look at the run or in the Checks tab.

    github-actions[bot] commented 4 months ago

    :boom: CI tests failed :see_no_evil:

    git-state

    all ok

    Please look at the run or in the Checks tab.

    sonarcloud[bot] commented 4 months ago

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    100.0% Coverage on New Code
    0.0% Duplication on New Code

    See analysis details on SonarCloud

    sonarcloud[bot] commented 4 months ago

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    100.0% Coverage on New Code
    0.0% Duplication on New Code

    See analysis details on SonarCloud

    sonarcloud[bot] commented 4 months ago

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    100.0% Coverage on New Code
    0.0% Duplication on New Code

    See analysis details on SonarCloud

    sonarcloud[bot] commented 4 months ago

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    100.0% Coverage on New Code
    0.0% Duplication on New Code

    See analysis details on SonarCloud