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,7 +6,8 @@
import { spawn } from 'node:child_process';
import type { HookConfig } from './types.js';
import { HookEventName } from './types.js';
import { HookEventName, ConfigSource } from './types.js';
import type { Config } from '../config/config.js';
import type {
HookInput,
HookOutput,
@@ -39,7 +40,11 @@ const EXIT_CODE_NON_BLOCKING_ERROR = 1;
* Hook runner that executes command hooks
*/
export class HookRunner {
constructor() {}
private readonly config: Config;
constructor(config: Config) {
this.config = config;
}
/**
* Execute a single hook
@@ -51,6 +56,23 @@ export class HookRunner {
): Promise<HookExecutionResult> {
const startTime = Date.now();
// Secondary security check: Ensure project hooks are not executed in untrusted folders
if (
hookConfig.source === ConfigSource.Project &&
!this.config.isTrustedFolder()
) {
const errorMessage =
'Security: Blocked execution of project hook in untrusted folder';
debugLogger.warn(errorMessage);
return {
hookConfig,
eventName,
success: false,
error: new Error(errorMessage),
duration: 0,
};
}
try {
return await this.executeCommandHook(
hookConfig,