关于nginx的ssi(包含路径)

原文链接: http://bbs.linuxtone.org/thread-6515-1-1.html IT运维专家网--"自由平等,互助分享!"

昨天帮人弄了一下nginx的ssi(原帖 http://bbs.linuxtone.org/thread-6489-1-1.html),发现如下问题:
如果shtml里面的网页代码包含语句写成如下:
<!--#include virtual="/test.html"-->
复制代码
这样是没有问题,可以包含的,但是如果写成这样:
<!--#include virtual="../test.html"-->
复制代码
由于需要包含当前代码文件所在目录路径的上级目录文件,nginx会为此请求产生的子请求uri为/../test.html,默认nginx会认为这个uri并不是安全的,日志(error_log)会输入如下错误:
unsafe URI "/../test.html" was detected while sending response to client, client: 192.168.10.204, server: test.aijuzhe.cn, request: "GET /test.shtml HTTP/1.1", host: "test.aijuzhe.cn", referrer: " http://test.aijuzhe.cn/test.shtml"
复制代码
不能正确包含文件,页面会输出[an error occurred while processing the directive],解决方法是找到nginx源代码目录的unsafe uri检查函数并强制使其返回一个NGX_OK,即如下文件:
vi nginx-VERSION/src/http/ngx_http_parse.c
复制代码
找到ngx_http_parse_unsafe_uri函数,并在前面加入一句return NGX_OK;
ngx_int_t
ngx_http_parse_unsafe_uri(ngx_http_request_t *r, ngx_str_t *uri,
ngx_str_t *args, ngx_uint_t *flags)
{
return NGX_OK;  /*这一句是后面加的*/
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] == '.' && (p[2] == '/'
#if (NGX_WIN32)
|| p[2] == '\\'
#endif
))
{
goto unsafe;
}

for ( /* void */ ; len; 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 == '\0') {
*flags |= NGX_HTTP_ZERO_IN_URI;
continue;
}

if ((ch == '/'
#if (NGX_WIN32)
|| ch == '\\'
#endif
) && len > 2)
{
/* detect "/../" */

if (p[0] == '.' && p[1] == '.' && p[2] == '/') {
goto unsafe;
}

#if (NGX_WIN32)

if (p[2] == '\\') {
goto unsafe;
}

if (len > 3) {

/* detect "/.../" */

if (p[0] == '.' && p[1] == '.' && p[2] == '.'
&& (p[3] == '/' || p[3] == '\\'))
{
goto unsafe;
}
}
#endif
}
}

return NGX_OK;

unsafe:

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"unsafe URI \"%V\" was detected", uri);

return NGX_ERROR;
}
重新编译以后nginx可以包含上级目录的文件,当然,带来的后果是安全性的下降。
参考资料:
http://forum.admon.org/webmasters/2498-nginx-unsafe-uri-detected-while-sending-response-client.html

你可能感兴趣的:(nginx,nginx,SSI,职场,休闲)