Skip to content

Commit

Permalink
feat: remove runtimeHook option and refactor to context object
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-julien committed Dec 11, 2023
1 parent 6e41f10 commit 1827273
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
10 changes: 7 additions & 3 deletions playground/server/plugins/headers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('nuxt-security:ready', () => {
nitroApp.hooks.callHook('nuxt-security:headers', '/api/runtime-hooks' ,{
contentSecurityPolicy: {
"script-src": ["'self'", "'unsafe-inline'"],
nitroApp.hooks.callHook('nuxt-security:headers',
{
route: '/api/runtime-hooks',
headers: {
contentSecurityPolicy: {
"script-src": ["'self'", "'unsafe-inline'"],
}
}
})
})
Expand Down
16 changes: 7 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,13 @@ export default defineNuxtModule<ModuleOptions>({
})
}


if(nuxt.options.security.runtimeHooks) {
addServerPlugin(resolve(runtimeDir, 'nitro/plugins/00-context'))
addServerHandler({
handler: normalize(
resolve(runtimeDir, 'server/middleware/headers')
)
})
}

addServerPlugin(resolve(runtimeDir, 'nitro/plugins/00-context'))
addServerHandler({
handler: normalize(
resolve(runtimeDir, 'server/middleware/headers')
)
})

const allowedMethodsRestricterConfig = nuxt.options.security
.allowedMethodsRestricter
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro/plugins/00-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OptionKey } from "~/src/module"
export default defineNitroPlugin((nitroApp) => {
const router = createRouter()

nitroApp.hooks.hook('nuxt-security:headers', (route, headersConfig) => {
nitroApp.hooks.hook('nuxt-security:headers', ({route, headers: headersConfig}) => {
const headers: Record<string, string |false > = {}

for (const [header, headerOptions] of Object.entries(headersConfig)) {
Expand Down
15 changes: 10 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export interface ModuleOptions {
nonce: boolean;
removeLoggers: RemoveOptions | false;
ssg: Ssg | false;
/**
* enable runtime nitro hooks to configure some options at runtime
*/
runtimeHooks: boolean;
sri: boolean
}

Expand All @@ -42,7 +38,16 @@ export type NuxtSecurityRouteRules = Pick<ModuleOptions,

declare module 'nitropack' {
interface NitroRuntimeHooks {
'nuxt-security:headers': (route: string, headers: SecurityHeaders) => void
'nuxt-security:headers': (config: {
/**
* The route for which the headers are being configured
*/
route: string,
/**
* The headers configuration for the route
*/
headers: SecurityHeaders
}) => void
'nuxt-security:ready': () => void
}
}
10 changes: 6 additions & 4 deletions test/fixtures/runtime-hooks/server/plugins/headers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export default defineNitroPlugin((nitroApp) => {
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('nuxt-security:ready', () => {
nitroApp.hooks.callHook('nuxt-security:headers', '/api/runtime-hooks' ,{
contentSecurityPolicy: {
"script-src": ["'self'", "'unsafe-inline'", '*.azure.com'],
nitroApp.hooks.callHook('nuxt-security:headers', {
route: '/api/runtime-hooks', headers: {
contentSecurityPolicy: {
"script-src": ["'self'", "'unsafe-inline'", '*.azure.com'],
}
}
})
})
Expand Down

0 comments on commit 1827273

Please sign in to comment.