mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Multer assumes latin1 for multipart filenames, and the skills routes have no decode step, so a non-ASCII name reached the handlers garbled. A file name content filter could not match it on import, and the file upload route stored the garbled name.
249 lines
8.3 KiB
JavaScript
249 lines
8.3 KiB
JavaScript
const path = require('path');
|
|
const multer = require('multer');
|
|
const express = require('express');
|
|
const {
|
|
createImportHandler,
|
|
createSkillUploadHandler,
|
|
createSkillBlobReserver,
|
|
createSkillBufferWriter,
|
|
createSkillStorageResolver,
|
|
generateCheckAccess,
|
|
restoreTenantContextFromReq,
|
|
} = require('@librechat/api');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const {
|
|
PermissionBits,
|
|
PermissionTypes,
|
|
Permissions,
|
|
mergeFileConfig,
|
|
} = require('librechat-data-provider');
|
|
const {
|
|
createSkill,
|
|
getSkillById,
|
|
upsertSkillFile,
|
|
getSkillFileByPath,
|
|
reserveSkillUploadIntent,
|
|
transitionSkillUploadIntent,
|
|
getRoleByName,
|
|
} = require('~/models');
|
|
const { requireJwtAuth, canAccessSkillResource } = require('~/server/middleware');
|
|
const { grantPermission } = require('~/server/services/PermissionService');
|
|
const { getStrategyFunctions } = require('~/server/services/Files/strategies');
|
|
const { createFileLimiters } = require('~/server/middleware/limiters/uploadLimiters');
|
|
const { maybeRunGitHubSkillSyncForRequest } = require('~/server/services/Skills/sync');
|
|
const { deleteSkillForLegacy } = require('~/server/services/Skills/deletion');
|
|
const configMiddleware = require('~/server/middleware/config/app');
|
|
const { getFileStrategy } = require('~/server/utils/getFileStrategy');
|
|
|
|
const router = express.Router();
|
|
const reserveSkillBlobForConfig = createSkillBlobReserver({
|
|
getFileStrategy,
|
|
getStrategyFunctions,
|
|
});
|
|
const resolveSkillStorageForConfig = createSkillStorageResolver({
|
|
getFileStrategy,
|
|
getStrategyFunctions,
|
|
});
|
|
const saveSkillBuffer = createSkillBufferWriter({ getStrategyFunctions });
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Multer: memory storage for skill imports (zip processed in-memory)
|
|
// ---------------------------------------------------------------------------
|
|
const ALLOWED_EXTENSIONS = new Set(['.md', '.zip', '.skill']);
|
|
const MAX_IMPORT_SIZE = 50 * 1024 * 1024; // 50 MB
|
|
|
|
const memoryStorage = multer.memoryStorage();
|
|
// Browsers send multipart filenames as raw UTF-8, and multer assumes latin1 unless told otherwise.
|
|
const defParamCharset = 'utf8';
|
|
|
|
function getSkillImportSizeLimit(req) {
|
|
const fileConfig = mergeFileConfig(req.config?.fileConfig);
|
|
return fileConfig.skills?.fileSizeLimit ?? MAX_IMPORT_SIZE;
|
|
}
|
|
|
|
const skillImportFilter = (_req, file, cb) => {
|
|
const ext = path.extname(file.originalname).toLowerCase();
|
|
if (ALLOWED_EXTENSIONS.has(ext)) {
|
|
cb(null, true);
|
|
} else {
|
|
// N.B. The error handler at the bottom of this file matches this "Only " prefix.
|
|
cb(new Error('Only .md, .zip, and .skill files are allowed'), false);
|
|
}
|
|
};
|
|
|
|
const skillUpload = (req, res, next) =>
|
|
multer({
|
|
storage: memoryStorage,
|
|
defParamCharset,
|
|
fileFilter: skillImportFilter,
|
|
limits: { fileSize: getSkillImportSizeLimit(req) },
|
|
}).single('file')(req, res, next);
|
|
|
|
// Per-file upload (for adding individual files to an existing skill)
|
|
const MAX_SINGLE_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
|
|
const singleFileUpload = multer({
|
|
storage: memoryStorage,
|
|
defParamCharset,
|
|
limits: { fileSize: MAX_SINGLE_FILE_SIZE },
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Role-based capability gates
|
|
// ---------------------------------------------------------------------------
|
|
const checkSkillAccess = generateCheckAccess({
|
|
permissionType: PermissionTypes.SKILLS,
|
|
permissions: [Permissions.USE],
|
|
getRoleByName,
|
|
});
|
|
const checkSkillCreate = generateCheckAccess({
|
|
permissionType: PermissionTypes.SKILLS,
|
|
permissions: [Permissions.USE, Permissions.CREATE],
|
|
getRoleByName,
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rate limiters (reuse existing file upload limiters)
|
|
// ---------------------------------------------------------------------------
|
|
const { fileUploadIpLimiter, fileUploadUserLimiter } = createFileLimiters();
|
|
|
|
router.use(requireJwtAuth);
|
|
router.use(configMiddleware);
|
|
router.use(checkSkillAccess);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// CRUD handlers
|
|
// ---------------------------------------------------------------------------
|
|
const { getSkillsHandlers } = require('~/server/services/Skills/handlers');
|
|
const handlers = getSkillsHandlers();
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// File storage helper: resolve the active strategy's saveBuffer
|
|
// ---------------------------------------------------------------------------
|
|
const resolveSkillStorage = (req, options) => resolveSkillStorageForConfig(req.config, options);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Import handler (zip/md/skill → create skill + files)
|
|
// ---------------------------------------------------------------------------
|
|
const importHandler = createImportHandler({
|
|
limits: (req) => ({
|
|
maxZipBytes: getSkillImportSizeLimit(req),
|
|
}),
|
|
createSkill,
|
|
getSkillById,
|
|
deleteSkill: deleteSkillForLegacy,
|
|
upsertSkillFile,
|
|
getSkillFileByPath,
|
|
reserveSkillUploadIntent,
|
|
transitionSkillUploadIntent,
|
|
reserveBlob: (req, options) => reserveSkillBlobForConfig(req.config, options),
|
|
saveBuffer: (_req, options) => saveSkillBuffer(options),
|
|
deleteFile: (req, file) => {
|
|
const { deleteFile } = getStrategyFunctions(file.source);
|
|
if (!deleteFile) throw new Error(`Skill storage backend ${file.source} cannot delete files`);
|
|
return deleteFile(req, file);
|
|
},
|
|
grantPermission,
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Per-file upload handler (add a single file to an existing skill)
|
|
// ---------------------------------------------------------------------------
|
|
const uploadFileHandler = createSkillUploadHandler({
|
|
getSkillById,
|
|
getSkillFileByPath,
|
|
upsertSkillFile,
|
|
resolveStorage: resolveSkillStorage,
|
|
getStrategyFunctions,
|
|
reserveSkillUploadIntent,
|
|
transitionSkillUploadIntent,
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Routes
|
|
// ---------------------------------------------------------------------------
|
|
async function maybeStartRequestSkillSync(req, _res, next) {
|
|
try {
|
|
await maybeRunGitHubSkillSyncForRequest(req);
|
|
} catch (error) {
|
|
logger.error('[GET /skills] Failed to start request-scoped skill sync:', error);
|
|
}
|
|
next();
|
|
}
|
|
|
|
// Import: accepts .md / .zip / .skill via multipart
|
|
router.post(
|
|
'/import',
|
|
checkSkillCreate,
|
|
fileUploadIpLimiter,
|
|
fileUploadUserLimiter,
|
|
skillUpload,
|
|
restoreTenantContextFromReq,
|
|
importHandler,
|
|
);
|
|
|
|
router.get('/', maybeStartRequestSkillSync, handlers.list);
|
|
router.post('/', checkSkillCreate, handlers.create);
|
|
|
|
router.get(
|
|
'/:id',
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.VIEW }),
|
|
handlers.get,
|
|
);
|
|
|
|
router.patch(
|
|
'/:id',
|
|
checkSkillCreate,
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.EDIT }),
|
|
handlers.patch,
|
|
);
|
|
|
|
router.delete(
|
|
'/:id',
|
|
checkSkillCreate,
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.DELETE }),
|
|
handlers.delete,
|
|
);
|
|
|
|
router.get(
|
|
'/:id/files',
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.VIEW }),
|
|
handlers.listFiles,
|
|
);
|
|
|
|
// Legacy upload and revision-checked editing. Older servers have no POST wildcard route.
|
|
router.post(
|
|
['/:id/files', '/:id/files/*relativePath'],
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.EDIT }),
|
|
fileUploadIpLimiter,
|
|
fileUploadUserLimiter,
|
|
singleFileUpload.single('file'),
|
|
restoreTenantContextFromReq,
|
|
uploadFileHandler,
|
|
);
|
|
|
|
// Wildcard splat (`*relativePath`) captures nested skill paths (e.g.
|
|
// `references/guide.md`) whether the client sends an encoded `%2F` or a proxy
|
|
// has already decoded it to a literal slash. A single `:relativePath` segment
|
|
// 404s in the latter case, which is why nested files failed behind proxies.
|
|
router.get(
|
|
'/:id/files/*relativePath',
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.VIEW }),
|
|
handlers.downloadFile,
|
|
);
|
|
|
|
router.delete(
|
|
'/:id/files/*relativePath',
|
|
canAccessSkillResource({ requiredPermission: PermissionBits.EDIT }),
|
|
handlers.deleteFile,
|
|
);
|
|
|
|
// Multer + file-filter error handler — surface as 400, forward everything else
|
|
|
|
router.use((err, _req, res, next) => {
|
|
if (err && (err.name === 'MulterError' || err.message?.startsWith('Only '))) {
|
|
return res.status(400).json({ error: err.message });
|
|
}
|
|
return next(err);
|
|
});
|
|
|
|
module.exports = router;
|