Giter VIP home page Giter VIP logo

nestjs-chatter-patrol's Introduction

Contributors Forks Stargazers Issues


nestjs-chatter-patrol

Shared NestJS communication sanitation functionality

· Report Bug · Request Feature ·

About The Project

A collection of sanitation functionality for NestJS.

Most functionality follows the opt-out principle. So you need to specifically whitelist stuff.

Another important design decision is to crash loudly, this avoid sanitation errors and issues happening unnoticed and posing a threat to your app´s integrity.

Installation

npm i @beuluis/nestjs-chatter-patrol

Unstable installation

The next dist-tag is kept in sync with the latest commit on main. So this contains always the latest changes but is highly unstable.

npm i @beuluis/nestjs-chatter-patrol@next

Usage

const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new SanitizeInterceptor());

With custom logger:

@Module({
    providers: [
        {
            provide: APP_INTERCEPTOR,
            inject: ['OtherLogger'],
            useFactory: (logger: OtherLogger) => new SanitizeInterceptor({ logger: logger }),
        },
    ],
})

Whitelisting

⚠️ Whitelists get applied based on what the find methods matches first.

As example we use this config:

new SanitizeInterceptor({
    whitelists: [
        {
            urlPath: '/exampleUrl',
            methods: 'all',
            scope: 'both',
            fields: ['exampleField', { fieldPath: /example/, allowedTags: ['b'] }],
        },
        {
            urlPath: /example/,
            methods: 'all',
            scope: 'both',
            whitelistAllContent: true,
        },
    ],
});
  • curl -X POST -H "Content-Type: application/json" -d '{"exampleField": "value"}' http://example.com/exampleUrl matches the first whitelist and exampleField gets not sanitized
  • curl -X POST -H "Content-Type: application/json" -d '{"exampleOtherField": "value"}' http://example.com/exampleUrl matches the first whitelist and exampleOtherField gets sanitized but b tags are allowed
  • curl -X POST -H "Content-Type: text/plain" -d 'Hello' http://example.com/exampleOtherUrl matches the second whitelist and nothing gets sanitized

Scope

  • Apply whitelist to request. See interceptors.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        scope: 'request',
    }]});
  • Apply whitelist to response. See interceptors.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        scope: 'response',
    }]});
  • Apply whitelist to both. See interceptors.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        scope: 'both',
    }]});

URL path

  • Apply whitelist to /example url path.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        urlPath: '/example',
    }]});
  • Apply whitelist to url paths matching /example/.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        urlPath: /example/,
    }]});

Methods

  • Apply whitelist to GET and POST methods.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        methods: ['GET', 'POST'],
    }]});
  • Apply whitelist to all methods.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        methods: 'all',
    }]});
  • To allow all b tags everywhere.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        sanitizeOptions: {
            allowedTags: ['b'],
        },
    }]});
  • Whitelist every content for matching urlPath and methods.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        whitelistAllContent: true,
    }]});
  • Whitelist the path example.example.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        fields: ['example.example'],
    }]});
  • Whitelist the path matching /example/.

    new SanitizeInterceptor({ whitelists: [{
        ...,
        fields: [/example/],
    }]});
  • Apply sanitizeOptions to field path example.example

    new SanitizeInterceptor({ whitelists: [{
        ...,
        fields: [{
            fieldsPath: 'example.example',
            allowedTags: ['b'],
        }],
    }]});
  • Apply sanitizeOptions to field path matching /example/

    new SanitizeInterceptor({ whitelists: [{
        ...,
        fields: ['example.[].example'],
    }]});

Whitelist field path in array element

Interfaces

SanitizeFieldOptions

  • fieldPath Defines which fields should not be sanitized.
  • ... This interface also extends the option interface of sanitize-html.

Whitelist

  • urlPath Defines which url paths should not be sanitized. You can also use a regex here.
  • methods Defines which http methods should not be sanitized. Use 'all' to whitelist all methods.
  • scope Defines if the whitelist should be applied to the request, response or both

Whitelist with additional field configuration

  • fields Defines which fields should not be sanitized. Can be a string, regex or SanitizeFieldOptions

Whitelist with general sanitization configuration

  • sanitizeOptions Defines which options to be used for sanitization. Uses option interface of sanitize-html.

Whitelist to ignore all content

  • whitelistAllContent Defines if you want to whitelist all content.

SanitizeInterceptorOptions

  • logger Instance of the logger to be used. Defaults to @nestjs/common´s logger
  • logLevel Log level to be used when something unexpected fails. Defaults to 'warn'
  • whitelist Whitelist of paths, methods and fields to be ignored by the interceptor. Uses array of Whitelist

Testing

Normally I would not test third party libs, but since this is such an important building block I follow a different approach to testing.

The test run the interceptor against multiple payloads compiled from known XSS payloads from github. Generally there are test that are probably too much, but hey much helps much. Right? RIGHT?

nestjs-chatter-patrol's People

Contributors

beuluis avatar

Watchers

 avatar

nestjs-chatter-patrol's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency jest-mock-extended to v3.0.5
  • Update dependency @beuluis/eslint-config to v2
  • Update dependency lint-staged to v14
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/github-npm-feature-publish.yml
.github/workflows/github-npm-feature-remove.yml
.github/workflows/npm-next-publish.yml
.github/workflows/npm-production-publish.yml
npm
package.json
  • @types/sanitize-html 2.9.0
  • rxjs ^7.8.1
  • sanitize-html ^2.10.0
  • @beuluis/eslint-config 1.2.3
  • @beuluis/hook-cli 1.2.1
  • @beuluis/prettier-config 2.0.2
  • @nestjs/common 9.4.1
  • @types/jest 29.5.1
  • @types/node 20.2.1
  • eslint 8.39.0
  • fastify 4.17.0
  • husky 8.0.3
  • jest 29.5.0
  • jest-mock-extended 3.0.4
  • lint-staged 13.2.2
  • ts-jest 29.1.0
  • ts-node 10.9.1
  • typescript 5.0.4
  • @nestjs/common ^9
  • fastify ^4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.