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

fix: percent decode path segments #129

Merged
merged 2 commits into from
Aug 26, 2021
Merged

fix: percent decode path segments #129

merged 2 commits into from
Aug 26, 2021

Conversation

ttys3
Copy link
Contributor

@ttys3 ttys3 commented Aug 26, 2021

Motivation

ServerDir does not do url decode.

When I try to GET a uri like /中文文件.txt

the user agent will send /%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6.txt , and ServerDir just try to open a file named /%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6.txt, cause a 404 file not found error.

related issue #114

Solution

decode uri path before open it as an file path

tower-http/Cargo.toml Outdated Show resolved Hide resolved
tower-http/src/services/fs/serve_dir.rs Outdated Show resolved Hide resolved
@ttys3 ttys3 force-pushed the master branch 2 times, most recently from 7f8c06d to b51e9ce Compare August 26, 2021 18:31
@ttys3 ttys3 requested a review from davidpdrsn August 26, 2021 18:47
@ttys3 ttys3 force-pushed the master branch 4 times, most recently from 68067f1 to bf1fa52 Compare August 26, 2021 19:15
@ttys3 ttys3 requested a review from davidpdrsn August 26, 2021 19:16
Comment on lines 72 to 73
let path_decoded: Cow<str>;
if let Ok(decoded_utf8) = percent_decode(path.as_ref()).decode_utf8() {
Copy link
Member

Choose a reason for hiding this comment

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

I think this should work as well.

Suggested change
let path_decoded: Cow<str>;
if let Ok(decoded_utf8) = percent_decode(path.as_ref()).decode_utf8() {
let path_decoded = if let Ok(decoded_utf8) = percent_decode(path.as_ref()).decode_utf8() {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow, this could save a Cow import

@davidpdrsn
Copy link
Member

Nice! Wanna add a line to changelog?

@ttys3 ttys3 force-pushed the master branch 2 times, most recently from f38408e to 30c4820 Compare August 26, 2021 19:33
@ttys3
Copy link
Contributor Author

ttys3 commented Aug 26, 2021

Nice! Wanna add a line to changelog?

Changelog added, thank you for being so patient to a Rust newbie.

today I learned a lot

@davidpdrsn
Copy link
Member

Thanks for fixing this!

@davidpdrsn davidpdrsn merged commit e25d4f0 into tower-rs:master Aug 26, 2021
davidpdrsn added a commit that referenced this pull request Nov 13, 2021
- New middleware: Add `Cors` for setting [CORS] headers ([#112])
- New middleware: Add `AsyncRequireAuthorization` ([#118])
- `Compression`: Don't recompress HTTP responses ([#140])
- `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132])
- `ServeDir` and `ServeFile`: Improve performance ([#137])
- `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117])
- `ServeDir`: Percent decode path segments ([#129])
- `ServeDir`: Use correct redirection status ([#130])
- `ServeDir`: Return `404 Not Found` on requests to directories if
  `append_index_html_on_directories` is set to `false` ([#122])

[#112]: #112
[#118]: #118
[#140]: #140
[#132]: #132
[#137]: #137
[#117]: #117
[#129]: #129
[#130]: #130
[#122]: #122
@davidpdrsn davidpdrsn mentioned this pull request Nov 13, 2021
davidpdrsn added a commit that referenced this pull request Nov 13, 2021
- New middleware: Add `Cors` for setting [CORS] headers ([#112])
- New middleware: Add `AsyncRequireAuthorization` ([#118])
- `Compression`: Don't recompress HTTP responses ([#140])
- `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132])
- `ServeDir` and `ServeFile`: Improve performance ([#137])
- `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117])
- `ServeDir`: Percent decode path segments ([#129])
- `ServeDir`: Use correct redirection status ([#130])
- `ServeDir`: Return `404 Not Found` on requests to directories if
  `append_index_html_on_directories` is set to `false` ([#122])

[#112]: #112
[#118]: #118
[#140]: #140
[#132]: #132
[#137]: #137
[#117]: #117
[#129]: #129
[#130]: #130
[#122]: #122
davidpdrsn added a commit that referenced this pull request Nov 13, 2021
- New middleware: Add `Cors` for setting [CORS] headers ([#112])
- New middleware: Add `AsyncRequireAuthorization` ([#118])
- `Compression`: Don't recompress HTTP responses ([#140])
- `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132])
- `ServeDir` and `ServeFile`: Improve performance ([#137])
- `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117])
- `ServeDir`: Percent decode path segments ([#129])
- `ServeDir`: Use correct redirection status ([#130])
- `ServeDir`: Return `404 Not Found` on requests to directories if
  `append_index_html_on_directories` is set to `false` ([#122])

[#112]: #112
[#118]: #118
[#140]: #140
[#132]: #132
[#137]: #137
[#117]: #117
[#129]: #129
[#130]: #130
[#122]: #122
davidpdrsn added a commit that referenced this pull request Nov 13, 2021
- New middleware: Add `Cors` for setting [CORS] headers ([#112])
- New middleware: Add `AsyncRequireAuthorization` ([#118])
- `Compression`: Don't recompress HTTP responses ([#140])
- `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132])
- `ServeDir` and `ServeFile`: Improve performance ([#137])
- `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117])
- `ServeDir`: Percent decode path segments ([#129])
- `ServeDir`: Use correct redirection status ([#130])
- `ServeDir`: Return `404 Not Found` on requests to directories if
  `append_index_html_on_directories` is set to `false` ([#122])

[#112]: #112
[#118]: #118
[#140]: #140
[#132]: #132
[#137]: #137
[#117]: #117
[#129]: #129
[#130]: #130
[#122]: #122
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.

2 participants