]> git.kaiwu.me - njs.git/commitdiff
QueryString: use null prototypes for parsed arguments
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 17 Nov 2026 00:19:41 +0000 (16:19 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 17 Nov 2026 20:41:43 +0000 (12:41 -0800)
Previously, querystring.parse() and r.args consulted inherited
properties when collecting duplicate parameters. Keys such as
"toString" included inherited functions in their values, and
"__proto__" replaced the prototype of the result with an array.

Now both engines create results with a null prototype, matching
Node.js and treating all parsed names as data. Object.prototype
methods, including hasOwnProperty(), are no longer inherited.

The njs regression was introduced in 8b9b297d (0.8.0). The QuickJS
r.args implementation introduced the same issue in 201b1276
(0.8.6), followed by querystring.parse() in f289dcb9 (0.8.10).

The issue was found by Basavaraj S m (basavaraj-sm05) in PR #1147:
https://github.com/nginx/njs/pull/1147

external/njs_query_string_module.c
external/qjs_query_string_module.c
nginx/ngx_http_js_module.c
nginx/t/js_args.t
src/njs.h
src/njs_object.c
src/njs_object.h
src/njs_vm.c
test/querystring.t.mjs
ts/ngx_http_js_module.d.ts
ts/njs_modules/querystring.d.ts

index 9426731744d8650042eedfc184bcf8cd592d1fcc..20080949037598f368fee46e547041cd4111efdb 100644 (file)
@@ -483,16 +483,24 @@ njs_query_string_parser(njs_vm_t *vm, u_char *query, u_char *end,
     const njs_str_t *sep, const njs_str_t *eq, njs_function_t *decode,
     njs_uint_t max_keys, njs_value_t *retval)
 {
-    size_t      size;
-    u_char      *part, *key, *val;
-    njs_int_t   ret;
-    njs_uint_t  count;
+    size_t              size;
+    u_char              *part, *key, *val;
+    njs_int_t           ret;
+    njs_uint_t          count;
+    njs_opaque_value_t  prototype;
 
     ret = njs_vm_object_alloc(vm, retval, NULL);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
 
+    njs_value_null_set(njs_value_arg(&prototype));
+
+    ret = njs_vm_prototype_set(vm, retval, njs_value_arg(&prototype));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
     count = 0;
 
     key = query;
index b3b429b20f44dc076f6db146c7aaef503206b9dc..605a2555e807b6076f408232ca803c1271acf322 100644 (file)
@@ -491,7 +491,7 @@ qjs_query_string_parser(JSContext *cx, u_char *query, u_char *end,
         eq->length = 1;
     }
 
-    obj = JS_NewObject(cx);
+    obj = JS_NewObjectProto(cx, JS_NULL);
     if (JS_IsException(obj)) {
         return JS_EXCEPTION;
     }
index 7e96ad5b313a164c31fee2728ff5477b6653a822..5c6a857d6887d6819d4d7b0aba90f5d9b869a20a 100644 (file)
@@ -6226,7 +6226,7 @@ ngx_http_qjs_ext_args(JSContext *cx, JSValueConst this_val)
         return JS_DupValue(cx, req->args);
     }
 
-    args = JS_NewObject(cx);
+    args = JS_NewObjectProto(cx, JS_NULL);
     if (JS_IsException(args)) {
         return JS_EXCEPTION;
     }
index 0adfddd888f95bffb3792401c38a988be4d8ce8d..c69e056277dde5db59dac7bd9e83132781c31051 100644 (file)
@@ -61,12 +61,26 @@ http {
         location /object {
             js_content test.object;
         }
+
+        location /prototype {
+            js_content test.prototype;
+        }
+
+        location /enumerate {
+            js_content test.enumerate;
+        }
+
+        location /roundtrip {
+            js_content test.roundtrip;
+        }
     }
 }
 
 EOF
 
 $t->write_file('test.js', <<EOF);
+    import qs from 'querystring';
+
     function test_njs(r) {
         r.return(200, njs.version);
     }
@@ -90,11 +104,36 @@ $t->write_file('test.js', <<EOF);
         r.return(200, JSON.stringify(r.args));
     }
 
