Skip to content
RCE Write-up Intermediate

Frappé Technologies ERPNext Server Side Template Injection

Brian Hyde Follow

Published on medium.com

{{media:0}}

hyde, on medium.com

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.

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. 6 min read.

Context

ERPNext is an enterprise resource planning application developed by Frappé Technologies Pvt. Ltd. It ships with a range of business modules, including healthcare modules, and the company runs a vulnerability disclosure programme. The researcher's original focus was on vulnerabilities in Electronic Medical Record web applications, and ERPNext came up during that research as a target with a disclosure programme attached.

The feature involved here is the user profile page of the web application. A profile page normally stores and displays personal details such as a first name and a last name, and those values are rendered back to the user when the page is viewed. In this case the values were being passed through a server-side template engine before rendering, which is what made the profile fields an injection point.

Setup

The researcher audited the free trial of the product rather than a production deployment. No specific version, deployment topology or tooling is described in the report. The only precondition that matters is an account with the ability to edit the first name and last name fields on the user profile page, which the trial provides.

Finding it

The first test was a pair of arithmetic expressions placed in the two name fields: {{7*7}} in the first name and {{8*8}} in the last name. When the profile page was rendered, the values appeared as 49 and 64 rather than as the literal strings. That is a clear indication that the server is evaluating the contents of the fields as template expressions rather than treating them as data.

Image for post
Server Side Template Injection Proof of Concept From the original on medium.com

The screenshot shows the profile page with the two expressions rendered as their computed results, which is the proof of concept for the injection.

To work out which template engine was in use, the researcher used the decision flowchart from James Kettle's PortSwigger article on server-side template injection at https://portswigger.net/blog/server-side-template-injection. Following that flowchart identified the engine as Jinja2.

Image for post
Template Engine Identification Flowchart From the original on medium.com

The flowchart maps the results of a series of template probes to a specific engine, and in this case the path led to Jinja2.

Exploitation

Step 1: Arbitrary file read

The first name field was set to the following payload:

Text
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/etc/passwd').read() }}

The payload walks the Python object graph from an empty string: __class__ gives the string class, __mro__[2] reaches object, and __subclasses__()[40] selects a subclass at a fixed index. That subclass is used as a file object, opened on /etc/passwd, and its contents are read and returned into the template output. The fixed index 40 is engine and version dependent, which is why the engine had to be identified first.

After refreshing the page, the contents of /etc/passwd were displayed, demonstrating read access to the server's filesystem.

Image for post
Successful Arbitrary File Read From the original on medium.com

The screenshot shows the profile page rendering the contents of the file, which confirms arbitrary file read on the host running the web application.

Step 2: Arbitrary file write

The first name was then set to:

Text
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/home/frappe/PoC.txt', 'w').write(' Proof of Concept: [redacted]') }}

This uses the same object-graph traversal, but opens the target path in write mode ('w') and writes a short string into it. The path /home/frappe/PoC.txt sits under a frappe home directory.

Step 3: Verify the write

The page was refreshed, and the first name was set to:

Text
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/home/frappe/PoC.txt').read() }}

This reads the file back through the same primitive. After one more refresh, the written content appeared on the page, confirming that files could be created and written on the filesystem as well as read.

Image for post
Read + Write Proof of Concept Successful From the original on medium.com

The screenshot shows the read-back of the proof-of-concept file, demonstrating both read and write capability.

The researcher also states that the SSH RSA private key could be read, but deliberately did not publish that image out of consideration for the vendor.

Step 4: Bypassing the first patch

The vulnerability was reported to Frappé, and a patch was rolled out quickly. The patch only partially resolved the issue: it blacklisted any name containing the string .__ and refused to render the template in that case. Anything else was still evaluated, so a payload such as {{ 7*7 }} still rendered as 49.

After reading Frappé's documentation and reviewing the ERPNext source code on GitHub, the researcher found a way to exfiltrate further sensitive information. The first or last name was set to:

