Files
claude-config/hooks/pre-tool-use.js
T

111 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* PreToolUse Hook - 工具使用前触发
*
* 功能:
* 1. 检查 Write/Edit 工具的路径权限
* 2. 提醒敏感操作
*
* 适用项目: JNPF + dacli 低代码平台
*/
const fs = require('fs');
// 从 stdin 读取输入
let inputData = '';
try {
inputData = fs.readFileSync(0, 'utf8');
} catch {
console.log(JSON.stringify({ continue: true }));
process.exit(0);
}
let input;
try {
input = JSON.parse(inputData);
} catch {
console.log(JSON.stringify({ continue: true }));
process.exit(0);
}
const toolName = input.tool_name;
const toolInput = input.tool_input || {};
const filePath = input.tool_input?.file_path || '';
// ==================== Write/Edit 工具检查 ====================
if (toolName === 'Write' || toolName === 'Edit') {
const filePath = toolInput.file_path || '';
const projectPath = process.env.ccWorkDir || '/ccWorkDir';
// 检查是否写入允许的路径
const allowedPaths = [
projectPath,
'/tmp/',
'/home/ubuntu/'
];
const isAllowed = allowedPaths.some(p => filePath.startsWith(p));
if (!isAllowed) {
const output = {
decision: 'block',
reason: `🚫 **路径被阻止**:\n\n文件: \`${filePath}\`\n\n禁止写入本项目路径(${projectPath})以外的任何文件夹(/tmp/ 和 /home/ubuntu/ 除外)`
};
console.log(JSON.stringify(output));
process.exit(0);
}
// 检查是否写入敏感文件
const sensitivePatterns = [
{ pattern: /\.env\.production/, warning: '生产环境配置文件' },
{ pattern: /application-prod/, warning: '生产环境配置' },
{ pattern: /secrets\.json/, warning: '密钥配置文件' },
{ pattern: /credentials\.json/, warning: '凭证配置文件' },
];
for (const { pattern } of sensitivePatterns) {
if (pattern.test(filePath)) {
const output = {
continue: true,
systemMessage: `⚠️ **敏感文件**: 正在操作 \`${filePath}\`\n\n请确保不要提交敏感信息到 Git`
};
console.log(JSON.stringify(output));
process.exit(0);
}
}
// 检查是否尝试写入禁止目录
const deniedPaths = [
{ path: '/jnpf/', reason: 'jnpf platform root' },
{ path: '/uniapp/', reason: 'uniapp/' },
{ path: '/web/apps/jnpf-web-apps-main/', reason: 'jnpf web apps (except ssw/)' },
];
// 特例允许的路径
const allowedExceptions = [
'web/apps/jnpf-web-apps-main/src/views/ssw/',
'web/apps/jnpf-web-apps-main/public/ssw',
'uniapp/pages/ssw/',
];
for (const { path: denied } of deniedPaths) {
if (filePath.includes(`/${denied}`) || filePath.includes(`/${denied}\\`)) {
let isException = false;
for (const allowed of allowedExceptions) {
if (filePath.includes(allowed)) {
isException = true;
break;
}
}
if (isException) continue;
const output = {
decision: 'block',
reason: `🚫 **路径被阻止**:\n\n文件: \`${filePath}\`\n\n根据 CLAUDE.md,以下路径禁止 AI 修改:\n- jnpf/ web/ uniapp/(除 web/apps/jnpf-web-apps-main/src/views/ssw/ uniapp/pages/ssw/ 外)\n `
};
console.log(JSON.stringify(output));
process.exit(0);
}
}
}
// ==================== 默认:允许继续 ====================
console.log(JSON.stringify({ continue: true }));