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 useXFrameOptions() composable. #13

Merged
merged 1 commit into from
May 30, 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
33 changes: 33 additions & 0 deletions src/internals/useXFrameOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*****************************************************************************************************************/

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

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

import { type H3Event, setHeader } from 'h3'

import { type XFrameOptions } from './types'

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

export const defaultXFrameOptions = 'SAMEORIGIN'

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

export const useXFrameOptions = (
event: H3Event,
policy: XFrameOptions | boolean = defaultXFrameOptions
) => {
if (typeof policy === 'boolean' && !policy) {
return
}

// If the policy is a boolean, then use the default policy:
if (typeof policy === 'boolean') {
policy = defaultXFrameOptions
}

setHeader(event, 'X-Frame-Options', policy)
}
35 changes: 35 additions & 0 deletions tests/mocks/helmet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import {
defaultXDownloadOptions
} from '../../src/internals/useXDownloadOptions'

import { useXFrameOptions, defaultXFrameOptions } from '../../src/internals/useXFrameOptions'

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

export const helmetHandlers: Handler[] = [
Expand Down Expand Up @@ -385,6 +387,39 @@ export const helmetHandlers: Handler[] = [
handler: eventHandler(async event => {
useXDownloadOptions(event, false)

return {
policy: false
}
})
},
{
method: 'GET',
url: '/helmet/x-frame-options',
handler: eventHandler(async event => {
useXFrameOptions(event, 'SAMEORIGIN')

return {
policy: 'SAMEORIGIN'
}
})
},
{
method: 'GET',
url: '/helmet/default-x-frame-options',
handler: eventHandler(async event => {
useXFrameOptions(event)

return {
policy: defaultXFrameOptions
}
})
},
{
method: 'GET',
url: '/helmet/no-x-frame-options',
handler: eventHandler(async event => {
useXFrameOptions(event, false)

return {
policy: false
}
Expand Down
52 changes: 52 additions & 0 deletions tests/useXFrameOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*****************************************************************************************************************/

// @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 { defaultXFrameOptions, useXFrameOptions } from '../src/internals/useXFrameOptions'

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

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

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

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

it('should set X-Frame-Options on the request', async () => {
const res = await request.get('/helmet/x-frame-options', {
method: 'GET'
})
expect(res.header['x-frame-options']).toEqual('SAMEORIGIN')
})

it('should set X-Frame-Options on the request', async () => {
const res = await request.get('/helmet/default-x-frame-options', {
method: 'GET'
})
expect(res.header['x-frame-options']).toEqual(defaultXFrameOptions)
})

it('should not set X-Frame-Options on the request', async () => {
const res = await request.get('/helmet/no-x-frame-options', {
method: 'GET'
})
expect(res.header['x-frame-options']).toEqual(undefined)
})
})