--- parser3/src/main/pa_http.C 2020/10/18 23:40:41 1.98 +++ 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.98 2020/10/18 23:40:41 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 @@ -921,6 +921,67 @@ 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; @@ -940,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); } @@ -1004,7 +1068,7 @@ size_t HTTPD_request::read_post(int sock 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) {