Add Folder Trust Support To Hooks (#15325)

This commit is contained in:
Sehoon Shon
2025-12-22 11:46:38 -05:00
committed by GitHub
parent d6a2f1d670
commit dced409ac4
10 changed files with 188 additions and 23 deletions

View File

@@ -6,9 +6,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs';
import { HookRegistry, ConfigSource } from './hookRegistry.js';
import { HookRegistry } from './hookRegistry.js';
import type { Storage } from '../config/storage.js';
import { HookEventName, HookType } from './types.js';
import { ConfigSource, HookEventName, HookType } from './types.js';
import type { Config } from '../config/config.js';
import type { HookDefinition } from './types.js';
@@ -47,6 +47,7 @@ describe('HookRegistry', () => {
getExtensions: vi.fn().mockReturnValue([]),
getHooks: vi.fn().mockReturnValue({}),
getDisabledHooks: vi.fn().mockReturnValue([]),
isTrustedFolder: vi.fn().mockReturnValue(true),
} as unknown as Config;
hookRegistry = new HookRegistry(mockConfig);
@@ -68,6 +69,35 @@ describe('HookRegistry', () => {
);
});
it('should not load hooks if folder is not trusted', async () => {
vi.mocked(mockConfig.isTrustedFolder).mockReturnValue(false);
const mockHooksConfig = {
BeforeTool: [
{
hooks: [
{
type: 'command',
command: './hooks/test.sh',
},
],
},
],
};
vi.mocked(mockConfig.getHooks).mockReturnValue(
mockHooksConfig as unknown as {
[K in HookEventName]?: HookDefinition[];
},
);
await hookRegistry.initialize();
expect(hookRegistry.getAllHooks()).toHaveLength(0);
expect(mockDebugLogger.warn).toHaveBeenCalledWith(
'Project hooks disabled because the folder is not trusted.',
);
});
it('should load hooks from project configuration', async () => {
const mockHooksConfig = {
BeforeTool: [