-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Overly strict FromSegments implementation for PathBuf #560
Comments
Hi, I have also encountered this behavior when I wanted to accept dotfiles (as you say) in my routes. So I made a workaround (used in one of my personal project) which is certainly not perfect but works. Use case: #[get("/<unsafe_p..>")]
pub fn dotfiles(unsafe_p: UnsafePBuf) -> ... {
let dotpath = unsafe_p.path();
/* Do wathever you want with your path */
} Hopefully, my answer met your expectations. |
Thank you @zxvfxwing , I was about to make a similar workaround myself, and you just saved me the effort. That said, it would still be great if such a change could be made in Rocket itself. It looks like there is interest for it, and I can submit a PR. The question is if @SergioBenitez would accept it, and which cases may be allowed. I'm not really a web dev, so I'm not familiar with all the forms of path traversal attacks. |
The danger is that users will unknowingly leak hidden files if this change is made. I'd like to keep the safe-by-default behavior while also allowing users to disable this on a per-case basis. In #239 (comment) I sketch out an API for mounting a "StaticFiles" handler. To resolve this issue, that handler could accept options on creation to allow this behavior: rocket::ignite()
.mount("/path_to_serve_on", StaticFiles::from("/path_to_serve_from").allow_hidden()); I've already laid out all of the ground work to enable the implementation of something like If this sounds like a good plan moving forward @Noughmad, let's close this and track progress in #239. (Edit: Whoops. Didn't mean to close it myself!) P.S: Complementary to this, we could have the |
Serving static files is not good for me, as I want to show other things in addition to the file content. However, as I said I the workaround meantioned above works for me, and is not overly complicated. So unless you wish to change the current implementation, feel free to close the issue. |
Has any progress been made on this in the last 2 and a half years? For reasons that aren't relevant using I would quite like to be able to decide myself whether or not I want to handle a provided path. Yeah security by default is nice but I'd like the option to turn it off. Also, @zxvfxwing, your workaround seems to not work in rocket 0.5.0-rc |
Yes. Rocket doesn't prohibit you from doing anything in this space, and never did. You can take a raw |
I probably should've clarified my issue is that routes refuse to accept any path with a segment starting in As far as I can tell doing this #[macro_use] extern crate rocket;
use std::path::PathBuf;
#[get("/<path..>")]
fn route(path: PathBuf) -> _ {
//
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/route", routes![route])
} will never let you access If there's a way to use raw |
As I said in my response, you simply need take a #[get("/<path..>")]
fn route(path: Segments<'_, Path>) -> _ {
let path = path.to_path_buf(true)?;
} Furthermore, you can create your own type implementing FromSegments that implements whatever security policy you want, entirely removing the need to use Segments in a route directly and centralizing said policy. |
Oh I didn't realize that was even possible Sorry 'bout that Edit for anyone who stumbles upon this: #[macro_use] extern crate rocket;
#[get("/<path..>")]
fn route(path: rocket::http::uri::Segments<'_, rocket::http::uri::fmt::Path>) -> _ {
let path = path.to_path_buf(true).unwrap();
// Stuff
}
Also the validation code currently exists at https://github.com/SergioBenitez/Rocket/blob/master/core/http/src/uri/segments.rs#L201 |
In the interest of safety, the implementation of
FromSegments
forPathBuf
(code) rejects all segments that start with a dot (.
). I believe this is overly strict, since..
) is already handled..
) could also be valid, and just skipped..
, such as.gitignore
and other config files.In my use case I only require the third case, as I am trying to create a file browser, where the path is specified as
<path..>
andpath: PathBuf
. However, such a route cannot browse a file with a name starting with a dot, and I get aFailed to parse 'path': BadStart('.')
error message.I can reproduce this behavior on version
0.3.6
on ArchLinux, but looking at the code I can see it is present inmaster
as well.The text was updated successfully, but these errors were encountered: