From: Dmitry Volyntsev Date: Tue, 17 Nov 2026 00:19:41 +0000 (-0800) Subject: QueryString: use null prototypes for parsed arguments X-Git-Url: https://git.kaiwu.me/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=437bfdbf516ead51c5397ebfd899e6722e29285b;p=njs.git QueryString: use null prototypes for parsed arguments 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 --- diff --git a/external/njs_query_string_module.c b/external/njs_query_string_module.c index 94267317..20080949 100644 --- a/external/njs_query_string_module.c +++ b/external/njs_query_string_module.c @@ -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; diff --git a/external/qjs_query_string_module.c b/external/qjs_query_string_module.c index b3b429b2..605a2555 100644 --- a/external/qjs_query_string_module.c +++ b/external/qjs_query_string_module.c @@ -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; } diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c index 7e96ad5b..5c6a857d 100644 --- a/nginx/ngx_http_js_module.c +++ b/nginx/ngx_http_js_module.c @@ -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; } diff --git a/nginx/t/js_args.t b/nginx/t/js_args.t index 0adfddd8..c69e0562 100644 --- a/nginx/t/js_args.t +++ b/nginx/t/js_args.t @@ -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', <write_file('test.js', <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"}/, diff --git a/src/njs.h b/src/njs.h index 54908c35..5e0e61fa 100644 --- 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, diff --git a/src/njs_object.c b/src/njs_object.c index 65964070..2d8c7b8d 100644 --- a/src/njs_object.c +++ b/src/njs_object.c @@ -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) { diff --git a/src/njs_object.h b/src/njs_object.h index 319de1db..2409f52b 100644 --- a/src/njs_object.h +++ b/src/njs_object.h @@ -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, diff --git a/src/njs_vm.c b/src/njs_vm.c index ed98c467..27d00712 100644 --- a/src/njs_vm.c +++ b/src/njs_vm.c @@ -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) { diff --git a/test/querystring.t.mjs b/test/querystring.t.mjs index d9ebc0a3..3da1d778 100644 --- a/test/querystring.t.mjs +++ b/test/querystring.t.mjs @@ -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, ]) diff --git a/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts index 39acbad0..fbeca1b0 100644 --- a/ts/ngx_http_js_module.d.ts +++ b/ts/ngx_http_js_module.d.ts @@ -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 diff --git a/ts/njs_modules/querystring.d.ts b/ts/njs_modules/querystring.d.ts index d4229fdf..6e7fd510 100644 --- a/ts/njs_modules/querystring.d.ts +++ b/ts/njs_modules/querystring.d.ts @@ -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.