fix(security): constrain image fallback hook to workspace (#1232)

This commit is contained in:
Kai (Tam Nhu) Tran authored and GitHub committed 2026-05-12 18:13:32 -04:00
1 parent 349db830df
commit 40300c286e
3 files changed
+106 -5

No files matched your search

+49 -3
View File
@@ -379,6 +379,47 @@ function outputSuccess(filePath, description, model, fileSize) {
/**
* Determine if hook should skip, with debug logging
*/
function normalizePathForComparison(value) {
return process.platform === 'win32' ? value.toLowerCase() : value;
}
function isPathWithinWorkspace(workspaceRoot, candidatePath) {
const relativePath = path.relative(workspaceRoot, candidatePath);
return (
relativePath === '' ||
(!relativePath.startsWith('..') &&
!relativePath.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relativePath))
);
}
function resolveWorkspaceFilePath(filePath) {
const workspaceRoot = (() => {
try {
return fs.realpathSync(process.cwd());
} catch {
return path.resolve(process.cwd());
}
})();
const absolutePath = path.resolve(process.cwd(), filePath);
const realFilePath = fs.realpathSync(absolutePath);
if (
!isPathWithinWorkspace(
normalizePathForComparison(workspaceRoot),
normalizePathForComparison(realFilePath)
)
) {
debugLog('Skipping: file is outside the current workspace', {
workspaceRoot,
filePath: realFilePath,
});
return null;
}
return realFilePath;
}
function shouldSkipHook() {
if (process.env.CCS_IMAGE_ANALYSIS_SKIP_HOOK === '1') {
debugLog('Skipping: CCS_IMAGE_ANALYSIS_SKIP_HOOK=1');
@@ -484,11 +525,16 @@ async function processHook() {
process.exit(0);
}
const debugContext = getDebugContext(filePath, null);
const workspaceFilePath = resolveWorkspaceFilePath(filePath);
if (!workspaceFilePath) {
process.exit(0);
}
const debugContext = getDebugContext(workspaceFilePath, null);
debugLog('Image analysis runtime prepared', debugContext);
const result = await analyzeFile(filePath);
outputSuccess(filePath, result.description, result.model, result.fileSize);
const result = await analyzeFile(workspaceFilePath);
outputSuccess(workspaceFilePath, result.description, result.model, result.fileSize);
} catch (err) {
if (process.env.CCS_DEBUG) {
console.error('[CCS Hook] Error:', err.message);