:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(hogvm): use own-property checks for nested access and async dispatch (#59218)
Meikel Ratz44fff20aa192
diff --git a/common/hogvm/typescript/src/__tests__/execute.test.ts b/common/hogvm/typescript/src/__tests__/execute.test.tsindex 1a5a300a1c63..81d5d45dd8e9 100644--- a/common/hogvm/typescript/src/__tests__/execute.test.ts+++ b/common/hogvm/typescript/src/__tests__/execute.test.ts@@ -391,6 +391,11 @@ describe('hogvm execute', () => {).toBe('zero')})+ test.each(['hasOwnProperty', 'constructor', '__proto__'])('global dispatch rejects inherited name %s', (name) => {+ const bytecode = ['_h', op.CALL_GLOBAL, name, 0, op.RETURN]+ expect(() => execSync(bytecode, { asyncFunctions: {} })).toThrow(`Unsupported function call: ${name}`)+ })+test('bytecode variable assignment', async () => {const bytecode = ['_h', op.INTEGER, 2, op.INTEGER, 1, op.PLUS, op.GET_LOCAL, 0, op.RETURN, op.POP]expect(execSync(bytecode)).toBe(3)diff --git a/common/hogvm/typescript/src/__tests__/utils.test.ts b/common/hogvm/typescript/src/__tests__/utils.test.tsindex d4ea3d7040b1..5c16aa5a2202 100644--- a/common/hogvm/typescript/src/__tests__/utils.test.ts+++ b/common/hogvm/typescript/src/__tests__/utils.test.ts@@ -1,4 +1,4 @@-import { calculateCost, convertHogToJS, convertJSToHog } from '../utils'+import { calculateCost, convertHogToJS, convertJSToHog, getNestedValue } from '../utils'const PTR_COST = 8@@ -51,4 +51,30 @@ describe('hogvm utils', () => {const js2 = convertHogToJS(map)expect(js2.a === js2).toBe(true)})++ describe('getNestedValue', () => {+ test.each([+ ['own string key on plain object', { a: 1 }, ['a'], 1],+ ['missing key on plain object', { a: 1 }, ['b'], null],+ ['nested own keys on plain object', { a: { b: 2 } }, ['a', 'b'], 2],+ ['inherited toString returns null', {}, ['toString'], null],+ ['inherited hasOwnProperty returns null', {}, ['hasOwnProperty'], null],+ ['inherited constructor returns null', {}, ['constructor'], null],+ ['__proto__ does not traverse the chain', {}, ['__proto__'], null],+ ['Map key lookup', new Map([['a', 1]]), ['a'], 1],+ ['array index 1 returns first element', ['x', 'y'], [1], 'x'],+ ['array index -1 returns last element', ['x', 'y'], [-1], 'y'],+ ])('%s', (_label, obj, chain, expected) => {+ expect(getNestedValue(obj, chain)).toEqual(expected)+ })++ test('throws on zero-index array access', () => {+ expect(() => getNestedValue([1, 2], [0])).toThrow('Hog arrays start from index 1')+ })++ test('does not return inherited values from an array prototype', () => {+ expect(getNestedValue([], ['push'])).toBeNull()+ expect(getNestedValue([], ['map'])).toBeNull()+ })+ })})diff --git a/common/hogvm/typescript/src/execute.ts b/common/hogvm/typescript/src/execute.tsindex d4f0a505498b..7001b51e90a2 100644--- a/common/hogvm/typescript/src/execute.ts+++ b/common/hogvm/typescript/src/execute.ts@@ -55,10 +55,13 @@ export async function execAsync(bytecode: any[] | VMState | Bytecodes, options?:}if (response.state && response.asyncFunctionName && response.asyncFunctionArgs) {vmState = response.state- if (options?.asyncFunctions && response.asyncFunctionName in options.asyncFunctions) {+ if (+ options?.asyncFunctions &&+ Object.prototype.hasOwnProperty.call(options.asyncFunctions, response.asyncFunctionName)+ ) {const result = await options?.asyncFunctions[response.asyncFunctionName](...response.asyncFunctionArgs)vmState.stack.push(result)- } else if (response.asyncFunctionName in ASYNC_STL) {+ } else if (Object.prototype.hasOwnProperty.call(ASYNC_STL, response.asyncFunctionName)) {const result = await ASYNC_STL[response.asyncFunctionName].fn(response.asyncFunctionArgs,response.asyncFunctionName,@@ -156,7 +159,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):if (!frame.chunk || frame.chunk === 'root') {chunkBytecode = rootBytecodechunkGlobals = rootGlobals- } else if (frame.chunk.startsWith('stl/') && frame.chunk.substring(4) in BYTECODE_STL) {+ } else if (frame.chunk.startsWith('stl/') && Object.hasOwn(BYTECODE_STL, frame.chunk.substring(4))) {chunkBytecode = BYTECODE_STL[frame.chunk.substring(4)][1]chunkGlobals = {}} else if (bytecodes[frame.chunk]) {@@ -471,7 +474,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):for (let i = 0; i < count; i++) {chain.push(popStack())}- if (chunkGlobals && chain[0] in chunkGlobals && Object.hasOwn(chunkGlobals, chain[0])) {+ if (chunkGlobals && Object.hasOwn(chunkGlobals, chain[0])) {pushStack(convertJSToHog(getNestedValue(chunkGlobals, chain, true)))} else if (options?.asyncFunctions &&@@ -490,7 +493,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):})))- } else if (chain.length == 1 && chain[0] in ASYNC_STL && Object.hasOwn(ASYNC_STL, chain[0])) {+ } else if (chain.length == 1 && Object.hasOwn(ASYNC_STL, chain[0])) {pushStack(newHogClosure(newHogCallable('async', {@@ -502,7 +505,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):})))- } else if (chain.length == 1 && chain[0] in STL && Object.hasOwn(STL, chain[0])) {+ } else if (chain.length == 1 && Object.hasOwn(STL, chain[0])) {pushStack(newHogClosure(newHogCallable('stl', {@@ -514,7 +517,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):})))- } else if (chain.length == 1 && chain[0] in BYTECODE_STL && Object.hasOwn(BYTECODE_STL, chain[0])) {+ } else if (chain.length == 1 && Object.hasOwn(BYTECODE_STL, chain[0])) {pushStack(newHogClosure(newHogCallable('stl', {@@ -700,7 +703,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):checkTimeout()const name = next()temp = next() // args.length- if (name in declaredFunctions && name !== 'toString') {+ if (Object.prototype.hasOwnProperty.call(declaredFunctions, name) && name !== 'toString') {// This is for backwards compatibility. We use a closure on the stack with local functions now.const [funcIp, argLen] = declaredFunctions[name]frame.ip += 1 // advance for when we return@@ -781,7 +784,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):((options?.asyncFunctions &&Object.hasOwn(options.asyncFunctions, name) &&options.asyncFunctions[name]) ||- name in ASYNC_STL)+ Object.hasOwn(ASYNC_STL, name))) {if (asyncSteps >= maxAsyncSteps) {throw new HogVMException(`Exceeded maximum number of async steps: ${maxAsyncSteps}`)@@ -806,7 +809,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):asyncSteps: asyncSteps + 1,},} satisfies ExecResult- } else if (name in STL) {+ } else if (Object.hasOwn(STL, name)) {const args =version === 0? Array(temp)@@ -814,7 +817,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):.map(() => popStack()): stackKeepFirstElements(stack.length - temp)pushStack(STL[name].fn(args, name, options))- } else if (name in BYTECODE_STL) {+ } else if (Object.hasOwn(BYTECODE_STL, name)) {const argNames = BYTECODE_STL[name][0]if (argNames.length !== temp) {throw new HogVMException(@@ -890,7 +893,7 @@ export function exec(input: any[] | VMState | Bytecodes, options?: ExecOptions):}continue // resume the loop without incrementing frame.ip} else if (closure.callable.__hogCallable__ === 'stl') {- if (!closure.callable.name || !(closure.callable.name in STL)) {+ if (!closure.callable.name || !Object.hasOwn(STL, closure.callable.name)) {throw new HogVMException(`Unsupported function call: ${closure.callable.name}`)}const stlFn = STL[closure.callable.name]diff --git a/common/hogvm/typescript/src/utils.ts b/common/hogvm/typescript/src/utils.tsindex 86aeaa1c0718..9c29dc680b92 100644--- a/common/hogvm/typescript/src/utils.ts+++ b/common/hogvm/typescript/src/utils.ts@@ -60,7 +60,7 @@ export function getNestedValue(obj: any, chain: any[], nullish = false): any {obj = obj[obj.length + key] ?? null}} else {- obj = obj[key] ?? null+ obj = Object.prototype.hasOwnProperty.call(obj, key) ? (obj[key] ?? null) : null}}return obj