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;
eq->length = 1;
}
- obj = JS_NewObject(cx);
+ obj = JS_NewObjectProto(cx, JS_NULL);
if (JS_IsException(obj)) {
return JS_EXCEPTION;
}
return JS_DupValue(cx, req->args);
}
- args = JS_NewObject(cx);
+ args = JS_NewObjectProto(cx, JS_NULL);
if (JS_IsException(args)) {
return JS_EXCEPTION;
}
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);
}
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);
###############################################################################
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"}',
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"}/,
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,
}
-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)
{
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,
}
+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)
{
{ 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=' } },
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) => {
run([
escape_tsuite,
parse_tsuite,
+ parse_prototype_tsuite,
stringify_tsuite,
unescape_tsuite,
])
/**
* 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
/**
* 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.