Recently, an error occurred while configuring ssi (nginx) on the Sichuan newspaper network server.
<! -# Include virtual = ".../../test/test.htm"-> this syntax format is not available in nginx due to exceptions in apache Logging.
Nginx <! -# Include virtual = "/test/test.htm"-> this method can be used. It indicates that nginx does not support the ".." path during parsing!
The problem should be solved. After all, nginx is open-source.
By the way, I also verified my previous article "I have been working for more than five years and have some knowledge about the industry environment and skills!". Why is open source more powerful than closed source?
It took some time to look at the nginx source code. The following two important file representations are the ssi module.
The code is as follows: |
Copy code |
Src/http/modules/ngx_http_ssi_filter_module.h Src/http/modules/ngx_http_ssi_filter_module.c |
The ngx_http_ssi_include function in this file should be used to explain the include command.
In this function, nginx error messages indicated by NGX_HTTP_LOG_UNSAFE and NGX_HTTP_SSI_ERROR are processed.
Then find the corresponding function ngx_http_parse_unsafe_uri in src/http/ngx_http_parse.c. The function is as follows:
The code is as follows: |
Copy code |
Gx_int_t Ngx_http_parse_unsafe_uri (ngx_http_request_t * r, ngx_str_t * uri, Ngx_str_t * args, ngx_uint_t * flags) { U_char ch, * p; Size_t len; Len = uri-> len; P = uri-> data; If (len = 0 | p [0] = '? '){ Goto unsafe; } If (p [0] = '. '& len = 3 & p [1] = '. '& (ngx_path_separator (p [2]) { Goto unsafe; } For (/* void */; len --){ Ch = * p ++; If (usual [ch> 5] & (1 <(ch & 0x1f ))){ Continue; } If (ch = '? '){ Args-> len = len-1; Args-> data = p; Uri-> len-= len; Return NGX_ OK; } If (ch = ''){ Goto unsafe; } If (ngx_path_separator (ch) & len> 2 ){ /* Detect "/../"*/ If (p [0] = '.' & p [1] = '.' & ngx_path_separator (p [2]) { Goto unsafe; } } } Return NGX_ OK; Unsafe: If (* flags & NGX_HTTP_LOG_UNSAFE ){ Ngx_log_error (NGX_LOG_ERR, r-> connection-> log, 0, "Unsafe URI" % V "was detected", uri ); } Return NGX_ERROR; }
|
When you see this/* detect "/../" */comment, you will understand why such a path.../../test/test.htm cannot be executed.
Nginx: This path has security risks.
The security method does not modify nginx, and the path of the program itself will not be discussed here. The current site has a solution. To use it, you need to modify the ssi module and re-compile nginx.
Modify as follows:
The code is as follows: |
Copy code |
Src/http/modules/ngx_http_ssi_filter_module.c. Comment out the file function ngx_http_ssi_include. If (ngx_http_parse_unsafe_uri (r, uri, & args, & flags )! = NGX_ OK ){ Return NGX_HTTP_SSI_ERROR; } |