Skip to content

Update handling of multiple headers changed in nginx 1.23.0 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

hnakamur
Copy link
Contributor

@hnakamur
Copy link
Contributor Author

On my local machiine, with changes in this pull request and Update handling of multiple headers changed in nginx 1.23.0 by hnakamur · Pull Request #2063 · openresty/lua-nginx-module, the t/bug.t fails with the following output:

t/bug.t .. 41/112 
#   Failed test 't/bug.t TEST 12: set multi-value header to a single value - response_body - response is expected (repeated req 0, req 0)'
#   at /usr/local/share/perl/5.34.0/Test/Nginx/Socket.pm line 1665.
#          got: "foo\x{0a}table: 0x7f821f260198\x{0a}"
#       length: 26
#     expected: "foo\x{0a}b\x{0a}"
#       length: 6
#     strings begin to differ at char 5 (line 2 column 1)

#   Failed test 't/bug.t TEST 12: set multi-value header to a single value - response_body - response is expected (repeated req 1, req 0)'
#   at /usr/local/share/perl/5.34.0/Test/Nginx/Socket.pm line 1665.
#          got: "foo\x{0a}table: 0x7f821f2614b8\x{0a}"
#       length: 26
#     expected: "foo\x{0a}b\x{0a}"
#       length: 6
#     strings begin to differ at char 5 (line 2 column 1)
t/bug.t .. 94/112 # Looks like you failed 2 tests of 112.
t/bug.t .. Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/112 subtests 

Test Summary Report
-------------------
t/bug.t (Wstat: 512 Tests: 112 Failed: 2)
  Failed tests:  49, 52
  Non-zero exit status: 2
Files=1, Tests=112,  4 wallclock secs ( 0.02 usr  0.00 sys +  0.35 cusr  0.15 csys =  0.52 CPU)
Result: FAIL

@hnakamur
Copy link
Contributor Author

It turned out the above failure is not caused by this pull request.

    location /main {
        set $footer '';
        proxy_pass http://127.0.0.1:$server_port/foo;
        more_set_headers 'Foo: b';
        header_filter_by_lua '
            ngx.var.footer = ngx.header.Foo
        ';
        echo_after_body $footer;
    }
    location /foo {
        echo foo;
        add_header Foo a;
        add_header Foo c;
    }

I found more_set_headers is processed after header_filter_by_lua in my local machine with a lot of debug logs added by me.

2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_get_resp_header start, key=Foo, key_len=3, max_nvalues=100 while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_get_resp_header (3) "Foo" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_get_resp_header found=0, i=0, (1) "a" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_get_resp_header (3) "Foo" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_get_resp_header found=1, i=1, (1) "c" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_get_resp_header start, key=Foo, key_len=3, max_nvalues=100, found=2 while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 [lua] response.lua:201: __index(): get_resp_header, n=2 while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 [lua] response.lua:221: __index(): get_resp_header, ret[1]=a while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 [lua] response.lua:221: __index(): get_resp_header, ret[2]=c while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 [lua] var.lua:132: __newindex(): resty.core.var value type=table while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 [lua] var.lua:136: __newindex(): resty.core.var value#2=table: 0x7f2bd1c27c48 while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: *1 ngx_http_lua_ffi_var_set name=footer, value=table: 0x7f2bd1c27c48 while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /main HTTP/1.1", upstream: "http://127.0.0.1:80/foo", host: "localhost"
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper start, value=b, wildcard=0, no_create=0
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper non wildcard key match, i=0, matched=0
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper matched label, i=0, matched=0, value->len=1
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper set header, i=0, value=b
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper non wildcard key match, i=1, matched=1
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper matched label, i=1, matched=1, value->len=1
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper clear header, i=1
2022/06/23 17:32:36 [notice] 2077562#2077562: ngx_http_set_header_helper after loop, matched=1

So, when header_filter_by_lua is processed, the value of the response header Foo is a and c, and they are returned as a table:
https://github.com/openresty/lua-resty-core/blob/3c3d0786d6e26282e76f39f4fe5577d316a47a09/lib/resty/core/response.lua#L216-L221

And when settings the value of those values to the variable footer, multiple values in a table are converted to a string using tostring:
https://github.com/openresty/lua-resty-core/blob/3c3d0786d6e26282e76f39f4fe5577d316a47a09/lib/resty/core/var.lua#L127-L129

And that yields a string like table: 0x7f821f260198 in #136 (comment).

@hnakamur
Copy link
Contributor Author

I found Re: What is the order of module execution and how can I change it? and I found the order of headers-more-nginx-module and lua-nginx-module in my configure script was not good.
I changed the order to move lua-nginx-module before headers-more-nginx-module and confirmed TEST 12 in t/bug.t passes now.

@hnakamur
Copy link
Contributor Author

@zhuizhuhaomeng Thanks for your approval. Should I revert .travis.yml to the original now? Or should I leave it as is?

@zhuizhuhaomeng
Copy link
Contributor

merged with the following adjustment

diff --git a/.travis.yml b/.travis.yml
index ca71950..bf40b31 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -32,7 +32,7 @@ before_install:
 
 install:
   - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
-  - git clone -b update_for_nginx_1_23_0 https://github.com/hnakamur/lua-nginx-module.git ../lua-nginx-module
+  - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
   - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
   - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
   - git clone https://github.com/openresty/nginx-eval-module.git ../eval-nginx-module
diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c
index fd027bf..11447ce 100644
--- a/src/ngx_http_headers_more_headers_in.c
+++ b/src/ngx_http_headers_more_headers_in.c
@@ -793,9 +793,11 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r,
     if (*headers) {
         for (ph = headers; *ph; ph = &(*ph)->next) { /* void */ }
         *ph = h;
+
     } else {
         *headers = h;
     }
+
     h->next = NULL;
 
     return NGX_OK;
