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

feat(server): do path joins more safely #247

Merged
merged 8 commits into from
Mar 6, 2024

Conversation

RealOrangeOne
Copy link
Contributor

Description

Check that a joined path is still a child of the original path.

Motivation and Context

When joining paths, there's little protection against directory traversal. From testing, Actix seems to nicely sanitises these in the request, which is great, but it's still better to add a little protection.

How Has This Been Tested?

Unit tests have been added, which pass.

Changelog Entry

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (no code change)
  • Refactor (refactoring production code)
  • Other

Checklist:

  • My code follows the code style of this project.
  • I have updated the documentation accordingly.
  • I have formatted the code with rustfmt.
  • I checked the lints with clippy.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@RealOrangeOne RealOrangeOne requested a review from orhun as a code owner February 16, 2024 13:59
@codecov-commenter
Copy link

codecov-commenter commented Feb 16, 2024

Codecov Report

Attention: Patch coverage is 94.59459% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 71.20%. Comparing base (db971e6) to head (0f83176).

Files Patch % Lines
src/main.rs 0.00% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #247      +/-   ##
==========================================
+ Coverage   70.88%   71.20%   +0.32%     
==========================================
  Files          11       11              
  Lines         625      639      +14     
==========================================
+ Hits          443      455      +12     
- Misses        182      184       +2     
Flag Coverage Δ
unit-tests 71.20% <94.59%> (+0.32%) ⬆️

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.

src/util.rs Outdated Show resolved Hide resolved
src/util.rs Show resolved Hide resolved
@RealOrangeOne RealOrangeOne requested a review from orhun February 16, 2024 18:13
src/paste.rs Outdated Show resolved Hide resolved
src/paste.rs Outdated Show resolved Hide resolved
@tessus
Copy link
Collaborator

tessus commented Feb 25, 2024

@orhun it seems that you have to approve the CI for the 2 PRs.

@tessus
Copy link
Collaborator

tessus commented Feb 25, 2024

But what I could ascertain from a quick look at the code is that prior to this PR errors were handled gracefully. After this PR rustypaste will panic. Not sure I like this.

@orhun
Copy link
Owner

orhun commented Feb 26, 2024

Yup, I also think that we should handle errors.

@RealOrangeOne
Copy link
Contributor Author

There's only 1 panic this introduces, and it's during server startup. Sure, I could facade it to convert a path issue to IoResult, and let Rust deal with returning that one, or I can call expect. The latter felt fine for a quick change.

@tessus
Copy link
Collaborator

tessus commented Feb 26, 2024

What about the occurrences of PasteType::Url.get_path(&config.server.upload_path).unwrap()?

Isn't it true that these can panic?

P.S.: but I also noticed the change from fs::create_dir_all(paste_type.get_path(&server_config.upload_path))? to use an expect, which is probably what you meant in your previous comment.

@RealOrangeOne
Copy link
Contributor Author

There are other uses of unwrap, but those are strictly in tests, which I've not considered to be an issue. If a test panics, it's functionally the same as a failure, which is what we want.

Yes, the fs::create_dir_all is the only new place the application could panic.

@tessus
Copy link
Collaborator

tessus commented Feb 27, 2024

but those are strictly in tests

Oops, once again the gh diff failed me again. all the test headers were not in the code, thus I didn't see that those were tests. All good then.

Yes, the fs::create_dir_all is the only new place the application could panic.

I would rather see an error message than a panic. But it's up to orhun.

@orhun
Copy link
Owner

orhun commented Feb 28, 2024

Yes, it would be better to print out an error message than panicking. Also, there are some lints about the usage of unwrap in tests 🙂 we can simply replace them with expect.

@RealOrangeOne RealOrangeOne requested a review from orhun February 28, 2024 18:12
src/main.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/paste.rs Outdated Show resolved Hide resolved
@orhun
Copy link
Owner

orhun commented Mar 5, 2024

Looks like there is a test failure:

 ---- util::tests::test_safe_join_path stdout ----
thread 'util::tests::test_safe_join_path' panicked at src/util.rs:211:9:
assertion failed: safe_path_join("/foo", "/foobar").is_ok()
stack backtrace:

Copy link
Owner

@orhun orhun left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

@orhun orhun changed the title Do path joins more safely feat(server): do path joins more safely Mar 6, 2024
@orhun orhun merged commit dae00c4 into orhun:master Mar 6, 2024
7 of 8 checks passed
@RealOrangeOne RealOrangeOne deleted the harden-path-joins branch March 6, 2024 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants