mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-09-04 13:38:46 +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
188 lines
6.2 KiB
JavaScript
188 lines
6.2 KiB
JavaScript
const {
|
|
handleError,
|
|
applyModelSpecPreset,
|
|
findModelSpecByName,
|
|
isModelSpecEndpointMatch,
|
|
resolveModelSpecPromptPrefixVariables,
|
|
inspectContent,
|
|
extractChatContent,
|
|
contentFilterBlockResponse,
|
|
} = require('@librechat/api');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const {
|
|
EndpointURLs,
|
|
EModelEndpoint,
|
|
isAgentsEndpoint,
|
|
parseCompactConvo,
|
|
getDefaultParamsEndpoint,
|
|
} = require('librechat-data-provider');
|
|
const azureAssistants = require('~/server/services/Endpoints/azureAssistants');
|
|
const assistants = require('~/server/services/Endpoints/assistants');
|
|
const { getEndpointsConfig } = require('~/server/services/Config');
|
|
const agents = require('~/server/services/Endpoints/agents');
|
|
const { updateFilesUsage } = require('~/models');
|
|
|
|
const buildFunction = {
|
|
[EModelEndpoint.agents]: agents.buildOptions,
|
|
[EModelEndpoint.assistants]: assistants.buildOptions,
|
|
[EModelEndpoint.azureAssistants]: azureAssistants.buildOptions,
|
|
};
|
|
|
|
/**
|
|
* Inspects only the user-authored value substituted for `{{current_user}}`.
|
|
* The surrounding model-spec prompt is administrator-authored, so treating the
|
|
* entire resolved prompt as user provenance would incorrectly apply user
|
|
* content policy to static deployment configuration.
|
|
*
|
|
* `extractChatContent` deliberately gives prompt prefixes both prompt and
|
|
* agent-instruction semantics, preserving the source-specific field controls
|
|
* for the exact value that becomes model-bound.
|
|
*
|
|
* @param {ServerRequest} req
|
|
* @param {unknown} promptPrefixTemplate
|
|
* @returns {import('@librechat/api').ProtectionFinding | null}
|
|
*/
|
|
function inspectResolvedCurrentUser(req, promptPrefixTemplate) {
|
|
if (
|
|
typeof promptPrefixTemplate !== 'string' ||
|
|
!/{{\s*current_user\s*}}/i.test(promptPrefixTemplate) ||
|
|
!req.user?.name
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return inspectContent(extractChatContent({ promptPrefix: String(req.user.name) }), {
|
|
filters: req.config?.filters,
|
|
});
|
|
}
|
|
|
|
async function buildEndpointOption(req, res, next) {
|
|
const { endpoint, endpointType } = req.body;
|
|
const isAgents =
|
|
isAgentsEndpoint(endpoint) || req.baseUrl.startsWith(EndpointURLs[EModelEndpoint.agents]);
|
|
|
|
let endpointsConfig;
|
|
try {
|
|
endpointsConfig = await getEndpointsConfig(req);
|
|
} catch (error) {
|
|
logger.error('Error fetching endpoints config in buildEndpointOption', error);
|
|
}
|
|
|
|
const defaultParamsEndpoint = getDefaultParamsEndpoint(endpointsConfig, endpoint);
|
|
|
|
let parsedBody;
|
|
try {
|
|
parsedBody = parseCompactConvo({
|
|
endpoint,
|
|
endpointType,
|
|
conversation: req.body,
|
|
defaultParamsEndpoint,
|
|
});
|
|
} catch (error) {
|
|
logger.error('Error parsing compact conversation', error);
|
|
return handleError(res, { text: 'Error parsing conversation' });
|
|
}
|
|
|
|
const appConfig = req.config;
|
|
let appliedModelSpecPrivateFields = new Set();
|
|
if (appConfig.modelSpecs?.list?.length && appConfig.modelSpecs?.enforce) {
|
|
/** @type {{ list: TModelSpec[] }}*/
|
|
const { list } = appConfig.modelSpecs;
|
|
const rawSpec = req.body.spec;
|
|
const spec = parsedBody.spec ?? (typeof rawSpec === 'string' ? rawSpec : undefined);
|
|
const rawChatProjectId = req.body.chatProjectId;
|
|
const parsedBodyForModelSpec =
|
|
parsedBody.chatProjectId === undefined &&
|
|
(typeof rawChatProjectId === 'string' || rawChatProjectId === null)
|
|
? { ...parsedBody, chatProjectId: rawChatProjectId }
|
|
: parsedBody;
|
|
|
|
if (!spec) {
|
|
return handleError(res, { text: 'No model spec selected' });
|
|
}
|
|
|
|
const currentModelSpec = findModelSpecByName({ list }, spec);
|
|
if (!currentModelSpec) {
|
|
return handleError(res, { text: 'Invalid model spec' });
|
|
}
|
|
|
|
if (!isModelSpecEndpointMatch(currentModelSpec, endpoint)) {
|
|
return handleError(res, { text: 'Model spec mismatch' });
|
|
}
|
|
|
|
try {
|
|
const result = applyModelSpecPreset({
|
|
modelSpec: currentModelSpec,
|
|
parsedBody: parsedBodyForModelSpec,
|
|
endpoint,
|
|
endpointType,
|
|
defaultParamsEndpoint,
|
|
includePresetDefaults: true,
|
|
});
|
|
parsedBody = result.parsedBody;
|
|
appliedModelSpecPrivateFields = result.appliedPrivateFields;
|
|
} catch (error) {
|
|
logger.error('Error parsing model spec', error);
|
|
return handleError(res, { text: 'Error parsing model spec' });
|
|
}
|
|
} else if (parsedBody.spec && appConfig.modelSpecs?.list) {
|
|
const modelSpec = findModelSpecByName(appConfig.modelSpecs, parsedBody.spec);
|
|
if (modelSpec) {
|
|
if (!isModelSpecEndpointMatch(modelSpec, endpoint)) {
|
|
return handleError(res, { text: 'Model spec mismatch' });
|
|
}
|
|
|
|
try {
|
|
const result = applyModelSpecPreset({
|
|
modelSpec,
|
|
parsedBody,
|
|
endpoint,
|
|
endpointType,
|
|
defaultParamsEndpoint,
|
|
});
|
|
parsedBody = result.parsedBody;
|
|
appliedModelSpecPrivateFields = result.appliedPrivateFields;
|
|
} catch (error) {
|
|
logger.error('Error parsing model spec', error);
|
|
return handleError(res, { text: 'Error parsing model spec' });
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isAgents && appliedModelSpecPrivateFields.has('promptPrefix')) {
|
|
const promptPrefixTemplate = parsedBody.promptPrefix;
|
|
parsedBody = resolveModelSpecPromptPrefixVariables(
|
|
parsedBody,
|
|
req.user,
|
|
req.body.clientTimestamp,
|
|
);
|
|
const finding = inspectResolvedCurrentUser(req, promptPrefixTemplate);
|
|
if (finding != null) {
|
|
return res.status(400).json(contentFilterBlockResponse(finding));
|
|
}
|
|
}
|
|
|
|
try {
|
|
const builder = isAgents
|
|
? (...args) => buildFunction[EModelEndpoint.agents](req, ...args)
|
|
: buildFunction[endpointType ?? endpoint];
|
|
|
|
// TODO: use object params
|
|
req.body = req.body || {}; // Express 5: ensure req.body exists
|
|
req.body.endpointOption = await builder(endpoint, parsedBody, endpointType);
|
|
|
|
if (req.body.files && !isAgents) {
|
|
req.body.endpointOption.attachments = updateFilesUsage(req.body.files, undefined, {
|
|
user: req.user.id,
|
|
tenantId: req.user.tenantId,
|
|
});
|
|
}
|
|
|
|
next();
|
|
} catch (error) {
|
|
logger.error('Error building endpoint option', error);
|
|
return handleError(res, { text: 'Error building endpoint option' });
|
|
}
|
|
}
|
|
|
|
module.exports = buildEndpointOption;
|