@@ -885,6 +887,7 @@ ngx_http_headers_more_validate_host(ngx_str_t *host, ngx_pool_t *pool,
             if (dot_pos == i - 1) {
                 return NGX_DECLINED;
             }
+
             dot_pos = i;
             break;
 
diff --git a/src/ngx_http_headers_more_util.c b/src/ngx_http_headers_more_util.c
index caf372e..e1f3636 100644
--- a/src/ngx_http_headers_more_util.c
+++ b/src/ngx_http_headers_more_util.c
@@ -295,6 +295,7 @@ ngx_http_headers_more_rm_header_helper(ngx_list_t *l, ngx_list_part_t *cur,
                         if (part->next == NULL) {
                             return NGX_ERROR;
                         }
+
                         part = part->next;
                     }
 
@@ -338,6 +339,7 @@ ngx_http_headers_more_rm_header_helper(ngx_list_t *l, ngx_list_part_t *cur,
                     if (part->next == NULL) {
                         return NGX_ERROR;
                     }
+
                     part = part->next;
                 }

@hnakamur hnakamur deleted the update_for_nginx_1_23_0 branch June 28, 2022 03:53
@RekGRpth
Copy link

test input.t TEST 42 seg faults!

@hnakamur
Copy link
Contributor Author

I cannot reproduce seg faults in t/input.t TEST 42.

nginx/headers-more-nginx-module$ TEST_NGINX_BINARY=../objs/nginx prove t/input.t 
t/input.t .. ok       
All tests successful.
Files=1, Tests=256, 10 wallclock secs ( 0.06 usr  0.00 sys +  0.64 cusr  0.31 csys =  1.01 CPU)
Result: PASS

Module versions I used:

  • openresty/echo-nginx-module f7952cb6be2e7dab8bc66fd3e1ce3b3f18778fc3
  • openresty/headers-more-nginx-module 91eb0db
  • openresty/lua-nginx-module b6d167cf1a93c0c885c28db5a439f2404874cb26
nginx/headers-more-nginx-module$ ../objs/nginx -V
nginx version: nginx/1.23.0
built by gcc 11.2.0 (Ubuntu 11.2.0-19ubuntu1) 
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_upstream_ip_hash_module --without-http_empty_gif_module --without-http_memcached_module --without-http_referer_module --without-http_autoindex_module --without-http_auth_basic_module --without-http_userid_module --with-http_realip_module --with-http_dav_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=./nginx-eval-module --add-module=./lua-nginx-module --add-module=./echo-nginx-module --add-module=./headers-more-nginx-module --with-debu

@RekGRpth
Copy link

RekGRpth commented Jun 28, 2022

I also has PASS, but on TEST 42 core dumped

prove t/input.t
t/input.t .. ok       
All tests successful.
Files=1, Tests=236,  9 wallclock secs ( 0.09 usr  0.00 sys +  0.84 cusr  0.41 csys =  1.34 CPU)
Result: PASS

my config is

nginx -V
nginx version: nginx/1.23.0
built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219) 
built with OpenSSL 1.1.1o  3 May 2022
TLS SNI support enabled
configure arguments: --add-dynamic-module='modules/ngx_devel_kit modules/echo-nginx-module modules/encrypted-session-nginx-module modules/form-input-nginx-module modules/iconv-nginx-module modules/nginx_csrf_prevent modules/nginx-push-stream-module modules/nginx-upload-module modules/nginx-upstream-fair modules/nginx-uuid4-module modules/ngx_brotli/filter modules/ngx_brotli/static modules/ngx_http_auth_basic_ldap_module modules/ngx_http_captcha_module modules/ngx_http_headers_module modules/ngx_http_htmldoc_module modules/ngx_http_json_module modules/ngx_http_mustach_module modules/ngx_http_sign_module modules/ngx_http_substitutions_filter_module modules/ngx_http_zip_var_module modules/set-misc-nginx-module modules/ngx_http_auth_pam_module modules/ngx_http_evaluate_module modules/ngx_http_response_body_module modules/headers-more-nginx-module modules/ngx_http_remote_passwd modules/ngx_http_json_var_module modules/ngx_http_time_var_module modules/ngx_http_error_page_inherit_module modules/ngx_http_include_server_module modules/nginx-ejwt-module modules/array-var-nginx-module modules/ngx_pq_module modules/ngx_upstream_jdomain' --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --group=nginx --http-client-body-temp-path=/var/tmp/nginx/client_body_temp --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/tmp/nginx/proxy_temp --http-scgi-temp-path=/var/tmp/nginx/scgi_temp --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp --lock-path=/run/nginx/nginx.lock --modules-path=/usr/local/lib/nginx --pid-path=/run/nginx/nginx.pid --prefix=/etc/nginx --sbin-path=/usr/local/bin/nginx --user=nginx --with-cc-opt='-Wextra -Wwrite-strings -Wmissing-prototypes -Werror -Wno-discarded-qualifiers' --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-pcre --with-pcre-jit --with-poll_module --with-select_module --with-stream=dynamic --with-stream_geoip_module=dynamic --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads

@RekGRpth
Copy link

and bad test is

# vi:filetype=

use lib 'lib';
use Test::Nginx::Socket; # 'no_plan';

repeat_each(2);

plan tests => repeat_each() * 3;

no_long_string();
#no_diff;

run_tests();

__DATA__




=== TEST 42: clear all and re-insert
--- main_config
    load_module /etc/nginx/modules/ngx_http_echo_module.so;
    load_module /etc/nginx/modules/ngx_http_headers_more_filter_module.so;
--- config
    location = /t {
        more_clear_input_headers Host Connection Cache-Control Accept
                                 User-Agent Accept-Encoding Accept-Language
                                 Cookie;

        more_set_input_headers "Host: a" "Connection: b" "Cache-Control: c"
                               "Accept: d" "User-Agent: e" "Accept-Encoding: f"
                               "Accept-Language: g" "Cookie: h";

        more_clear_input_headers Host Connection Cache-Control Accept
                                 User-Agent Accept-Encoding Accept-Language
                                 Cookie;

        more_set_input_headers "Host: a" "Connection: b" "Cache-Control: c"
                               "Accept: d" "User-Agent: e" "Accept-Encoding: f"
                               "Accept-Language: g" "Cookie: h";

        echo ok;
    }

