Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

Fix/power pages toctou race (#696)

Tharun-Varshan-S02cb19138e7a
diff --git a/plugins/power-pages/scripts/provision-template-site.js b/plugins/power-pages/scripts/provision-template-site.js
index 7c1e1af57..320a1844a 100644
--- a/plugins/power-pages/scripts/provision-template-site.js
+++ b/plugins/power-pages/scripts/provision-template-site.js
@@ -75,13 +75,40 @@ function copyMissingTemplateFiles(sourcePath, clonedPath, fsImpl = fs) {
}
if (sourceStat.isDirectory()) {
- if (fsImpl.existsSync(clonedEntryPath)) {
- const clonedStat = fsImpl.lstatSync(clonedEntryPath);
- if (clonedStat.isSymbolicLink() || !clonedStat.isDirectory()) {
- throw new Error(`Clone path conflicts with template directory: ${relativePath}`);
+ const MAX_DIR_CREATION_ATTEMPTS = 3;
+ let created = false;
+ let lastError = null;
+ for (let attempt = 1; attempt <= MAX_DIR_CREATION_ATTEMPTS; attempt++) {
+ try {
+ // Use an atomic mkdir rather than a separate existence check to avoid a TOCTOU race.
+ fsImpl.mkdirSync(clonedEntryPath, { recursive: false });
+ created = true;
+ break;
+ } catch (err) {
+ lastError = err;
+ if (err.code !== 'EEXIST' && err.code !== 'ENOTDIR' && err.code !== 'ENOENT') throw err;
+ try {
+ const clonedStat = fsImpl.lstatSync(clonedEntryPath);
+ if (clonedStat.isSymbolicLink() || !clonedStat.isDirectory()) {
+ throw new Error(`Clone path conflicts with template directory: ${relativePath}`);
+ }
+ created = true;
+ break;
+ } catch (lstatErr) {
+ if (lstatErr.code === 'ENOENT') {
+ // The conflicting path vanished before we could stat it. Retry.
+ lastError = lstatErr;
+ continue;
+ }
+ if (lstatErr.code === 'ENOTDIR') {
+ throw new Error(`Clone path conflicts with template directory: ${relativePath}`);
+ }
+ throw lstatErr;
+ }
}
- } else {
- fsImpl.mkdirSync(clonedEntryPath, { recursive: true });
+ }
+ if (!created) {
+ throw new Error(`Failed to create directory after ${MAX_DIR_CREATION_ATTEMPTS} attempts: ${relativePath} (last error: ${lastError.code})`);
}
queue.push(relativePath);
continue;
diff --git a/plugins/power-pages/scripts/tests/provision-template-site.test.js b/plugins/power-pages/scripts/tests/provision-template-site.test.js
index 42c4e9925..038ceeb19 100644
--- a/plugins/power-pages/scripts/tests/provision-template-site.test.js
+++ b/plugins/power-pages/scripts/tests/provision-template-site.test.js
@@ -1089,3 +1089,207 @@ test('runNpm invokes the Windows npm.cmd shim through cmd.exe without a command
assert.equal(calls[0][2].cwd, '/tmp/site');
assert.equal(calls[0][2].shell, false);
});
+
+test('copyMissingTemplateFiles resolves check-then-act TOCTOU when another process creates the directory', (t) => {
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = path.join(dir, 'source');
+ const cloned = path.join(dir, 'cloned');
+ fs.mkdirSync(path.join(source, 'nested'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+
+ const customFs = {
+ ...fs,
+ mkdirSync(targetPath, options) {
+ // Simulate another process creating the directory immediately before this call.
+ fs.mkdirSync(targetPath, options);
+ // The original non-recursive mkdir now receives EEXIST, as it would in the race.
+ return fs.mkdirSync(targetPath, options);
+ }
+ };
+
+ const restored = copyMissingTemplateFiles(source, cloned, customFs);
+ assert.deepEqual(restored, []);
+ assert.equal(fs.existsSync(path.join(cloned, 'nested')), true);
+});
+
+test('copyMissingTemplateFiles rejects when a file exists where a directory is expected', (t) => {
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = path.join(dir, 'source');
+ const cloned = path.join(dir, 'cloned');
+ fs.mkdirSync(path.join(source, 'conflict-dir'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+ fs.writeFileSync(path.join(cloned, 'conflict-dir'), 'this is a file');
+
+ assert.throws(
+ () => copyMissingTemplateFiles(source, cloned, fs),
+ /Clone path conflicts with template directory: conflict-dir/
+ );
+});
+
+
+test('copyMissingTemplateFiles maps ENOTDIR to conflict error during race condition', (t) => {
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = path.join(dir, 'source');
+ const cloned = path.join(dir, 'cloned');
+ fs.mkdirSync(path.join(source, 'nested'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+
+ const customFs = {
+ ...fs,
+ mkdirSync(targetPath, options) {
+ if (targetPath.includes('cloned') && targetPath.endsWith('nested')) {
+ const err = new Error('ENOTDIR');
+ err.code = 'ENOTDIR';
+ throw err;
+ }
+ return fs.mkdirSync(targetPath, options);
+ },
+ lstatSync(targetPath) {
+ if (targetPath.includes('cloned') && targetPath.endsWith('nested')) {
+ const err = new Error('ENOTDIR');
+ err.code = 'ENOTDIR';
+ throw err;
+ }
+ return fs.lstatSync(targetPath);
+ }
+ };
+
+ assert.throws(
+ () => copyMissingTemplateFiles(source, cloned, customFs),
+ /Clone path conflicts with template directory: nested/
+ );
+});
+
+test('copyMissingTemplateFiles rejects when a symlink exists where a directory is expected', (t) => {
+ if (process.platform === 'win32') {
+ t.skip('Symlinks require privileges on Windows');
+ return;
+ }
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = require('path').join(dir, 'source');
+ const cloned = require('path').join(dir, 'cloned');
+ fs.mkdirSync(require('path').join(source, 'conflict-dir'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+ fs.symlinkSync(require('path').join(cloned, 'dummy'), require('path').join(cloned, 'conflict-dir'));
+
+ assert.throws(
+ () => copyMissingTemplateFiles(source, cloned, fs),
+ /Clone path conflicts with template directory: conflict-dir/
+ );
+});
+
+test('copyMissingTemplateFiles rejects when a symlink to an existing directory exists where a directory is expected', (t) => {
+ if (process.platform === 'win32') {
+ t.skip('Symlinks require privileges on Windows');
+ return;
+ }
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = require('path').join(dir, 'source');
+ const cloned = require('path').join(dir, 'cloned');
+ fs.mkdirSync(require('path').join(source, 'conflict-dir'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+
+ const targetDir = require('path').join(dir, 'target');
+ fs.mkdirSync(targetDir, { recursive: true });
+
+ fs.symlinkSync(targetDir, require('path').join(cloned, 'conflict-dir'));
+
+ assert.throws(
+ () => copyMissingTemplateFiles(source, cloned, fs),
+ /Clone path conflicts with template directory: conflict-dir/
+ );
+});
+
+test('copyMissingTemplateFiles retries when lstat throws ENOENT and succeeds on retry', (t) => {
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = require('path').join(dir, 'source');
+ const cloned = require('path').join(dir, 'cloned');
+ fs.mkdirSync(require('path').join(source, 'retry-dir'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+
+ let attempts = 0;
+ const customFs = {
+ ...fs,
+ mkdirSync(targetPath, options) {
+ attempts++;
+ if (attempts === 1) {
+ const err = new Error('EEXIST');
+ err.code = 'EEXIST';
+ throw err;
+ }
+ return fs.mkdirSync(targetPath, options);
+ },
+ lstatSync(targetPath) {
+ if (attempts === 1 && targetPath.includes('cloned')) {
+ const err = new Error('ENOENT');
+ err.code = 'ENOENT';
+ throw err;
+ }
+ return fs.lstatSync(targetPath);
+ }
+ };
+
+ const restored = copyMissingTemplateFiles(source, cloned, customFs);
+ assert.deepEqual(restored, []);
+ assert.equal(attempts, 2);
+});
+
+test('copyMissingTemplateFiles throws after exhausted retries for ENOENT', (t) => {
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = require('path').join(dir, 'source');
+ const cloned = require('path').join(dir, 'cloned');
+ fs.mkdirSync(require('path').join(source, 'fail-dir'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+
+ const customFs = {
+ ...fs,
+ mkdirSync(targetPath, options) {
+ const err = new Error('EEXIST');
+ err.code = 'EEXIST';
+ throw err;
+ },
+ lstatSync(targetPath) {
+ if (targetPath.includes('cloned')) {
+ const err = new Error('ENOENT');
+ err.code = 'ENOENT';
+ throw err;
+ }
+ return fs.lstatSync(targetPath);
+ }
+ };
+
+ assert.throws(
+ () => copyMissingTemplateFiles(source, cloned, customFs),
+ /Failed to create directory after 3 attempts: fail-dir \(last error: ENOENT\)/
+ );
+});
+
+test('copyMissingTemplateFiles propagates EACCES unchanged', (t) => {
+ const dir = tempDir();
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const source = require('path').join(dir, 'source');
+ const cloned = require('path').join(dir, 'cloned');
+ fs.mkdirSync(require('path').join(source, 'err-dir'), { recursive: true });
+ fs.mkdirSync(cloned, { recursive: true });
+
+ const customFs = {
+ ...fs,
+ mkdirSync(targetPath, options) {
+ const err = new Error('EACCES');
+ err.code = 'EACCES';
+ throw err;
+ }
+ };
+
+ assert.throws(
+ () => copyMissingTemplateFiles(source, cloned, customFs),
+ { code: 'EACCES' }
+ );
+});