Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

2.2 KB
Raw Blame
#!/usr/bin/env node

/**
 * Explicit validator: protect generated and release-owned paths.
 *
 * Blocks writes to:
 *   - any path under `src/generated/`  (regenerated by `pa app add data-source`)
 *   - any `power.config.json`          (regenerated by `pa app init`)
 *   - any `vendor/<name>.tgz`          (part of the reviewed published dependency set)
 *
 * Generated files are CLI-owned; runtime archives belong to the reviewed release.
 * Block manual writes and point callers to the owning workflow.
 *
 * Exit codes: 0 pass, 2 block.
 */

const path = require('path');

function isWriteTool(t) {
  return t === 'Write' || t === 'Edit' || t === 'MultiEdit';
}

const PROTECTED = [
  {
    rx: /(^|\/)src\/generated\//,
    reason:
      '`src/generated/` is regenerated by `$PA app add data-source`. Manual edits are lost on the next run. Add or refresh the data source via the CLI instead.',
  },
  {
    rx: /(^|\/)power\.config\.json$/,
    reason:
      '`power.config.json` is owned by `pa app init`. Re-run `$PA app init -t MobileApp --display-name <name> --environment-id <id> --non-interactive` to change environment binding.',
  },
  {
    rx: /(^|\/)vendor\/[^/]+\.tgz$/,
    reason:
      '`vendor/*.tgz` files are part of the app\'s reviewed published npm template/dependency set. Use `/check-updates` to review a compatible published release. Do not edit archives in-project or replace them from the skills repository.',
  },
];

let buf = '';
process.stdin.on('data', (c) => (buf += c));
process.stdin.on('end', () => {
  let input;
  try {
    input = JSON.parse(buf || '{}');
  } catch {
    process.exit(0);
  }
  const toolName = input.tool_name || input.toolName;
  const toolInput = input.tool_input || input.toolInput || {};
  if (!isWriteTool(toolName)) process.exit(0);
  const fp = toolInput.file_path || toolInput.filePath;
  if (typeof fp !== 'string') process.exit(0);

  const normalizedPath = fp.replace(/\\/g, '/');
  const hit = PROTECTED.find(({ rx }) => rx.test(normalizedPath));
  if (!hit) process.exit(0);

  const rel = path.relative(process.cwd(), fp) || fp;
  process.stderr.write(`BLOCKED: protected path ${rel}\n\n  - ${hit.reason}\n`);
  process.exit(2);
});