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(plugin-hooks): support nested PowerShell quoting across plugins (#844)

Priyanshu Agrawal8021d27d517d
diff --git a/.github/workflows/validate-repository-metadata.yml b/.github/workflows/validate-repository-metadata.yml
index f8a9e15eb..1fbd9f887 100644
--- a/.github/workflows/validate-repository-metadata.yml
+++ b/.github/workflows/validate-repository-metadata.yml
@@ -89,6 +89,9 @@ jobs:
POWER_PLATFORM_SKILLS_TELEMETRY_POWER_PAGES_OPTOUT: "1"
POWER_PLATFORM_SKILLS_TELEMETRY_MODEL_APPS_OPTOUT: "1"
POWER_PLATFORM_SKILLS_TELEMETRY_PCF_OPTOUT: "1"
+ POWER_PLATFORM_SKILLS_TELEMETRY_MOBILE_APP_OPTOUT: "1"
+ POWER_PLATFORM_SKILLS_TELEMETRY_POWER_AUTOMATE_OPTOUT: "1"
+ POWER_PLATFORM_SKILLS_TELEMETRY_POWER_APPS_MOBILE_EXTENSION_OPTOUT: "1"
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
@@ -103,3 +106,6 @@ jobs:
- name: test-installer
run: node --test scripts/tests/install.test.js
+
+ - name: test-windows-hook-command-quoting
+ run: node --test scripts/tests/hook-command-quoting.test.cjs
diff --git a/docs/development.md b/docs/development.md
index 810766372..c4eb76955 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -41,6 +41,17 @@ To develop and test plugins locally, follow these steps:
For Process Intelligence, first follow its [setup instructions](../plugins/process-intelligence/README.md#installation),
including the explicit profile and absolute plugin root needed to avoid duplicate workspace MCP loads.
+### Cross-platform hook commands
+
+Hook commands may be launched through nested shells on Windows. Inline JavaScript wrapped in
+double quotes can lose those quotes before `node -e` receives it, causing PowerShell to parse the
+JavaScript itself. Use a single-quoted `node -e` script with any string arguments passed after the
+script and read from `process.argv`; see [issue #788](https://github.com/microsoft/power-platform-skills/issues/788).
+Telemetry-only commands should fail open, but do not suppress failures from validation hooks.
+
+Run `node --test scripts/tests/hook-command-quoting.test.cjs` to exercise affected manifests through
+POSIX shells and, on Windows, direct and nested PowerShell.
+
## Repository Structure
```text
diff --git a/plugins/code-apps/com.github.copilot/hooks/hooks.json b/plugins/code-apps/com.github.copilot/hooks/hooks.json
index b5bd1e216..b4bdbfc9d 100644
--- a/plugins/code-apps/com.github.copilot/hooks/hooks.json
+++ b/plugins/code-apps/com.github.copilot/hooks/hooks.json
@@ -4,7 +4,7 @@
"preToolUse": [
{
"type": "command",
- "command": "node -e \"try { const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root) throw new Error('Plugin root is not set'); require(path.resolve(root,'hooks','set-cli-origin.js')); } catch(error) { console.error('[code-apps] CLI attribution was not applied: '+error.message); }\"",
+ "command": "node -e 'try{const path=require(process.argv[1]);const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)throw new Error(process.argv[3]);process.argv[1]=undefined;require(path.resolve(root,process.argv[2]))}catch(error){console.error(process.argv[4]+error.message)}' node:path hooks/set-cli-origin.js 'Plugin root is not set' '[code-apps] CLI attribution was not applied: '",
"matcher": "bash|powershell",
"timeoutSec": 10
}
diff --git a/plugins/code-apps/hooks.json b/plugins/code-apps/hooks.json
index b5bd1e216..b4bdbfc9d 100644
--- a/plugins/code-apps/hooks.json
+++ b/plugins/code-apps/hooks.json
@@ -4,7 +4,7 @@
"preToolUse": [
{
"type": "command",
- "command": "node -e \"try { const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root) throw new Error('Plugin root is not set'); require(path.resolve(root,'hooks','set-cli-origin.js')); } catch(error) { console.error('[code-apps] CLI attribution was not applied: '+error.message); }\"",
+ "command": "node -e 'try{const path=require(process.argv[1]);const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)throw new Error(process.argv[3]);process.argv[1]=undefined;require(path.resolve(root,process.argv[2]))}catch(error){console.error(process.argv[4]+error.message)}' node:path hooks/set-cli-origin.js 'Plugin root is not set' '[code-apps] CLI attribution was not applied: '",
"matcher": "bash|powershell",
"timeoutSec": 10
}
diff --git a/plugins/code-apps/hooks/hooks.json b/plugins/code-apps/hooks/hooks.json
index 25614c895..46a1765a2 100644
--- a/plugins/code-apps/hooks/hooks.json
+++ b/plugins/code-apps/hooks/hooks.json
@@ -6,7 +6,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"try { const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root) throw new Error('Plugin root is not set'); require(path.resolve(root,'hooks','set-cli-origin.js')); } catch(error) { console.error('[code-apps] CLI attribution was not applied: '+error.message); }\"",
+ "command": "node -e 'try{const path=require(process.argv[1]);const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)throw new Error(process.argv[3]);process.argv[1]=undefined;require(path.resolve(root,process.argv[2]))}catch(error){console.error(process.argv[4]+error.message)}' node:path hooks/set-cli-origin.js 'Plugin root is not set' '[code-apps] CLI attribution was not applied: '",
"timeout": 10
}
]
diff --git a/plugins/code-apps/scripts/tests/cli-origin.test.js b/plugins/code-apps/scripts/tests/cli-origin.test.js
index 1b0afc0a6..9a55748ff 100644
--- a/plugins/code-apps/scripts/tests/cli-origin.test.js
+++ b/plugins/code-apps/scripts/tests/cli-origin.test.js
@@ -236,11 +236,19 @@ test('hook process reads UTF-8 JSON, reports malformed input, and runs from bund
assert.match('Bash', new RegExp(`^(?:${claudeHook.matcher})$`));
for (const tool of ['bash', 'powershell']) assert.match(tool, new RegExp(`^(?:${copilotHook.matcher})$`));
for (const tool of ['Write', 'shell']) assert.doesNotMatch(tool, new RegExp(`^(?:${copilotHook.matcher})$`));
+ // Node's shell:true defaults to cmd.exe on Windows, but these hooks run in
+ // PowerShell there. cmd.exe does not treat single quotes as quoting.
+ const shell = process.platform === 'win32' ? 'powershell.exe' : '/bin/sh';
+ const launch = (command, options) => spawnSync(shell,
+ process.platform === 'win32'
+ ? ['-NoLogo', '-NoProfile', '-Command', command]
+ : ['-c', command],
+ options);
for (const [launcher, data] of [
[claudeHook.hooks[0].command, payload],
[copilotHook.command, copilot('npx --no-install pa app list')],
]) {
- const launched = spawnSync(launcher, { shell: true, input: JSON.stringify(data), env, encoding: 'utf8' });
+ const launched = launch(launcher, { input: JSON.stringify(data), env, encoding: 'utf8' });
assert.equal(launched.status, 0, launched.stderr);
assert.ok(updated(JSON.parse(launched.stdout)).command.endsWith(data.tool_input?.command || data.toolArgs.command));
for (const root of [undefined, path.join(pluginRoot, 'missing-plugin-root')]) {
@@ -248,7 +256,7 @@ test('hook process reads UTF-8 JSON, reports malformed input, and runs from bund
delete badEnv.PLUGIN_ROOT;
delete badEnv.CLAUDE_PLUGIN_ROOT;
if (root) badEnv.PLUGIN_ROOT = root;
- const failed = spawnSync(launcher, { shell: true, input: JSON.stringify(data), env: badEnv, encoding: 'utf8' });
+ const failed = launch(launcher, { input: JSON.stringify(data), env: badEnv, encoding: 'utf8' });
assert.equal(failed.status, 0, failed.stderr);
assert.equal(failed.stdout, '');
assert.match(failed.stderr, /CLI attribution was not applied/);
diff --git a/plugins/mobile-apps/hooks/hooks.json b/plugins/mobile-apps/hooks/hooks.json
index 829ed374c..0e0266710 100644
--- a/plugins/mobile-apps/hooks/hooks.json
+++ b/plugins/mobile-apps/hooks/hooks.json
@@ -6,7 +6,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root)process.exit(0); try{require(path.resolve(root,'hooks','run-telemetry.js')).start('pretool');}catch{}\"",
+ "command": "node -e 'process.on(process.argv[1],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)process.exit(0);try{require(require(process.argv[2]).resolve(root,process.argv[3])).start(process.argv[4])}catch{}' exit node:path hooks/run-telemetry.js pretool",
"timeout": 10
}
]
@@ -17,7 +17,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root)process.exit(0); try{require(path.resolve(root,'hooks','run-telemetry.js')).start('prompt');}catch{}\"",
+ "command": "node -e 'process.on(process.argv[1],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)process.exit(0);try{require(require(process.argv[2]).resolve(root,process.argv[3])).start(process.argv[4])}catch{}' exit node:path hooks/run-telemetry.js prompt",
"timeout": 10
}
]
diff --git a/plugins/mobile-apps/scripts/tests/mobileapp-hook-utils.test.js b/plugins/mobile-apps/scripts/tests/mobileapp-hook-utils.test.js
index 5984b391f..467079740 100644
--- a/plugins/mobile-apps/scripts/tests/mobileapp-hook-utils.test.js
+++ b/plugins/mobile-apps/scripts/tests/mobileapp-hook-utils.test.js
@@ -4,6 +4,8 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
+const os = require('node:os');
+const { spawnSync } = require('node:child_process');
const {
TRACKED_SKILL_NAMES,
@@ -37,13 +39,34 @@ test('discovers every invocable top-level skill without tracking direct-read hel
assert.equal(TRACKED_SKILL_NAMES.includes('add-table-to-offline-profile'), false);
});
-test('hook manifest registers start telemetry surfaces only', () => {
+test('hook manifest registers start telemetry surfaces only', t => {
const manifest = JSON.parse(fs.readFileSync(path.join(PLUGIN_ROOT, 'hooks', 'hooks.json'), 'utf8'));
assert.deepEqual(Object.keys(manifest.hooks).sort(), ['PreToolUse', 'UserPromptSubmit']);
- const pretoolCommand = manifest.hooks.PreToolUse[0].hooks[0].command;
- const promptCommand = manifest.hooks.UserPromptSubmit[0].hooks[0].command;
- assert.match(pretoolCommand, /run-telemetry\.js.+start\('pretool'\)/);
- assert.match(promptCommand, /run-telemetry\.js.+start\('prompt'\)/);
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'mobile hook modes '));
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
+ fs.mkdirSync(path.join(root, 'hooks'));
+ // Substitute only the hook body to verify the registered command calls start
+ // with the correct mode, without PAC enrichment or telemetry transmission.
+ fs.writeFileSync(path.join(root, 'hooks/run-telemetry.js'),
+ 'module.exports = { start(mode) { console.log(JSON.stringify({ mode })); } };');
+ const shell = process.platform === 'win32' ? 'powershell.exe' : '/bin/sh';
+ for (const [event, mode] of [['PreToolUse', 'pretool'], ['UserPromptSubmit', 'prompt']]) {
+ const groups = manifest.hooks[event];
+ assert.equal(groups.length, 1);
+ assert.equal(groups[0].hooks.length, 1);
+ const command = groups[0].hooks[0].command;
+ const args = process.platform === 'win32'
+ ? ['-NoLogo', '-NoProfile', '-Command', command]
+ : ['-c', command];
+ const result = spawnSync(shell, args, {
+ env: { ...process.env, PLUGIN_ROOT: root, CLAUDE_PLUGIN_ROOT: '' },
+ encoding: 'utf8',
+ timeout: 15000,
+ });
+ assert.equal(result.status, 0, result.stderr);
+ assert.equal(result.stderr, '');
+ assert.deepEqual(JSON.parse(result.stdout), { mode });
+ }
});
test('accepts every tracked skill in bare and qualified forms regardless of cwd', () => {
diff --git a/plugins/model-apps/hooks/hooks.json b/plugins/model-apps/hooks/hooks.json
index ed5ed8e2d..8a97775d7 100644
--- a/plugins/model-apps/hooks/hooks.json
+++ b/plugins/model-apps/hooks/hooks.json
@@ -6,7 +6,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','validate-write-safety.js'));\"",
+ "command": "node -e 'const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[3]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/validate-write-safety.js 'PLUGIN_ROOT is not set'",
"timeout": 10
}
]
@@ -16,7 +16,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-skill-pretool-telemetry.js'));\"",
+ "command": "node -e 'process.on(process.argv[3],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[4]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-skill-pretool-telemetry.js exit 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
@@ -28,7 +28,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-skill-posttool-validation.js'));\"",
+ "command": "node -e 'const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[3]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-skill-posttool-validation.js 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
@@ -38,7 +38,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','validate-icon-imports.js'));\"",
+ "command": "node -e 'const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[3]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/validate-icon-imports.js 'PLUGIN_ROOT is not set'",
"timeout": 10
}
]
@@ -49,7 +49,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-user-prompt-telemetry.js'));\"",
+ "command": "node -e 'process.on(process.argv[3],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[4]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-user-prompt-telemetry.js exit 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
diff --git a/plugins/pcf/hooks/hooks.json b/plugins/pcf/hooks/hooks.json
index 2d683efa9..8feff0b0d 100644
--- a/plugins/pcf/hooks/hooks.json
+++ b/plugins/pcf/hooks/hooks.json
@@ -6,7 +6,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','validate-write-safety.js'));\"",
+ "command": "node -e 'const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[3]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/validate-write-safety.js 'PLUGIN_ROOT is not set'",
"timeout": 10
}
]
@@ -16,7 +16,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-skill-pretool-telemetry.js'));\"",
+ "command": "node -e 'process.on(process.argv[3],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[4]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-skill-pretool-telemetry.js exit 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
@@ -27,7 +27,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-user-prompt-telemetry.js'));\"",
+ "command": "node -e 'process.on(process.argv[3],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[4]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-user-prompt-telemetry.js exit 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
diff --git a/plugins/power-apps-mobile-extension/hooks/hooks.json b/plugins/power-apps-mobile-extension/hooks/hooks.json
index 63e8949b8..0c850ea0e 100644
--- a/plugins/power-apps-mobile-extension/hooks/hooks.json
+++ b/plugins/power-apps-mobile-extension/hooks/hooks.json
@@ -6,7 +6,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path');const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)process.exit(0);try{require(path.resolve(root,'hooks','run-telemetry.js')).start('pretool');}catch{}\"",
+ "command": "node -e 'process.on(process.argv[1],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)process.exit(0);try{require(require(process.argv[2]).resolve(root,process.argv[3])).start(process.argv[4])}catch{}' exit node:path hooks/run-telemetry.js pretool",
"timeout": 30
}
]
@@ -17,7 +17,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path');const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)process.exit(0);try{require(path.resolve(root,'hooks','run-telemetry.js')).start('prompt');}catch{}\"",
+ "command": "node -e 'process.on(process.argv[1],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root)process.exit(0);try{require(require(process.argv[2]).resolve(root,process.argv[3])).start(process.argv[4])}catch{}' exit node:path hooks/run-telemetry.js prompt",
"timeout": 30
}
]
diff --git a/plugins/power-apps-mobile-extension/scripts/tests/telemetry.test.js b/plugins/power-apps-mobile-extension/scripts/tests/telemetry.test.js
index c2347e2a8..96d416c8a 100644
--- a/plugins/power-apps-mobile-extension/scripts/tests/telemetry.test.js
+++ b/plugins/power-apps-mobile-extension/scripts/tests/telemetry.test.js
@@ -528,14 +528,31 @@ test("registered hook commands honor hard-disable even with spaces in plugin roo
fs.writeFileSync(path.join(copiedRoot, "scripts/lib/telemetry/ikey.json"), JSON.stringify({ ...fixtureData.config, disabled: true }));
const hooks = require("../../hooks/hooks.json").hooks;
assert.deepEqual(Object.keys(hooks), ["PreToolUse", "UserPromptSubmit"]);
+ // Restrict PATH to exclude PAC, but preserve the host environment PowerShell
+ // needs to start. An empty environment can hang Windows PowerShell before Node.
+ const shell = process.platform === "win32"
+ ? path.join(process.env.SystemRoot, "System32/WindowsPowerShell/v1.0/powershell.exe")
+ : "/bin/sh";
for (const name of Object.keys(hooks)) {
const command = hooks[name][0].hooks[0].command;
for (const rootVariable of ["PLUGIN_ROOT", "CLAUDE_PLUGIN_ROOT"]) {
- const result = spawnSync(command, {
- shell: true, input: JSON.stringify({ prompt: `/${PLUGIN_NAME}:generate-ppmplugin`, tool_input: { skill: `${PLUGIN_NAME}:generate-ppmplugin` } }),
- env: { PATH: path.dirname(process.execPath), [rootVariable]: copiedRoot, ...fixtureData.env },
- timeout: 3000, encoding: "utf8",
+ const args = process.platform === "win32"
+ ? ["-NoLogo", "-NoProfile", "-Command", command]
+ : ["-c", command];
+ const result = spawnSync(shell, args, {
+ input: JSON.stringify({ prompt: `/${PLUGIN_NAME}:generate-ppmplugin`, tool_input: { skill: `${PLUGIN_NAME}:generate-ppmplugin` } }),
+ env: {
+ ...process.env,
+ PATH: path.dirname(process.execPath),
+ PLUGIN_ROOT: "",
+ CLAUDE_PLUGIN_ROOT: "",
+ [rootVariable]: copiedRoot,
+ ...fixtureData.env,
+ },
+ // Include PowerShell cold-start time, as the shared hook-command suite does.
+ timeout: 15000, encoding: "utf8",
});
+ assert.ifError(result.error);
assert.equal(result.status, 0, result.stderr);
assert.equal(result.stdout, "");
assert.equal(result.stderr, "");
diff --git a/plugins/power-automate/hooks/hooks.json b/plugins/power-automate/hooks/hooks.json
index 8ce3ef672..b573f12e0 100644
--- a/plugins/power-automate/hooks/hooks.json
+++ b/plugins/power-automate/hooks/hooks.json
@@ -6,7 +6,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-skill-pretool-telemetry.js'));\"",
+ "command": "node -e 'process.on(process.argv[3],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[4]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-skill-pretool-telemetry.js exit 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
@@ -17,7 +17,7 @@
"hooks": [
{
"type": "command",
- "command": "node -e \"const path=require('node:path'); const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT; if(!root){console.error('PLUGIN_ROOT is not set'); process.exit(0);} require(path.resolve(root,'hooks','run-user-prompt-telemetry.js'));\"",
+ "command": "node -e 'process.on(process.argv[3],()=>{process.exitCode=0});const root=process.env.PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;if(!root){console.error(process.argv[4]);process.exit(0)}require(require(process.argv[1]).resolve(root,process.argv[2]))' node:path hooks/run-user-prompt-telemetry.js exit 'PLUGIN_ROOT is not set'",
"timeout": 30
}
]
diff --git a/scripts/tests/hook-command-quoting.test.cjs b/scripts/tests/hook-command-quoting.test.cjs
new file mode 100644
index 000000000..66b4851df
--- /dev/null
+++ b/scripts/tests/hook-command-quoting.test.cjs
@@ -0,0 +1,237 @@
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const os = require('node:os');
+const path = require('node:path');
+const { spawnSync } = require('node:child_process');
+
+const REPO_ROOT = path.resolve(__dirname, '../..');
+const PLUGINS = [
+ {
+ name: 'code-apps',
+ manifests: [
+ { file: 'plugins/code-apps/hooks.json', files: ['set-cli-origin.js'] },
+ { file: 'plugins/code-apps/hooks/hooks.json', files: ['set-cli-origin.js'] },
+ { file: 'plugins/code-apps/com.github.copilot/hooks/hooks.json', files: ['set-cli-origin.js'] },
+ ],
+ bestEffortFiles: ['set-cli-origin.js'],
+ },
+ {
+ name: 'mobile-apps',
+ manifests: [
+ { file: 'plugins/mobile-apps/hooks/hooks.json', files: ['run-telemetry.js', 'run-telemetry.js'] },
+ ],
+ telemetryFiles: ['run-telemetry.js'],
+ },
+ {
+ name: 'model-apps',
+ manifests: [
+ {
+ file: 'plugins/model-apps/hooks/hooks.json',
+ files: [
+ 'validate-write-safety.js',
+ 'run-skill-pretool-telemetry.js',
+ 'run-skill-posttool-validation.js',
+ 'validate-icon-imports.js',
+ 'run-user-prompt-telemetry.js',
+ ],
+ },
+ ],
+ telemetryFiles: ['run-skill-pretool-telemetry.js', 'run-user-prompt-telemetry.js'],
+ },
+ {
+ name: 'pcf',
+ manifests: [
+ {
+ file: 'plugins/pcf/hooks/hooks.json',
+ files: ['validate-write-safety.js', 'run-skill-pretool-telemetry.js', 'run-user-prompt-telemetry.js'],
+ },
+ ],
+ telemetryFiles: ['run-skill-pretool-telemetry.js', 'run-user-prompt-telemetry.js'],
+ },
+ {
+ name: 'power-automate',
+ manifests: [
+ {
+ file: 'plugins/power-automate/hooks/hooks.json',
+ files: ['run-skill-pretool-telemetry.js', 'run-user-prompt-telemetry.js'],
+ },
+ ],
+ telemetryFiles: ['run-skill-pretool-telemetry.js', 'run-user-prompt-telemetry.js'],
+ },
+ {
+ name: 'power-apps-mobile-extension',
+ manifests: [
+ {
+ file: 'plugins/power-apps-mobile-extension/hooks/hooks.json',
+ files: ['run-telemetry.js', 'run-telemetry.js'],
+ },
+ ],
+ telemetryFiles: ['run-telemetry.js'],
+ },
+];
+
+const TELEMETRY_OPTOUTS = [
+ 'POWER_PLATFORM_SKILLS_TELEMETRY_MOBILE_APP_OPTOUT',
+ 'POWER_PLATFORM_SKILLS_TELEMETRY_MODEL_APPS_OPTOUT',
+ 'POWER_PLATFORM_SKILLS_TELEMETRY_PCF_OPTOUT',
+ 'POWER_PLATFORM_SKILLS_TELEMETRY_POWER_AUTOMATE_OPTOUT',
+ 'POWER_PLATFORM_SKILLS_TELEMETRY_POWER_APPS_MOBILE_EXTENSION_OPTOUT',
+];
+const INPUT = '{"tool_input":{"skill":"example"}}';
+
+function collectCommands(value, commands = []) {
+ if (Array.isArray(value)) {
+ for (const item of value) collectCommands(item, commands);
+ } else if (value && typeof value === 'object') {
+ if (typeof value.command === 'string') {
+ commands.push(value.command);
+ } else {
+ for (const child of Object.values(value)) collectCommands(child, commands);
+ }
+ }
+ return commands;
+}
+
+function getHookFile(command) {
+ const pathArgument = command.match(/hooks\/([^/'"]+\.js)/)
+ || command.match(/'hooks'\s*,\s*'([^']+\.js)'/);
+ assert.ok(pathArgument, `hook command does not identify a hooks/*.js entry point: ${command}`);
+ return pathArgument[1];
+}
+
+const HOOKS = PLUGINS.flatMap(plugin => plugin.manifests.flatMap(manifest => {
+ const commands = collectCommands(JSON.parse(fs.readFileSync(path.join(REPO_ROOT, manifest.file), 'utf8')));
+ assert.equal(commands.length, manifest.files.length, `${manifest.file}: registered hook count changed`);
+ return commands.map((command, index) => ({
+ plugin: plugin.name,
+ command,
+ file: getHookFile(command),
+ expectedFile: manifest.files[index],
+ telemetry: (plugin.telemetryFiles || []).includes(manifest.files[index]),
+ bestEffort: (plugin.bestEffortFiles || []).includes(manifest.files[index]),
+ }));
+}));
+
+function fixture(t) {
+ const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'plugin hooks '));
+ t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
+ const root = path.join(directory, "plugin's files & scripts");
+ fs.mkdirSync(path.join(root, 'hooks'), { recursive: true });
+ for (const file of new Set(HOOKS.map(hook => hook.expectedFile))) {
+ fs.writeFileSync(path.join(root, 'hooks', file), `
+const path = require('node:path');
+let input = '';
+process.stdin.setEncoding('utf8');
+process.stdin.on('data', chunk => { input += chunk; });
+process.stdin.on('end', () => {
+ console.log(JSON.stringify({ file: path.basename(__filename), input, cwd: process.cwd() }));
+ if (process.env.HOOK_TEST_STATUS) process.exitCode = Number(process.env.HOOK_TEST_STATUS);
+});
+module.exports = { start() {} };
+`);
+ }
+ return { directory, root };
+}
+
+function environment(root, rootVariable, status = '') {
+ const env = {
+ ...process.env,
+ PLUGIN_ROOT: '',
+ CLAUDE_PLUGIN_ROOT: '',
+ HOOK_TEST_STATUS: status,
+ };
+ for (const variable of TELEMETRY_OPTOUTS) env[variable] = '1';
+ env[rootVariable] = root;
+ if (rootVariable === 'PLUGIN_ROOT') env.CLAUDE_PLUGIN_ROOT = path.join(root, 'wrong root');
+ return env;
+}
+
+const runners = process.platform === 'win32' ? [] : [{
+ name: 'POSIX shell',
+ binary: '/bin/sh',
+ args: command => ['-c', command],
+ failureStatus: code => code,
+}];
+const shells = process.platform === 'win32'
+ ? ['powershell.exe', 'pwsh.exe']
+ : (process.env.HOOK_TEST_POWERSHELL ? [process.env.HOOK_TEST_POWERSHELL] : []);
+for (const shell of shells) {
+ for (const nested of [false, true]) {
+ runners.push({
+ name: `${path.basename(shell)} (${nested ? 'nested' : 'direct'})`,
+ binary: shell,
+ args(command) {
+ // The extra -Command "..." layer reproduces the quoting loss reported in #788.
+ const script = nested
+ ? `& '${shell.replaceAll("'", "''")}' -NoLogo -NoProfile -Command "${command}"; exit (Get-Variable LASTEXITCODE -ValueOnly)`
+ : command;
+ return ['-NoLogo', '-NoProfile', '-Command', script];
+ },
+ failureStatus: () => 1,
+ });
+ }
+}
+
+test('all affected manifests register the expected hooks without nested double quotes', () => {
+ assert.ok(HOOKS.length > 0);
+ for (const hook of HOOKS) {
+ assert.equal(hook.file, hook.expectedFile, `${hook.plugin}: hook entry point changed`);
+ assert.ok(!hook.command.includes('"'), `${hook.plugin}: nested double quotes regress #788`);
+ }
+});
+
+for (const runner of runners) {
+ function launch(hook, options) {
+ return spawnSync(runner.binary, runner.args(hook.command), {
+ ...options,
+ encoding: 'utf8',
+ timeout: 15000,
+ });
+ }
+
+ test(`${runner.name}: hook commands preserve stdin, cwd, paths and plugin-root fallback`, t => {
+ const { directory, root } = fixture(t);
+ for (const [index, hook] of HOOKS.entries()) {
+ const rootVariable = index % 2 === 0 ? 'PLUGIN_ROOT' : 'CLAUDE_PLUGIN_ROOT';
+ const result = launch(hook, {
+ cwd: directory,
+ env: environment(root, rootVariable),
+ input: INPUT,
+ });
+ assert.equal(result.status, 0, `${hook.plugin}/${hook.file}: ${result.stderr}`);
+ assert.equal(result.stderr, '');
+ const output = JSON.parse(result.stdout);
+ assert.equal(output.file, hook.expectedFile);
+ assert.equal(output.input, INPUT);
+ assert.equal(fs.realpathSync.native(output.cwd), fs.realpathSync.native(directory));
+ }
+ });
+
+ test(`${runner.name}: telemetry failures fail open and validator failures propagate`, t => {
+ const { directory, root } = fixture(t);
+ for (const hook of HOOKS) {
+ const result = launch(hook, {
+ cwd: directory,
+ env: environment(root, 'PLUGIN_ROOT', '1'),
+ input: '{}',
+ });
+ assert.equal(JSON.parse(result.stdout).file, hook.expectedFile);
+ const expectedStatus = hook.telemetry ? 0 : runner.failureStatus(1);
+ assert.equal(result.status, expectedStatus, `${hook.plugin}/${hook.file}`);
+ }
+ });
+
+ test(`${runner.name}: missing modules do not block telemetry or best-effort hooks`, t => {
+ const { directory, root } = fixture(t);
+ for (const hook of HOOKS) {
+ const result = launch(hook, {
+ cwd: directory,
+ env: environment(path.join(root, 'missing'), 'PLUGIN_ROOT'),
+ input: '{}',
+ });
+ const expectedStatus = hook.telemetry || hook.bestEffort ? 0 : runner.failureStatus(1);
+ assert.equal(result.status, expectedStatus, `${hook.plugin}/${hook.file}: ${result.stderr}`);
+ }
+ });
+}