mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-17 05:22:17 +00:00
fix(hooks): escape curly single quotes in desktop-notify PowerShell command
This commit is contained in:
2 files changed
+89
-4
No files matched your search
@@ -68,15 +68,23 @@ function findPowerShell() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Quote a value as a PowerShell single-quoted string literal.
|
||||
* PowerShell also ends single-quoted strings at the typographic quotes
|
||||
* U+2018-U+201B, so those are doubled along with the ASCII apostrophe.
|
||||
* Otherwise a summary with a curly apostrophe breaks the command.
|
||||
*/
|
||||
function quotePowerShellString(value) {
|
||||
return `'${String(value).replace(/['\u2018\u2019\u201A\u201B]/g, '$&$&')}'`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Windows Toast notification via PowerShell BurntToast.
|
||||
* Returns { success: boolean, reason: string|null }.
|
||||
* reason is null on success, or contains error detail on failure.
|
||||
*/
|
||||
function notifyWindows(pwshPath, title, body) {
|
||||
const safeBody = body.replace(/'/g, "''");
|
||||
const safeTitle = title.replace(/'/g, "''");
|
||||
const command = `Import-Module BurntToast; New-BurntToastNotification -Text '${safeTitle}', '${safeBody}'`;
|
||||
const command = `Import-Module BurntToast; New-BurntToastNotification -Text ${quotePowerShellString(title)}, ${quotePowerShellString(body)}`;
|
||||
const result = spawnSync(pwshPath, ['-Command', command],
|
||||
{ stdio: ['ignore', 'pipe', 'pipe'], timeout: 5000 });
|
||||
if (result.status === 0) {
|
||||
@@ -230,7 +238,7 @@ function run(raw) {
|
||||
return raw;
|
||||
}
|
||||
|
||||
module.exports = { run };
|
||||
module.exports = { run, quotePowerShellString };
|
||||
|
||||
// Legacy stdin path (when invoked directly rather than via run-with-flags)
|
||||
if (require.main === module) {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
const { quotePowerShellString } = require('../../scripts/hooks/desktop-notify');
|
||||
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` ✓ ${name}`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log(` ✗ ${name}`);
|
||||
console.log(` Error: ${err.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function findPowerShell() {
|
||||
for (const candidate of ['pwsh', 'powershell']) {
|
||||
const result = spawnSync(candidate, ['-NoProfile', '-Command', 'exit 0'], {
|
||||
stdio: 'ignore',
|
||||
timeout: 10000,
|
||||
});
|
||||
if (result.status === 0) return candidate;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const SAMPLES = [
|
||||
"I've finished",
|
||||
'I\u2019ve finished the task',
|
||||
'Done\u2019; Write-Output INJECTED; \u2019',
|
||||
'\u2018a\u2019 \u201Ab\u201B c\'\u2019',
|
||||
'cost: $(Get-Date) $env:PATH `n',
|
||||
];
|
||||
|
||||
function runTests() {
|
||||
console.log('\n=== Testing desktop-notify.js PowerShell quoting ===\n');
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
(test('doubles ASCII and typographic single quotes', () => {
|
||||
assert.strictEqual(quotePowerShellString("it's"), "'it''s'");
|
||||
assert.strictEqual(quotePowerShellString('it\u2019s'), "'it\u2019\u2019s'");
|
||||
assert.strictEqual(
|
||||
quotePowerShellString('\u2018\u2019\u201A\u201B'),
|
||||
"'\u2018\u2018\u2019\u2019\u201A\u201A\u201B\u201B'"
|
||||
);
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
const ps = findPowerShell();
|
||||
if (!ps) {
|
||||
console.log(' - skipping PowerShell round-trip: no pwsh or powershell on PATH');
|
||||
} else {
|
||||
for (const sample of SAMPLES) {
|
||||
(test(`${ps} parses ${JSON.stringify(sample)} back unchanged`, () => {
|
||||
// Print code points so the console encoding cannot affect the check.
|
||||
const command = `$s = ${quotePowerShellString(sample)}; ($s.ToCharArray() | ForEach-Object { [int]$_ }) -join ','`;
|
||||
const result = spawnSync(ps, ['-NoProfile', '-Command', command], {
|
||||
encoding: 'utf8',
|
||||
timeout: 15000,
|
||||
});
|
||||
assert.strictEqual(result.status, 0, `exit ${result.status}: ${result.stderr}`);
|
||||
const expected = Array.from(sample, ch => ch.charCodeAt(0)).join(',');
|
||||
assert.strictEqual(result.stdout.trim(), expected);
|
||||
}) ? passed++ : failed++);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
runTests();
|
||||
Reference in new issue
Block a user