-    export default {njs: test_njs, iter, keys, object};
+    function prototype(r) {
+        var args = r.args;
+        r.return(200, JSON.stringify({
+            nullPrototype: Object.getPrototypeOf(args) === null,
+            missingToString: args.toString === undefined,
+            missingConstructor: args.constructor === undefined,
+            missingHasOwnProperty: args.hasOwnProperty === undefined,
+            cached: args === r.args
+        }));
+    }
+
+    function enumerate(r) {
+        var keys = [];
+        for (var key in r.args) {
+            keys.push(key);
+        }
+
+        r.return(200, keys.sort());
+    }
+
+    function roundtrip(r) {
+        r.return(200, JSON.stringify(qs.parse(qs.stringify(r.args))));
+    }
+
+    export default {njs: test_njs, iter, keys, object, prototype, enumerate,
+                    roundtrip};
 
 EOF
 
-$t->try_run('no njs')->plan(20);
+$t->try_run('no njs')->plan(29);
 
 ###############################################################################
 
@@ -129,6 +168,10 @@ like(http_get('/iter?foo=1&foo=2'), qr/1,2/m, 'r.args iteration 5');
 like(http_get('/keys?b=1&c=2&a=5'), qr/a,b,c/m, 'r.args sorted keys');
 like(http_get('/keys?b=1&b=2'), qr/b/m, 'r.args duplicate keys');
 like(http_get('/keys?b=1&a&c='), qr/a,b,c/m, 'r.args empty value');
