From ff89bf3ea1c9730988e23851d4bb11270ab89588 Mon Sep 17 00:00:00 2001 From: Datong Sun Date: Thu, 29 Mar 2018 16:57:17 -0700 Subject: [PATCH] resolv.conf: fixed a bug that when a newline character is present at the end of the resolv.conf file, the parser incorrectly included such newline in the parsed address. Signed-off-by: Yichun Zhang (agentzh) --- .../nginx-1.13.6-resolver_conf_parsing.patch | 15 ++++++-- t/001-resolver.t | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/patches/nginx-1.13.6-resolver_conf_parsing.patch b/patches/nginx-1.13.6-resolver_conf_parsing.patch index cb89a21..cefb3ff 100644 --- a/patches/nginx-1.13.6-resolver_conf_parsing.patch +++ b/patches/nginx-1.13.6-resolver_conf_parsing.patch @@ -1,5 +1,5 @@ diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c -index cd55520c..d8dc49e8 100644 +index cd55520c..df55a484 100644 --- a/src/core/ngx_resolver.c +++ b/src/core/ngx_resolver.c @@ -5,6 +5,7 @@ @@ -25,7 +25,7 @@ index cd55520c..d8dc49e8 100644 typedef struct { u_char ident_hi; -@@ -131,6 +140,182 @@ static ngx_resolver_node_t *ngx_resolver_lookup_addr6(ngx_resolver_t *r, +@@ -131,6 +140,189 @@ static ngx_resolver_node_t *ngx_resolver_lookup_addr6(ngx_resolver_t *r, #endif @@ -127,7 +127,14 @@ index cd55520c..d8dc49e8 100644 + ngx_memzero(&u, sizeof(ngx_url_t)); + + u.url.data = buf + address; -+ u.url.len = (i == n - 1) ? n - address : i - address; ++ ++ if (i == n - 1 && buf[i] != CR && buf[i] != LF) { ++ u.url.len = n - address; ++ ++ } else { ++ u.url.len = i - address; ++ } ++ + u.default_port = 53; + + /* IPv6? */ @@ -208,7 +215,7 @@ index cd55520c..d8dc49e8 100644 ngx_resolver_t * ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) { -@@ -246,6 +431,37 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) +@@ -246,6 +438,37 @@ ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) } #endif diff --git a/t/001-resolver.t b/t/001-resolver.t index 0c75ac6..8ca5e6a 100644 --- a/t/001-resolver.t +++ b/t/001-resolver.t @@ -343,3 +343,40 @@ done --- error_log IPv6 resolver address is too long: "2001:4860:4860::8888:2001:4860:4860::8888:2001" unable to parse local resolver + + + +=== TEST 11: new line at the end of the file +--- config + resolver local=../html/resolv.conf ipv6=off; + resolver_timeout 5s; + + location /t { + content_by_lua_block { + local sock = ngx.socket.tcp() + local ok, err = sock:connect("openresty.org", 80) + if not ok then + ngx.say("failed to connect to openresty.org: ", err) + return + end + ngx.say("successfully connected to openresty.org") + sock:close() + } + } +--- user_files eval +">>> resolv.conf +domain example.com +nameserver 8.8.8.8 +nameserver 8.8.4.4 +" +--- request +GET /t +--- response_body +successfully connected to openresty.org +--- no_error_log +[error] +[crit] +--- grep_error_log eval: qr/parsed a resolver: ".+"/ +--- grep_error_log_out +parsed a resolver: "8.8.8.8" +parsed a resolver: "8.8.4.4"