]> git.kaiwu.me - njs.git/commitdiff
HTTP: fixed buffer_type inheritance in if blocks.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 12 Dec 2025 05:57:18 +0000 (21:57 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 13 Dec 2025 04:45:54 +0000 (20:45 -0800)
Previously, when js_body_filter was used inside an if block that
evaluated to true, the data parameter received Buffer type instead
of the expected String type. This happened because buffer_type field
in ngx_http_js_loc_conf_t was not properly initialized, causing the
configuration merge to fail when nginx created a new location context
for if blocks.

This fixes #999 issue on Github.

nginx/ngx_http_js_module.c
nginx/t/js_body_filter_if.t

index 8b38dbfde95d7b0eef079e13a9ad548f043af9fe..63e55c8003082743923788543e753955d1f1474c 100644 (file)
@@ -8260,6 +8260,8 @@ ngx_http_js_create_loc_conf(ngx_conf_t *cf)
     conf->ssl_verify = NGX_CONF_UNSET;
     conf->ssl_verify_depth = NGX_CONF_UNSET;
 #endif
+    conf->buffer_type = NGX_CONF_UNSET_UINT;
+
     return conf;
 }
 
index af0aa865d941733509e6cd86ae0118dfecd6a178..dd9bc82f0fece4198deb4740f0e06082917d5f4e 100644 (file)
@@ -58,6 +58,24 @@ http {
 
             proxy_pass http://127.0.0.1:8081/source;
         }
+
+        location /type_check_true {
+            if ($arg_check) {
+                set $dummy 1;
+            }
+
+            js_body_filter test.type_check;
+            proxy_pass http://127.0.0.1:8081/backend;
+        }
+
+        location /type_check_false {
+            if ($arg_nonexistent = dummy) {
+                set $dummy 1;
+            }
+
+            js_body_filter test.type_check;
+            proxy_pass http://127.0.0.1:8081/backend;
+        }
     }
 
     server {
@@ -68,6 +86,10 @@ http {
             postpone_output 1;
             js_content test.source;
         }
+
+        location /backend {
+            return 200 'payload';
+        }
     }
 }
 
@@ -113,15 +135,27 @@ $t->write_file('test.js', <<EOF);
         r.done();
     }
 
-    export default {njs: test_njs, append, prepend, source};
+    function type_check(r, data, flags) {
+        if (flags.last) {
+            return r.sendBuffer(data, flags);
+        }
+
+        r.sendBuffer(data.constructor.name + ": " + data, flags);
+    }
+
+    export default {njs: test_njs, append, prepend, source, type_check};
 
 EOF
 
-$t->try_run('no njs body filter')->plan(2);
+$t->try_run('no njs body filter')->plan(4);
 
 ###############################################################################
 
 like(http_get('/filter?name=append'), qr/AAABBCDDDDXXX/, 'append');
 like(http_get('/filter?name=prepend'), qr/XXXAAABBCDDDD/, 'prepend');
+like(http_get('/type_check_true?check=1'), qr/String: payload/,
+    'type check with if block true');
+like(http_get('/type_check_false'), qr/String: payload/,
+    'type check with if block false');
 
 ###############################################################################