+like(http_get('/keys?__proto__=1&toString=2&a=3'), qr/^__proto__,a,toString$/m,
+       'r.args special names keys');
+like(http_get('/enumerate?__proto__=1&__proto__=2&a=3'), qr/^__proto__,a$/m,
+       'r.args enumeration has no inherited keys');
 
 is(get_json('/object'), '{}', 'empty object');
 is(get_json('/object?a=1&b=2&c=3'), '{"a":"1","b":"2","c":"3"}',
@@ -144,6 +187,24 @@ is(get_json('/object?a=%62%63&b=%63%64'), '{"a":"bc","b":"cd"}',
 is(get_json('/object?a=%6&b=%&c=%%&d=%zz'),
        '{"a":"%6","b":"%","c":"%%","d":"%zz"}',
        'values percent-encoded broken object');
+is(get_json('/object?__proto__=1&__proto__=2&__proto__=3'),
+       '{"__proto__":["1","2","3"]}', 'duplicate __proto__ object');
+is(get_json('/object?__%70roto__=1&__proto__=2'),
+       '{"__proto__":["1","2"]}', 'percent-encoded __proto__ object');
+is(get_json('/object?toString=1&constructor=2&hasOwnProperty=3'),
+       '{"constructor":"2","hasOwnProperty":"3","toString":"1"}',
+       'special names object');
+is(get_json('/object?hasOwnProperty=1&hasOwnProperty=2'),
+       '{"hasOwnProperty":["1","2"]}', 'duplicate hasOwnProperty object');
+
+my $prototype = '{"cached":true,"missingConstructor":true,'
+       . '"missingHasOwnProperty":true,"missingToString":true,'
+       . '"nullPrototype":true}';
+
+is(get_json('/prototype'), $prototype, 'empty args null prototype');
+is(get_json('/prototype?a=1'), $prototype, 'args null prototype');
+is(get_json('/roundtrip?__proto__=1&__proto__=2&toString=3'),
+       '{"__proto__":["1","2"],"toString":"3"}', 'special names roundtrip');
 
 is(get_json('/object?%D8%41%42=%E2%82%41%42'),
        qq/{"\x{fffd}AB":"\x{fffd}AB"}/,
index 54908c359c13ee638974161903c71912be17f89e..5e0e61fa3d1419827405b64e6623d753263e49ab 100644 (file)
--- a/src/njs.h
+++ b/src/njs.h
@@ -409,6 +409,8 @@ NJS_EXPORT njs_function_t *njs_vm_function(njs_vm_t *vm, const njs_str_t *name);
 NJS_EXPORT njs_bool_t njs_vm_constructor(njs_vm_t *vm);
 NJS_EXPORT njs_int_t njs_vm_prototype(njs_vm_t *vm, njs_value_t *value,
     njs_value_t *retval);
+NJS_EXPORT njs_int_t njs_vm_prototype_set(njs_vm_t *vm, njs_value_t *value,
+    njs_value_t *prototype);
 
 NJS_EXPORT void njs_vm_throw(njs_vm_t *vm, const njs_value_t *value);
 NJS_EXPORT void njs_vm_error2(njs_vm_t *vm, unsigned error_type,
index 65964070aa19d3e44f80b6e72ec2a0528900c5c4..2d8c7b8d0b1fde17b3d8a17ea623baff76bbac94 100644 (file)
@@ -1817,7 +1817,7 @@ njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 }
 
 
-static njs_int_t
+njs_int_t
 njs_object_set_prototype_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused, njs_value_t *retval)
 {
index 319de1dbfbdf4d235a4467f9f146bc27b287c796..2409f52bea0df31266b7d849af0f3ce295b90485 100644 (file)
@@ -112,6 +112,8 @@ njs_int_t njs_object_prop_descriptor(njs_vm_t *vm, njs_value_t *dest,
     njs_value_t *value, njs_value_t *setval);
 njs_int_t njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
+njs_int_t njs_object_set_prototype_of(njs_vm_t *vm, njs_value_t *args,
+    njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
 const char *njs_prop_type_string(njs_object_prop_type_t type);
 njs_int_t njs_object_props_init(njs_vm_t *vm, const njs_object_init_t* init,
     njs_object_prop_t *base, uint32_t atom_id, njs_value_t *value,
index ed98c4674ed1aa42460b72dd995d8acec85241a6..27d0071241d4638db1f0a9ee96a8f25f56a02bca 100644 (file)
@@ -1349,6 +1349,19 @@ njs_vm_prototype(njs_vm_t *vm, njs_value_t *value, njs_value_t *retval)
 }
 
 
+njs_int_t
+njs_vm_prototype_set(njs_vm_t *vm, njs_value_t *value, njs_value_t *prototype)
+{
+    njs_value_t  arguments[3], retval;
+
+    njs_set_undefined(&arguments[0]);
+    njs_value_assign(&arguments[1], value);
+    njs_value_assign(&arguments[2], prototype);
+
+    return njs_object_set_prototype_of(vm, arguments, 3, 0, &retval);
+}
+
+
 njs_int_t
 njs_vm_array_alloc(njs_vm_t *vm, njs_value_t *retval, uint32_t spare)
 {
index d9ebc0a315830e79e999041a7f9ceb766a6903f5..3da1d77825998ecc5d3c04fec641732a4947563a 100644 (file)
@@ -74,6 +74,10 @@ let parse_tsuite = {
 
         { value: 'baz=fuz&baz=bar', sep: '&', eq: '=', expected: { baz:['fuz', 'bar'] } },
         { value: 'baz=fuz&baz=bar&baz=zap', expected: { baz:['fuz', 'bar', 'zap'] } },
+        { value: 'toString=fuz', expected: { toString: 'fuz' } },
+        { value: 'constructor=fuz', expected: { constructor: 'fuz' } },
+        { value: 'hasOwnProperty=fuz&hasOwnProperty=bar',
+          expected: { hasOwnProperty: ['fuz', 'bar'] } },
         { value: 'baz=', expected: { baz:'' } },
         { value: '=fuz', expected: { '':'fuz' } },
         { value: '=fuz=', expected: { '':'fuz=' } },
@@ -131,6 +135,87 @@ let parse_tsuite = {
           exception: 'TypeError: Cannot convert object to primitive value' },
 ]};
 
+let parse_prototype_tsuite = {
+    name: "querystring.parse() null prototype tests",
+    T: async (params) => {
+        let r = qs.parse(params.value, null, null, params.options);
+
+        if (Object.getPrototypeOf(r) !== null) {
+            throw Error('unexpected prototype');
+        }
+
+        if (JSON.stringify(r) !== params.expected) {
+            throw Error(`unexpected output "${JSON.stringify(r)}"`);
+        }
+
+        let keys = Object.keys(r).sort();
+        let enumerated = [];
+
+        for (let key in r) {
+            enumerated.push(key);
+        }
+
+        if (JSON.stringify(enumerated.sort()) !== JSON.stringify(keys)) {
+            throw Error('unexpected inherited keys');
+        }
+
+        for (let i = 0; i < keys.length; i++) {
+            let key = keys[i];
+            let desc = Object.getOwnPropertyDescriptor(r, key);
+
+            if (!desc.enumerable || !desc.configurable || !desc.writable
+                || !Object.prototype.hasOwnProperty.call(desc, 'value'))
+            {
+                throw Error(`unexpected descriptor for "${key}"`);
+            }
+        }
+
+        let names = ['__proto__', 'constructor', 'toString',
+                     'hasOwnProperty', 'valueOf'];
+
+        for (let i = 0; i < names.length; i++) {
+            let key = names[i];
+
+            if (!Object.prototype.hasOwnProperty.call(r, key)
+                && r[key] !== undefined)
+            {
+                throw Error(`unexpected inherited value for "${key}"`);
+            }
+        }
+
+        let roundtrip = qs.parse(qs.stringify(r));
+
+        if (Object.getPrototypeOf(roundtrip) !== null
+            || JSON.stringify(roundtrip) !== params.expected)
+        {
+            throw Error('unexpected roundtrip result');
+        }
+
+        return 'SUCCESS';
+    },
+
+    tests: [
+        { value: '', expected: '{}' },
+        { value: '&&', expected: '{}' },
+        { value: 'a=1', expected: '{"a":"1"}' },
+        { value: '__proto__=fuz', expected: '{"__proto__":"fuz"}' },
+        { value: '__proto__=fuz&__proto__=bar&__proto__=zap',
+          expected: '{"__proto__":["fuz","bar","zap"]}' },
+        { value: '__%70roto__=fuz&__proto__=bar',
+          expected: '{"__proto__":["fuz","bar"]}' },
+        { value: '__proto__=fuz&1=bar',
+          expected: '{"1":"bar","__proto__":"fuz"}' },
+        { value: 'toString=fuz&constructor=bar&valueOf=zap',
+          expected: '{"toString":"fuz","constructor":"bar",'
+                    + '"valueOf":"zap"}' },
+        { value: 'hasOwnProperty=fuz&hasOwnProperty=bar',
+          expected: '{"hasOwnProperty":["fuz","bar"]}' },
+        { value: '__proto__&__proto__=', expected: '{"__proto__":["",""]}' },
+        { value: 'name=fuz',
+          options: { decodeURIComponent: s => s == 'name' ? '__proto__' : s },
+          expected: '{"__proto__":"fuz"}' },
+]};
+
 let stringify_tsuite = {
     name: "querystring.stringify() tests",
     T: async (params) => {
@@ -232,6 +317,7 @@ let unescape_tsuite = {
 run([
     escape_tsuite,
     parse_tsuite,
+    parse_prototype_tsuite,
     stringify_tsuite,
     unescape_tsuite,
 ])
index 39acbad0e5b0dfe2184dc844566d54f959146d17..fbeca1b082650c54e5dbc64cd19844c361f37598 100644 (file)
@@ -295,6 +295,9 @@ interface NginxHTTPRequest {
     /**
      * Request arguments object.
      *
+     * The object has a null prototype. Methods inherited from Object.prototype,
+     * such as toString() and hasOwnProperty(), are absent.
+     *
      * Since 0.7.6, duplicate keys are returned as an array, keys are
      * case-sensitive, both keys and values are percent-decoded.
      * For example, the query string
index d4229fdfb13e13456f5ab9272ca1bbec3ab595b6..6e7fd510100f1ed2bde74bbd78c82e023376444b 100644 (file)
@@ -45,6 +45,10 @@ declare module "querystring" {
         /**
          * Parses the query string URL and returns an object.
          *
+         * The returned object has a null prototype. Methods inherited from
+         * Object.prototype, such as toString() and hasOwnProperty(),
+         * are absent.
+         *
          * By default, percent-encoded characters within the query string are assumed to use the
          * UTF-8 encoding, invalid UTF-8 sequences will be replaced with the `U+FFFD` replacement
          * character.