Seventh way to call a JavaScript function without parentheses

99bc twittercard the seventh way to call a javascript function twitter


Gareth Hayes

JavaScript code with gradient background

I thought I knew all about how to call functions without parentheses.

throw onerror=alert,1337
x=new DOMMatrix;matrix=alert;x.a=1337;location='javascript'+':'+x
// or any DOMXSS sink such as location=name

This post presents yet another surprising method to help you understand how tagged template strings work. , which can be used to deepen your understanding of the JavaScript language and provides a foundation for avoiding JavaScript sandboxes and WAFs. It all started with my post about running non-alphanumeric JavaScript without parentheses. I found that you can pass a string to the tagged template. A tagged template means using the prefixed function in front of the template string literal.for example alert`123` A tagged template that calls alert I noticed in a previous post that you can pass multiple arguments to these functions with just strings, as the following code shows.

function x(){

What happens here is that all the strings are added to the first argument as an array and the second argument gets the string. alert(1) But why is the string alert(1) Passed as the second argument to a function? Strings are treated differently than placeholders. A normal string without placeholders is added as an array to the first argument, but the placeholders are added as a new argument of that type. This last point is important. What I didn’t realize at the time was that the placeholders were added as type arguments instead of strings! The following code shows this.

function x(){
function y(str){

This works great. This means you can call a function and pass multiple arguments of any type. But there is a problem. If you use strings in tagged templates, they will always be added as the first argument, breaking functions that use the first argument. The goal here is to call the function with the arguments of your choice. For example, you might want to call: setTimeout Because the first argument accepts a function or a string, and the third argument calls that function with that value.

setTimeout(alert, 0, 'I get passed to alert')

let’s call setTimeout:

setTimeout`${alert}${0}${1}`//Uncaught SyntaxError: Unexpected token ','

Using your custom function again, you can see what’s going on.

function x(){

A screenshot of the console showing the arguments sent to the function

So we know that the first argument contains an array of empty strings, and the last one contains another array full of empty strings.when setTimeout When I convert these arrays to strings, I get a series of commas that cause syntax errors. somehow, setTimeout A function that ignores the first argument, how would you do that? because the first argument will be an array that will be assigned to “this” in setTimeout The function and alert are now passed as the first argument to the function, but…`${alert}${0}${1}`//Illegal invocation

Since we no longer call the function directly, JavaScript throws an exception and we can no longer call the function because “this” is no longer a window object.I thought it was game over, but realized I’d done some JS hacking in the past [].sort others. These allow the function to be called without the illegal call error.


Of course, other features are also available, such as: eval and other array methods map:


I later discovered that you can use Reflect that too:

View code that uses Reflect.apply to call the navigate function

above is new navigation.navigate Chrome’s method that causes a redirect with a payload from To call navigate You have to provide the correct “thisObject” to the function. This is done in the function’s second argument. Reflect.apply. of is used with the third argument, which must be an array of arguments sent to the function.use Reflect The methods set and apply can assign to almost any object or call any function! Note the use of “” to hide the payload. The payload is usually retrieved from another page or domain by passing it within the “name” property of a window that is passed between domains.


It’s pretty amazing that template strings support this behavior, allowing browsers to use sorting and other features this way. By hacking JavaScript, you can learn new and interesting ways to exploit its capabilities to produce unexpected results.

I hope someone finds an eighth way to run JavaScript without parentheses!

back to all articles


Source link

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings


    AI-based artwork wins awards

    Punekar News Fact Check

    Leading Web Design Firm Launches Free Website Design Cost Calculator – Punekar News