mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-13 08:15:17 +00:00
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:
1 parent
40caff13ad
commit
0e7b9c9190
1 file changed
+13
-4
@@ -75,7 +75,7 @@ function parseProviderModels(envValue) {
|
|||||||
const result = {};
|
const result = {};
|
||||||
envValue.split(',').forEach((pair) => {
|
envValue.split(',').forEach((pair) => {
|
||||||
const [provider, model] = pair.split(':');
|
const [provider, model] = pair.split(':');
|
||||||
if (provider && model) {
|
if (provider && model && model.trim()) {
|
||||||
result[provider.trim()] = model.trim();
|
result[provider.trim()] = model.trim();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -200,12 +200,21 @@ function analyzeViaCliProxy(base64Data, mediaType, model, timeoutMs) {
|
|||||||
data += chunk;
|
data += chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
res.on('error', (err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
res.on('end', () => {
|
res.on('end', () => {
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200) {
|
||||||
reject(new Error(`CLIProxy returned status ${res.statusCode}: ${data}`));
|
reject(new Error(`CLIProxy returned status ${res.statusCode}: ${data}`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!data || !data.trim()) {
|
||||||
|
reject(new Error('Empty response from CLIProxy'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = JSON.parse(data);
|
const response = JSON.parse(data);
|
||||||
const text = response.content?.[0]?.text;
|
const text = response.content?.[0]?.text;
|
||||||
@@ -384,8 +393,8 @@ async function processHook() {
|
|||||||
|
|
||||||
// Check file size
|
// Check file size
|
||||||
const stats = fs.statSync(filePath);
|
const stats = fs.statSync(filePath);
|
||||||
if (stats.size > MAX_FILE_SIZE_BYTES) {
|
if (stats.size >= MAX_FILE_SIZE_BYTES) {
|
||||||
outputError(filePath, `File too large (${(stats.size / 1024 / 1024).toFixed(2)}MB > ${MAX_FILE_SIZE_MB}MB)`);
|
outputError(filePath, `File too large (${(stats.size / 1024 / 1024).toFixed(2)}MB >= ${MAX_FILE_SIZE_MB}MB)`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,7 +410,7 @@ async function processHook() {
|
|||||||
|
|
||||||
const model = getModelForProvider();
|
const model = getModelForProvider();
|
||||||
const timeout = parseInt(process.env.CCS_IMAGE_ANALYSIS_TIMEOUT || DEFAULT_TIMEOUT_SEC, 10);
|
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) {
|
if (process.env.CCS_DEBUG) {
|
||||||
console.error(`[CCS Hook] Analyzing ${path.basename(filePath)} via CLIProxy (${model})`);
|
console.error(`[CCS Hook] Analyzing ${path.basename(filePath)} via CLIProxy (${model})`);
|
||||||
|
|||||||
Reference in new issue
Block a user