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

guarddog

v1.0.2

Published

API url and body json validator based on set of expected API calls,data types and formats. The format can be given by the user in a json.

Downloads

2

Readme

Guard Dog -v1.0.0

enter image description here npm install guarddog [https://www.npmjs.com/package/guarddog][8]

A middleware mainly for expressjs to validate and filter out unwanted API requests and, to define an expected schema with data types for JSON objects.

Architecture

Node.JS APIs are mostly designed to receive http requests with JSON objects. Almost always there is an expected object schema for that particular API.

Note:

  • Even though the API is secured using a tokening mechanism , attacker can obtain a token , he can inject the server code with unnecessary API requests .
  • Attacker can do minor changes to JSON payloads and attack directly in to your code and it may give valid results from the backend.
  • Running unexpected objects all over the application can cause unhandled exceptions and in unexpected places

Example

Expected object :- { credentials: { username: "John" , password :"1234"}}

Actual Object { cred: { username: "John" , password :"1234"}}

var username = object.credentials.username ; This code will return an error saying, cannot read property username of null.

How to use

1. Install the NPM package Guarddog

You can install the npm using following command in your project,

[npm install --save guarddog][8]

As a prerequisite ,a body parser has to be used. If you haven't any, you can install by npm install --save body-parser


2. Require Guarddog in your project

var bodyParser = require('body-parser'); var guardDog=require('guarddog');


3. Define a strategy for Guarddog

guardDog.setStratergyJson(stratergyJson);

stratergyJson should have 2 major properties.

  • apis : expected api schema
  • onFail: fail event ( this function will be called in case of a mismatch)

Sample object :

var stratergyJson={

onFail:function(msg){
    console.log("Failure : ",msg)
},
apis:{
    "/user/token":{
        POST:{
            body:[{
                    "credentials":{
                        "username":"string",
                        "password":"string"
                    }
                },
                {
                    "details":{
                        "age":"number",
                        "address":"string",
                        "code":"number",
                        "hobbies":"array"
                    }
                }

            ],
          

        }
    }
}

};

Here in this explanation , multiple body schemes are passed to body property of the "/user/token" api. Such scenarios you can use an object array to define multiple objects.

Data types: Datatype should be given as values for the leaf nodes of json object. In above example, username is an string , age is a number and hobbies is an array. list of supporting data types are

  • string
  • number
  • array

Method types: currently Guarddog supports below methods only

  • GET
  • POST
  • PUT
  • DELETE
  • PATCH

3. Put the guarddog to guard

Here as a prerequisite ,stratergy should be set and the request should be parsed by a body parser as a must before it reaches the guarddog middleware . (since guarddog expects a body property in the request)

guardDog.setStratergyJson(stratergyJson);

app.use(bodyParser.json()); app.user(guardDog.guard)

Please feel free to point out issues in [github issues of guarddog][7] . Gurddog 2.0.0 is under development and will be released soon. Author

Don Rajitha Dissanayake [email protected] [7]:https://github.com/rajithadon/guarddog/issues [8]: https://www.npmjs.com/package/guarddog