From 9d26803b656e53a084d5ab48a563f4eef20fe5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Raffray?= Date: Sat, 28 Oct 2023 19:01:54 +0200 Subject: [PATCH 1/2] modify execution order of nitro plugins - put our security plugins last - allows to insert nonces after all external scripts have been inserted --- src/module.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/module.ts b/src/module.ts index 015d6315..d60f43e1 100644 --- a/src/module.ts +++ b/src/module.ts @@ -285,4 +285,29 @@ const registerSecurityNitroPlugins = ( ) } }) + + // Make sure our nitro plugins will be applied last + // After all other third-party modules that might have loaded their own nitro plugins + nuxt.hook('nitro:init', nitro => { + const securityPluginsPrefix = normalize( + fileURLToPath( + new URL('./runtime/nitro/plugins', import.meta.url) + ) + ) + nitro.options.plugins.sort((a, b) => { + if (a.startsWith(securityPluginsPrefix)) { + if (b.startsWith(securityPluginsPrefix)) { + return 0 + } else { + return 1 + } + } else { + if (b.startsWith(securityPluginsPrefix)) { + return -1 + } else { + return 0 + } + } + }) + }) } From 076f819d43bc2d07c15e6f027adde8e35b6ac391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Raffray?= Date: Sun, 29 Oct 2023 12:18:16 +0100 Subject: [PATCH 2/2] also reorders plugins in SSG mode --- src/module.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/module.ts b/src/module.ts index d60f43e1..0c8558d0 100644 --- a/src/module.ts +++ b/src/module.ts @@ -294,6 +294,7 @@ const registerSecurityNitroPlugins = ( new URL('./runtime/nitro/plugins', import.meta.url) ) ) + // SSR: Reorder plugins in Nitro options nitro.options.plugins.sort((a, b) => { if (a.startsWith(securityPluginsPrefix)) { if (b.startsWith(securityPluginsPrefix)) { @@ -309,5 +310,23 @@ const registerSecurityNitroPlugins = ( } } }) + // SSG: Reorder plugins in Nitro hook + nitro.hooks.hook('prerender:config', config => { + config.plugins?.sort((a, b) => { + if (a?.startsWith(securityPluginsPrefix)) { + if (b?.startsWith(securityPluginsPrefix)) { + return 0 + } else { + return 1 + } + } else { + if (b?.startsWith(securityPluginsPrefix)) { + return -1 + } else { + return 0 + } + } + }) + }) }) }