fix(hooks): add edge case validation in image analyzer

- Empty model validation: check `model.trim()` before using
- 10MB boundary: use `>=` instead of `>` for consistent messaging
- Timeout clamping: ensure timeout is between 1-600 seconds
- Response stream error: add error handler for network failures
- Empty response validation: check before JSON.parse to prevent crashes
This commit is contained in:
kaitranntt
2026-02-03 21:44:44 -05:00
parent 40caff13ad
commit 0e7b9c9190
+13 -4
View File
@@ -75,7 +75,7 @@ function parseProviderModels(envValue) {
const result = {};
envValue.split(',').forEach((pair) => {
const [provider, model] = pair.split(':');
if (provider && model) {
if (provider && model && model.trim()) {
result[provider.trim()] = model.trim();
}
});
@@ -200,12 +200,21 @@ function analyzeViaCliProxy(base64Data, mediaType, model, timeoutMs) {
data += chunk;
});
res.on('error', (err) => {
reject(err);
});
res.on('end', () => {
if (res.statusCode !== 200) {
reject(new Error(`CLIProxy returned status ${res.statusCode}: ${data}`));
return;
}
if (!data || !data.trim()) {
reject(new Error('Empty response from CLIProxy'));
return;
}
try {
const response = JSON.parse(data);
const text = response.content?.[0]?.text;
@@ -384,8 +393,8 @@ async function processHook() {
// Check file size
const stats = fs.statSync(filePath);
if (stats.size > MAX_FILE_SIZE_BYTES) {
outputError(filePath, `File too large (${(stats.size / 1024 / 1024).toFixed(2)}MB > ${MAX_FILE_SIZE_MB}MB)`);
if (stats.size >= MAX_FILE_SIZE_BYTES) {
outputError(filePath, `File too large (${(stats.size / 1024 / 1024).toFixed(2)}MB >= ${MAX_FILE_SIZE_MB}MB)`);
return;
}
@@ -401,7 +410,7 @@ async function processHook() {
const model = getModelForProvider();
const timeout = parseInt(process.env.CCS_IMAGE_ANALYSIS_TIMEOUT || DEFAULT_TIMEOUT_SEC, 10);
const timeoutMs = timeout * 1000;
const timeoutMs = Math.max(1, Math.min(600, timeout)) * 1000;
if (process.env.CCS_DEBUG) {
console.error(`[CCS Hook] Analyzing ${path.basename(filePath)} via CLIProxy (${model})`);