Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added onResponseHelmetMiddleware() middleware utility. #23

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/onResponseHelmetMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*****************************************************************************************************************/

// @author Michael Roberts <michael@observerly.com>
// @package @observerly/nitro-helmet
// @license Copyright © 2021-2023 observerly

/*****************************************************************************************************************/

import { defineResponseMiddleware } from 'h3'

import { type H3HelmetOptions } from './internals/types'

import { defaultOptions } from './internals/defaultOptions'

import { useContentSecurityPolicy } from './internals/useContentSecurityPolicy'
import { useCrossOriginEmbedderPolicy } from './internals/useCrossOriginEmbedderPolicy'
import { useCrossOriginOpenerPolicy } from './internals/useCrossOriginOpenerPolicy'
import { useCrossOriginResourcePolicy } from './internals/useCrossOriginResourcePolicy'
import { useOriginAgentCluster } from './internals/useOriginAgentCluster'
import { useReferrerPolicy } from './internals/useReferrerPolicy'
import { useStrictTransportSecurity } from './internals/useStrictTransportSecurity'
import { useXContentTypeOptions } from './internals/useXContentTypeOptions'
import { useXDNSPrefetchControl } from './internals/useXDNSPrefetchControl'
import { useXDownloadOptions } from './internals/useXDownloadOptions'
import { useXFrameOptions } from './internals/useXFrameOptions'
import { useXPermittedCrossDomainPolicies } from './internals/useXPermittedCrossDomainPolicies'
import { useXXSSProtection } from './internals/useXXSSProtection'

/*****************************************************************************************************************/

export const onResponseHelmetMiddleware = (options?: H3HelmetOptions) =>
defineResponseMiddleware(event => {
// Merge the user provided options with the default options:
const opts = { ...defaultOptions, ...options }

// Append security helmet headers:
useContentSecurityPolicy(event, opts.contentSecurityPolicy)
useCrossOriginEmbedderPolicy(event, opts.crossOriginEmbedderPolicy)
useCrossOriginOpenerPolicy(event, opts.crossOriginOpenerPolicy)
useCrossOriginResourcePolicy(event, opts.crossOriginResourcePolicy)
useOriginAgentCluster(event, opts.originAgentCluster)
useReferrerPolicy(event, opts.referrerPolicy)
useStrictTransportSecurity(event, opts.strictTransportSecurity)
useXContentTypeOptions(event, opts.xContentTypeOptions)
useXDNSPrefetchControl(event, opts.xDNSPrefetchControl)
useXDownloadOptions(event, opts.xDownloadOptions)
useXFrameOptions(event, opts.xFrameOptions)
useXPermittedCrossDomainPolicies(event, opts.xPermittedCrossDomainPolicies)
useXXSSProtection(event, opts.xXSSProtection)
})

/*****************************************************************************************************************/

// Expose the onResponseHelmetMiddleware as helmet alias for ease of use:
export const helmet = onResponseHelmetMiddleware

/*****************************************************************************************************************/
4 changes: 3 additions & 1 deletion tests/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

import { helmetHandlers } from './helmet'

import { helmetOnResponseMiddlewareHandlers } from './helmetOnResponseMiddleware'

import { type Handler } from '../shared/handler'

/*****************************************************************************************************************/

export const handlers: Handler[] = [...helmetHandlers]
export const handlers: Handler[] = [...helmetHandlers, ...helmetOnResponseMiddlewareHandlers]

/*****************************************************************************************************************/
30 changes: 30 additions & 0 deletions tests/mocks/helmetOnResponseMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*****************************************************************************************************************/

// @author Michael Roberts <michael@observerly.com>
// @package @observerly/nitro-helmet
// @license Copyright © 2021-2023 observerly

/*****************************************************************************************************************/

import { eventHandler } from 'h3'

import { type Handler } from '../shared/handler'

import { helmet } from '../../src/onResponseHelmetMiddleware'

/*****************************************************************************************************************/

export const helmetOnResponseMiddlewareHandlers: Handler[] = [
{
method: 'GET',
url: '/on-response-middleware/helmet',
handler: eventHandler({
onBeforeResponse: [helmet()],
async handler(event) {
return 'Hello Helmet!'
}
})
}
]

/*****************************************************************************************************************/
50 changes: 50 additions & 0 deletions tests/onResponseHelmetMiddleware.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*****************************************************************************************************************/

// @author Michael Roberts <michael@observerly.com>
// @package @observerly/nitro-helmet
// @license Copyright © 2021-2023 observerly

/*****************************************************************************************************************/

import { beforeEach, describe, expect, it } from 'vitest'

import supertest, { type SuperTest, type Test } from 'supertest'

import { toNodeListener } from 'h3'

import { server } from './utilities/server'

import { helmetEventHandler } from '../src'

/*****************************************************************************************************************/

describe('helmetEventHandler', () => {
let request: SuperTest<Test>

beforeEach(() => {
request = supertest(toNodeListener(server))
})

it('should be defined', () => {
expect(helmetEventHandler).toBeDefined()
})

it('should set all security headers correctly on the request', async () => {
const res = await request.get('/on-response-middleware/helmet', {
method: 'GET'
})
expect(res.header['content-security-policy']).toBeDefined()
expect(res.header['cross-origin-embedder-policy']).toBeDefined()
expect(res.header['cross-origin-opener-policy']).toBeDefined()
expect(res.header['cross-origin-resource-policy']).toBeDefined()
expect(res.header['origin-agent-cluster']).toBeDefined()
expect(res.header['referrer-policy']).toBeDefined()
expect(res.header['strict-transport-security']).toBeDefined()
expect(res.header['x-content-type-options']).toBeDefined()
expect(res.header['x-dns-prefetch-control']).toBeDefined()
expect(res.header['x-download-options']).toBeDefined()
expect(res.header['x-frame-options']).toBeDefined()
expect(res.header['x-permitted-cross-domain-policies']).toBeDefined()
expect(res.header['x-xss-protection']).toBeDefined()
})
})