Validating and Sanitizing API Input

Tutorial 3 of 5

1. Introduction

Goal of the tutorial

This tutorial aims to teach you how to validate and sanitize data in Express with the express-validator package. By ensuring the data your API receives is valid and safe, you can prevent many common security vulnerabilities and errors.

What you will learn

By the end of this tutorial, you'll be able to:
* Understand the importance of data validation and sanitization
* Install and set up express-validator
* Write middleware to validate and sanitize API input
* Handle validation errors

Prerequisites

Before starting, some experience with Node.js and Express.js is beneficial. If you're not familiar with these, consider reading up on them first.

2. Step-by-Step Guide

Concepts Explanation
* Validation is the process of checking if the data meets certain criteria, like being the correct data type, within a certain range, etc.
* Sanitization cleanses the data to ensure it contains no harmful or unnecessary characters, such as JavaScript code that could be used for cross-site scripting (XSS) attacks.

Best Practices and Tips
* Always validate and sanitize data at the entry point of your application.
* Handle validation errors properly and provide useful error messages.
* Sanitize data before using it in your application.

3. Code Examples

Example 1: Basic Validation with express-validator

const { check, validationResult } = require('express-validator');

app.post('/api/user', [
  // username must be an email
  check('email').isEmail(),
  // password must be at least 5 chars long
  check('password').isLength({ min: 5 })
], (req, res) => {
  // Finds the validation errors in this request and wraps them in an object with handy functions
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }

  // If no validation errors, proceed with user creation or other actions
});

This example validates the email and password fields of a POST request to '/api/user'. If there are validation errors, it returns a 400 status with the errors; otherwise, it proceeds to the next middleware.

Example 2: Sanitization with express-validator

const { check, validationResult } = require('express-validator');

app.post('/api/user', [
  // username must be an email
  check('email').isEmail().normalizeEmail(),
  // password must be at least 5 chars long and is hashed
  check('password').isLength({ min: 5 }).trim().escape()
], (req, res) => {
  // Finds the validation errors in this request and wraps them in an object with handy functions
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }

  // If no validation errors, proceed with user creation or other actions
});

This example not only validates but also sanitizes the email and password fields. It normalizes the email address and trims and escapes the password input.

4. Summary

In this tutorial, we've covered how to validate and sanitize input in an Express API using express-validator. We've seen how to set up validation checks in middleware and handle validation errors.

For further learning, consider exploring more express-validator functions and how to customize them. Read the express-validator documentation for more information.

5. Practice Exercises

  1. Exercise: Create an API endpoint for user registration that validates and sanitizes the user's name, email, and password.
  2. Solution: Similar to the examples above, but add validation and sanitization for the 'name'.
  3. Tips: Consider what validation and sanitization might be appropriate for a 'name' field.

  4. Exercise: Add more detailed error messages to the user registration endpoint.

  5. Solution: See the custom error messages section in the express-validator docs.
  6. Tips: Make your error messages user-friendly and informative.

  7. Exercise: Create an API endpoint for a blog post that validates and sanitizes the post's title and content.

  8. Solution: Similar to the examples above, but you are dealing with a 'title' and 'content' field.
  9. Tips: Consider the appropriate length and character restrictions for a blog post title and content.