--- parser3/src/main/pa_http.C 2020/10/14 21:22:59 1.96 +++ parser3/src/main/pa_http.C 2020/11/12 15:09:41 1.100 @@ -14,7 +14,7 @@ #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.96 2020/10/14 21:22:59 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.100 2020/11/12 15:09:41 moko Exp $" IDENT_PA_HTTP_H; #ifdef _MSC_VER #include @@ -105,9 +105,8 @@ public: size_t body_offset; HTTP_Headers headers; - const String &url; - HTTP_response(const String& aurl) : buf(NULL), length(0), buf_size(0), body_offset(0), url(aurl){} + HTTP_response() : buf(NULL), length(0), buf_size(0), body_offset(0){} void resize(size_t size){ buf_size=size; @@ -122,7 +121,7 @@ public: return false; if(received_size<0) { if(int no=pa_socks_errno()) - throw Exception("http.timeout", &url, "error receiving response body: %s (%d)", pa_socks_strerr(no), no); + throw Exception("http.timeout", 0, "error receiving response body: %s (%d)", pa_socks_strerr(no), no); return false; } length+=received_size; @@ -186,7 +185,7 @@ public: for(;i.has_next();){ const char *line=i.next()->cstr(); if(!headers.add_header(line)) - throw Exception("http.response", &url, "bad response from host - bad header \"%s\"", line); + throw Exception("http.response", 0, "bad response from host - bad header \"%s\"", line); } } @@ -227,7 +226,7 @@ int HTTP_response::read_response(int soc parse_headers(); - size_t content_length=check_file_size(headers.content_length, &url); + size_t content_length=check_file_size(headers.content_length, 0); if(content_length>0 && (content_length + body_offset) > length){ resize(content_length + body_offset + 0x400*64); } @@ -244,7 +243,7 @@ int HTTP_response::read_response(int soc } if(state==HTTP_STATUS_CODE) - throw Exception("http.response", &url, "bad response from host - no status found (size=%u)", length); + throw Exception("http.response", 0, "bad response from host - no status found (size=%u)", length); if(state==HTTP_HEADERS){ parse_headers(); @@ -864,7 +863,7 @@ File_read_http_result pa_internal_file_r } - HTTP_response response(connect_string); + HTTP_response response; // sending request int status_code; @@ -922,12 +921,73 @@ File_read_http_result pa_internal_file_r /* ********************** httpd *************************** */ +enum EscapeState { + Initial, + Default, + EscapeFirst, + EscapeSecond +}; + +static bool check_uri(const char *uri){ + EscapeState state=Initial; + uint escapedValue; + + const char *pattern="/../"; + const char *pos=pattern; + + while(*uri){ + uchar c=(uchar)*(uri++); + switch(state) { + case Initial: + if(c!='/') + return false; + state=Default; + break; + case Default: + if(c=='%'){ + state=EscapeFirst; + continue; + } + if(c=='?') + return true; + break; + case EscapeFirst: + if(isxdigit(c)){ + state=EscapeSecond; + escapedValue=hex_value[c] << 4; + continue; + } + return false; + case EscapeSecond: + if(isxdigit(c)){ + state=Default; + c=escapedValue + hex_value[c]; + + // implementing Apache AllowEncodedSlashes Off just in case + if(c=='/' || c=='\\') + return false; + + break; + } + return false; + } + + if(c==*pos || c=='\\' && *pos=='/'){ + if(!*(++pos)) + return false; + } else { + pos=pattern; + } + } + return true; +} + class HTTPD_request : public HTTP_response { public: const char *method; const char *uri; - HTTPD_request() : HTTP_response(String::Empty), method(NULL), uri(NULL){}; + HTTPD_request() : HTTP_response(), method(NULL), uri(NULL){}; const char *extract_method(char *method_line){ char* uri_start = strchr(method_line, ' '); @@ -941,6 +1001,9 @@ public: return NULL; uri=pa_strdup(uri_start+1, uri_end-uri_start-1); + if(!check_uri(uri)) + throw Exception("httpd.request", 0, "invalid uri '%s'", uri); + return str_upper(method_line, uri_start-method_line); } @@ -1002,15 +1065,15 @@ void HTTPD_request::read_header(int sock size_t HTTPD_request::read_post(int sock, char *body, size_t max_bytes) { size_t total_read = min(length - body_offset, max_bytes); - memcpy(body, buf, total_read); + memcpy(body, buf + body_offset, total_read); while (total_read < max_bytes){ - ssize_t received_size = recv(sock, buf + total_read, max_bytes - total_read, 0); + ssize_t received_size = recv(sock, body + total_read, max_bytes - total_read, 0); if(received_size == 0) return total_read; if(received_size < 0) { if(int no = pa_socks_errno()) - throw Exception("httpd.timeout", &url, "error receiving request body: %s (%d)", pa_socks_strerr(no), no); + throw Exception("httpd.timeout", new String(uri), "error receiving request body: %s (%d)", pa_socks_strerr(no), no); return total_read; } total_read += received_size;