-
Notifications
You must be signed in to change notification settings - Fork 0
fix: replace koa-helmet #379
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5e366c
fix: replace koa-helmet
csikb 5366159
fix: replace koa-helmet
csikb cdc7208
fix: replace koa-helmet
csikb bc79cc4
test: enhance helmet middleware tests for Koa integration
csikb 8a79eba
fix: replace koa-helmet
csikb 5385686
test: improve helmet middleware tests for Koa
csikb 32fa2f0
refactor: rename helmet middleware import and instance
csikb 1f60e6e
refactor: rename helmet middleware to koaHelmet
csikb 8975d84
test: update index tests for improved clarity
csikb 1aead7c
fix: update PostGraphile plugin import to use default export
csikb 7d2fe89
fix: remove unnecessary return statement in health check route
csikb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,97 @@ | ||
| import helmet from 'koa-helmet' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import helmet from 'helmet' | ||
| import type { Context } from 'koa' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('koa-helmet') | ||
| vi.mock('helmet') | ||
|
|
||
| describe('helmet', () => { | ||
| it('should export helmet', async () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('should export a factory that calls helmet and returns Koa middleware', async () => { | ||
| expect.assertions(3) | ||
|
|
||
| const mockExpressHelmet = vi.fn() | ||
| const mockHelmet = vi.mocked(helmet) | ||
| mockHelmet.mockReturnValue(mockExpressHelmet) | ||
|
|
||
| const { koaHelmet } = await import('./helmet.js') | ||
|
|
||
| const middleware = koaHelmet() | ||
|
|
||
| expect(mockHelmet).toHaveBeenCalledOnce() | ||
| expect(mockHelmet).toHaveBeenCalledWith(undefined) | ||
| expect(middleware).toBeTypeOf('function') | ||
| }) | ||
|
|
||
| it('should call helmet with options when provided', async () => { | ||
| expect.assertions(3) | ||
|
|
||
| const mockExpressHelmet = vi.fn() | ||
| const mockHelmet = vi.mocked(helmet) | ||
| mockHelmet.mockReturnValue(mockExpressHelmet) | ||
|
|
||
| const { koaHelmet } = await import('./helmet.js') | ||
|
|
||
| const options = { contentSecurityPolicy: false } | ||
| const middleware = koaHelmet(options) | ||
|
|
||
| expect(mockHelmet).toHaveBeenCalledOnce() | ||
| expect(mockHelmet).toHaveBeenCalledWith(options) | ||
| expect(middleware).toBeTypeOf('function') | ||
| }) | ||
|
|
||
| it('should call the express helmet middleware and continue to next', async () => { | ||
| expect.assertions(2) | ||
|
|
||
| const { default: actual } = await import('./helmet.js') | ||
| const mockExpressHelmet = vi.fn((_req, _res, next) => { | ||
| next() | ||
| }) | ||
| const mockHelmet = vi.mocked(helmet) | ||
| mockHelmet.mockReturnValue(mockExpressHelmet) | ||
|
|
||
| const { koaHelmet } = await import('./helmet.js') | ||
|
|
||
| const middleware = koaHelmet() | ||
|
|
||
| const ctx = { | ||
| req: {}, | ||
| res: {}, | ||
| } as Context | ||
| const next = vi.fn().mockResolvedValue(undefined) | ||
|
|
||
| await middleware(ctx, next) | ||
|
|
||
| expect(mockExpressHelmet).toHaveBeenCalledWith( | ||
| ctx.req, | ||
| ctx.res, | ||
| expect.any(Function), | ||
| ) | ||
| expect(next).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('should reject promise when express helmet middleware returns error', async () => { | ||
| expect.assertions(2) | ||
|
|
||
| const error = new Error('Helmet error') | ||
| const mockExpressHelmet = vi.fn((_req, _res, callback) => { | ||
| callback(error) | ||
| }) | ||
| const mockHelmet = vi.mocked(helmet) | ||
| mockHelmet.mockReturnValue(mockExpressHelmet) | ||
|
|
||
| const { koaHelmet } = await import('./helmet.js') | ||
|
|
||
| const middleware = koaHelmet() | ||
|
|
||
| const ctx = { | ||
| req: {}, | ||
| res: {}, | ||
| } as Context | ||
| const next = vi.fn() | ||
|
|
||
| expect(helmet.default).toHaveBeenCalledOnce() | ||
| expect(actual).toStrictEqual(helmet.default()) | ||
| await expect(middleware(ctx, next)).rejects.toThrow('Helmet error') | ||
| expect(next).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| import helmet from 'koa-helmet' | ||
| import helmet, { type HelmetOptions } from 'helmet' | ||
| import type { Middleware } from 'koa' | ||
|
|
||
| export default helmet.default() | ||
| /** | ||
| * Koa-compatible Helmet middleware. | ||
| * | ||
| * Accepts the same options helmet() does. | ||
| */ | ||
| export function koaHelmet(options?: HelmetOptions): Middleware { | ||
| const expressHelmet = helmet(options) | ||
| return async (ctx, next) => { | ||
| await new Promise<void>((resolve, reject) => { | ||
| expressHelmet(ctx.req, ctx.res, (err) => { | ||
| if (err) return reject(err) | ||
| resolve() | ||
| }) | ||
| }) | ||
| return next() | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export { default as bodyParser } from './bodyparser.js' | ||
| export { default as compress } from './compress.js' | ||
| export { default as helmet } from './helmet.js' | ||
| export * from './helmet.js' | ||
| export { default as postGraphile } from './postgraphile.js' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.