Yet another way to invoke functions without parentheses
Recorded: Oct. 16, 2026, 2:07 p.m.
| Original | Summarized |
yet another way to invoke functions without parentheses - bartosz's blog Menu bartosz's blog Home bypassing csp for exfiltration exfiltration using numeric-only outputs getting carlos' secret getting the pid from random numbers prototype pollution for endless jwt tokens sleep function alternative for time-based sql injection yet another way to invoke functions without parentheses Previous Next yet another way to invoke functions without parentheses this payload works by overwriting the prepareStackTrace function of the Error object to generate arbitrary functions. it is usually used to format stack traces and is invoked when the attribute stack of an error is accessed. the parameters of this function are the error and "a structured representation of the stack" , which is just an array of the code positions. it has the same function signature the Function constructor which is used to generate javascript functions, as long as the second argument is valid javascript code. if we try to invoke the function which was generated using this method, we get a syntax error. it is to be expected, because a stack trace is not a valid javascript code, but an Array of code positions in order to make the payload work, we need to control the output of the Array.prototype.toString() function such that it returns our desired javascript payload. this can be done in two ways or we can use prototype pollution to sneak our payload into another function, and then overwrite the Array prototype. i found that we can stay in the Error realm and use its toString function which can be manipulated by polluting the Object.prototype.message variable. i am sure there are more ways to achieve this. now if we put everything together - the function generation using the prepareStackTrace function and way to convert arrays into arbitrary strings, we can generate arbitrary javascript functions last thing we have to do is to invoke our new function. luckily there are many ways to do so and i chose the valueOf trick. it works by overwriting the default valueOf function of our object and then invoking it as a primitive. alternatively, we can use toString and then concatenate the object with a string. the final payload is as follows. feel free to try it out in the browser console. Made with Dracula Theme for MkDocs |
The presented text describes a technique for invoking arbitrary functions in JavaScript without the use of parentheses, achieved through a multi-stage process involving the manipulation of object prototypes and function properties. The core of the technique relies on exploiting the functionality related to error handling to generate executable JavaScript code. The initial step involves overwriting the prepareStackTrace function of the Error object to generate arbitrary functions. This function is typically invoked when the stack attribute of an error is accessed, accepting the error object and a structured representation of the stack, which is an array of code positions. Although the generated function shares the signature of the Function constructor, the input array of code positions is not valid JavaScript code, which would result in a syntax error upon direct invocation. To enable the payload to function, the text details methods for controlling the output of Array.prototype.toString() to return the desired JavaScript payload. Two primary approaches are suggested for this manipulation. First, a direct overwrite is proposed by replacing Array.prototype.toString() with a constant function that returns the target payload. Second, prototype pollution can be utilized to introduce the payload into another function, followed by overwriting the Array prototype. An alternative method involves remaining within the Error object realm by manipulating the toString function, specifically by polluting the Object.prototype.message variable. By combining the function generation via prepareStackTrace with the method of converting arrays into arbitrary strings, the process allows for the generation of arbitrary JavaScript functions. The final critical step involves invoking this newly generated function. The author chooses the valueOf trick for this invocation, which operates by overwriting the default valueOf function of an object and subsequently invoking it as a primitive. Alternatively, concatenation of the object with a string can also be used for this purpose. The text concludes by providing a specific payload demonstrating the sequence of these manipulations, which attempts to set the message property, manage prototype chaining, redefine prepareStackTrace, and set up the invocation mechanism. |