From ebdde9ca1901e3b5c5e225dce7377e25ae96d460 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 11 Aug 2023 09:07:21 +0000 Subject: [PATCH] fix(nextjs): Ensure imports are valid relative paths Fixes https://github.com/getsentry/sentry-javascript/issues/8798 In our logic that injects the Sentry config files to be executed, `path.relative()` may return something like `sentry.server.config.js` which is not allowed. Imports from the current directory need to start with './'. This is why we prepend the path with './', which should always again be a valid relative path. --- packages/nextjs/src/config/loaders/wrappingLoader.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/src/config/loaders/wrappingLoader.ts b/packages/nextjs/src/config/loaders/wrappingLoader.ts index f2e07ab6537d..57b913b23ab1 100644 --- a/packages/nextjs/src/config/loaders/wrappingLoader.ts +++ b/packages/nextjs/src/config/loaders/wrappingLoader.ts @@ -96,7 +96,12 @@ export default function wrappingLoader( const sentryConfigImportPath = path .relative(path.dirname(this.resourcePath), sentryConfigFilePath) .replace(/\\/g, '/'); - templateCode = templateCode.replace(/__SENTRY_CONFIG_IMPORT_PATH__/g, sentryConfigImportPath); + + // path.relative() may return something like `sentry.server.config.js` which is not allowed. Imports from the + // current directory need to start with './'.This is why we prepend the path with './', which should always again + // be a valid relative path. + // https://github.com/getsentry/sentry-javascript/issues/8798 + templateCode = templateCode.replace(/__SENTRY_CONFIG_IMPORT_PATH__/g, `./${sentryConfigImportPath}`); } else { // Bail without doing any wrapping this.callback(null, userCode, userModuleSourceMap);