fiber icon indicating copy to clipboard operation
fiber copied to clipboard

Fix(🩹): error handling in CSRF token storage retrieval

Open renanbastos93 opened this issue 1 year ago • 9 comments

Description

Resolved TODO for error handling to ensure code quality in retrieving CSRF token data from storage.

Fixes #3020

Changes introduced

List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.

  • [ ] Benchmarks: Describe any performance benchmarks and improvements related to the changes.
  • [ ] Documentation Update: Detail the updates made to the documentation and links to the changed files.
  • [ ] Changelog/What's New: Include a summary of the additions for the upcoming release notes.
  • [ ] Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
  • [ ] API Alignment with Express: Explain how the changes align with the Express API.
  • [ ] API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.
  • [ ] Examples: Provide examples demonstrating the new features or changes in action.

Type of change

Please delete options that are not relevant.

  • [ ] New feature (non-breaking change which adds functionality)
  • [x] Enhancement (improvement to existing features and functionality)
  • [ ] Documentation update (changes to documentation)
  • [ ] Performance improvement (non-breaking change which improves efficiency)
  • [x] Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • [ ] Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • [ ] Conducted a self-review of the code and provided comments for complex or critical parts.
  • [ ] Updated the documentation in the /docs/ directory for Fiber's documentation.
  • [x] Added or updated unit tests to validate the effectiveness of the changes or new features.
  • [ ] Ensured that new and existing unit tests pass locally with the changes.
  • [ ] Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • [ ] Aimed for optimal performance with minimal allocations in the new code.
  • [ ] Provided benchmarks for the new code to analyze and improve upon.

Commit formatting

Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md

renanbastos93 avatar May 30 '24 21:05 renanbastos93

[!IMPORTANT]

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes enhance error handling in the CSRF middleware by introducing new error variables and updating function signatures to return errors. This involves modifications in csrf.go, session_manager.go, and storage_manager.go to improve robustness and provide more informative error responses.

Changes

Files Change Summary
middleware/csrf/csrf.go Added new error variables, updated error messages, modified error handling in New function, changed return type of getRawFromStorage.
middleware/csrf/session_manager.go Updated getRaw method in sessionManager to return a tuple of rawToken []byte and err error.
middleware/csrf/storage_manager.go Added error handling and updated return types for getRaw, setRaw, and delRaw methods in storageManager.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant Middleware
    participant Storage

    Client->>Middleware: Request with CSRF token
    Middleware->>Storage: getRaw(key)
    Storage-->>Middleware: raw, err
    alt err is not nil
        Middleware-->>Client: Error Response (ErrNotGetStorage)
    else
        Middleware->>Middleware: Validate Token
        alt Token Invalid
            Middleware-->>Client: Error Response (ErrTokenInvalid)
        else
            Middleware-->>Client: Success Response
        end
    end

Assessment against linked issues

Objective (Issue #3020) Addressed Explanation
Enhance error handling in session_manager.go and storage_manager.go
Refactor error handling logic to provide robust responses

Poem

In the land of code, where errors roam,
A rabbit hopped, to make it known,
With careful tweaks and thoughtful care,
Improved the CSRF, errors now rare.
Robust and strong, the middleware stands,
Thanks to the rabbit, with its nimble hands.
🌟🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar May 30 '24 21:05 coderabbitai[bot]

Codecov Report

Attention: Patch coverage is 61.76471% with 13 lines in your changes missing coverage. Please review.

Project coverage is 83.01%. Comparing base (4e5a501) to head (f301d39).

Files Patch % Lines
middleware/csrf/storage_manager.go 47.61% 11 Missing :warning:
middleware/csrf/session_manager.go 75.00% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3021      +/-   ##
==========================================
- Coverage   83.03%   83.01%   -0.02%     
==========================================
  Files         115      115              
  Lines        8323     8339      +16     
==========================================
+ Hits         6911     6923      +12     
- Misses       1079     1087       +8     
+ Partials      333      329       -4     
Flag Coverage Δ
unittests 83.01% <61.76%> (-0.02%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar May 30 '24 21:05 codecov[bot]

Why isnt this fix in the rewrite PR #3016 ? It's a 1 line change @sixcolors

gaby avatar May 30 '24 23:05 gaby

Why isnt this fix in the rewrite PR #3016 ? It's a 1 line change @sixcolors

@gaby Since he opened an issue to address error handling, I tried to help him with just that part to save him time so that he could focus on the other changes.

renanbastos93 avatar May 30 '24 23:05 renanbastos93

@gaby sorry, Did I make a mistake?

renanbastos93 avatar May 31 '24 00:05 renanbastos93

@renanbastos93 Might need to update the tests, the coverage dropped. Existing tests are passing though.

gaby avatar Jun 02 '24 14:06 gaby

@gaby and @sixcolors of course I will do it. Thanks

renanbastos93 avatar Jun 02 '24 16:06 renanbastos93

@renanbastos93 Any updates on this?

gaby avatar Jun 25 '24 00:06 gaby

@renanbastos93 Any updates on this?

Hey @gaby , I am so sorry you were waiting for it. I've been sick for the last two weeks and need to finish the unit tests. I will focus on them this week. Do we have any other details to adjust?

renanbastos93 avatar Jun 25 '24 01:06 renanbastos93

@renanbastos93 can you refresh the code with the last master

ReneWerner87 avatar Nov 15 '24 07:11 ReneWerner87

@renanbastos93 can you refresh the code with the last master

Of course, I've been in a rush around here and haven't been able to give it the attention it deserves. I'm thinking about closing the PR and opening a new one, what do you think?

renanbastos93 avatar Nov 15 '24 13:11 renanbastos93

ok is also fine for me

ReneWerner87 avatar Nov 15 '24 14:11 ReneWerner87