koa-safe-redirect
Version:
safe redirect middleware for koa 2.x
142 lines (102 loc) • 2.64 kB
Markdown
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]
[![npm download month][download-month-image]][download-url]
[]: https://img.shields.io/npm/v/koa-safe-redirect.svg?style=flat-square
[]: https://npmjs.org/package/koa-safe-redirect
[]: https://img.shields.io/travis/forthedamn/koa-safe-redirect.svg?style=flat-square
[]: https://travis-ci.org/forthedamn/koa-safe-redirect
[]: https://codecov.io/gh/forthedamn/koa-safe-redirect/branch/master/graph/badge.svg
[]: https://codecov.io/gh/forthedamn/koa-safe-redirect
[]: https://img.shields.io/npm/dt/koa-safe-redirect.svg
[]: https://npmjs.org/package/koa-safe-redirect
[]: https://img.shields.io/npm/dm/koa-safe-redirect.svg
[]: https://npmjs.org/package/koa-safe-redirect
safe redirect middleware for koa 2.x
---
```
npm install koa-safe-redirect --save
```
```
// ./config/default.js
module.exports = {
urlWhiteList: [
/**
* allow the hostname end with 'github.com'
* like 'https://github.com/**'
*/
/github\.com$/
]
}
```
```
const Koa = require('koa');
const safeRedirect = require('koa-safe-redirect')('urlWhiteList');
const app = new Koa();
app
.use(safeRedirect)
.use(async function (ctx, next) {
// will get 403
ctx.redirect('https://www.test.com');
return await next();
});
```
---
Safe redirect get white list from ./config/{NODE_ENV}.js,same strategy with [node-config](https://github.com/lorenwest/node-config)
If config whitelist is empty or not an array,safe redirect will do nothing
```
module.exports = {
urlWhiteList: [
/**
* allow the hostname end with 'github.com'
* like 'https://xxx.github.com/**'
*/
/github\.com$/
]
}
```
```
module.exports = {
urlWhiteList: [
/**
* only allow the hostname equal 'github.com'
* like 'https://github.com/**'
*/
'github.com'
]
}
```
Default config name is 'whiteList'
```
// 1. config
module.exports = {
'whiteList': [
...
]
}
// 2. init middleware
const safeRedirect = require('koa-safe-redirect')();
```
```
// 1. config
module.exports = {
'xxx': [
...
]
}
// 2. init middleware
const safeRedirect = require('koa-safe-redirect')('xxx');
```