#!/usr/bin/env node
import { createHook } from './_runtime.mjs';

const BLOCKED = [
  '**/.env',
  '**/.env.*',
  '**/*.pem',
  '**/*.key',
  '**/*.p12',
  '**/*.pfx',
  '**/id_rsa*',
  '**/id_ed25519*',
  '**/.ssh/*',
  '**/*credentials*',
  '**/*.secret',
  '**/.netrc',
  '**/.pgpass',
  '**/secret.*',
  '**/secrets.*',
];

const READ_TOOLS = new Set(['Read', 'read_file', 'readFile']);

/** Blocks reads of secret-bearing files and key material. */
export const privacyGuard = async (context) => {
  if (context.event !== 'PreToolUse' || !READ_TOOLS.has(context.toolName))
    return { decision: 'allow' };
  const blockedPath = context.filePaths.find((filePath) =>
    context.matchesPattern(filePath, BLOCKED),
  );
  return blockedPath
    ? {
        decision: 'deny',
        reason: `Blocked: reading sensitive file ${blockedPath}. Use environment variables or secrets manager instead.`,
      }
    : { decision: 'allow' };
};

createHook(privacyGuard);
