All files / src/profiles scope-enforcer.ts

100% Statements 63/63
100% Branches 41/41
100% Functions 14/14
100% Lines 62/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262                          3x                                 3x   4x 4x   4x 4x 4x               44x 26x   18x 8x   10x 8x 6x   2x   2x                     95x   95x 3x   92x                         13x 13x     13x                       3x         32x 32x     32x 17x     32x                           2x 1x   1x                   36x     36x 14x       22x 8x         14x 1x       1x     13x                   7x 4x             4x               1x             4x             5x 5x 5x 5x                             3x 10x     10x                 10x 60x 60x 9x       10x                       3x 4x   4x 4x      
/**
 * Scope Enforcer - enforces project/namespace restrictions
 *
 * When a project config defines a scope, the ScopeEnforcer ensures
 * that all operations are limited to the allowed projects/namespaces.
 *
 * Security model:
 * - Scope is ADDITIVE RESTRICTION only - can only narrow, never expand
 * - Applied at tool invocation time, not registration time
 * - Throws clear error when operation would exceed scope
 */
 
import { ProjectPreset } from "./types";
import { logger } from "../logger";
 
/**
 * Scope configuration extracted from ProjectPreset
 */
export interface ScopeConfig {
  /** Single project restriction */
  project?: string;
  /** Namespace restriction (all projects in group) */
  namespace?: string;
  /** Explicit list of allowed projects */
  projects?: string[];
}
 
/**
 * Error thrown when an operation exceeds the allowed scope
 */
export class ScopeViolationError extends Error {
  constructor(
    public readonly attemptedTarget: string,
    public readonly allowedScope: ScopeConfig
  ) {
    const scopeDescription = getScopeDescription(allowedScope);
    super(`Operation on '${attemptedTarget}' is outside the allowed scope (${scopeDescription})`);
    this.name = "ScopeViolationError";
  }
}
 
/**
 * Get a human-readable description of the scope
 */
function getScopeDescription(scope: ScopeConfig): string {
  if (scope.project) {
    return `project: ${scope.project}`;
  }
  if (scope.namespace) {
    return `namespace: ${scope.namespace}/*`;
  }
  if (scope.projects && scope.projects.length > 0) {
    if (scope.projects.length <= 3) {
      return `projects: ${scope.projects.join(", ")}`;
    }
    return `${scope.projects.length} allowed projects`;
  }
  return "unrestricted";
}
 
/**
 * Normalize a project path for comparison
 *
 * - Removes leading/trailing slashes
 * - Converts to lowercase
 * - Handles numeric IDs (returns as-is)
 */
function normalizeProjectPath(path: string): string {
  const trimmed = path.trim().replace(/^\/+|\/+$/g, "");
  // If it's a numeric ID, return as-is
  if (/^\d+$/.test(trimmed)) {
    return trimmed;
  }
  return trimmed.toLowerCase();
}
 
/**
 * Check if a project path matches a namespace
 *
 * Examples:
 * - "mygroup/project" matches namespace "mygroup"
 * - "mygroup/subgroup/project" matches namespace "mygroup"
 * - "mygroup/subgroup/project" matches namespace "mygroup/subgroup"
 * - "other/project" does NOT match namespace "mygroup"
 */
function isInNamespace(projectPath: string, namespace: string): boolean {
  const normalizedProject = normalizeProjectPath(projectPath);
  const normalizedNamespace = normalizeProjectPath(namespace);
 
  // Project must start with namespace followed by /
  return (
    normalizedProject === normalizedNamespace ||
    normalizedProject.startsWith(normalizedNamespace + "/")
  );
}
 
/**
 * Scope Enforcer class
 *
 * Enforces project/namespace restrictions defined in project config.
 * Use isAllowed() to check before operations, or enforce() to throw on violation.
 */
export class ScopeEnforcer {
  private readonly scope: ScopeConfig;
  private readonly allowedProjectsSet: Set<string>;
 
