@@ -251,13 +251,35 @@ int HttpHandler::invokeHttpHandler(const http_handler* handler) {
251251
252252void HttpHandler::onHeadersComplete () {
253253 // printf("onHeadersComplete\n");
254- int status_code = handleRequestHeaders ();
255- if (status_code != HTTP_STATUS_OK) {
256- error = ERR_REQUEST;
257- return ;
254+ handleRequestHeaders ();
255+ if (service->headerHandler ) {
256+ const int status_code = customHttpHandler (service->headerHandler );
257+ if (status_code != HTTP_STATUS_OK && status_code != HTTP_STATUS_NEXT) {
258+ SetError (ERR_REQUEST, static_cast <http_status>(status_code));
259+ return ;
260+ }
258261 }
259262
260263 HttpRequest* pReq = req.get ();
264+ const char *p = pReq->path .c_str ();
265+ while (*p != ' \0 ' ) {
266+ switch (*p) {
267+ case ' %' :
268+ if (p[1 ] != ' 0' ) break ;
269+ if (p[2 ] != ' d' && p[2 ] != ' D' && p[2 ] != ' a' && p[2 ] != ' A' ) break ;
270+ case ' \r ' :
271+ case ' \n ' :
272+ // fix CVE-2023-26147
273+ hloge (" [%s:%d] Illegal crlf path: %s" , ip, port, pReq->path .c_str ());
274+ SetError (ERR_REQUEST);
275+ return ;
276+
277+ default :
278+ break ;
279+ }
280+ ++p;
281+ }
282+
261283 if (service && service->pathHandlers .size () != 0 ) {
262284 service->GetRoute (pReq, &api_handler);
263285 }
@@ -339,7 +361,7 @@ void HttpHandler::onMessageComplete() {
339361 }
340362}
341363
342- int HttpHandler::handleRequestHeaders () {
364+ void HttpHandler::handleRequestHeaders () {
343365 HttpRequest* pReq = req.get ();
344366 pReq->scheme = ssl ? " https" : " http" ;
345367 pReq->client_addr .ip = ip;
@@ -367,16 +389,6 @@ int HttpHandler::handleRequestHeaders() {
367389
368390 // printf("url=%s\n", pReq->url.c_str());
369391 pReq->ParseUrl ();
370- // printf("path=%s\n", pReq->path.c_str());
371- // fix CVE-2023-26147
372- if (pReq->path .find (" %" ) != std::string::npos) {
373- std::string unescaped_path = HUrl::unescape (pReq->path );
374- if (unescaped_path.find (" \r\n " ) != std::string::npos) {
375- hlogw (" Illegal path: %s\n " , unescaped_path.c_str ());
376- resp->status_code = HTTP_STATUS_BAD_REQUEST;
377- return resp->status_code ;
378- }
379- }
380392
381393 if (proxy) {
382394 // Proxy-Connection
@@ -404,7 +416,6 @@ int HttpHandler::handleRequestHeaders() {
404416 }
405417
406418 // TODO: rewrite url
407- return HTTP_STATUS_OK;
408419}
409420
410421void HttpHandler::handleExpect100 () {
0 commit comments