Text
{{ [redacted].conf }}

This expression references the application's own frappe object and its local.conf attribute. It contains no .__ sequence, so the blacklist did not block it.

After refreshing the page, the configuration was displayed.

Image for post
Successful Frappe Local Configuration Exfiltration From the original on medium.com

The screenshot shows the local configuration values rendered on the profile page, which is the second proof of concept against the partially patched application.

Step 5: Second report and reward

This issue was reported a second time, and the subsequent patch appeared to work much better than the first. The ERPNext developers were grateful for the audit and report and paid a reward of $1,250. The report notes that Frappé's vulnerability disclosure programme does not officially offer rewards, but does provide recognition and will get a CVE assigned to vulnerabilities that are identified and responsibly disclosed.

Additional findings

Beyond the SSTI described above, the researcher also found multiple XSS vulnerabilities, an SQL Injection vulnerability, and another SSTI in the E-Mail responses.

Image for post
SSTI Proof of Concept From the original on medium.com
Image for post
SSTI Proof of Concept

Both screenshots are captioned as SSTI proof of concept and relate to the findings in the e-mail responses.

Root cause

The profile fields were passed to the Jinja2 template engine as template source rather than as data, so any expression syntax in a first or last name was evaluated on the server. The first patch blocked the specific .__ substring used by the original payloads, but left the engine evaluating everything else, so arithmetic expressions still worked. The report does not describe the code change that produced the final fix.

Impact

An authenticated user who can edit their profile could execute template expressions on the server. In practice this gave arbitrary file read, including /etc/passwd, and the researcher states the SSH RSA private key could be read. It also gave arbitrary file write to /home/frappe/PoC.txt. After the first patch, the same access allowed exfiltration of the site's local configuration through [redacted].conf. The report describes the flaw as very serious. The exact privileges of the application process beyond the frappe home directory are not stated.

Fix

Frappé rolled out a first patch that blacklisted names containing .__ and skipped rendering for those, which the researcher bypassed with {{ 7*7 }} and later used {{ [redacted].conf }}. A second patch followed the second report and, according to the report, works much better than the first. The report does not describe what the second patch changed.

Variants to test

  1. Put {{7*7}} and {{8*8}} into profile name fields and view the profile. A positive result is 49 and 64 rendered in place of the expressions.
  2. Run the engine identification flowchart from the PortSwigger article against any confirmed injection to determine the engine before choosing payloads, since object-graph indices differ between engines and versions.
  3. Try the file read payload with the index adjusted for the target's Python and Jinja2 version, reading /etc/passwd. A positive result is the file contents appearing in the rendered page.
  4. Try the file write payload against a path the application user can write to, then read it back with the read payload. A positive result is the written string appearing on the page.
  5. If a blacklist blocks .__, try expressions that avoid it, such as {{ 7*7 }} for confirmation and {{ [redacted].conf }} for configuration disclosure on ERPNext. A positive result is the expression being evaluated rather than displayed literally.
  6. Test other fields that are rendered back to the user, including e-mail responses, for the same behaviour, since the report found a second SSTI there.

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. 16 min read.

What happened, in plain words

A security researcher spent a few months looking at web applications used in healthcare, specifically Electronic Medical Record systems. During that work he came across ERPNext, a piece of software made by a company called Frappé Technologies Pvt. Ltd. ERPNext is an enterprise resource planning application: think of it as one big program that a business uses to run many departments at once, such as sales, stock, accounts and, in this case, healthcare modules. The company also runs a vulnerability disclosure programme, which is a formal way for outside people to report security problems to them. Because ERPNext was open to reports, the researcher decided to audit the free trial of the product.

hyde
hyde From the original on medium.com

It did not take long. He found a Server Side Template Injection vulnerability in the user profile page of the web application. In plain words, the profile page was taking what a user typed into their name fields and treating it as instructions for the server to run, instead of treating it as harmless text to display. That let him read files on the server, write files on the server, and later pull out the site's configuration. He reported it, the company patched it, the patch was incomplete, he got around it, reported again, and was paid $1,250.

