Home

Awesome

Koa-Protect by Altonotch

Known Vulnerabilities
<a href="https://communityinviter.com/apps/koa-js/koajs" rel="KoaJs Slack Community">KoaJs Slack</a>

Works on Node.js v7 and newer.

The purpose of this module is to provide out-of-box, proactive protection for common security problems, like
SQL injection attacks, XSS attacks, brute force, etc...

This module is not a silver bullet, and is not a substitute for security-minded engineering work. But it can help you to achieve your goals.

Basic usage

npm i koa-protect --save  

With Express

const protect = require('koa-protect')  
const Koa = require('koa')  
const ratelimit = require('koa-ratelimit')
const bodyParser = require('koa-bodyparser')
const redis = require('redis')
const client = redis.createClient()  
  
const app = new Koa()
  
app.use(bodyParser.json())  

const router = require('koa-router')()
  
app.use(protect.koa.sqlInjection({  
    body: true,  
    loggerFunction: console.error  
}))  
  
app.use(protect.koa.xss({  
    body: true,  
    loggerFunction: console.error  
}))  
  
app.use(ratelimit({  
    db: client,  
    duration: 60000,  
    errorMessage: 'Sometimes You Just Have to Slow Down.',  
    id: (ctx) => ctx.ip,  
    headers: {  
        remaining: 'Rate-Limit-Remaining',  
        reset: 'Rate-Limit-Reset',  
        total: 'Rate-Limit-Total'  
    },  
    max: 100  
}))
  
app.use(router.routes())  
app.use(router.allowedMethods());  
  
app.listen(process.env.PORT || 3000, (err) => {  
    if (err) {  
        return console.error('server could not start')  
    }  
    console.log('server is running')  
})  

API

protect.koa.sqlInjection([options])

Returns an Koa middleware, which checks for SQL injections.

protect.koa.xss([options])

Returns an Koa middleware, which checks for XSS attacks.

** The example app is making use of koa ratelimit module (https://github.com/koajs/ratelimit)

ratelimit([options])

protect.koa.headers([options])

The headers object is a reference to the main helmet object exported.
For docs on the options object, please refer to the helmet documentation.

Security Recommendations

As mentioned, this module isn't a silver bullet to solve your security issues completely. The following information is provided to hopefully point you in the right direction for solving other security concerns or alternatives that may be useful based on your budget or scale.

Other Aspects

There are plenty of other areas that you should be concerned about when it comes to security, that this module doesn't cover (yet or won't) for various reasons. Here are a few that are worth researching:

Resources

Dedicated WAF

If you have the resources available (budget or hosting environment), a dedicated WAF (Web Application Firewall) can offer a robust solution to various security issues, such as blocking potential attackers and flagging their activity.