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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | 21x 21x 8x 8x 8x 8x 8x 101x 101x 32x 101x 36x 36x 101x 12x 101x 10x 8x 2x 101x 11x 9x 2x 101x 198x 198x 5x 193x 21x 25x 25x 25x 21x 77x 77x 77x 77x 21x 77x 77x 26x 77x 2x 1x 1x 37x 37x 37x 15x 22x 8x 14x 14x 1x 1x 13x 22x 22x 22x 11x 11x 10x 12x 2x 9x 9x 9x 8x 4x 4x 6x 4x 4x 1x 8x 42x 42x 42x 42x 42x 42x 30x 30x 30x 30x 5x 21x 13x 13x 13x 78x 78x 10x 13x 21x 12x 12x 12x 36x 36x 8x 12x 21x 7x 7x 5x 5x 5x 3x | /**
* Scope Enforcer - enforces project/namespace/group restrictions
*
* When a project config defines a scope, the ScopeEnforcer ensures
* that all operations are limited to the allowed projects/namespaces/groups.
*
* 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, ScopeConfig } from "./types";
import { logDebug, logWarn } from "../logger";
// Re-export ScopeConfig for backward compatibility
export type { ScopeConfig } from "./types";
/**
* 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 {
const parts: string[] = [];
if (scope.project) {
parts.push(`project: ${scope.project}`);
}
if (scope.group) {
const subgroupSuffix = scope.includeSubgroups !== false ? "/*" : "";
parts.push(`group: ${scope.group}${subgroupSuffix}`);
}
if (scope.namespace) {
parts.push(`namespace: ${scope.namespace}/*`);
}
if (scope.projects && scope.projects.length > 0) {
if (scope.projects.length <= 3) {
parts.push(`projects: ${scope.projects.join(", ")}`);
} else {
parts.push(`${scope.projects.length} allowed projects`);
}
}
if (scope.groups && scope.groups.length > 0) {
if (scope.groups.length <= 3) {
parts.push(`groups: ${scope.groups.join(", ")}`);
} else {
parts.push(`${scope.groups.length} allowed groups`);
}
}
return parts.length > 0 ? parts.join("; ") : "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 path matches a namespace (used for both projects and groups)
*
* 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"
*/
export 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/group restrictions defined in project config.
* Use isAllowed()/isGroupAllowed() to check before operations,
* or enforce()/enforceGroup() to throw on violation.
*/
export class ScopeEnforcer {
private readonly scope: ScopeConfig;
private readonly allowedProjectsSet: Set<string>;
private readonly allowedGroupsSet: Set<string>;
private readonly includeSubgroups: boolean;
constructor(scope: ScopeConfig) {
this.scope = scope;
this.includeSubgroups = scope.includeSubgroups !== false; // Default true
// Initialize allowed projects set
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));
}
// Initialize allowed groups set
this.allowedGroupsSet = new Set((scope.groups ?? []).map(g => normalizeProjectPath(g)));
// Add single group to set if specified
if (scope.group) {
this.allowedGroupsSet.add(normalizeProjectPath(scope.group));
}
logDebug("ScopeEnforcer initialized", {
scope: getScopeDescription(scope),
allowedProjectsCount: this.allowedProjectsSet.size,
allowedGroupsCount: this.allowedGroupsSet.size,
includeSubgroups: this.includeSubgroups,
});
}
/**
* 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 {
// If no project restrictions are defined, allow all projects
Iif (!this.hasProjectRestrictions()) {
return true;
}
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 project is within an allowed group (when group scope is set)
Iif (this.allowedGroupsSet.size > 0) {
for (const allowedGroup of this.allowedGroupsSet) {
if (this.includeSubgroups) {
// Project must be in group or its subgroups
if (isInNamespace(projectPath, allowedGroup)) {
return true;
}
} else {
// Project must be directly in group (first-level only)
const parts = normalized.split("/");
if (parts.length >= 2) {
const projectGroup = parts.slice(0, -1).join("/");
if (projectGroup === allowedGroup) {
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)) {
logWarn("Numeric project ID not in allowed scope - denying access", {
projectId: normalized,
});
return false;
}
return false;
}
/**
* Check if a group path is within the allowed scope
*
* @param groupPath Group path or ID to check (e.g., "mygroup" or "parent/child")
* @returns true if allowed, false if outside scope
*/
isGroupAllowed(groupPath: string): boolean {
// If no group restrictions are defined, allow all groups
Iif (!this.hasGroupRestrictions()) {
return true;
}
const normalized = normalizeProjectPath(groupPath);
// Check explicit group list (includes single group from scope.group if set)
if (this.allowedGroupsSet.size > 0 && this.allowedGroupsSet.has(normalized)) {
return true;
}
// Check if group is a subgroup of an allowed group (when includeSubgroups is true)
if (this.includeSubgroups && this.allowedGroupsSet.size > 0) {
for (const allowedGroup of this.allowedGroupsSet) {
if (isInNamespace(groupPath, allowedGroup)) {
return true;
}
}
}
// Check namespace (groups in namespace are allowed)
Iif (this.scope.namespace && isInNamespace(groupPath, this.scope.namespace)) {
return true;
}
// Check if numeric ID is in allowed groups (can't validate without API call)
// For security, we deny numeric IDs unless they're in the explicit list
Iif (/^\d+$/.test(normalized)) {
logWarn("Numeric group ID not in allowed scope - denying access", { groupId: normalized });
return false;
}
return false;
}
/**
* Enforce project 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)) {
logWarn("Project scope violation attempted", {
attempted: projectPath,
scope: getScopeDescription(this.scope),
});
throw new ScopeViolationError(projectPath, this.scope);
}
}
/**
* Enforce group scope restriction, throwing if violated
*
* @param groupPath Group path to check
* @throws ScopeViolationError if outside allowed scope
*/
enforceGroup(groupPath: string): void {
if (!this.isGroupAllowed(groupPath)) {
logWarn("Group scope violation attempted", {
attempted: groupPath,
scope: getScopeDescription(this.scope),
});
throw new ScopeViolationError(groupPath, 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 project restrictions
*/
hasProjectRestrictions(): boolean {
const hasProject = Boolean(this.scope.project);
const hasNamespace = Boolean(this.scope.namespace);
const hasProjects = Boolean(this.scope.projects && this.scope.projects.length > 0);
const hasGroup = Boolean(this.scope.group);
const hasGroups = Boolean(this.scope.groups && this.scope.groups.length > 0);
// Groups also restrict projects (projects must be within allowed groups)
return hasProject || hasNamespace || hasProjects || hasGroup || hasGroups;
}
/**
* Check if scope has any group restrictions
*/
hasGroupRestrictions(): boolean {
const hasGroup = Boolean(this.scope.group);
const hasNamespace = Boolean(this.scope.namespace);
const hasGroups = Boolean(this.scope.groups && this.scope.groups.length > 0);
return hasGroup || hasNamespace || hasGroups;
}
/**
* Check if scope has any restrictions (projects or groups)
*/
hasRestrictions(): boolean {
return this.hasProjectRestrictions() || this.hasGroupRestrictions();
}
}
/**
* 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;
}
/**
* Extract group path from tool arguments
*
* Tools may specify group in different ways:
* - group_id: "mygroup" or "parent/child" or "123"
* - groupId: "mygroup"
* - group: "mygroup"
*
* @param args Tool arguments object
* @returns Array of group paths found in arguments
*/
export function extractGroupsFromArgs(args: Record<string, unknown>): string[] {
const groups: string[] = [];
// Common parameter names for group identification
const groupFields = ["group_id", "groupId", "group"];
for (const field of groupFields) {
const value = args[field];
if (typeof value === "string" && value.trim()) {
groups.push(value.trim());
}
}
return groups;
}
/**
* Enforce scope on tool arguments
*
* Checks all project-related and group-related fields in arguments against the scope.
*
* @param enforcer ScopeEnforcer instance
* @param args Tool arguments
* @throws ScopeViolationError if any project or group is outside scope
*/
export function enforceArgsScope(enforcer: ScopeEnforcer, args: Record<string, unknown>): void {
// Check project paths
const projects = extractProjectsFromArgs(args);
for (const project of projects) {
enforcer.enforce(project);
}
// Check group paths
const groups = extractGroupsFromArgs(args);
for (const group of groups) {
enforcer.enforceGroup(group);
}
}
|