Background: the product and the feature

ERPNext is described in the report as an enterprise resource planning application developed by Frappé Technologies Pvt. Ltd. It ships with a range of business modules, including healthcare modules. The company runs a vulnerability disclosure programme.

The researcher's original focus was vulnerabilities in Electronic Medical Record web applications. ERPNext came up during that research as a target that had a disclosure programme attached, so he decided to look at it.

The feature involved is the user profile page. A profile page normally stores and displays personal details such as a first name and a last name, and those values are shown back to the user when the page is viewed. In this case the values were being passed through a server-side template engine before being rendered, and that is what made the profile fields an injection point.

Two ideas need unpacking here.

A template engine is a piece of software on the server that builds web pages from a template, which is a page with blanks in it. The template says something like "Hello, put the user's first name here", and the engine fills in the blank. The special syntax for a blank in the engine used here is a pair of curly braces, {{ ... }}. Anything inside those braces is treated as an instruction to the engine, not as text. That is the whole point of the syntax: it marks the parts the engine should work out.

Server-side template injection (SSTI) is what happens when user input ends up inside those braces, or is itself treated as template source, so the engine evaluates it. The everyday comparison: imagine a form where you write your name, and instead of printing your name on a badge, the print shop reads your name as a set of instructions and follows them. If you write "print my name", you get your name. If you write "open the safe and copy what is inside", and the shop obediently does that, you have a problem. SSTI is that problem for a web server.

Setting up

The researcher audited the free trial of the product rather than a production deployment. No specific version, deployment topology or tooling is described in the report. The only precondition that matters is an account with the ability to edit the first name and last name fields on the user profile page, which the trial provides.

In other words, you do not need any special access to try this. You need a normal account on the trial, and you need to be able to type into your own profile.

Finding it

The first test was a pair of arithmetic expressions placed in the two name fields: {{7*7}} in the first name and {{8*8}} in the last name. When the profile page was rendered, the values appeared as 49 and 64 rather than as the literal strings. That is a clear indication that the server is evaluating the contents of the fields as template expressions rather than treating them as data.

Why this test? Because it is the safest possible probe. If the page shows {{7*7}} back to you exactly as typed, the server is treating your input as text and there is nothing to see. If the page shows 49, the server did the multiplication for you, which means it ran your input as an instruction. The arithmetic is trivial on purpose: it proves execution without doing anything harmful.

Image for post
Server Side Template Injection Proof of Concept From the original on medium.com

The screenshot shows the profile page with the two expressions rendered as their computed results, which is the proof of concept for the injection. The caption reads "Server Side Template Injection Proof of Concept".

To work out which template engine was in use, the researcher used the decision flowchart from James Kettle's PortSwigger article on server-side template injection at https://portswigger.net/blog/server-side-template-injection. Following that flowchart identified the engine as Jinja2.

Why bother identifying the engine? Because the payloads that follow depend on it. Different engines are written in different programming languages and expose different internal objects, so a payload that works on one engine does nothing on another. The flowchart is a series of small probes whose answers narrow down the engine, a bit like a troubleshooting tree for a broken appliance: answer each question and you arrive at one model.

Image for post
Template Engine Identification Flowchart From the original on medium.com

The flowchart maps the results of a series of template probes to a specific engine, and in this case the path led to Jinja2. The caption reads "Template Engine Identification Flowchart".

Jinja2 is a template engine written in the Python programming language. That matters for the next step, because the payloads reach into Python's internal object structure.

Exploitation

Step 1: Arbitrary file read

The first name field was set to the following payload:

Text
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/etc/passwd').read() }}

The payload walks the Python object graph from an empty string: __class__ gives the string class, __mro__[2] reaches object, and __subclasses__()[40] selects a subclass at a fixed index. That subclass is used as a file object, opened on /etc/passwd, and its contents are read and returned into the template output. The fixed index 40 is engine and version dependent, which is why the engine had to be identified first.

