diff --git a/src/jsc/bindings/BunPlugin.cpp b/src/jsc/bindings/BunPlugin.cpp index 5773394aed3..bf2e350e2f4 100644 --- a/src/jsc/bindings/BunPlugin.cpp +++ b/src/jsc/bindings/BunPlugin.cpp @@ -302,8 +302,11 @@ static inline JSC::EncodedJSValue setupBunPlugin(JSC::JSGlobalObject* globalObje auto targetValue = obj->getIfPropertyExists(globalObject, Identifier::fromString(vm, "target"_s)); RETURN_IF_EXCEPTION(throwScope, {}); if (targetValue) { - if (auto* targetJSString = targetValue.toStringOrNull(globalObject)) { + auto* targetJSString = targetValue.toStringOrNull(globalObject); + RETURN_IF_EXCEPTION(throwScope, {}); + if (targetJSString) { String targetString = targetJSString->value(globalObject); + RETURN_IF_EXCEPTION(throwScope, {}); if (!(targetString == "node"_s || targetString == "bun"_s || targetString == "browser"_s)) { JSC::throwTypeError(globalObject, throwScope, "plugin target must be one of 'node', 'bun' or 'browser'"_s); return {}; diff --git a/test/js/bun/plugin/plugins.test.ts b/test/js/bun/plugin/plugins.test.ts index 1ccd453b224..6c0990f38e3 100644 --- a/test/js/bun/plugin/plugins.test.ts +++ b/test/js/bun/plugin/plugins.test.ts @@ -560,3 +560,17 @@ it("recursion throws stack overflow at entry point", () => { expect(result.stderr.toString()).toContain("RangeError: Maximum call stack size exceeded."); }); + +it("does not crash when target property throws on string conversion", () => { + const badTarget = { + toString() { + return {}; + }, + valueOf() { + return {}; + }, + }; + expect(() => { + plugin({ name: "bad-target", target: badTarget as any, setup() {} }); + }).toThrow(); +});