-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e8a997
commit 0c7e5ab
Showing
6 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* @adonisjs/shield | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
export function noop (_ctx: HttpContextContract) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* @adonisjs/shield | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../adonis-typings/index.ts" /> | ||
|
||
import { XSSOptions } from '@ioc:Adonis/Addons/Shield' | ||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
import { noop } from './noop' | ||
|
||
/** | ||
* A boolean to know if user agent is of IE8 | ||
*/ | ||
function isOldIE (userAgent?: string) { | ||
if (!userAgent) { | ||
return false | ||
} | ||
|
||
const match = /msie\s*(\d{1,2})/i.exec(userAgent) | ||
return match ? parseFloat(match[1]) < 9 : false | ||
} | ||
|
||
/** | ||
* Adds `X-Content-Type-Options` header based upon given | ||
* user options | ||
*/ | ||
export function xssProtection (options: XSSOptions) { | ||
if (!options.enabled) { | ||
return noop | ||
} | ||
|
||
let isBlock = true | ||
if (options.mode === null) { | ||
isBlock = false | ||
} | ||
|
||
let value = '1' | ||
if (isBlock) { | ||
value += '; mode=block' | ||
} | ||
|
||
if (options.reportUri) { | ||
value += `; report=${options.reportUri}` | ||
} | ||
|
||
/** | ||
* Returned when `X-XSS-Protection` is enabled on all browser | ||
*/ | ||
if (options.enableOnOldIE) { | ||
return function xssProtectionMiddlewareFn ({ response }: HttpContextContract) { | ||
response.header('X-XSS-Protection', value) | ||
} | ||
} | ||
|
||
/** | ||
* Returned when disabled for IE < 9 needs | ||
*/ | ||
return function xssProtectionMiddlewareFn ({ request, response }: HttpContextContract) { | ||
if (isOldIE(request.header('user-agent'))) { | ||
response.header('X-XSS-Protection', '0') | ||
} else { | ||
response.header('X-XSS-Protection', value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* @adonisjs/shield | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import test from 'japa' | ||
import { HttpContext } from '@adonisjs/http-server/build/standalone' | ||
import { xssProtection } from '../src/xssProtection' | ||
|
||
test.group('Xss Protection', () => { | ||
test('return noop function when enabled is false', (assert) => { | ||
const middlewareFn = xssProtection({ enabled: false }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.isUndefined(ctx.response.getHeader('X-XSS-Protection')) | ||
}) | ||
|
||
test('set X-XSS-Protection header', (assert) => { | ||
const middlewareFn = xssProtection({ enabled: true }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.equal(ctx.response.getHeader('X-XSS-Protection'), '1; mode=block') | ||
}) | ||
|
||
test('disable block mode', (assert) => { | ||
const middlewareFn = xssProtection({ enabled: true, mode: null }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.equal(ctx.response.getHeader('X-XSS-Protection'), '1') | ||
}) | ||
|
||
test('set report uri', (assert) => { | ||
const middlewareFn = xssProtection({ enabled: true, reportUri: '/' }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.equal(ctx.response.getHeader('X-XSS-Protection'), '1; mode=block; report=/') | ||
}) | ||
}) |