mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-02 12:12:17 +00:00
* feat: introduce optional content protection seam * feat: enforce source-aware content filters * feat: complete source-aware content enforcement * test: activate skill file-text fail-close fixtures * fix: harden source-aware content filters * fix: harden model-bound content filtering * fix: preserve legacy filters and generated files * fix: inspect shared scalar metadata * test: align mocks with current dev dependencies * feat: add persisted content filter safeguards * feat: complete source-aware content filter enforcement * fix: move resume content preflight into TypeScript * fix: close content inspection edge cases * fix: harden content protection boundaries * fix: complete content protection safeguards * test: align persisted memory filter coverage * fix: reconcile content protection with current dev * fix: reconcile content protection with latest dev * fix: close content protection review gaps * fix: enforce source-aware provider boundaries * fix: preserve legacy PII preflight semantics * test: stabilize stored branch preflight fixture * fix: defer agent writes until protected model admission * perf: harden source-aware model-bound filtering * fix: canonicalize provider lineage before validation * fix: satisfy model-bound callback type checks * perf: Bound content protection filtering work * fix: Bound submission array traversal * fix: Stabilize bounded content snapshots * fix: Scope model-bound traversal overflows * fix: Preserve scoped content inspection * fix: Accumulate aggregate traversal scopes * fix: centralize content policy boundaries * test: align deferred tool policy context * test: align controller policy mocks * style: normalize content protection imports * fix: close content policy review gaps * fix: narrow active skill policy config * fix: address content protection review boundaries * fix: retain exact provenance overflow sentinel * fix: preserve literal and scoped provenance updates * fix: narrow persisted edit provenance * fix: isolate exact overflow attribution * fix: centralize stored prompt protection * fix: fail closed on incomplete transcript evidence * fix: align canonical transcript routing * refactor: centralize content policy preflights * fix: isolate upload policy error typing * style: sort policy preflight imports * refactor: centralize content policy boundaries
133 lines
3.7 KiB
JavaScript
133 lines
3.7 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs').promises;
|
|
const express = require('express');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const {
|
|
getSafeErrorMetadata,
|
|
shouldUseUploadSse,
|
|
startUploadSseStream,
|
|
sendUploadPolicyError,
|
|
resolveUploadErrorMessage,
|
|
verifyAgentUploadPermission,
|
|
assertUploadContentAllowed,
|
|
hasActiveFilePolicy,
|
|
sanitizeFilename,
|
|
} = require('@librechat/api');
|
|
const {
|
|
isAssistantsEndpoint,
|
|
hasActivePiiPatterns,
|
|
mergeFileConfig,
|
|
} = require('librechat-data-provider');
|
|
const {
|
|
processAgentFileUpload,
|
|
processImageFile,
|
|
filterFile,
|
|
} = require('~/server/services/Files/process');
|
|
const { checkPermission } = require('~/server/services/PermissionService');
|
|
const db = require('~/models');
|
|
|
|
const router = express.Router();
|
|
|
|
router.post('/', async (req, res) => {
|
|
const metadata = req.body;
|
|
const appConfig = req.config;
|
|
|
|
/** Opened only once auth/validation has passed, right before the potentially
|
|
* long-running upload processing begins — see `startUploadSseStream`. */
|
|
let sseStream = null;
|
|
const openSseStreamIfRequested = () => {
|
|
if (shouldUseUploadSse(req)) {
|
|
sseStream = startUploadSseStream(res);
|
|
}
|
|
};
|
|
|
|
try {
|
|
req.file.originalname = sanitizeFilename(req.file.originalname);
|
|
filterFile({ req, image: true });
|
|
|
|
await assertUploadContentAllowed({
|
|
filters: req.config?.filters,
|
|
file: req.file,
|
|
endpoint: metadata.endpoint,
|
|
toolResource: metadata.tool_resource,
|
|
fileConfig: mergeFileConfig(req.config?.fileConfig),
|
|
ocrConfigured: req.config?.ocr != null,
|
|
ragConfigured: !!process.env.RAG_API_URL,
|
|
rawFileMode: 'opaque',
|
|
});
|
|
|
|
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: db.getAgent,
|
|
checkPermission,
|
|
});
|
|
if (denied) {
|
|
return;
|
|
}
|
|
openSseStreamIfRequested();
|
|
return await processAgentFileUpload({ req, res, metadata, sseStream });
|
|
}
|
|
|
|
openSseStreamIfRequested();
|
|
await processImageFile({ req, res, metadata, sseStream });
|
|
} catch (error) {
|
|
// TODO: delete remote file if it exists
|
|
logger.error('[/files/images] Error processing file:', getSafeErrorMetadata(error));
|
|
|
|
try {
|
|
const filepath = path.join(
|
|
appConfig.paths.imageOutput,
|
|
req.user.id,
|
|
path.basename(req.file.filename),
|
|
);
|
|
await fs.unlink(filepath);
|
|
} catch (cleanupError) {
|
|
logger.error('[/files/images] Error deleting file:', getSafeErrorMetadata(cleanupError));
|
|
}
|
|
if (
|
|
sendUploadPolicyError(res, sseStream, error, {
|
|
tempFileId: metadata.temp_file_id,
|
|
toolResource: metadata.tool_resource,
|
|
})
|
|
) {
|
|
return;
|
|
}
|
|
const contentProtectionActive =
|
|
hasActiveFilePolicy(req.config?.filters) ||
|
|
hasActivePiiPatterns(req.config?.messageFilter?.pii);
|
|
const message = resolveUploadErrorMessage(
|
|
error,
|
|
'Error processing file',
|
|
contentProtectionActive,
|
|
);
|
|
if (sseStream) {
|
|
sseStream.sendError({
|
|
message,
|
|
code: 500,
|
|
temp_file_id: metadata.temp_file_id,
|
|
tool_resource: metadata.tool_resource,
|
|
display_to_user: true,
|
|
});
|
|
} else {
|
|
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');
|
|
}
|
|
if (sseStream) {
|
|
sseStream.close();
|
|
}
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|