Unverified Commit 8b858018 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure null bytes in resolved path are handled (#30313)

This ensures we remove null bytes from the resolved path as it isn't valid when using `path`/`fs`. Additional tests have been added to ensure this is handled properly. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/30298
parent 0ab8c7ad
...@@ -432,7 +432,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -432,7 +432,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
// package.json could be needed for resolving e.g. stylis // package.json could be needed for resolving e.g. stylis
// stylis/package.json -> stylis/dist/umd/package.json // stylis/package.json -> stylis/dist/umd/package.json
if (result.includes('node_modules')) { if (result.includes('node_modules')) {
let requestPath = result.replace(/\\/g, '/') let requestPath = result
.replace(/\\/g, '/')
.replace(/\0/g, '')
if ( if (
!nodePath.isAbsolute(request) && !nodePath.isAbsolute(request) &&
...@@ -444,7 +446,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -444,7 +446,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
request.substr(getPkgName(request)?.length || 0) + request.substr(getPkgName(request)?.length || 0) +
nodePath.sep + nodePath.sep +
'package.json' 'package.json'
).replace(/\\/g, '/') )
.replace(/\\/g, '/')
.replace(/\0/g, '')
} }
const rootSeparatorIndex = requestPath.indexOf('/') const rootSeparatorIndex = requestPath.indexOf('/')
...@@ -524,7 +528,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -524,7 +528,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
if (!res) { if (!res) {
throw new Error(`failed to resolve ${request} from ${parent}`) throw new Error(`failed to resolve ${request} from ${parent}`)
} }
return res return res.replace(/\0/g, '')
} }
this.tapfinishModules( this.tapfinishModules(
......
import Link from 'next/link' import Link from 'next/link'
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
try {
let file = 'clear.js'
require('es5-ext/array/#/' + file)
} catch (_) {}
import('nanoid').then((mod) => console.log(mod.nanoid())) import('nanoid').then((mod) => console.log(mod.nanoid()))
} }
......
...@@ -123,7 +123,7 @@ describe('Production Usage', () => { ...@@ -123,7 +123,7 @@ describe('Production Usage', () => {
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
], ],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/], notTests: [/node_modules\/react\/cjs\/react\.development\.js/, /\0/],
}, },
{ {
page: '/client-error', page: '/client-error',
...@@ -139,7 +139,7 @@ describe('Production Usage', () => { ...@@ -139,7 +139,7 @@ describe('Production Usage', () => {
/next\/dist\/pages\/_error\.js/, /next\/dist\/pages\/_error\.js/,
/next\/error\.js/, /next\/error\.js/,
], ],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/], notTests: [/node_modules\/react\/cjs\/react\.development\.js/, /\0/],
}, },
{ {
page: '/dynamic', page: '/dynamic',
...@@ -153,7 +153,7 @@ describe('Production Usage', () => { ...@@ -153,7 +153,7 @@ describe('Production Usage', () => {
/next\/dist\/client\/link\.js/, /next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/], notTests: [/node_modules\/react\/cjs\/react\.development\.js/, /\0/],
}, },
{ {
page: '/index', page: '/index',
...@@ -168,12 +168,14 @@ describe('Production Usage', () => { ...@@ -168,12 +168,14 @@ describe('Production Usage', () => {
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
/node_modules\/nanoid\/index\.js/, /node_modules\/nanoid\/index\.js/,
/node_modules\/nanoid\/url-alphabet\/index\.js/, /node_modules\/nanoid\/url-alphabet\/index\.js/,
/node_modules\/es5-ext\/array\/#\/clear\.js/,
], ],
notTests: [ notTests: [
/node_modules\/react\/cjs\/react\.development\.js/, /node_modules\/react\/cjs\/react\.development\.js/,
/node_modules\/nanoid\/index\.cjs/, /node_modules\/nanoid\/index\.cjs/,
/next\/dist\/pages\/_error\.js/, /next\/dist\/pages\/_error\.js/,
/next\/error\.js/, /next\/error\.js/,
/\0/,
], ],
}, },
{ {
...@@ -188,7 +190,7 @@ describe('Production Usage', () => { ...@@ -188,7 +190,7 @@ describe('Production Usage', () => {
/next\/dist\/client\/router\.js/, /next\/dist\/client\/router\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/], notTests: [/node_modules\/react\/cjs\/react\.development\.js/, /\0/],
}, },
{ {
page: '/next-import', page: '/next-import',
...@@ -202,7 +204,7 @@ describe('Production Usage', () => { ...@@ -202,7 +204,7 @@ describe('Production Usage', () => {
/next\/dist\/client\/link\.js/, /next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/next\/dist\/server\/next\.js/, /next\/dist\/bin/], notTests: [/next\/dist\/server\/next\.js/, /next\/dist\/bin/, /\0/],
}, },
] ]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment