File size: 1,539 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Sanitize hook name to remove potentially sensitive information.
* Extracts the base command name without arguments or full paths.
*
* This function protects PII by removing:
* - Full file paths that may contain usernames
* - Command arguments that may contain credentials, API keys, tokens
* - Environment variables with sensitive values
*
* Examples:
* - "/path/to/.gemini/hooks/check-secrets.sh --api-key=abc123" -> "check-secrets.sh"
* - "python /home/user/script.py --token=xyz" -> "python"
* - "node index.js" -> "node"
* - "C:\\Windows\\System32\\cmd.exe /c secret.bat" -> "cmd.exe"
* - "" or " " -> "unknown-command"
*
* @param hookName Full command string.
* @returns Sanitized command name.
*/
export function sanitizeHookName(hookName: string): string {
// Handle empty or whitespace-only strings
if (!hookName || !hookName.trim()) {
return 'unknown-command';
}
// Split by spaces to get command parts
const parts = hookName.trim().split(/\s+/);
if (parts.length === 0) {
return 'unknown-command';
}
// Get the first part (the command)
const command = parts[0];
if (!command) {
return 'unknown-command';
}
// If it's a path, extract just the basename
if (command.includes('/') || command.includes('\\')) {
const pathParts = command.split(/[/\\]/);
const basename = pathParts[pathParts.length - 1];
return basename || 'unknown-command';
}
return command;
}
|