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/migl/162 #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/c/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ bool Connection::processHeaders(uint8_t* first, uint8_t* last) {
if (requestUri == nullptr) {
return sendBadRequest("Malformed request line");
}
if (std::string(requestUri).find("..") != std::string::npos) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it safe to assume an URL shouldn't contain any ..? Eg. a/b/c/../d seems ok to me.

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean something like that?

std::string merge_uri(std::string & uri) {
    std::istringstream is_uri {uri};
    std::vector<std::string> v;
    std::string parsed_uri;

    for (std::string line; std::getline(is_uri, line, '/'); ) {
        if(line == ".") {
            continue;    
        } if(line == "..") {
            if(!v.empty()) {
                v.pop_back();
            }
        } else if(line != "") {
            line = "/" + line;
            v.push_back(line);
        }
    }

    for (auto const & dir : v){
        parsed_uri += dir;
    }

    return parsed_uri.empty() ? "/" : parsed_uri;
}

For comparison, nginx has your described behavior. e.g. a/b/c/../d becomes a/b/d . But one must also say, nginx has a complex state machine that not only protects against directory triversal, but can also handle all other special characters. If that's what we want, I'd at least like to take commit 5e7d92c and drop the other one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, the empty root path fix is fine to merge 👍

I'd say the path traversal needs some more work. Maybe std filesystem / path could get handy here?

return sendBadRequest("Malformed uri");
}

const char* httpVersion = shift(requestLine);
if (httpVersion == nullptr) {
Expand Down Expand Up @@ -1197,6 +1200,9 @@ std::list<Connection::Range> Connection::processRangesForStaticData(const std::l
#endif
bool Connection::sendStaticData() {
// TODO: fold this into the handler way of doing things.
if (_server.getStaticPath().empty()) {
return send404();
}
std::string path = _server.getStaticPath() + getRequestUri();
auto rangeHeader = getHeader("Range");
// Trim any trailing queries.
Expand Down