File size: 3,561 Bytes
7a1ad33 | 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 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import path from 'node:path';
import { resolveToRealPath } from './paths.js';
/**
* Exact build file basenames that define build targets, dependencies,
* compilation scripts, or lifecycle hooks.
*/
export const EXACT_BUILD_FILENAMES: ReadonlySet<string> = new Set([
// Bazel / Blaze
'BUILD',
'BUILD.bazel',
'WORKSPACE',
'WORKSPACE.bazel',
'MODULE.bazel',
'.bazelrc',
'.blazerc',
// Make & CMake
'Makefile',
'makefile',
'GNUmakefile',
'CMakeLists.txt',
// Node.js
'package.json',
'package-lock.json',
'pnpm-lock.yaml',
'yarn.lock',
'bun.lockb',
// Python
'setup.py',
'setup.cfg',
'pyproject.toml',
// Go
'go.mod',
'go.sum',
// Rust
'Cargo.toml',
'Cargo.lock',
// Java / Kotlin / Gradle / Maven
'pom.xml',
'build.gradle',
'build.gradle.kts',
'settings.gradle',
'settings.gradle.kts',
// Containers
'Dockerfile',
'Containerfile',
]);
/**
* Extensions indicating build or build-configuration logic.
*/
export const BUILD_FILE_EXTENSIONS: ReadonlySet<string> = new Set([
'.bzl',
'.bazel',
'.bzlmod',
'.mk',
'.cmake',
]);
/**
* Deterministically checks whether a given file path corresponds to a build
* configuration or definition file.
*
* @param filePath The file path (relative or absolute).
* @returns True if the file is a recognized build file.
*/
export function isBuildFile(filePath: string): boolean {
if (!filePath) {
return false;
}
// Sanitize null byte characters (\0) and trailing dots/spaces on Windows to prevent path injection bypasses
let cleanPath = filePath.replace(/\0/g, '');
if (process.platform === 'win32') {
let end = cleanPath.length;
while (
end > 0 &&
(cleanPath[end - 1] === '.' || cleanPath[end - 1] === ' ')
) {
end--;
}
cleanPath = cleanPath.slice(0, end);
}
// Normalize backslashes to forward slashes first to support cross-platform path parsing (e.g. Windows paths on POSIX)
const normalizedPath = cleanPath.replace(/\\/g, '/');
// Consistent path resolution using a single, robust helper to handle traversals (. or ..) and absolute/relative conversions
let resolvedPath = normalizedPath;
try {
resolvedPath = resolveToRealPath(normalizedPath);
} catch {
resolvedPath = path.resolve(normalizedPath);
}
const basename = path.basename(resolvedPath);
if (EXACT_BUILD_FILENAMES.has(basename)) {
return true;
}
// Handle Dockerfile.<suffix> and Containerfile.<suffix>
if (
basename.startsWith('Dockerfile.') ||
basename.startsWith('Containerfile.')
) {
return true;
}
const ext = path.extname(basename).toLowerCase();
if (BUILD_FILE_EXTENSIONS.has(ext)) {
return true;
}
return false;
}
interface FilePathLike {
file_path?: unknown;
path?: unknown;
filePath?: unknown;
file?: unknown;
}
/****************************************************************ESLint-Bypass****************************************************************/
function isFilePathLike(value: unknown): value is FilePathLike {
return typeof value === 'object' && value !== null;
}
/**
* Extracts a target file path from tool invocation arguments if present.
*/
export function extractFilePathFromArgs(args: unknown): string | undefined {
if (!isFilePathLike(args)) {
return undefined;
}
const target = args.file_path ?? args.path ?? args.filePath ?? args.file;
return typeof target === 'string' ? target : undefined;
}
|