Line by line, in plain words:

  1. '' is an empty piece of text, a string. It is the starting point because it is something the template already has and can inspect.
  2. .__class__ asks that string "what kind of thing are you?" and gets back the answer "I am a string".
  3. .__mro__[2] walks up the family tree of that class. In Python every class has a list of its ancestors, and position 2 in that list lands on object, the ancestor of everything in Python.
  4. .__subclasses__() asks that top-level ancestor for a list of every class defined underneath it. It is a long list of everything the running program has loaded.
  5. [40] picks item number 40 from that list. The researcher already knew, from identifying the engine and its version, which class sits at that position. It happens to be a class that can open files.
  6. ('/etc/passwd') creates one of those file objects pointed at /etc/passwd, a standard file on Unix-like systems that lists user accounts.
  7. .read() reads the whole file and hands the contents back.
  8. The outer {{ ... }} tells the template engine to evaluate all of the above and put the result on the page.

The reason for the fixed index is worth repeating: the list of subclasses depends on what the program has loaded, so position 40 is not universal. That is exactly why the engine had to be identified before choosing the payload.

After refreshing the page, the contents of /etc/passwd were displayed, demonstrating read access to the server's filesystem.

Image for post
Successful Arbitrary File Read From the original on medium.com

The screenshot shows the profile page rendering the contents of the file, which confirms arbitrary file read on the host running the web application. The caption reads "Successful Arbitrary File Read".

"Arbitrary file read" means the attacker can ask the server to open and show any file the server process is allowed to open. The server is doing the reading on the attacker's behalf, using its own permissions.

Step 2: Arbitrary file write

The first name was then set to:

Text
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/home/frappe/PoC.txt', 'w').write(' Proof of Concept: [redacted]') }}

This uses the same object-graph traversal, but opens the target path in write mode ('w') and writes a short string into it. The path /home/frappe/PoC.txt sits under a frappe home directory.

Line by line:

  1. The first part, up to [40], is identical to the read payload. It reaches the same file-handling class.
  2. ('/home/frappe/PoC.txt', 'w') opens a file called PoC.txt in the directory /home/frappe/. The second argument, 'w', means "write mode": create the file if it does not exist, and overwrite it if it does.
  3. .write(' Proof of Concept: [redacted]') puts that short piece of text into the file.
  4. The outer {{ ... }} makes the engine run it when the page is rendered.

Why write a file? Because reading a file proves you can see things, but writing one proves you can change things. Writing a file you chose, in a directory you chose, is a much stronger demonstration. The path also tells you something: /home/frappe/ is a home directory, and the application could write there.

Step 3: Verify the write

The page was refreshed, and the first name was set to:

Text
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/home/frappe/PoC.txt').read() }}

This reads the file back through the same primitive. After one more refresh, the written content appeared on the page, confirming that files could be created and written on the filesystem as well as read.

Line by line:

  1. Again the same traversal to the file-handling class.
  2. ('/home/frappe/PoC.txt') opens the file that was just written, this time with no mode argument, which means read mode by default.
  3. .read() returns its contents.
  4. The outer {{ ... }} puts the result on the page.

Why read it back? Because a write that you cannot confirm is only a guess. Reading the file back through the same mechanism closes the loop: the text you planted is now coming out of the server, so both directions work.

Image for post
Read + Write Proof of Concept Successful From the original on medium.com

The screenshot shows the read-back of the proof-of-concept file, demonstrating both read and write capability. The caption reads "Read + Write Proof of Concept Successful".

The researcher also states that the SSH RSA private key could be read, but deliberately did not publish that image out of consideration for the vendor. An SSH RSA private key is the secret half of a key pair used to log in to a server without a password; anyone holding it can often log in as that user. Publishing it would have been far more damaging than publishing /etc/passwd.

Step 4: Bypassing the first patch