--- raw_request eval
"GET /t HTTP/1.1\r
Host: localhost\r
Connection: close\r
Cache-Control: max-age=0\r
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36\r
Accept-Encoding: gzip,deflate,sdch\r
Accept-Language: en-US,en;q=0.8\r
Cookie: test=cookie;\r
\r
"
--- response_body
ok
--- no_error_log
[error]


@hnakamur
Copy link
Contributor Author

I tried again with change to dynamic modules and without debug, but I got still no core dumped.

$ ./objs/nginx -V
nginx version: nginx/1.23.0
built by gcc 11.2.0 (Ubuntu 11.2.0-19ubuntu1) 
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_upstream_ip_hash_module --without-http_empty_gif_module --without-http_memcached_module --without-http_referer_module --without-http_autoindex_module --without-http_auth_basic_module --without-http_userid_module --with-http_realip_module --with-http_dav_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=./nginx-eval-module --add-dynamic-module=./lua-nginx-module --add-dynamic-module=./echo-nginx-module --add-dynamic-module=./headers-more-nginx-module

I confirmed a core dumped if I inject an intentional bug like below:

$ diff -u ../../../openresty/headers-more-nginx-module/src/ngx_http_headers_more_headers_in.c src/ngx_http_headers_more_headers_in.c 
--- ../../../openresty/headers-more-nginx-module/src/ngx_http_headers_more_headers_in.c 2022-06-28 13:00:32.546690534 +0900
+++ src/ngx_http_headers_more_headers_in.c      2022-06-28 18:36:41.751299182 +0900
@@ -762,6 +762,9 @@
     ngx_table_elt_t  **headers, **ph, *h;
     int                nelts;
 
