From f54ee5bf015c78f3d4b70668d8a02f579139dbd5 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 9 Nov 2021 19:15:56 +0100 Subject: [PATCH 1/2] run middleware parser handler only for middleware modules --- .../build/webpack/plugins/middleware-plugin.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/next/build/webpack/plugins/middleware-plugin.ts b/packages/next/build/webpack/plugins/middleware-plugin.ts index a992c1a5dcceeee..335b4520ac7f191 100644 --- a/packages/next/build/webpack/plugins/middleware-plugin.ts +++ b/packages/next/build/webpack/plugins/middleware-plugin.ts @@ -205,7 +205,12 @@ export default class MiddlewarePlugin { }) const handler = (parser: webpack5.javascript.JavascriptParser) => { + const isMiddlewareModule = () => + parser.state.module && parser.state.module.layer === 'middleware' + const wrapExpression = (expr: any) => { + if (!isMiddlewareModule()) return + if (dev) { const dep1 = new wp.dependencies.ConstDependency( '__next_eval__(function() { return ', @@ -242,10 +247,14 @@ export default class MiddlewarePlugin { } const expressionHandler = () => { + if (!isMiddlewareModule()) return + wp.optimize.InnerGraph.onUsage(parser.state, flagModule) } const ignore = () => { + if (!isMiddlewareModule()) return + return true } @@ -282,12 +291,7 @@ export default class MiddlewarePlugin { .tap(PLUGIN_NAME, ignore) const memberChainHandler = (_expr: any, members: string[]) => { - if ( - !parser.state.module || - parser.state.module.layer !== 'middleware' - ) { - return - } + if (!isMiddlewareModule()) return if (members.length >= 2 && members[0] === 'env') { const envName = members[1] From 4809737831dabb2b477eeda3c2416e9c79193282 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 10 Nov 2021 15:33:18 +0100 Subject: [PATCH 2/2] add test case --- .../integration/middleware/with-eval/lib/utils.js | 5 +++++ .../middleware/with-eval/pages/index.js | 15 +++++++++++++++ .../middleware/with-eval/test/index.test.js | 11 ++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/integration/middleware/with-eval/pages/index.js diff --git a/test/integration/middleware/with-eval/lib/utils.js b/test/integration/middleware/with-eval/lib/utils.js index 3c17949f4c9f05f..60fdfbe824117ba 100644 --- a/test/integration/middleware/with-eval/lib/utils.js +++ b/test/integration/middleware/with-eval/lib/utils.js @@ -6,3 +6,8 @@ export async function usingEval() { export async function notUsingEval() { return { value: 100 } } + +export function usingEvalSync() { + // eslint-disable-next-line no-eval + return { value: eval('100') } +} diff --git a/test/integration/middleware/with-eval/pages/index.js b/test/integration/middleware/with-eval/pages/index.js new file mode 100644 index 000000000000000..e9e193bcad67e0e --- /dev/null +++ b/test/integration/middleware/with-eval/pages/index.js @@ -0,0 +1,15 @@ +import { usingEvalSync, usingEval } from '../lib/utils' + +export async function getServerSideProps() { + return { + props: await usingEval(), + } +} + +export default function Page(props) { + return ( +
+ {props.value} and {usingEvalSync().value} +
+ ) +} diff --git a/test/integration/middleware/with-eval/test/index.test.js b/test/integration/middleware/with-eval/test/index.test.js index acd86cf95292078..3a5c44757db4adf 100644 --- a/test/integration/middleware/with-eval/test/index.test.js +++ b/test/integration/middleware/with-eval/test/index.test.js @@ -8,6 +8,7 @@ import { killApp, launchApp, nextBuild, + renderViaHTTP, waitFor, } from 'next-test-utils' @@ -45,7 +46,8 @@ describe('Middleware usage of dynamic code evaluation', () => { expect(output).toContain(DYNAMIC_CODE_ERROR) expect(output).toContain('DynamicCodeEvaluationWarning') expect(output).toContain('pages/_middleware') - expect(output).toContain('lib/utils.js') + // TODO check why that has a backslash on windows + expect(output).toMatch(/lib[\\/]utils\.js/) expect(output).toContain('usingEval') expect(stripAnsi(output)).toContain("value: eval('100')") }) @@ -57,6 +59,13 @@ describe('Middleware usage of dynamic code evaluation', () => { expect(json.value).toEqual(100) expect(output).not.toContain(DYNAMIC_CODE_ERROR) }) + + it('does not has problems with eval in page or server code', async () => { + const html = await renderViaHTTP(context.appPort, `/`) + expect(html).toMatch(/>100 and 100<\//) + await waitFor(500) + expect(output).not.toContain(DYNAMIC_CODE_ERROR) + }) }) describe('production mode', () => {