mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-15 12:16:33 +01:00
* fix: add agent permission check to image upload route
* refactor: remove unused SystemRoles import and format test file for clarity
* fix: address review findings for image upload agent permission check
* refactor: move agent upload auth logic to TypeScript in packages/api
Extract pure authorization logic from agentPermCheck.js into
checkAgentUploadAuth() in packages/api/src/files/agentUploadAuth.ts.
The function returns a structured result ({ allowed, status, error })
instead of writing HTTP responses directly, eliminating the dual
responsibility and confusing sentinel return value. The JS wrapper
in /api is now a thin adapter that translates the result to HTTP.
* test: rewrite image upload permission tests as integration tests
Replace mock-heavy images-agent-perm.spec.js with integration tests
using MongoMemoryServer, real models, and real PermissionService.
Follows the established pattern in files.agents.test.js. Moves test
to sibling location (images.agents.test.js) matching backend convention.
Adds temp file cleanup assertions on 403/404 responses and covers
message_file exemption paths (boolean true, string "true", false).
* fix: widen AgentUploadAuthDeps types to accept ObjectId from Mongoose
The injected getAgent returns Mongoose documents where _id and author
are Types.ObjectId at runtime, not string. Widen the DI interface to
accept string | Types.ObjectId for _id, author, and resourceId so the
contract accurately reflects real callers.
* chore: move agent upload auth into files/agents/ subdirectory
* refactor: delete agentPermCheck.js wrapper, move verifyAgentUploadPermission to packages/api
The /api-only dependencies (getAgent, checkPermission) are now passed
as object-field params from the route call sites. Both images.js and
files.js import verifyAgentUploadPermission from @librechat/api and
inject the deps directly, eliminating the intermediate JS wrapper.
* style: fix import type ordering in agent upload auth
* fix: prevent token TTL race in MCPTokenStorage.storeTokens
When expires_in is provided, use it directly instead of round-tripping
through Date arithmetic. The previous code computed accessTokenExpiry
as a Date, then after an async encryptV2 call, recomputed expiresIn by
subtracting Date.now(). On loaded CI runners the elapsed time caused
Math.floor to truncate to 0, triggering the 1-year fallback and making
the token appear permanently valid — so refresh never fired.
77 lines
2.1 KiB
JavaScript
77 lines
2.1 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs').promises;
|
|
const express = require('express');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const { verifyAgentUploadPermission } = require('@librechat/api');
|
|
const { isAssistantsEndpoint } = require('librechat-data-provider');
|
|
const {
|
|
processAgentFileUpload,
|
|
processImageFile,
|
|
filterFile,
|
|
} = require('~/server/services/Files/process');
|
|
const { checkPermission } = require('~/server/services/PermissionService');
|
|
const { getAgent } = require('~/models/Agent');
|
|
|
|
const router = express.Router();
|
|
|
|
router.post('/', async (req, res) => {
|
|
const metadata = req.body;
|
|
const appConfig = req.config;
|
|
|
|
try {
|
|
filterFile({ req, image: true });
|
|
|
|
metadata.temp_file_id = metadata.file_id;
|
|
metadata.file_id = req.file_id;
|
|
|
|
if (!isAssistantsEndpoint(metadata.endpoint) && metadata.tool_resource != null) {
|
|
const denied = await verifyAgentUploadPermission({
|
|
req,
|
|
res,
|
|
metadata,
|
|
getAgent,
|
|
checkPermission,
|
|
});
|
|
if (denied) {
|
|
return;
|
|
}
|
|
return await processAgentFileUpload({ req, res, metadata });
|
|
}
|
|
|
|
await processImageFile({ req, res, metadata });
|
|
} catch (error) {
|
|
// TODO: delete remote file if it exists
|
|
logger.error('[/files/images] Error processing file:', error);
|
|
|
|
let message = 'Error processing file';
|
|
|
|
if (
|
|
error.message?.includes('Invalid file format') ||
|
|
error.message?.includes('No OCR result') ||
|
|
error.message?.includes('exceeds token limit')
|
|
) {
|
|
message = error.message;
|
|
}
|
|
|
|
try {
|
|
const filepath = path.join(
|
|
appConfig.paths.imageOutput,
|
|
req.user.id,
|
|
path.basename(req.file.filename),
|
|
);
|
|
await fs.unlink(filepath);
|
|
} catch (error) {
|
|
logger.error('[/files/images] Error deleting file:', error);
|
|
}
|
|
res.status(500).json({ message });
|
|
} finally {
|
|
try {
|
|
await fs.unlink(req.file.path);
|
|
logger.debug('[/files/images] Temp. image upload file deleted');
|
|
} catch {
|
|
logger.debug('[/files/images] Temp. image upload file already deleted');
|
|
}
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|