Skip to content

Commit 681811d

Browse files
committed
fix(react-scripts): prevent path traversal in template copy
Add filter and dereference options to fs.copySync in init.js to prevent malicious templates from writing files outside the target app directory. The filter validates that each resolved destination path stays within appPath, blocking path traversal via '../' patterns and symlinks. Signed-off-by: Srikanth Patchava <spatchava@meta.com>
1 parent d64e1b2 commit 681811d

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

  • packages/react-scripts/scripts

packages/react-scripts/scripts/init.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,16 @@ module.exports = function (
232232
// Copy the files for the user
233233
const templateDir = path.join(templatePath, 'template');
234234
if (fs.existsSync(templateDir)) {
235-
fs.copySync(templateDir, appPath);
235+
fs.copySync(templateDir, appPath, {
236+
dereference: true,
237+
filter: (src) => {
238+
// Prevent path traversal: ensure all paths resolve within appPath
239+
const relativePath = path.relative(templateDir, src);
240+
const resolvedDest = path.resolve(appPath, relativePath);
241+
const normalizedAppPath = path.resolve(appPath) + path.sep;
242+
return resolvedDest.startsWith(normalizedAppPath) || resolvedDest === path.resolve(appPath);
243+
},
244+
});
236245
} else {
237246
console.error(
238247
`Could not locate supplied template: ${chalk.green(templateDir)}`

0 commit comments

Comments
 (0)