Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: Improve path validation in Static Middleware #3105

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ashermyers
Copy link

@ashermyers ashermyers commented Aug 20, 2024

Description

The code uses filepath.Abs to resolve both the root directory and the requested path to their absolute forms. This improves security by further preventing advanced directory traversal attacks.

Changes introduced

Absolute Path Resolution

Type of change

  • [ X ] Enhancement (improvement to existing features and functionality)

@ashermyers ashermyers requested a review from a team as a code owner August 20, 2024 06:31
@ashermyers ashermyers requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team August 20, 2024 06:31
Copy link

welcome bot commented Aug 20, 2024

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Copy link
Contributor

coderabbitai bot commented Aug 20, 2024

Walkthrough

The recent changes enhance the security of the New function in the middleware/static/static.go file. By implementing explicit path validation, the function now ensures that requested paths stay within a specified root directory. This prevents unauthorized access to files outside the intended area, responding with appropriate status codes for various error conditions.

Changes

Files Change Summary
middleware/static/static.go Added path validation in the New function, enforcing that paths do not escape the designated root directory. Responds with 403 Forbidden for invalid accesses and 500 Internal Server Error for internal errors.

Poem

In the garden where we play,
A rabbit hops the safe way,
With paths that lead us near and far,
No wandering past the garden's bar.
So here’s a cheer for rules so bright,
Keeping our frolics safe and light! 🐇✨


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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 25e3992 and 63afcdf.

Files selected for processing (1)
  • middleware/static/static.go (1 hunks)
Additional comments not posted (1)
middleware/static/static.go (1)

105-117: Security Enhancement: Absolute Path Validation Approved

The implementation of absolute path resolution using filepath.Abs effectively mitigates directory traversal attacks by ensuring that the requested path is within the root directory. The use of appropriate HTTP status codes (500 for internal errors and 403 for forbidden access) enhances security and robustness.

Copy link

codecov bot commented Aug 20, 2024

Codecov Report

Attention: Patch coverage is 20.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 82.64%. Comparing base (7126e48) to head (14cbaa0).

Files with missing lines Patch % Lines
middleware/static/static.go 20.00% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3105      +/-   ##
==========================================
- Coverage   82.76%   82.64%   -0.13%     
==========================================
  Files         114      114              
  Lines       11149    11159      +10     
==========================================
- Hits         9228     9222       -6     
- Misses       1521     1533      +12     
- Partials      400      404       +4     
Flag Coverage Δ
unittests 82.64% <20.00%> (-0.13%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gaby gaby changed the title enhancement: absolute path resolution for added security v3: Improve path validation in Static Middleware Aug 20, 2024
@ReneWerner87
Copy link
Member

@ashermyers thx for the adjustment
can you pls provide some test cases for the functionallity extension

@ReneWerner87
Copy link
Member

@ashermyers can you add some test cases code you have changed

@gaby
Copy link
Member

gaby commented Sep 8, 2024

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Sep 8, 2024

Actions performed

Full review triggered.

Copy link
Contributor

coderabbitai bot commented Sep 8, 2024

Walkthrough

The changes enhance the path validation logic in the New function of the middleware/static/static.go file. The updated implementation computes the absolute path of the provided root parameter and checks for errors. It constructs the absolute path of the requested resource and validates it against the root path. If the validation fails, the function responds with a 403 Forbidden status code, improving security by preventing unauthorized access to paths outside the designated root directory.

Changes

File Change Summary
middleware/static/static.go Enhanced path validation logic in New function to prevent unauthorized access and improve security.

Poem

🐇 In the meadow where paths align,
A rabbit hops with joy divine.
With checks in place, we guard the way,
No sneaky paths will lead astray!
Hooray for safety, let’s rejoice,
For every hop, we now have choice! 🌼


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f668537 and 12b8bf0.

Files selected for processing (1)
  • middleware/static/static.go (1 hunks)
Additional context used
GitHub Check: codecov/patch
middleware/static/static.go

[warning] 108-109: middleware/static/static.go#L108-L109
Added lines #L108 - L109 were not covered by tests


[warning] 114-115: middleware/static/static.go#L114-L115
Added lines #L114 - L115 were not covered by tests

Additional comments not posted (2)
middleware/static/static.go (2)

Line range hint 183-205: Well-implemented helper function.

The isFile function is well-implemented with robust error handling and flexibility to work with both custom file systems and the OS file system. This function supports the main functionality by determining if the root is a file, which is crucial for setting the correct path behavior in the middleware.

Tools
GitHub Check: codecov/patch

[warning] 108-109: middleware/static/static.go#L108-L109
Added lines #L108 - L109 were not covered by tests


[warning] 114-115: middleware/static/static.go#L114-L115
Added lines #L114 - L115 were not covered by tests


105-117: Approve changes but recommend adding test coverage.

The implementation of absolute path resolution is a significant enhancement for security, effectively mitigating directory traversal attacks. The logic to check for errors and ensure the requested path does not escape the designated root directory is correctly implemented.

However, the static analysis tool has flagged that new lines 108-109 and 114-115 are not covered by tests. It is crucial to ensure that these parts of the code are tested to maintain high code quality and reliability.

Tools
GitHub Check: codecov/patch

[warning] 108-109: middleware/static/static.go#L108-L109
Added lines #L108 - L109 were not covered by tests


[warning] 114-115: middleware/static/static.go#L114-L115
Added lines #L114 - L115 were not covered by tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants