Skip to content
RCE Write-up Advanced

PayPal Node.js code injection (RCE)

The PayPal demo site used the Dust.js templating engine, whose 'if' helper passed a template condition into JavaScript's eval. An attacker could inject JavaScript through the condition and execute code on the server.

Michael Stepankin Follow

Published on artsploit.blogspot.hk

return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"').replace(SQUOT, ''');

Michael Stepankin, on artsploit.blogspot.hk

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 4 min read.

What the report shows

The target is demo.paypal.com. The researcher, Michael Stepankin, found a Dust.js template that used the if helper, whose implementation evaluates its condition with eval.. The if helper evaluates its cond parameter with JavaScript eval, so a value that lands in that parameter could be turned into injected JavaScript..

The report opens with two screenshots.

From the original on artsploit.blogspot.hk

The template shown uses the if helper to choose between a desktop and a mobile layout, based on a {device} reference.:

Text
          {@if cond="'{device}' == 'desktop'"}

            <div> Desktop version </div>

          {:else}

            <div> Mobile version </div>

          {/if}

The {device} value is placed inside single quotes inside the condition. The helper implementation then takes that condition string and passes it to eval:

Text
          "if": function( chunk, context, bodies, params ){

            var body = bodies.block,

                skip = bodies['else'];

            if( params && params.cond){

              var cond = params.cond;

              cond = dust.helpers.tap(cond, chunk, context);

              // eval expressions with given dust references

              if(eval(cond)){

               if(body) {

                return chunk.render( bodies.block, context );

               }

               else {

                 _log("Missing body block in the if helper!");

                 return chunk;

               }

              }

The researcher shows what the evaluated string looks like for a normal request:

Text
          eval("'xxx\' == 'desktop'");

The report stops at this point: it does not include the injection payload, the HTTP request that carried it, the server response, or any statement about bounty, severity or a fix. The third screenshot sits at the end of the text.

From the original on artsploit.blogspot.hk

Root cause

The mistake is likely the use of eval inside a template helper on a condition string. Dust escapes HTML output through dust.escapeHtml, which the report quotes:

Text
          var HCHARS = /[&<>"']/,

                AMP    = /&/g,

                LT     = /</g,

                GT     = />/g,

                QUOT   = /\"/g,

                SQUOT  = /\'/g;

          dust.escapeHtml = function(s) {

          if (typeof s === 'string') {

            if (!HCHARS.test(s)) {

              return s;

            }

            return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;').replace(SQUOT, '&#39;');

          }

          return s;

          };

That escaping protects rendered HTML, not a JavaScript expression that is evaluated before rendering. The cond string is assembled first and evaluated as code, so HTML encoding of the final output does nothing to stop the injection.

Impact

On a Node.js server, JavaScript passed to eval runs in the process that serves the application, which is why the title frames the issue as remote code execution. The report does not demonstrate a specific command, file read or data access, and gives no evidence of what an attacker could reach.

How this kind of bug works

Server-side template injection happens when user input is treated as part of a template rather than as data. A related and often easier variant is expression injection: the template engine, or a helper inside it, evaluates a string as code. If that string contains user input, the attacker can break out of the surrounding syntax and append their own expression.

Dust.js is a logic-less template engine by design, but helpers such as if add logic back. When a helper calls eval on a condition built by concatenation, the template author has effectively created a code evaluation sink. The same pattern appears in other engines: expression languages in Java, eval in PHP or Ruby templates, and helpers in Handlebars or similar libraries that compile strings at runtime.

The defences are straightforward: never evaluate template parameters as code, use a real expression parser that only accepts a restricted grammar, and keep user data out of template source entirely. Output encoding is not a defence here because the dangerous step happens before output.

How to test for it

  1. Identify parameters that influence rendering: layout switches, locale, device type, feature flags, anything reflected into a page that looks templated.
  2. Send a value containing template syntax and quote characters, such as a single quote followed by simple arithmetic, and compare the response with a benign value.
  3. Look for signs that the input was evaluated rather than echoed: a changed branch of the page, an error mentioning the template engine or eval, or a stack trace.
  4. If arithmetic is evaluated, escalate carefully to confirm code execution in a way the program allows, and stop at proof of concept.

A positive result is a response that changes because your expression was computed, not merely reflected. Common false positives are inputs that are HTML-encoded and reflected verbatim, client-side templating that only runs in the browser, and errors caused by malformed input rather than evaluation. Test both quoted and unquoted contexts, since the surrounding syntax decides which characters break out.

How it is fixed

Remove eval from helpers and replace it with a parser that evaluates only a fixed set of comparisons and references. Treat all request data as data, never as template source. Where a template must be selected dynamically, use an allowlist of known template names. Add tests that feed quote characters and template syntax into every parameter that reaches a template.

Hunting notes

Expression injection tends to hide in features that adapt output: device detection, A/B tests, localisation, email and PDF rendering, and admin previews of templates. Recon signals include error pages naming a template engine, responses that differ subtly with the same input, and JavaScript-heavy stacks such as Node.js where eval is common. To get a report accepted, show the smallest input that changes behaviour, explain the sink from the code or error message, and demonstrate impact within the program's rules rather than running destructive commands.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 6 min read.

What happened, in plain words

A security researcher, Michael Stepankin, looked at demo.paypal.com. The report shows a Dust.js template, a JavaScript templating engine. A template is a page skeleton with blanks that get filled in with data, a bit like a form letter where the name and address are inserted before it is sent. Dust.js fills those blanks on the server, the computer that produces the page before it reaches your browser.

The template places a value called {device} inside a Dust if helper, a small piece of logic that chooses between two versions of the page. It was placed inside a Dust if helper, a small piece of logic that chooses between two versions of the page. That helper takes its condition and runs it through JavaScript's eval, a function that executes a string as code. If a value from the request ends up inside that string, whoever controls the value could add their own JavaScript. The title calls this remote code execution.

The report opens with two screenshots.

From the original on artsploit.blogspot.hk

The template in question picks a desktop or a mobile layout depending on {device}:

Text
          {@if cond="'{device}' == 'desktop'"}

            <div> Desktop version </div>

          {:else}

            <div> Mobile version </div>

          {/if}

The {device} value is dropped inside single quotes in the condition. The helper's own code then takes that condition and hands it to eval:

Text
          "if": function( chunk, context, bodies, params ){

            var body = bodies.block,

                skip = bodies['else'];

            if( params && params.cond){

              var cond = params.cond;

              cond = dust.helpers.tap(cond, chunk, context);

              // eval expressions with given dust references

              if(eval(cond)){

               if(body) {

                return chunk.render( bodies.block, context );

               }

               else {

                 _log("Missing body block in the if helper!");

                 return chunk;

               }

              }

For a normal request the string being evaluated looks like this:

Text
          eval("'xxx\' == 'desktop'");

Read it slowly: the outer double quotes belong to eval, the inner single quotes wrap the device value, and xxx stands in for whatever the visitor's device is.

The report stops there. It does not include the injection payload, the HTTP request that carried it, the server's response, or any statement about a bounty, a severity rating or a fix. The third screenshot sits at the end of the text.

From the original on artsploit.blogspot.hk

Root cause

The likely mistake is calling eval inside a template helper on a condition string. Dust does escape HTML output, and the report quotes that escaping code:

Text
          var HCHARS = /[&<>"']/,

                AMP    = /&/g,

                LT     = /</g,

                GT     = />/g,

                QUOT   = /\"/g,

                SQUOT  = /\'/g;

          dust.escapeHtml = function(s) {

          if (typeof s === 'string') {

            if (!HCHARS.test(s)) {

              return s;

            }

            return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;').replace(SQUOT, '&#39;');

          }

          return s;

          };

This function turns characters such as <, > and quotes into harmless HTML entities so they display as text instead of being interpreted by the browser. That protects the finished HTML page. It does nothing for a JavaScript expression that is evaluated before the page is rendered. The condition string is assembled first and run as code, so encoding the final output cannot stop the injection.

Impact

On a Node.js server, JavaScript passed to eval runs inside the process that serves the application, which is why the title describes the issue as remote code execution. The report does not demonstrate a specific command, file read or data access, and gives no evidence of what an attacker could reach.

In general, how this kind of bug works

Server-side template injection happens when user input is treated as part of a template rather than as data. A close relative, and often an easier one to spot, is expression injection: the template engine, or a helper inside it, evaluates a string as code. If that string contains user input, the attacker can break out of the surrounding syntax and append an expression of their own.

Dust.js is designed as a logic-less template engine, but helpers such as if add logic back. When a helper calls eval on a condition built by joining strings together, the template author has created a place where code gets evaluated. The same pattern appears in other engines: expression languages in Java, eval in PHP or Ruby templates, and helpers in Handlebars or similar libraries that compile strings at runtime.

The defences are straightforward: never evaluate template parameters as code, use a real expression parser that accepts only a restricted grammar, and keep user data out of template source entirely. Output encoding is not a defence here, because the dangerous step happens before output.

In general, how to test for it

  1. Identify parameters that influence rendering: layout switches, locale, device type, feature flags, anything reflected into a page that looks templated.
  2. Send a value containing template syntax and quote characters, such as a single quote followed by simple arithmetic, and compare the response with a benign value.
  3. Look for signs that the input was evaluated rather than echoed: a changed branch of the page, an error mentioning the template engine or eval, or a stack trace.
  4. If arithmetic is evaluated, escalate carefully to confirm code execution in a way the program allows, and stop at proof of concept.

A positive result is a response that changes because your expression was computed, not merely reflected. Common false positives are inputs that are HTML-encoded and reflected verbatim, client-side templating that only runs in the browser, and errors caused by malformed input rather than evaluation. Test both quoted and unquoted contexts, since the surrounding syntax decides which characters break out.

In general, how it is fixed

Remove eval from helpers and replace it with a parser that evaluates only a fixed set of comparisons and references. Treat all request data as data, never as template source. Where a template must be selected dynamically, use an allowlist of known template names. Add tests that feed quote characters and template syntax into every parameter that reaches a template.

In general, where to look

Expression injection tends to hide in features that adapt output: device detection, A/B tests, localisation, email and PDF rendering, and admin previews of templates. Recon signals include error pages naming a template engine, responses that differ subtly with the same input, and JavaScript-heavy stacks such as Node.js where eval is common. To get a report accepted, show the smallest input that changes behaviour, explain the sink from the code or error message, and demonstrate impact within the program's rules rather than running destructive commands.

Glossary

  • Template: a page skeleton with blanks that are filled with data before the page is sent.
  • Templating engine: software that fills in a template's blanks and produces the final page.
  • Dust.js: a JavaScript templating engine, used here on the server.
  • Helper: a small function inside a template engine that adds logic, such as choosing between two layouts.
  • eval: a JavaScript function that runs a string as code.
  • Expression injection: a bug where user input becomes part of an expression that the system evaluates as code.
  • Server-side template injection: a bug where user input is treated as part of a template rather than as data.
  • Remote code execution (RCE): running an attacker's code on a remote machine, here the server.
  • Node.js: a runtime that runs JavaScript on a server.
  • HTTP request: the message a browser sends to a server to ask for a page or send data.
  • Payload: the crafted input an attacker sends to trigger a bug.
  • HTML encoding (escaping): replacing characters such as < and quotes with safe entities so they display as text.
  • Output encoding: encoding data at the moment it is written into a page, to stop it being interpreted as markup or code.
  • Sink: the place in code where dangerous input is finally used, such as an eval call.
  • Allowlist: a fixed list of permitted values, with everything else rejected.
  • Proof of concept: the smallest demonstration that a bug is real, without causing harm.
  • Stack trace: an error message listing the internal steps where a program failed.

More by Michael Stepankin

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.