From 3373d771ac8ae052a2acf6a287ec71e367522fb7 Mon Sep 17 00:00:00 2001 From: NotAdam Date: Mon, 17 Feb 2020 15:45:27 +1100 Subject: [PATCH 1/2] fix an issue where some versions of std::fs would handle path generation differently this changes the builtin http server to generate more reliable paths when parsing request bodies, fixes #635 --- src/api/main.cpp | 5 +++-- src/api/server_http.hpp | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/api/main.cpp b/src/api/main.cpp index 78d02861..3874ae1d 100644 --- a/src/api/main.cpp +++ b/src/api/main.cpp @@ -671,8 +671,9 @@ void defaultGet( shared_ptr< HttpServer::Response > response, shared_ptr< HttpSe print_request_info( request ); try { - auto web_root_path = fs::canonical( "web" ); - auto path = fs::canonical( web_root_path / request->path ); + auto web_root_path = fs::current_path() / "web"; + auto path = web_root_path / request->path; + //Check if path is within web_root_path if( distance( web_root_path.begin(), web_root_path.end() ) > distance( path.begin(), path.end() ) || !std::equal( web_root_path.begin(), web_root_path.end(), path.begin() ) ) diff --git a/src/api/server_http.hpp b/src/api/server_http.hpp index 8a6d2226..8ceb5810 100644 --- a/src/api/server_http.hpp +++ b/src/api/server_http.hpp @@ -306,6 +306,12 @@ namespace SimpleWeb { request->method=line.substr(0, method_end); request->path=line.substr(method_end+1, path_end-method_end-1); + // strip first / from path if it exists + if( request->path[ 0 ] == '/' ) + { + request->path = request->path.substr( 1 ); + } + size_t protocol_end; if((protocol_end=line.find('/', path_end+1))!=std::string::npos) { if(line.compare(path_end+1, protocol_end-path_end-1, "HTTP")!=0) From 93e4ced60198d342944634e913733be539dd2b38 Mon Sep 17 00:00:00 2001 From: NotAdam Date: Mon, 17 Feb 2020 15:57:44 +1100 Subject: [PATCH 2/2] fix api routing, bit nicer code for fixing urls --- src/api/main.cpp | 26 +++++++++++++------------- src/api/server_http.hpp | 8 +------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/api/main.cpp b/src/api/main.cpp index 3874ae1d..32d4b794 100644 --- a/src/api/main.cpp +++ b/src/api/main.cpp @@ -719,19 +719,19 @@ int main( int argc, char* argv[] ) Logger::setLogLevel( m_config.global.general.logLevel ); - server.resource[ "^/ZoneName/([0-9]+)$" ][ "GET" ] = &getZoneName; - server.resource[ "^/sapphire-api/lobby/createAccount" ][ "POST" ] = &createAccount; - server.resource[ "^/sapphire-api/lobby/login" ][ "POST" ] = &login; - server.resource[ "^/sapphire-api/lobby/deleteCharacter" ][ "POST" ] = &deleteCharacter; - server.resource[ "^/sapphire-api/lobby/createCharacter" ][ "POST" ] = &createCharacter; - server.resource[ "^/sapphire-api/lobby/insertSession" ][ "POST" ] = &insertSession; - server.resource[ "^/sapphire-api/lobby/checkNameTaken" ][ "POST" ] = &checkNameTaken; - server.resource[ "^/sapphire-api/lobby/checkSession" ][ "POST" ] = &checkSession; - server.resource[ "^/sapphire-api/lobby/getNextCharId" ][ "POST" ] = &getNextCharId; - server.resource[ "^/sapphire-api/lobby/getNextContentId" ][ "POST" ] = &getNextContentId; - server.resource[ "^/sapphire-api/lobby/getCharacterList" ][ "POST" ] = &getCharacterList; - server.resource[ "^(/frontier-api/ffxivsupport/view/get_init)(.*)" ][ "GET" ] = &get_init; - server.resource[ "^(/frontier-api/ffxivsupport/information/get_headline_all)(.*)" ][ "GET" ] = &get_headline_all; + server.resource[ "^ZoneName/([0-9]+)$" ][ "GET" ] = &getZoneName; + server.resource[ "^sapphire-api/lobby/createAccount" ][ "POST" ] = &createAccount; + server.resource[ "^sapphire-api/lobby/login" ][ "POST" ] = &login; + server.resource[ "^sapphire-api/lobby/deleteCharacter" ][ "POST" ] = &deleteCharacter; + server.resource[ "^sapphire-api/lobby/createCharacter" ][ "POST" ] = &createCharacter; + server.resource[ "^sapphire-api/lobby/insertSession" ][ "POST" ] = &insertSession; + server.resource[ "^sapphire-api/lobby/checkNameTaken" ][ "POST" ] = &checkNameTaken; + server.resource[ "^sapphire-api/lobby/checkSession" ][ "POST" ] = &checkSession; + server.resource[ "^sapphire-api/lobby/getNextCharId" ][ "POST" ] = &getNextCharId; + server.resource[ "^sapphire-api/lobby/getNextContentId" ][ "POST" ] = &getNextContentId; + server.resource[ "^sapphire-api/lobby/getCharacterList" ][ "POST" ] = &getCharacterList; + server.resource[ "^(frontier-api/ffxivsupport/view/get_init)(.*)" ][ "GET" ] = &get_init; + server.resource[ "^(frontier-api/ffxivsupport/information/get_headline_all)(.*)" ][ "GET" ] = &get_headline_all; server.default_resource[ "GET" ] = &defaultGet; diff --git a/src/api/server_http.hpp b/src/api/server_http.hpp index 8ceb5810..b9166c57 100644 --- a/src/api/server_http.hpp +++ b/src/api/server_http.hpp @@ -304,13 +304,7 @@ namespace SimpleWeb { size_t path_end; if((path_end=line.find(' ', method_end+1))!=std::string::npos) { request->method=line.substr(0, method_end); - request->path=line.substr(method_end+1, path_end-method_end-1); - - // strip first / from path if it exists - if( request->path[ 0 ] == '/' ) - { - request->path = request->path.substr( 1 ); - } + request->path=line.substr(method_end+2, path_end-method_end-2); size_t protocol_end; if((protocol_end=line.find('/', path_end+1))!=std::string::npos) {