The vulnerability was reported to Frappé, and a patch was rolled out quickly. A patch is a software update that fixes a problem. The patch only partially resolved the issue: it blacklisted any name containing the string .__ and refused to render the template in that case. Anything else was still evaluated, so a payload such as {{ 7*7 }} still rendered as 49.

A blacklist is a list of things you are not allowed to do. It is the opposite of a whitelist, which is a list of things you are allowed to do. Blacklists are fragile because they only stop the exact things someone thought of. Here the developer looked at the payloads and noticed they all contained .__, the double underscore used to reach Python's internals, so they blocked that substring. But the underlying behaviour, the engine evaluating whatever is in the name field, was untouched.

After reading Frappé's documentation and reviewing the ERPNext source code on GitHub, the researcher found a way to exfiltrate further sensitive information. The first or last name was set to:

Text
{{ [redacted].conf }}

This expression references the application's own frappe object and its local.conf attribute. It contains no .__ sequence, so the blacklist did not block it.

Line by line:

  1. frappe is a name the application itself makes available inside its templates. It is the application's own handle on itself.
  2. .local reaches into that handle for the local site information.
  3. .conf reaches further for the configuration.
  4. The outer {{ ... }} makes the engine evaluate it and print the result.

Why this works: the payload never touches the double-underscore internals that the blacklist was watching for. It uses the application's own vocabulary, which the application has to expose for its templates to function. The lesson is that blocking a pattern is not the same as stopping evaluation.

After refreshing the page, the configuration was displayed.

Image for post
Successful Frappe Local Configuration Exfiltration From the original on medium.com

The screenshot shows the local configuration values rendered on the profile page, which is the second proof of concept against the partially patched application. The caption reads "Successful Frappe Local Configuration Exfiltration".

Configuration files commonly hold database connection details, secret keys and other settings that are not meant to be public. Exfiltration means getting data out of a system that was not supposed to give it to you.

Step 5: Second report and reward

This issue was reported a second time, and the subsequent patch appeared to work much better than the first. The ERPNext developers were grateful for the audit and report and paid a reward of $1,250. The report notes that Frappé's vulnerability disclosure programme does not officially offer rewards, but does provide recognition and will get a CVE assigned to vulnerabilities that are identified and responsibly disclosed.

A CVE, or Common Vulnerabilities and Exposures identifier, is a public reference number given to a known security flaw so that everyone can talk about the same issue by the same name. Responsible disclosure means telling the vendor privately first and giving them time to fix it, rather than publishing the details immediately.

Additional findings

Beyond the SSTI described above, the researcher also found multiple XSS vulnerabilities, an SQL Injection vulnerability, and another SSTI in the E-Mail responses.

XSS, or cross-site scripting, is when a site displays user-supplied content in a way that lets an attacker run their own code in another visitor's browser. SQL injection is when user input is mixed into a database query so that the input changes the query's meaning, letting an attacker read or alter data in the database. Both are different classes of bug from SSTI, but they share the same root habit: treating user input as code instead of as data.

Image for post
SSTI Proof of Concept From the original on medium.com
Image for post
SSTI Proof of Concept

Both screenshots are captioned "SSTI Proof of Concept" and relate to the findings in the e-mail responses. The report does not give the payloads used there.

Root cause

The profile fields were passed to the Jinja2 template engine as template source rather than as data, so any expression syntax in a first or last name was evaluated on the server. The first patch addressed the symptom rather than the cause: it blocked the specific .__ substring used by the original payloads, but left the engine evaluating everything else, so arithmetic expressions and references such as [redacted].conf still worked. The report does not describe the code change that produced the final fix.

A useful comparison: if a door is being forced open with a particular crowbar, painting over that crowbar does not make the door stronger. The right fix is to stop treating the name field as a template at all, so that whatever is typed is displayed as text and never evaluated.

Impact

