Arbitrary code execution via untrusted schemas in is-my-json-valid
The is-my-json-valid library builds JavaScript source code from a JSON schema, and it inserts property names into that generated code without escaping them. If an attacker can supply the schema, they can place code in a property name so that it runs when the validator is compiled or used.
- Triaged in 20 days
- Fixed in 22 days
- Disclosed after 52 days
I would like to report an arbitrary code execution vulnerability in is-my-json-valid.
It allows to execute arbitrary code if an attacker-controlled schema is passed to is-my-json-valid.
The module Readme doesn't say anything about the risks of untrusted schemas, so I by default assume that this is applicable. If it's not applicable, please place a warning in the readme that users should never use untrusted schemas.
Module
module name: is-my-json-valod
version: 2.20.0
npm page: https://www.npmjs.com/package/is-my-json-valid
Module Description
A JSONSchema validator that uses code generation to be extremely fast.
Module Stats
1 517 862 weekly downloads
Vulnerability
Vulnerability Description
See steps to reproduce.
The problem is in formatName function.
Steps To Reproduce:
const validator = require('is-my-json-valid')
const schema = {
type: 'object',
properties: {
'x[console.log(process.mainModule.require(`child_process`).execSync(`cat /etc/passwd`).toString(`utf-8`))]': {
required: true,
type:'string'
}
},
}
var validate = validator(schema);
validate({})
Wrap up
- I contacted the maintainer to let them know: N
- 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 formatName function inserts schema property names into generated JavaScript source without escaping or validating them, so the names become executable code.
Impact
An attacker who controls the schema could execute arbitrary JavaScript and shell commands in the context of the application using the library.
What the attacker needed
Attacker controls the JSON schema; application compiles the schema
The fix
The readme should warn that users should never use untrusted schemas.
How it was found
- Load the is-my-json-valid module in a Node.js script.
- Build a schema whose property name contains JavaScript code that runs a shell command.
- Compile a validator from that schema.
- Call the validator with an empty object to trigger the generated code.
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.
This library turns a JSON schema, which is a description of what valid data should look like, into JavaScript code so it can check data quickly. It pastes the names of schema properties straight into that generated code without cleaning them first. So if someone else controls the schema, they can hide their own JavaScript in a property name and it will run 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.