  constructor(scope: ScopeConfig) {
    this.scope = scope;
    this.allowedProjectsSet = new Set((scope.projects ?? []).map(p => normalizeProjectPath(p)));
 
    // Add single project to set if specified
    if (scope.project) {
      this.allowedProjectsSet.add(normalizeProjectPath(scope.project));
    }
 
    logger.debug(
      {
        scope: getScopeDescription(scope),
        allowedCount: this.allowedProjectsSet.size,
      },
      "ScopeEnforcer initialized"
    );
  }
 
  /**
   * Create a ScopeEnforcer from a ProjectPreset
   * Returns null if preset has no scope restrictions
   */
  static fromPreset(preset: ProjectPreset): ScopeEnforcer | null {
    if (!preset.scope) {
      return null;
    }
    return new ScopeEnforcer(preset.scope);
  }
 
  /**
   * Check if a project path is within the allowed scope
   *
   * @param projectPath Project path or ID to check (e.g., "group/project" or "123")
   * @returns true if allowed, false if outside scope
   */
  isAllowed(projectPath: string): boolean {
    const normalized = normalizeProjectPath(projectPath);
 
    // Check explicit project list (includes single project from scope.project if set)
    if (this.allowedProjectsSet.size > 0 && this.allowedProjectsSet.has(normalized)) {
      return true;
    }
 
    // Check namespace
    if (this.scope.namespace && isInNamespace(projectPath, this.scope.namespace)) {
      return true;
    }
 
    // Check if numeric ID is in allowed projects (can't validate without API call)
    // For security, we deny numeric IDs unless they're in the explicit list
    if (/^\d+$/.test(normalized)) {
      logger.warn(
        { projectId: normalized },
        "Numeric project ID not in allowed scope - denying access"
      );
      return false;
    }
 
    return false;
  }
 
  /**
   * Enforce scope restriction, throwing if violated
   *
   * @param projectPath Project path to check
   * @throws ScopeViolationError if outside allowed scope
   */
  enforce(projectPath: string): void {
    if (!this.isAllowed(projectPath)) {
      logger.warn(
        {
          attempted: projectPath,
          scope: getScopeDescription(this.scope),
        },
        "Scope violation attempted"
      );
      throw new ScopeViolationError(projectPath, this.scope);
    }
  }
 
  /**
   * Get the scope configuration
   */
  getScope(): ScopeConfig {
    return this.scope;
  }
 
  /**
   * Get scope description for display
   */
  getScopeDescription(): string {
    return getScopeDescription(this.scope);
  }
 
  /**
   * Check if scope has any restrictions
   */
  hasRestrictions(): boolean {
    const hasProject = Boolean(this.scope.project);
    const hasNamespace = Boolean(this.scope.namespace);
    const hasProjects = Boolean(this.scope.projects && this.scope.projects.length > 0);
    return hasProject || hasNamespace || hasProjects;
  }
}
 
/**
 * Extract project path from tool arguments
 *
 * Tools may specify project in different ways:
 * - project_id: "group/project" or "123"
 * - namespace: "group/project"
 * - projectId: "group/project"
 *
 * @param args Tool arguments object
 * @returns Array of project paths found in arguments
 */
export function extractProjectsFromArgs(args: Record<string, unknown>): string[] {
  const projects: string[] = [];
 
  // Common parameter names for project identification
  const projectFields = [
    "project_id",
    "projectId",
    "project",
    "namespace",
    "namespacePath",
    "fullPath",
  ];
 
  for (const field of projectFields) {
    const value = args[field];
    if (typeof value === "string" && value.trim()) {
      projects.push(value.trim());
    }
  }
 
  return projects;
}
 
/**
 * Enforce scope on tool arguments
 *
 * Checks all project-related fields in arguments against the scope.
 *
 * @param enforcer ScopeEnforcer instance
 * @param args Tool arguments
 * @throws ScopeViolationError if any project is outside scope
 */
export function enforceArgsScope(enforcer: ScopeEnforcer, args: Record<string, unknown>): void {
  const projects = extractProjectsFromArgs(args);
 
  for (const project of projects) {
    enforcer.enforce(project);
  }
}