diff --git a/src/bun.js/bindings/BunPlugin.cpp b/src/bun.js/bindings/BunPlugin.cpp index 5162ba1571..d3617b677f 100644 --- a/src/bun.js/bindings/BunPlugin.cpp +++ b/src/bun.js/bindings/BunPlugin.cpp @@ -523,6 +523,8 @@ extern "C" JSC_DEFINE_HOST_FUNCTION(JSMock__jsModuleMock, (JSC::JSGlobalObject * auto resolveSpecifier = [&]() -> void { JSC::SourceOrigin sourceOrigin = callframe->callerSourceOrigin(vm); + if (sourceOrigin.isNull()) + return; const URL& url = sourceOrigin.url(); if (specifier.startsWith("file:"_s)) { diff --git a/test/regression/issue/ENG-24434.test.ts b/test/regression/issue/ENG-24434.test.ts new file mode 100644 index 0000000000..64d7585256 --- /dev/null +++ b/test/regression/issue/ENG-24434.test.ts @@ -0,0 +1,29 @@ +import { expect, test } from "bun:test"; + +// Regression test for ENG-24434 +// jest.mock() with invalid arguments should throw TypeError, not crash +test("jest.mock() with non-string first argument should throw TypeError", () => { + const jestObj = Bun.jest(import.meta.path).jest; + + // Passing the jest object itself as the first argument should throw + // a TypeError, not crash with stack-buffer-overflow + expect(() => { + jestObj.mock(jestObj); + }).toThrow(TypeError); +}); + +test("jest.mock() with object as first argument should throw TypeError", () => { + const jestObj = Bun.jest(import.meta.path).jest; + + expect(() => { + jestObj.mock({}); + }).toThrow(TypeError); +}); + +test("jest.mock() with missing callback should throw TypeError", () => { + const jestObj = Bun.jest(import.meta.path).jest; + + expect(() => { + jestObj.mock("some-module"); + }).toThrow(TypeError); +});