+    h = NULL;
+    h->hash = 3;
+
     if (r->headers_out.status == 400 || r->headers_in.headers.last == NULL) {
         /* must be a 400 Bad Request */
         return NGX_OK;

@RekGRpth
Copy link

I have seg fault NOT IN this module:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  ngx_http_variable_headers_internal (r=0x7efc786202b0, v=0x7efc78620ff0, data=<optimized out>, sep=sep@entry=44 ',') at src/http/ngx_http_variables.c:834
834	        if (th->hash == 0) {
(gdb) bt
#0  ngx_http_variable_headers_internal (r=0x7efc786202b0, v=0x7efc78620ff0, data=<optimized out>, sep=sep@entry=44 ',') at src/http/ngx_http_variables.c:834
#1  0x0000555cfa2f95fa in ngx_http_variable_header (r=<optimized out>, v=<optimized out>, data=<optimized out>) at src/http/ngx_http_variables.c:808
#2  0x0000555cfa2faf12 in ngx_http_get_indexed_variable (r=0x7efc786202b0, index=<optimized out>) at src/http/ngx_http_variables.c:631
#3  0x0000555cfa2f5746 in ngx_http_log_variable_getlen (r=<optimized out>, data=<optimized out>) at src/http/modules/ngx_http_log_module.c:956
#4  0x0000555cfa2f618f in ngx_http_log_handler (r=0x7efc786202b0) at src/http/modules/ngx_http_log_module.c:305
#5  0x0000555cfa2ea00e in ngx_http_log_request (r=r@entry=0x7efc786202b0) at src/http/ngx_http_request.c:3727
#6  0x0000555cfa2ebfbd in ngx_http_free_request (r=r@entry=0x7efc786202b0, rc=rc@entry=0) at src/http/ngx_http_request.c:3673
#7  0x0000555cfa2ec148 in ngx_http_close_request (r=r@entry=0x7efc786202b0, rc=rc@entry=0) at src/http/ngx_http_request.c:3619
#8  0x0000555cfa2ed257 in ngx_http_finalize_connection (r=r@entry=0x7efc786202b0) at src/http/ngx_http_request.c:2762
#9  0x0000555cfa2ee120 in ngx_http_finalize_request (r=r@entry=0x7efc786202b0, rc=0) at src/http/ngx_http_request.c:2635
#10 0x0000555cfa2e7cc5 in ngx_http_core_content_phase (r=0x7efc786202b0, ph=<optimized out>) at src/http/ngx_http_core_module.c:1261
#11 0x0000555cfa2e1afd in ngx_http_core_run_phases (r=r@entry=0x7efc786202b0) at src/http/ngx_http_core_module.c:875
#12 0x0000555cfa2e1ba0 in ngx_http_handler (r=r@entry=0x7efc786202b0) at src/http/ngx_http_core_module.c:858
#13 0x0000555cfa2eef13 in ngx_http_process_request (r=r@entry=0x7efc786202b0) at src/http/ngx_http_request.c:2094
#14 0x0000555cfa2ef6d5 in ngx_http_process_request_headers (rev=rev@entry=0x7efc78640370) at src/http/ngx_http_request.c:1496
#15 0x0000555cfa2efb71 in ngx_http_process_request_line (rev=rev@entry=0x7efc78640370) at src/http/ngx_http_request.c:1163
#16 0x0000555cfa2efd8b in ngx_http_wait_request_handler (rev=0x7efc78640370) at src/http/ngx_http_request.c:501
#17 0x0000555cfa2cded1 in ngx_epoll_process_events (cycle=0x7efc78648250, timer=<optimized out>, flags=<optimized out>) at src/event/modules/ngx_epoll_module.c:901
#18 0x0000555cfa2c002e in ngx_process_events_and_timers (cycle=cycle@entry=0x7efc78648250) at src/event/ngx_event.c:248
#19 0x0000555cfa2ccc18 in ngx_single_process_cycle (cycle=cycle@entry=0x7efc78648250) at src/os/unix/ngx_process_cycle.c:300
#20 0x0000555cfa29c309 in main (argc=<optimized out>, argv=<optimized out>) at src/core/nginx.c:380

@RekGRpth
Copy link

but WITH this test file #136 (comment)

@RekGRpth
Copy link

I don't know how, but this fix solve segfault

diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c
index 7c28b23..e5e9182 100644
--- a/src/ngx_http_headers_more_headers_in.c
+++ b/src/ngx_http_headers_more_headers_in.c
@@ -335,6 +335,7 @@ matched:
 
     h->key = hv->key;
     h->value = *value;
+    h->next = NULL;
 
     h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
     if (h->lowcase_key == NULL) {
@@ -392,6 +393,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r,
 
     h->hash = hv->hash;
     h->value = *value;
+    h->next = NULL;
 
     return NGX_OK;
 }

@xiaocang
Copy link
Contributor

Hi @RekGRpth, I can't reproduce your segfault either, just to confirm a few more conditions with you:

  1. Are you compiling on alpine using glibc or musl?
  2. Could you print the th value in gdb when segfault happens?
  3. Can I see your output of ldd $(which nginx)?

@RekGRpth
Copy link

  1. Are you compiling on alpine using glibc or musl?

musl

  1. Could you print the th value in gdb when segfault happens?

it prints letter e

  1. Can I see your output of ldd $(which nginx)?
ldd $(which nginx)
	/lib/ld-musl-x86_64.so.1 (0x7fc97e7c7000)
	libpcre2-8.so.0 => /usr/lib/libpcre2-8.so.0 (0x7fc97e5af000)
	libssl.so.1.1 => /lib/libssl.so.1.1 (0x7fc97e52e000)
	libcrypto.so.1.1 => /lib/libcrypto.so.1.1 (0x7fc97e2ac000)
	libz.so.1 => /lib/libz.so.1 (0x7fc97e292000)
	libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7fc97e7c7000)

@jessp01
Copy link

jessp01 commented Jun 30, 2022

Hi all,

Compiling from master against Nginx 1.23.0 works fine for me. If there are no pending changes related to that, could you please create a tag?

Thanks in advance,

@RekGRpth
Copy link

Compiling from master against Nginx 1.23.0 works fine for me.

How about including tests?

@zhuizhuhaomeng
Copy link
Contributor

@RekGRpth
I think set h->next to NULL on line 338 would solve the problem.
I am not sure if we need to set h->next to NULL on line 395.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

headers-more-nginx-module not compatible with nginx 1.23.0
5 participants