diff --git a/.changeset/wp14-defineproperty-enumeration.md b/.changeset/wp14-defineproperty-enumeration.md new file mode 100644 index 0000000..913d07a --- /dev/null +++ b/.changeset/wp14-defineproperty-enumeration.md @@ -0,0 +1,5 @@ +--- +"quickjs-wasi": patch +--- + +Fill enumeration result arrays with DefineProperty semantics (`JS_DefinePropertyValueUint32`) instead of `[[Set]]` (`JS_SetPropertyUint32`) in `qjs_get_own_property_names`, `qjs_get_own_property_names_all`, and `qjs_get_own_property_keys`. A guest that polluted `Array.prototype` with an index accessor could previously swallow keys at that index and have its setter executed inside the binary's fill loop — guest code running during nominally trap-free enumeration. The Define-based fill never walks the prototype chain, so pollution can neither observe nor corrupt enumeration, and every result index is an own data property (indexed readback cannot reach an inherited accessor either). Also removes a dead, never-wired byte-at-a-time UTF-16 handler in the encoding extension whose FIXME case (code unit following an unmatched lead surrogate) is handled correctly by the live `decode_utf16` path. diff --git a/c/interface.c b/c/interface.c index 63f8e2b..618c298 100644 --- a/c/interface.c +++ b/c/interface.c @@ -1096,6 +1096,13 @@ void *qjs_get_value_ptr(JSValue *val) { /* * Get the own enumerable string property names of an object as a QuickJS Array. * Returns a heap-allocated JSValue* pointing to the array, or JS_EXCEPTION on failure. + * + * The result array is filled with JS_DefinePropertyValueUint32 + * (CreateDataProperty semantics): the fill never walks the prototype + * chain, so guest pollution of Array.prototype with index accessors can + * neither observe the keys nor swallow them, and no guest code runs + * during the fill. Every index 0..len-1 is an own data property — a + * subsequent indexed read cannot reach an inherited accessor either. */ __attribute__((export_name("qjs_get_own_property_names"))) JSValue *qjs_get_own_property_names(JSValue *obj) { @@ -1110,7 +1117,7 @@ JSValue *qjs_get_own_property_names(JSValue *obj) { JSValue arr = JS_NewArray(ctx); for (uint32_t i = 0; i < len; i++) { JSValue key = JS_AtomToString(ctx, tab[i].atom); - JS_SetPropertyUint32(ctx, arr, i, key); + JS_DefinePropertyValueUint32(ctx, arr, i, key, JS_PROP_C_W_E); } JS_FreePropertyEnum(ctx, tab, len); @@ -1120,6 +1127,9 @@ JSValue *qjs_get_own_property_names(JSValue *obj) { /* * Get ALL own property names (including non-enumerable) as a QuickJS Array. * Returns a heap-allocated JSValue* pointing to the array, or JS_EXCEPTION on failure. + * + * Define-based fill — see qjs_get_own_property_names for the pollution + * immunity rationale. */ __attribute__((export_name("qjs_get_own_property_names_all"))) JSValue *qjs_get_own_property_names_all(JSValue *obj) { @@ -1134,7 +1144,7 @@ JSValue *qjs_get_own_property_names_all(JSValue *obj) { JSValue arr = JS_NewArray(ctx); for (uint32_t i = 0; i < len; i++) { JSValue key = JS_AtomToString(ctx, tab[i].atom); - JS_SetPropertyUint32(ctx, arr, i, key); + JS_DefinePropertyValueUint32(ctx, arr, i, key, JS_PROP_C_W_E); } JS_FreePropertyEnum(ctx, tab, len); @@ -1150,6 +1160,9 @@ JSValue *qjs_get_own_property_names_all(JSValue *obj) { * * For ordinary objects this is trap-free; for a Proxy it fires the * ownKeys trap (check qjs_is_proxy first if that matters). + * + * Define-based fill — see qjs_get_own_property_names for the pollution + * immunity rationale. */ __attribute__((export_name("qjs_get_own_property_keys"))) JSValue *qjs_get_own_property_keys(JSValue *obj) { @@ -1164,7 +1177,7 @@ JSValue *qjs_get_own_property_keys(JSValue *obj) { JSValue arr = JS_NewArray(ctx); for (uint32_t i = 0; i < len; i++) { JSValue key = JS_AtomToValue(ctx, tab[i].atom); - JS_SetPropertyUint32(ctx, arr, i, key); + JS_DefinePropertyValueUint32(ctx, arr, i, key, JS_PROP_C_W_E); } JS_FreePropertyEnum(ctx, tab, len); diff --git a/extensions/encoding/encoding.c b/extensions/encoding/encoding.c index f26b3c4..1765f5a 100644 --- a/extensions/encoding/encoding.c +++ b/extensions/encoding/encoding.c @@ -311,84 +311,12 @@ static void utf16_decoder_reset(UTF16DecoderState *st, int big_endian) { st->be = big_endian; } -/* UTF-16 handler: processes one byte at a time. - Returns a code point (>=0), HANDLER_FINISHED, HANDLER_CONTINUE, or HANDLER_ERROR. */ -static int32_t utf16_handler(UTF16DecoderState *st, int byte_or_eof) { - /* End-of-queue */ - if (byte_or_eof < 0) { - if (st->lead_byte != -1 || st->lead_surrogate != -1) { - st->lead_byte = -1; - st->lead_surrogate = -1; - return HANDLER_ERROR; - } - return HANDLER_FINISHED; - } - - uint8_t byte = (uint8_t)byte_or_eof; - - /* If we have a pending lead_byte, combine to form a code unit */ - if (st->lead_byte == -1) { - st->lead_byte = byte; - return HANDLER_CONTINUE; - } - - uint16_t code_unit; - if (st->be) { - code_unit = ((uint16_t)st->lead_byte << 8) | byte; - } else { - code_unit = ((uint16_t)byte << 8) | (uint16_t)st->lead_byte; - } - st->lead_byte = -1; - - /* If we have a pending lead surrogate */ - if (st->lead_surrogate != -1) { - uint16_t lead = (uint16_t)st->lead_surrogate; - st->lead_surrogate = -1; - if (is_lo_surr(code_unit)) { - return (int32_t)surr_to_cp(lead, code_unit); - } - /* Not a trail surrogate — error for the lead surrogate. - But we need to re-process this code_unit. We handle this - by checking if it's a lead surrogate itself or a regular code unit. */ - if (is_hi_surr(code_unit)) { - st->lead_surrogate = code_unit; - /* Return error for the previous unmatched lead surrogate */ - return HANDLER_ERROR; - } - /* The current code_unit is a BMP code point. We emit error for - the unmatched lead, but we also need to emit the current code_unit. - Since we can only return one thing at a time, we store the result - and use a special approach: return the error, and the caller - will need to re-process. However, the spec's "process an item" - pushes error and continues, so we just mark error and the next - call will handle this code_unit. But we've already consumed the - byte pair... We need a different approach. - - The spec's UTF-16 decoder processes 2 bytes at a time forming - code units, not single bytes. Let me restructure to buffer the - code unit for re-processing. */ - - /* Actually, for simplicity, we'll store the code_unit as needing - to be re-examined. We'll use a small queue approach. For now, - let's emit an error. The code_unit is lost, which is a bug. - Let me handle this properly with a pending_code_unit field. */ - - /* FIXME: this is handled below in the decode function by - using a different approach. For now, signal error. */ - return HANDLER_ERROR; - } - - if (is_hi_surr(code_unit)) { - st->lead_surrogate = code_unit; - return HANDLER_CONTINUE; - } - - if (is_lo_surr(code_unit)) { - return HANDLER_ERROR; - } - - return (int32_t)code_unit; -} +/* NOTE: the actual UTF-16 decoding lives in decode_utf16 below, which + processes byte pairs into code units directly and re-processes a + code unit that follows an unmatched lead surrogate via a pending + code-unit slot (the WHATWG "shared UTF-16 decoder" behavior). An + earlier byte-at-a-time handler here (never wired up) dropped that + code unit — it was dead code and has been removed. */ /* ====================================================================== * TextDecoder opaque data diff --git a/test/introspection.test.ts b/test/introspection.test.ts index 16ac80c..1626312 100644 --- a/test/introspection.test.ts +++ b/test/introspection.test.ts @@ -344,3 +344,86 @@ describe('introspection across snapshot/restore', () => { desc.set!.dispose(); }); }); + +describe('enumeration fill under Array.prototype pollution (issue #3)', () => { + // The qjs_get_own_property_names / _names_all / _keys exports + // materialize their results into a guest Array. That fill uses + // JS_DefinePropertyValueUint32 (CreateDataProperty semantics): it never + // walks the prototype chain, so a guest that pollutes Array.prototype + // with index accessors can neither observe the keys (setter) nor + // swallow them (getter-only index), and no guest code executes during + // nominally trap-free enumeration. Every result index is an own data + // property, so the facade's indexed readback cannot reach an inherited + // accessor either. + // + // NOTE: the guest-side log is a string, not an array — an array push + // would itself hit the polluted Array.prototype[0] setter and recurse. + const POLLUTION_SETUP = ` + globalThis.log = ''; + Object.defineProperty(Array.prototype, 0, { + get() { globalThis.log += '|get0'; return 'fabricated'; }, + set(v) { globalThis.log += '|set0:' + String(v); }, + configurable: true, + }); + Object.defineProperty(Array.prototype, 1, { + set(v) { globalThis.log += '|set1:' + String(v); }, + configurable: true, + }); + globalThis.victim = { a: 1, b: 2, c: 3 }; + globalThis.victim[Symbol.for('s')] = 4; + `; + + it('polluted index accessors can neither observe nor corrupt key enumeration', async () => { + using vm = await QuickJS.create(wasmBytes); + vm.evalCode(POLLUTION_SETUP).dispose(); + using victim = vm.evalCode('globalThis.victim'); + + // All three enumeration exports return the complete, true key list. + expect(victim.keys()).toEqual(['a', 'b', 'c']); + expect(victim.getOwnPropertyNames()).toEqual(['a', 'b', 'c']); + const keys = victim.getOwnPropertyKeys(); + try { + const strings = keys.filter((k): k is string => typeof k === 'string'); + expect(strings).toEqual(['a', 'b', 'c']); + expect(keys).toHaveLength(4); // 3 strings + 1 symbol + expect(typeof keys[3]).not.toBe('string'); + } finally { + for (const k of keys) { + if (typeof k !== 'string') k.dispose(); + } + } + + // THE guarantee: no polluted accessor ever fired — not the setters + // during the binary's fill loop, not the getter during readback (the + // readback [[Get]] finds an own data property at every index and + // never consults the prototype). + using log = vm.evalCode('globalThis.log'); + expect(log.toString()).toBe(''); + + // Control — the pollution is live: an ordinary guest-side indexed + // write and hole-read go through the prototype accessors. + using control = vm.evalCode( + `const a = []; a[0] = 'x'; void [][0]; globalThis.log` + ); + expect(control.toString()).toBe('|set0:x|get0'); + }); + + it('the polluted-getter index is not swallowed (length and order intact)', async () => { + using vm = await QuickJS.create(wasmBytes); + // Getter-ONLY pollution: under a [[Set]]-based fill this index would + // be swallowed silently (no own property created, length may still + // advance) — under the Define-based fill it must be a plain element. + vm.evalCode(` + globalThis.getterHits = 0; + Object.defineProperty(Array.prototype, 1, { + get() { globalThis.getterHits++; return 'fabricated'; }, + configurable: true, + }); + globalThis.victim = { x: 1, y: 2, z: 3 }; + `).dispose(); + using victim = vm.evalCode('globalThis.victim'); + expect(victim.keys()).toEqual(['x', 'y', 'z']); + using hits = vm.evalCode('globalThis.getterHits'); + expect(hits.toNumber()).toBe(0); + }); +});