Skip to content

Commit 47aa415

Browse files
committed
fix(installer): validate hooks and settings before install
1 parent d7e6bb2 commit 47aa415

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

scripts/ecc.js

100644100755
File mode changed.

scripts/install-apply.js

100644100755
File mode changed.

scripts/lib/install/apply.js

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ const path = require('path');
55

66
const { writeInstallState } = require('../install-state');
77

8+
function readJsonObject(filePath, label) {
9+
let parsed;
10+
try {
11+
parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
12+
} catch (error) {
13+
throw new Error(`Failed to parse ${label} at ${filePath}: ${error.message}`);
14+
}
15+
16+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
17+
throw new Error(`Invalid ${label} at ${filePath}: expected a JSON object`);
18+
}
19+
20+
return parsed;
21+
}
22+
823
function mergeHookEntries(existingEntries, incomingEntries) {
924
const mergedEntries = [];
1025
const seenEntries = new Set();
@@ -22,39 +37,32 @@ function mergeHookEntries(existingEntries, incomingEntries) {
2237
return mergedEntries;
2338
}
2439

25-
function mergeHooksIntoSettings(plan) {
26-
if (!plan.adapter || plan.adapter.target !== 'claude') {
27-
return;
28-
}
40+
function findHooksSourcePath(plan, hooksDestinationPath) {
41+
const operation = plan.operations.find(item => item.destinationPath === hooksDestinationPath);
42+
return operation ? operation.sourcePath : null;
43+
}
2944

30-
const hooksJsonPath = path.join(plan.targetRoot, 'hooks', 'hooks.json');
31-
if (!fs.existsSync(hooksJsonPath)) {
32-
return;
45+
function buildMergedSettings(plan) {
46+
if (!plan.adapter || plan.adapter.target !== 'claude') {
47+
return null;
3348
}
3449

35-
let hooksConfig;
36-
try {
37-
hooksConfig = JSON.parse(fs.readFileSync(hooksJsonPath, 'utf8'));
38-
} catch (error) {
39-
throw new Error(`Failed to parse hooks config at ${hooksJsonPath}: ${error.message}`);
50+
const hooksDestinationPath = path.join(plan.targetRoot, 'hooks', 'hooks.json');
51+
const hooksSourcePath = findHooksSourcePath(plan, hooksDestinationPath) || hooksDestinationPath;
52+
if (!fs.existsSync(hooksSourcePath)) {
53+
return null;
4054
}
4155

56+
const hooksConfig = readJsonObject(hooksSourcePath, 'hooks config');
4257
const incomingHooks = hooksConfig.hooks;
4358
if (!incomingHooks || typeof incomingHooks !== 'object' || Array.isArray(incomingHooks)) {
44-
return;
59+
throw new Error(`Invalid hooks config at ${hooksSourcePath}: expected "hooks" to be a JSON object`);
4560
}
4661

4762
const settingsPath = path.join(plan.targetRoot, 'settings.json');
4863
let settings = {};
4964
if (fs.existsSync(settingsPath)) {
50-
try {
51-
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
52-
if (!settings || typeof settings !== 'object' || Array.isArray(settings)) {
53-
throw new Error('root value must be a JSON object');
54-
}
55-
} catch (error) {
56-
throw new Error(`Failed to parse existing settings at ${settingsPath}: ${error.message}`);
57-
}
65+
settings = readJsonObject(settingsPath, 'existing settings');
5866
}
5967

6068
const existingHooks = settings.hooks && typeof settings.hooks === 'object' && !Array.isArray(settings.hooks)
@@ -73,17 +81,29 @@ function mergeHooksIntoSettings(plan) {
7381
hooks: mergedHooks,
7482
};
7583

76-
fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
77-
fs.writeFileSync(settingsPath, JSON.stringify(mergedSettings, null, 2) + '\n', 'utf8');
84+
return {
85+
settingsPath,
86+
mergedSettings,
87+
};
7888
}
7989

8090
function applyInstallPlan(plan) {
91+
const mergedSettingsPlan = buildMergedSettings(plan);
92+
8193
for (const operation of plan.operations) {
8294
fs.mkdirSync(path.dirname(operation.destinationPath), { recursive: true });
8395
fs.copyFileSync(operation.sourcePath, operation.destinationPath);
8496
}
8597

86-
mergeHooksIntoSettings(plan);
98+
if (mergedSettingsPlan) {
99+
fs.mkdirSync(path.dirname(mergedSettingsPlan.settingsPath), { recursive: true });
100+
fs.writeFileSync(
101+
mergedSettingsPlan.settingsPath,
102+
JSON.stringify(mergedSettingsPlan.mergedSettings, null, 2) + '\n',
103+
'utf8'
104+
);
105+
}
106+
87107
writeInstallState(plan.installStatePath, plan.statePreview);
88108

89109
return {

tests/scripts/install-apply.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function readJson(filePath) {
2222
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
2323
}
2424

25+
const REPO_ROOT = path.join(__dirname, '..', '..');
26+
2527
function run(args = [], options = {}) {
2628
const env = {
2729
...process.env,
@@ -435,6 +437,8 @@ function runTests() {
435437
assert.strictEqual(result.code, 1);
436438
assert.ok(result.stderr.includes('Failed to parse existing settings at'));
437439
assert.strictEqual(fs.readFileSync(settingsPath, 'utf8'), '{ invalid json\n');
440+
assert.ok(!fs.existsSync(path.join(claudeRoot, 'hooks', 'hooks.json')), 'hooks.json should not be copied on validation failure');
441+
assert.ok(!fs.existsSync(path.join(claudeRoot, 'ecc', 'install-state.json')), 'install state should not be written on validation failure');
438442
} finally {
439443
cleanup(homeDir);
440444
cleanup(projectDir);
@@ -453,10 +457,36 @@ function runTests() {
453457

454458
const result = run(['--profile', 'core'], { cwd: projectDir, homeDir });
455459
assert.strictEqual(result.code, 1);
456-
assert.ok(result.stderr.includes('Failed to parse existing settings at'));
457-
assert.ok(result.stderr.includes('root value must be a JSON object'));
460+
assert.ok(result.stderr.includes('Invalid existing settings at'));
461+
assert.ok(result.stderr.includes('expected a JSON object'));
458462
assert.strictEqual(fs.readFileSync(settingsPath, 'utf8'), '[]\n');
463+
assert.ok(!fs.existsSync(path.join(claudeRoot, 'hooks', 'hooks.json')), 'hooks.json should not be copied on validation failure');
464+
assert.ok(!fs.existsSync(path.join(claudeRoot, 'ecc', 'install-state.json')), 'install state should not be written on validation failure');
465+
} finally {
466+
cleanup(homeDir);
467+
cleanup(projectDir);
468+
}
469+
})) passed++; else failed++;
470+
471+
if (test('fails when source hooks.json root is not an object before copying files', () => {
472+
const homeDir = createTempDir('install-apply-home-');
473+
const projectDir = createTempDir('install-apply-project-');
474+
const sourceHooksPath = path.join(REPO_ROOT, 'hooks', 'hooks.json');
475+
const originalHooks = fs.readFileSync(sourceHooksPath, 'utf8');
476+
477+
try {
478+
fs.writeFileSync(sourceHooksPath, '[]\n');
479+
480+
const result = run(['--profile', 'core'], { cwd: projectDir, homeDir });
481+
assert.strictEqual(result.code, 1);
482+
assert.ok(result.stderr.includes('Invalid hooks config at'));
483+
assert.ok(result.stderr.includes('expected a JSON object'));
484+
485+
const claudeRoot = path.join(homeDir, '.claude');
486+
assert.ok(!fs.existsSync(path.join(claudeRoot, 'hooks', 'hooks.json')), 'hooks.json should not be copied when source hooks are invalid');
487+
assert.ok(!fs.existsSync(path.join(claudeRoot, 'ecc', 'install-state.json')), 'install state should not be written when source hooks are invalid');
459488
} finally {
489+
fs.writeFileSync(sourceHooksPath, originalHooks);
460490
cleanup(homeDir);
461491
cleanup(projectDir);
462492
}

0 commit comments

Comments
 (0)