Skip to content

appsecengineer/json-schema-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js Input Validation using JSON Schema

1. What is Node.js Input Validation using JSON Schema?

Node.js input validation using JSON Schema is a robust approach to ensuring the integrity and security of data entering Node.js applications. JSON Schema provides a standardized and structured way to define the structure, format, and constraints of JSON data. By validating input against predefined schemas, developers can prevent invalid or malicious data from compromising the application's functionality and security.

2. Why is Node.js Input Validation using JSON Schema important?

Input validation is crucial for preventing a wide range of security vulnerabilities, including injection attacks, data corruption, and unauthorized access. By validating input data using JSON Schema, developers can enforce strict data validation rules, ensuring that only well-formed and expected data is processed by the application. This helps prevent security breaches, data loss, and other adverse effects resulting from invalid input.

3. When should Node.js Input Validation using JSON Schema be used?

Node.js input validation using JSON Schema should be used whenever applications accept input from users, external systems, or untrusted sources. It is particularly important for validating JSON data transmitted via HTTP requests, API calls, or other communication channels. By validating input at the entry points of the application, developers can mitigate the risk of security vulnerabilities and ensure the reliability of data processing.

To effectively implement Node.js input validation using JSON Schema, consider the following best practices and strategies:

  1. Define Clear Data Structures:

    • Establish clear and precise JSON Schema definitions that describe the expected structure and properties of incoming JSON data.
  2. Enforce Strict Data Types:

    • Specify strict data types for each property within the JSON Schema to ensure that incoming data adheres to the expected format.
  3. Implement Mandatory Fields:

    • Define required fields within the JSON Schema to enforce the presence of essential data elements.
  4. Utilize Enumerations and Constraints:

    • Employ enumerations and constraints within the JSON Schema to limit the range of acceptable values for properties.
  5. Validate Data Length and Format:

    • Validate the length and format of input data using JSON Schema constraints, ensuring consistency and reliability.
  6. Handle Nested Data Structures:

    • Handle nested data structures effectively by defining nested JSON Schema objects and arrays, maintaining data integrity at all levels.
  7. Perform Comprehensive Testing:

    • Conduct thorough testing of JSON Schema validation rules to identify and address potential edge cases and validation failures.
  8. Monitor and Update Schemas:

    • Regularly monitor and update JSON Schema definitions to accommodate changes in application requirements and data structures.

By adhering to these best practices and leveraging JSON Schema for input validation, developers can strengthen the security and reliability of Node.js applications while safeguarding against data corruption and security threats.

Let's put theory into practice and explore Node.js input validation using JSON Schema hands-on!

Deploy the vulnerable application

  • Open terminal

  • Access Directory with codebase

cd /root/node-json-schema
  • Build the insecure app
docker build -t node-json-schema .
  • Run the insecure app
docker run -d -p 8080:8080 node-input-validation
  • Try and load some malicious payloads in the JSON input
http POST http://localhost:8080/signup firstName="'" lastName="<img src=1/>" age:=25 email="john.doe@<script>alert(1)</script>"

Try a variation of different payloads. You should see that there's no input validation

  • Stop all the containers
curl -sSL https://raw.githubusercontent.com/we45/xml-files/master/clean-docker.sh | sh

Now that we have witnessed how lack of input validation can exploit vulnerabilities in our code.

Rebuild the app using solution-index.js for a secure version of the app

About

Validates incoming JSON against a schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published