permission-gate.ts 1019 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Permission Gate Extension
  3. *
  4. * Prompts for confirmation before running potentially dangerous bash commands.
  5. * Patterns checked: rm -rf, sudo, chmod/chown 777
  6. */
  7. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  8. export default function (pi: ExtensionAPI) {
  9. const dangerousPatterns = [/\brm\s+(-rf?|--recursive)/i, /\bsudo\b/i, /\b(chmod|chown)\b.*777/i];
  10. pi.on("tool_call", async (event, ctx) => {
  11. if (event.toolName !== "bash") return undefined;
  12. const command = event.input.command as string;
  13. const isDangerous = dangerousPatterns.some((p) => p.test(command));
  14. if (isDangerous) {
  15. if (!ctx.hasUI) {
  16. // In non-interactive mode, block by default
  17. return { block: true, reason: "Dangerous command blocked (no UI for confirmation)" };
  18. }
  19. const choice = await ctx.ui.select(`⚠️ Dangerous command:\n\n ${command}\n\nAllow?`, ["Yes", "No"]);
  20. if (choice !== "Yes") {
  21. return { block: true, reason: "Blocked by user" };
  22. }
  23. }
  24. return undefined;
  25. });
  26. }