An authenticated user who can edit their profile could execute template expressions on the server. In practice this gave arbitrary file read, including /etc/passwd, and the researcher states the SSH RSA private key could be read. It also gave arbitrary file write to /home/frappe/PoC.txt. After the first patch, the same access allowed exfiltration of the site's local configuration through [redacted].conf. The report describes the flaw as very serious. The exact privileges of the application process beyond the frappe home directory are not stated.

"Authenticated" simply means logged in. The attacker here did not need to be an administrator; a normal account with the ability to edit its own profile was enough.

Fix

Frappé rolled out a first patch that blacklisted names containing .__ and skipped rendering for those, which the researcher bypassed with {{ 7*7 }} and later used {{ [redacted].conf }}. A second patch followed the second report and, according to the report, works much better than the first.

Variants to test

These are the same checks, written so that someone new can follow them in order.

  1. Put {{7*7}} and {{8*8}} into profile name fields and view the profile. A positive result is 49 and 64 rendered in place of the expressions.
  2. Run the engine identification flowchart from the PortSwigger article against any confirmed injection to determine the engine before choosing payloads, since object-graph indices differ between engines and versions.
  3. Try the file read payload with the index adjusted for the target's Python and Jinja2 version, reading /etc/passwd. A positive result is the file contents appearing in the rendered page.
  4. Try the file write payload against a path the application user can write to, then read it back with the read payload. A positive result is the written string appearing on the page.
  5. If a blacklist blocks .__, try expressions that avoid it, such as {{ 7*7 }} for confirmation and {{ [redacted].conf }} for configuration disclosure on ERPNext. A positive result is the expression being evaluated rather than displayed literally.
  6. Test other fields that are rendered back to the user, including e-mail responses, for the same behaviour, since the report found a second SSTI there.

Glossary

  • API: a defined way for one piece of software to talk to another, like a service counter with set questions you can ask.
  • Arbitrary file read: the ability to make a server open and show any file it has permission to open.
  • Arbitrary file write: the ability to make a server create or change files on its filesystem.
  • Authenticated: logged in with a valid account.
  • Blacklist: a list of things that are forbidden; anything not on the list is allowed.
  • Configuration (config): the settings a program uses, often including database details and secret keys.
  • CVE: Common Vulnerabilities and Exposures, a public reference number for a known security flaw.
  • Electronic Medical Record (EMR): software that stores and manages patient medical records.
  • Enterprise resource planning (ERP): software that runs many business departments, such as sales, stock and accounts, in one system.
  • Exfiltration: getting data out of a system that was not meant to give it to you.
  • Free trial: a time-limited or feature-limited version of a product offered so people can try it.
  • Injection point: a place where user input enters a system and can influence how the system behaves.
  • Jinja2: a template engine written in Python, used to build pages from templates.
  • Object graph: the network of objects a running program has in memory, and the links between them.
  • Patch: a software update that fixes a problem.
  • Payload: the specific piece of input an attacker sends to trigger a vulnerability.
  • Proof of concept (PoC): a minimal demonstration that a vulnerability is real.
  • Python: a widely used programming language; Jinja2 is written in it.
  • Rendering: the process of turning a template plus data into the final page a browser displays.
  • Responsible disclosure: reporting a flaw privately to the vendor and giving them time to fix it before details are published.
  • Server-side template injection (SSTI): a flaw where user input is evaluated as template code by the server instead of being treated as plain data.
  • SQL injection: a flaw where user input changes the meaning of a database query.
  • SSH RSA private key: the secret half of a key pair used to log in to a server without a password.
  • Subclass: in object-oriented programming, a class that inherits from another class.
  • Template: a page with blanks in it that a template engine fills in.
  • Template engine: software that builds pages by filling in the blanks in a template.
  • Vulnerability disclosure programme: a formal channel a company provides for outsiders to report security problems.
  • Whitelist: a list of things that are allowed; anything not on the list is refused.
  • XSS (cross-site scripting): a flaw that lets an attacker run their own code in another visitor's browser through a website.

More by Brian Hyde

View all

3 more disclosed reports, best first.

More from ERPNext

View all

The newest disclosed reports for this program.

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.