From 5d5c2225f6f6e979f32fbfee19e2a33cf86c52bd Mon Sep 17 00:00:00 2001 From: Dustin Healy <54083382+dustinhealy@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:20:20 -0700 Subject: [PATCH] fix: narrow skill management CRUD and honor blob owners --- .../agents/__tests__/openapi.contract.spec.js | 10 +- .../routes/agents/__tests__/skills.spec.js | 1 - api/server/routes/agents/skills.js | 1 - api/server/routes/skills.test.js | 3 +- api/server/services/Files/Azure/crud.js | 34 ++-- api/server/services/Files/Firebase/crud.js | 46 ++---- .../Files/Local/__tests__/crud-delete.spec.js | 94 ----------- api/server/services/Files/Local/crud.js | 99 +----------- docs/skills-management-api.md | 10 +- packages/api/openapi/agents.openapi.json | 148 ------------------ packages/api/src/openapi/skills.ts | 15 -- packages/api/src/skills/handlers.spec.ts | 64 ++++++++ packages/api/src/skills/management.spec.ts | 22 --- packages/api/src/skills/management.ts | 22 +-- packages/api/src/storage/index.ts | 1 + packages/api/src/storage/local-delete.spec.ts | 69 ++++++++ packages/api/src/storage/local.ts | 71 +++++++++ .../api/src/storage/ownedBlobDelete.spec.ts | 60 +++++++ packages/api/src/storage/ownedBlobDelete.ts | 61 ++++++++ 19 files changed, 359 insertions(+), 472 deletions(-) delete mode 100644 api/server/services/Files/Local/__tests__/crud-delete.spec.js create mode 100644 packages/api/src/storage/local-delete.spec.ts create mode 100644 packages/api/src/storage/ownedBlobDelete.spec.ts create mode 100644 packages/api/src/storage/ownedBlobDelete.ts diff --git a/api/server/routes/agents/__tests__/openapi.contract.spec.js b/api/server/routes/agents/__tests__/openapi.contract.spec.js index 8842196c30..7c44bae8d1 100644 --- a/api/server/routes/agents/__tests__/openapi.contract.spec.js +++ b/api/server/routes/agents/__tests__/openapi.contract.spec.js @@ -268,7 +268,6 @@ describe('Agents OpenAPI actual HTTP contract', () => { app.post('/api/agents/v1/skills', skillHandlers.create); app.put('/api/agents/v1/skills/:id/files/*relativePath', skillHandlers.updateFile); app.delete('/api/agents/v1/skills/:id/files/*relativePath', skillHandlers.deleteFile); - app.delete('/api/agents/v1/skills/:id', skillHandlers.delete); app.get('/api/agents/v1/agents/:id', (_req, _res, next) => next(new Error('contract fixture escaped error')), ); @@ -358,7 +357,7 @@ describe('Agents OpenAPI actual HTTP contract', () => { expect(fs.readFileSync(diskPath, 'utf8')).toBe(content); }); - it('documents and validates Skill creation and complete deletion cleanup', async () => { + it('documents and validates Skill creation and file deletion', async () => { const created = await request(app) .post('/api/agents/v1/skills') .set('Authorization', `Bearer ${token}`) @@ -390,13 +389,6 @@ describe('Agents OpenAPI actual HTTP contract', () => { relativePath: 'references/delete-me.md', }).lean(), ).toBeNull(); - - const deleted = await request(app) - .delete(`/api/agents/v1/skills/${created.body.id}`) - .set('Authorization', `Bearer ${token}`); - expect(deleted.status).toBe(200); - expectResponseToMatchSpec(spec, 'deleteSkill', deleted); - expect(await mongoose.models.Skill.findById(created.body.id).lean()).toBeNull(); }); it('validates parser and normalized management errors against their documented JSON forms', async () => { diff --git a/api/server/routes/agents/__tests__/skills.spec.js b/api/server/routes/agents/__tests__/skills.spec.js index 1275642cc0..e22d1f7552 100644 --- a/api/server/routes/agents/__tests__/skills.spec.js +++ b/api/server/routes/agents/__tests__/skills.spec.js @@ -17,7 +17,6 @@ jest.mock('@librechat/api', () => ({ create: mockWrite, get: mockList, update: mockWrite, - delete: mockWrite, listFiles: mockList, getFile: mockList, updateFile: mockWrite, diff --git a/api/server/routes/agents/skills.js b/api/server/routes/agents/skills.js index 62c4e6445a..c8f738b9e1 100644 --- a/api/server/routes/agents/skills.js +++ b/api/server/routes/agents/skills.js @@ -30,7 +30,6 @@ router.get('/', handlers.list); router.post('/', handlers.create); router.get('/:id', handlers.get); router.patch('/:id', handlers.update); -router.delete('/:id', handlers.delete); router.get('/:id/files', handlers.listFiles); router.get('/:id/files/*relativePath', handlers.getFile); router.put('/:id/files/*relativePath', handlers.updateFile); diff --git a/api/server/routes/skills.test.js b/api/server/routes/skills.test.js index 92272ae899..545c98dafe 100644 --- a/api/server/routes/skills.test.js +++ b/api/server/routes/skills.test.js @@ -58,6 +58,7 @@ jest.mock('~/server/middleware/config/app', () => (req, _res, next) => { jest.mock('~/server/services/Files/strategies', () => ({ getStrategyFunctions: jest.fn().mockReturnValue({ saveBuffer: jest.fn().mockResolvedValue('/uploads/test/file.txt'), + deleteFile: jest.fn().mockResolvedValue(undefined), getDownloadStream: jest.fn().mockResolvedValue({ pipe: jest.fn(), on: jest.fn(), @@ -871,7 +872,7 @@ describe('Skill routes', () => { expect((await upload('stale', initial.body.file_id)).status).toBe(409); const stored = await SkillFile.findOne({ skillId: created.body._id }); expect(stored.file_id).toBe(replaced.body.file_id); - await request(app).delete(`${url}/references/revision.md`); + await request(app).delete(`${url}/references/revision.md`).expect(200); expect((await upload('resurrect', replaced.body.file_id)).status).toBe(409); expect(await SkillFile.countDocuments({ skillId: created.body._id })).toBe(0); }); diff --git a/api/server/services/Files/Azure/crud.js b/api/server/services/Files/Azure/crud.js index 1fea4db7d5..ee2cb8e40d 100644 --- a/api/server/services/Files/Azure/crud.js +++ b/api/server/services/Files/Azure/crud.js @@ -5,6 +5,8 @@ const fetch = require('node-fetch'); const { logger } = require('@librechat/data-schemas'); const { deleteRagFile, + createOwnedBlobFileDeleter, + extractAzureBlobPath, assertRemoteFileURL, getSafeErrorMetadata, getAzureContainerClient, @@ -120,26 +122,18 @@ async function getAzureURL({ fileName, basePath = defaultBasePath, userId, conta * @param {ServerRequest} params.req - The Express request object. * @param {MongoFile} params.file - The file object. */ -async function deleteFileFromAzure(req, file) { - await deleteRagFile({ userId: req.user.id, file }); - - try { - const containerClient = await getAzureContainerClient(AZURE_CONTAINER_NAME); - const blobPath = file.filepath.split(`${AZURE_CONTAINER_NAME}/`)[1]; - if (!blobPath.includes(req.user.id)) { - throw new Error('User ID not found in blob path'); - } - const blockBlobClient = containerClient.getBlockBlobClient(blobPath); - await blockBlobClient.delete(); - logger.debug('[deleteFileFromAzure] Blob deleted successfully from Azure Blob Storage'); - } catch (error) { - logger.error('[deleteFileFromAzure] Error deleting blob:', error); - if (error.statusCode === 404) { - return; - } - throw error; - } -} +const deleteFileFromAzure = (req, file) => + createOwnedBlobFileDeleter({ + extractPath: (filepath) => extractAzureBlobPath(filepath, AZURE_CONTAINER_NAME), + deleteBlob: async (blobPath) => { + const containerClient = await getAzureContainerClient(AZURE_CONTAINER_NAME); + const blockBlobClient = containerClient.getBlockBlobClient(blobPath); + await blockBlobClient.delete(); + logger.debug('[deleteFileFromAzure] Blob deleted successfully from Azure Blob Storage'); + }, + isMissing: (error) => error?.statusCode === 404, + deleteRagFile, + })(req, file); /** * Streams a file from disk directly to Azure Blob Storage without loading diff --git a/api/server/services/Files/Firebase/crud.js b/api/server/services/Files/Firebase/crud.js index 174b473751..997f871f88 100644 --- a/api/server/services/Files/Firebase/crud.js +++ b/api/server/services/Files/Firebase/crud.js @@ -5,6 +5,8 @@ const fetch = require('node-fetch'); const { logger } = require('@librechat/data-schemas'); const { deleteRagFile, + createOwnedBlobFileDeleter, + extractFirebaseBlobPath, getFirebaseStorage, assertRemoteFileURL, getSafeErrorMetadata, @@ -153,26 +155,6 @@ async function saveBufferToFirebase({ userId, buffer, fileName, basePath = 'imag * @param {string} urlString - The Firebase Storage URL. * @returns {string} The decoded file path. */ -function extractFirebaseFilePath(urlString) { - try { - const url = new URL(urlString); - const pathRegex = /\/o\/(.+?)(\?|$)/; - const match = url.pathname.match(pathRegex); - - if (match && match[1]) { - return decodeURIComponent(match[1]); - } - - return ''; - } catch { - logger.debug( - '[extractFirebaseFilePath] Failed to extract Firebase file path from URL, returning empty string', - ); - // If URL parsing fails, return an empty string - return ''; - } -} - /** * Deletes a file from Firebase storage. This function determines the filepath from the * Firebase storage URL via regex for deletion. Validated by the user's ID. @@ -185,23 +167,13 @@ function extractFirebaseFilePath(urlString) { * A promise that resolves when the file has been successfully deleted from Firebase storage. * Throws an error if there is an issue with deletion. */ -const deleteFirebaseFile = async (req, file) => { - await deleteRagFile({ userId: req.user.id, file }); - - const fileName = extractFirebaseFilePath(file.filepath); - if (!fileName.includes(req.user.id)) { - throw new Error('Invalid file path'); - } - try { - await deleteFile('', fileName); - } catch (error) { - logger.error('Error deleting file from Firebase:', error); - if (error.code === 'storage/object-not-found') { - return; - } - throw error; - } -}; +const deleteFirebaseFile = (req, file) => + createOwnedBlobFileDeleter({ + extractPath: extractFirebaseBlobPath, + deleteBlob: (filepath) => deleteFile('', filepath), + isMissing: (error) => error?.code === 'storage/object-not-found', + deleteRagFile, + })(req, file); /** * Uploads a file to Firebase Storage. diff --git a/api/server/services/Files/Local/__tests__/crud-delete.spec.js b/api/server/services/Files/Local/__tests__/crud-delete.spec.js deleted file mode 100644 index dacb477323..0000000000 --- a/api/server/services/Files/Local/__tests__/crud-delete.spec.js +++ /dev/null @@ -1,94 +0,0 @@ -/** Exactly what `deleteLocalFile` reaches for, so the double does not depend on a built package. */ -jest.mock('@librechat/api', () => ({ - deleteRagFile: jest.fn().mockResolvedValue(undefined), - stripCacheBust: (filepath) => String(filepath).split('?')[0], -})); -jest.mock('@librechat/data-schemas', () => ({ - logger: { warn: jest.fn(), error: jest.fn() }, -})); - -const fs = require('fs'); -const os = require('os'); -const path = require('path'); -const { deleteRagFile } = require('@librechat/api'); -const { deleteLocalFile } = require('../crud'); - -/* The resolved promise of a delete is what `processDeleteRequest` reads to decide that a record may - lose its metadata and its agent references, so this adapter may only resolve once the bytes are - actually gone. Storage that was already missing is the one benign case. */ -describe('deleteLocalFile failure reporting', () => { - const userId = 'user-1'; - let tmpBase; - let req; - - beforeEach(() => { - jest.restoreAllMocks(); - jest.clearAllMocks(); - tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), 'crud-delete-')); - fs.mkdirSync(path.join(tmpBase, 'uploads', userId), { recursive: true }); - req = { - user: { id: userId }, - config: { - paths: { - publicPath: path.join(tmpBase, 'public'), - uploads: path.join(tmpBase, 'uploads'), - }, - }, - }; - }); - - afterEach(() => { - fs.rmSync(tmpBase, { recursive: true, force: true }); - }); - - const uploadedFile = (filename, ownerId = userId) => { - fs.mkdirSync(path.join(tmpBase, 'uploads', ownerId), { recursive: true }); - const filepath = path.join(tmpBase, 'uploads', ownerId, filename); - fs.writeFileSync(filepath, 'contents'); - return { file_id: 'file-1', filepath: `/uploads/${ownerId}/${filename}`, user: ownerId }; - }; - - it('removes the file and resolves', async () => { - const file = uploadedFile('knowledge.txt'); - - await expect(deleteLocalFile(req, file)).resolves.toBeUndefined(); - expect(fs.existsSync(path.join(tmpBase, 'uploads', userId, 'knowledge.txt'))).toBe(false); - }); - - it('resolves when the file is already gone', async () => { - await expect( - deleteLocalFile(req, { file_id: 'file-1', filepath: `/uploads/${userId}/missing.txt` }), - ).resolves.toBeUndefined(); - }); - - it('rejects when the bytes survive the delete', async () => { - const file = uploadedFile('locked.txt'); - jest - .spyOn(fs.promises, 'unlink') - .mockRejectedValue(Object.assign(new Error('permission denied'), { code: 'EACCES' })); - - await expect(deleteLocalFile(req, file)).rejects.toThrow('permission denied'); - expect(fs.existsSync(path.join(tmpBase, 'uploads', userId, 'locked.txt'))).toBe(true); - }); - - it("uses the recorded owner when a manager deletes another user's file", async () => { - const ownerId = 'user-2'; - const file = uploadedFile('managed.txt', ownerId); - req.user.id = 'manager-1'; - - await expect(deleteLocalFile(req, file)).resolves.toBeUndefined(); - - expect(deleteRagFile).toHaveBeenCalledWith({ userId: ownerId, file }); - expect(fs.existsSync(path.join(tmpBase, 'uploads', ownerId, 'managed.txt'))).toBe(false); - }); - - it('rejects a path outside the recorded owner namespace', async () => { - const file = uploadedFile('protected.txt', 'user-2'); - file.user = 'user-3'; - - await expect(deleteLocalFile(req, file)).rejects.toThrow('Invalid file path'); - - expect(deleteRagFile).not.toHaveBeenCalled(); - expect(fs.existsSync(path.join(tmpBase, 'uploads', 'user-2', 'protected.txt'))).toBe(true); - }); -}); diff --git a/api/server/services/Files/Local/crud.js b/api/server/services/Files/Local/crud.js index ad93b07d12..c0acebdc86 100644 --- a/api/server/services/Files/Local/crud.js +++ b/api/server/services/Files/Local/crud.js @@ -3,6 +3,7 @@ const path = require('path'); const axios = require('axios'); const { deleteRagFile, + createLocalFileDeleter, moveLocalFile, stripCacheBust, writeLocalFile, @@ -13,7 +14,6 @@ const { saveLocalBuffer: saveBufferToLocalPath, } = require('@librechat/api'); const { logger } = require('@librechat/data-schemas'); -const { EModelEndpoint } = require('librechat-data-provider'); const { resizeImageBuffer } = require('~/server/services/Files/images/resize'); const { getBufferMetadata } = require('~/server/utils'); const paths = require('~/config/paths'); @@ -150,102 +150,7 @@ async function getLocalFileURL({ fileName, basePath = 'images' }) { return path.posix.join('/', basePath, fileName); } -/** - * Validates that a filepath is strictly contained within a subdirectory under a base path, - * using path.relative to prevent prefix-collision bypasses. - * - * @param {string} base - The base directory path. - * @param {string} subfolder - The subdirectory under the base path. - * @param {string} ownerId - The persisted owner whose storage namespace contains the file. - * @param {string} filepath - The complete file path to be validated. - * - * @returns {boolean} - * Returns true if the filepath is within the specified base and subfolder, false otherwise. - */ -const isValidPath = (base, subfolder, ownerId, filepath) => { - const normalizedBase = path.resolve(base, subfolder, ownerId); - const normalizedFilepath = path.resolve(filepath); - const rel = path.relative(normalizedBase, normalizedFilepath); - return !rel.startsWith('..') && !path.isAbsolute(rel) && !rel.includes(`..${path.sep}`); -}; - -/** - * @param {string} filepath - */ -/** - * A file whose bytes are still on disk must not be reported as deleted: callers use the resolved - * promise to decide that a record may lose its metadata and its agent references. Storage that was - * already gone is the one benign case, and `processDeleteRequest` treats it as deleted by design. - */ -const unlinkFile = async (filepath) => { - try { - await fs.promises.unlink(filepath); - } catch (error) { - if (error?.code === 'ENOENT') { - logger.warn('Local file was already missing during delete:', error); - return; - } - logger.error('Error deleting file:', error); - throw error; - } -}; - -/** - * Deletes a file from the filesystem. This function takes a file object, constructs the full path, and - * verifies the path's validity before deleting the file. If the path is invalid, an error is thrown. - * - * @param {ServerRequest} req - The request object from Express. - * @param {MongoFile} file - The file object to be deleted. It should have a `filepath` property that is - * a string representing the path of the file relative to the publicPath. - * - * @returns {Promise} - * A promise that resolves when the file has been successfully deleted, or throws an error if the - * file path is invalid or if there is an error in deletion. - */ -const deleteLocalFile = async (req, file) => { - const appConfig = req.config; - const { publicPath, uploads } = appConfig.paths; - const ownerId = file.user?.toString?.() || req.user.id; - - /** Filepath stripped of query parameters (e.g., ?manual=true, ?v=) */ - const cleanFilepath = stripCacheBust(file.filepath); - const uploadPrefix = `/uploads/${ownerId}/`; - - if (cleanFilepath.startsWith(uploadPrefix)) { - const userUploadDir = path.join(uploads, ownerId); - const basePath = cleanFilepath.slice(uploadPrefix.length); - - if (!basePath) { - throw new Error(`Invalid file path: ${cleanFilepath}`); - } - - const filepath = path.join(userUploadDir, basePath); - - const rel = path.relative(userUploadDir, filepath); - if (rel.startsWith('..') || path.isAbsolute(rel) || rel.includes(`..${path.sep}`)) { - throw new Error(`Invalid file path: ${cleanFilepath}`); - } - - await deleteRagFile({ userId: ownerId, file }); - await unlinkFile(filepath); - return; - } - - const parts = cleanFilepath.split(path.sep); - const subfolder = parts[1]; - if (!subfolder && parts[0] === EModelEndpoint.agents) { - logger.warn(`Agent File ${file.file_id} is missing filepath, may have been deleted already`); - return; - } - const filepath = path.join(publicPath, cleanFilepath); - - if (!isValidPath(publicPath, subfolder, ownerId, filepath)) { - throw new Error('Invalid file path'); - } - - await deleteRagFile({ userId: ownerId, file }); - await unlinkFile(filepath); -}; +const deleteLocalFile = (req, file) => createLocalFileDeleter({ deleteRagFile, logger })(req, file); /** * Uploads a file to the specified upload directory. diff --git a/docs/skills-management-api.md b/docs/skills-management-api.md index 8f9ecd7551..6bd5944301 100644 --- a/docs/skills-management-api.md +++ b/docs/skills-management-api.md @@ -10,7 +10,6 @@ execution router and does not accept browser sessions or execution API keys as f | POST | `/api/agents/v1/skills` | Create an inline Skill | | GET | `/api/agents/v1/skills/:id` | Read Skill metadata, body, and frontmatter | | PATCH | `/api/agents/v1/skills/:id` | Update an inline Skill | -| DELETE | `/api/agents/v1/skills/:id` | Delete an inline Skill | | GET | `/api/agents/v1/skills/:id/files` | List file metadata | | GET | `/api/agents/v1/skills/:id/files/*relativePath` | Read a file as JSON | | PUT | `/api/agents/v1/skills/:id/files/*relativePath` | Create or replace a text file | @@ -32,8 +31,7 @@ Create an inline Skill with its required identity and instruction fields: ``` Creation also accepts the editable optional fields described below. The authenticated principal is -recorded as the owner; callers cannot set ownership, tenant, source, or permissions. Skill deletion -uses the existing cleanup path for Agent allowlists, Skill files, permissions, and stored blobs. +recorded as the owner; callers cannot set ownership, tenant, source, or permissions. Updates require the version returned by the latest read: @@ -54,9 +52,9 @@ permissions. Inaccessible and cross-tenant IDs return the same 404 envelope. Text-file writes accept `{ "content": "replacement text" }`, capped at 1 MiB of UTF-8 data. Use a relative path such as `references/guide.md`. Absolute paths, traversal, and NUL bytes are rejected. Delete the same path to remove the file. Update `SKILL.md` through the Skill's `body` -field with `expectedVersion`, not the file endpoint. File writes use replacement semantics without a version precondition; serialize writers -to the same path. After a file write, read the Skill again before a versioned metadata/body update: file writes also -increment the parent version. Storage selection stays server-controlled. File JSON reads omit content for +field with `expectedVersion`, not the file endpoint. File writes use replacement semantics without +a version precondition; serialize writers to the same path. After a file write, read the Skill again +before a versioned metadata/body update: file writes also increment the parent version. Storage selection stays server-controlled. File JSON reads omit content for binary or oversized files; raw download mode is not supported on this surface. File deletion completes storage cleanup before returning success and reports a conflict if another writer replaced the path. diff --git a/packages/api/openapi/agents.openapi.json b/packages/api/openapi/agents.openapi.json index 5d7ff75ed5..b54c81e888 100644 --- a/packages/api/openapi/agents.openapi.json +++ b/packages/api/openapi/agents.openapi.json @@ -1658,134 +1658,6 @@ } } } - }, - "delete": { - "operationId": "deleteSkill", - "summary": "Delete a skill", - "tags": [ - "Skills" - ], - "security": [ - { - "oidcBearer": [] - } - ], - "parameters": [ - { - "in": "path", - "name": "id", - "description": "The skill id", - "schema": { - "type": "string" - }, - "required": true - } - ], - "responses": { - "200": { - "description": "The skill was deleted", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/SkillDeleted" - } - } - } - }, - "400": { - "description": "Invalid request, or a malformed JSON body", - "content": { - "application/json": { - "schema": { - "anyOf": [ - { - "$ref": "#/components/schemas/Error" - }, - { - "$ref": "#/components/schemas/JsonParseError" - } - ] - } - } - } - }, - "401": { - "description": "Authentication failed", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorMessage" - } - } - } - }, - "403": { - "description": "Permission denied, the caller is banned, or the request fails tenant isolation", - "content": { - "application/json": { - "schema": { - "anyOf": [ - { - "$ref": "#/components/schemas/Error" - }, - { - "$ref": "#/components/schemas/MessageError" - }, - { - "$ref": "#/components/schemas/ErrorMessage" - } - ] - } - } - } - }, - "404": { - "description": "Not found", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Error" - } - } - } - }, - "409": { - "description": "The bound account is being deleted", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/AccountDeletionConflict" - } - } - } - }, - "500": { - "description": "Internal server error. Errors normalized by the route use JSON; the final application error controller sends a text body with the text/html media type.", - "content": { - "application/json": { - "schema": { - "anyOf": [ - { - "$ref": "#/components/schemas/Error" - }, - { - "$ref": "#/components/schemas/ErrorMessage" - } - ] - } - }, - "text/html": { - "schema": { - "type": "string", - "enum": [ - "An unknown error occurred.", - "Processing error in ErrorController." - ] - } - } - } - } - } } }, "/skills/{id}/files": { @@ -4286,26 +4158,6 @@ "additionalProperties": false, "minProperties": 2 }, - "SkillDeleted": { - "type": "object", - "properties": { - "id": { - "type": "string", - "pattern": "^[a-f\\d]{24}$" - }, - "deleted": { - "type": "boolean", - "const": true - }, - "cleanupComplete": { - "type": "boolean" - } - }, - "required": [ - "id", - "deleted" - ] - }, "SkillFile": { "type": "object", "properties": { diff --git a/packages/api/src/openapi/skills.ts b/packages/api/src/openapi/skills.ts index 8a2b863cac..6fa595fb68 100644 --- a/packages/api/src/openapi/skills.ts +++ b/packages/api/src/openapi/skills.ts @@ -10,7 +10,6 @@ import { skillFileContentSchema, skillFileUpdateSchema, skillFrontmatterValueSchema, - skillDeleteResponseSchema, skillFileDeleteResponseSchema, } from '../skills/management'; import { @@ -47,7 +46,6 @@ export const skillComponentSchemas: Record = { SkillList: skillListResponseSchema, SkillCreateRequest: skillManagementCreateSchema, SkillUpdateRequest: skillManagementUpdateSchema, - SkillDeleted: skillDeleteResponseSchema, SkillFile: skillFileSchema, SkillFileList: skillFileListResponseSchema, SkillFileContent: skillFileContentSchema, @@ -152,19 +150,6 @@ export const skillContracts: EndpointContract[] = [ }, ], }, - { - operationId: 'deleteSkill', - method: 'delete', - path: '/skills/{id}', - tags: [TAG], - summary: 'Delete a skill', - security: SECURITY, - pathParams: [{ name: 'id', description: 'The skill id' }], - responses: [ - { status: 200, description: 'The skill was deleted', schema: skillDeleteResponseSchema }, - ...errorResponses, - ], - }, { operationId: 'listSkillFiles', method: 'get', diff --git a/packages/api/src/skills/handlers.spec.ts b/packages/api/src/skills/handlers.spec.ts index c3dcc30072..8055cf113a 100644 --- a/packages/api/src/skills/handlers.spec.ts +++ b/packages/api/src/skills/handlers.spec.ts @@ -3,6 +3,11 @@ import type { DeleteSkillResult, ISkillFile } from '@librechat/data-schemas'; import type { Response } from 'express'; import type { SkillsHandlersDeps } from './handlers'; import type { ServerRequest } from '~/types'; +import { + createOwnedBlobFileDeleter, + extractAzureBlobPath, + extractFirebaseBlobPath, +} from '~/storage/ownedBlobDelete'; import { createSkillsHandlers } from './handlers'; function mockResponse(): Response { @@ -109,6 +114,65 @@ describe('skill delete handler', () => { }); }); +describe.each([ + { + source: 'azure_blob', + filepath: 'https://example.blob.core.windows.net/files/uploads/author-1/file.txt', + extractPath: (url: string) => extractAzureBlobPath(url, 'files'), + }, + { + source: 'firebase', + filepath: 'https://firebasestorage.googleapis.com/v0/b/bucket/o/uploads%2Fauthor-1%2Ffile.txt', + extractPath: extractFirebaseBlobPath, + }, +])( + 'cross-author Skill file deletion through $source storage', + ({ source, filepath, extractPath }) => { + it('deletes with the persisted author while the manager remains the requester', async () => { + const skillId = new Types.ObjectId().toString(); + const file = { + file_id: 'file-1', + filepath, + source, + author: new Types.ObjectId(), + relativePath: 'references/file.txt', + } as ISkillFile & { _id: Types.ObjectId }; + const ownerId = file.author.toString(); + const ownedFilepath = filepath.replace('author-1', ownerId); + file.filepath = ownedFilepath; + const deleteBlob = jest.fn().mockResolvedValue(undefined); + const deleteRagFile = jest.fn().mockResolvedValue(undefined); + const storageDelete = createOwnedBlobFileDeleter({ + extractPath, + deleteBlob, + deleteRagFile, + isMissing: () => false, + }); + const deleteSkillFile = jest.fn().mockResolvedValue({ deleted: true }); + const handlers = createSkillsHandlers({ + getSkillFileByPath: jest.fn().mockResolvedValue(file), + getStrategyFunctions: jest.fn(() => ({ deleteFile: storageDelete })), + deleteSkillFile, + } as unknown as SkillsHandlersDeps); + const req = { + params: { id: skillId, relativePath: 'references/file.txt' }, + user: { id: 'manager-1' }, + } as unknown as ServerRequest; + const res = mockResponse(); + + await handlers.deleteFile(req, res); + + expect(deleteBlob).toHaveBeenCalledWith(`uploads/${ownerId}/file.txt`); + expect(deleteRagFile).toHaveBeenCalledWith({ + userId: ownerId, + file: expect.objectContaining({ user: ownerId }), + }); + expect(deleteSkillFile).toHaveBeenCalledWith(skillId, 'references/file.txt', 'file-1'); + expect(res.status).toHaveBeenCalledWith(200); + }); + }, +); + describe('skill create handler', () => { it('retries dependent cleanup after owner permission setup fails', async () => { const skillId = new Types.ObjectId(); diff --git a/packages/api/src/skills/management.spec.ts b/packages/api/src/skills/management.spec.ts index f7df84be12..de4faddab7 100644 --- a/packages/api/src/skills/management.spec.ts +++ b/packages/api/src/skills/management.spec.ts @@ -21,7 +21,6 @@ let db: AllMethods; let app: express.Express; let skillId: string; let allowEdit: boolean; -let allowDelete: boolean; let allowView: boolean; let allowUse: boolean; let allowCreate: boolean; @@ -56,7 +55,6 @@ afterAll(async () => { beforeEach(async () => { await mongoose.connection.dropDatabase(); allowEdit = true; - allowDelete = true; allowView = true; allowUse = true; allowCreate = true; @@ -110,7 +108,6 @@ beforeEach(async () => { }) as IRole, checkPermission: async ({ requiredPermission }) => { if (requiredPermission === PermissionBits.EDIT) return allowEdit; - if (requiredPermission === PermissionBits.DELETE) return allowDelete; return allowView; }, saveFile, @@ -135,9 +132,6 @@ beforeEach(async () => { app.patch('/skills/:id', async (req, res) => { await management.update(req, res); }); - app.delete('/skills/:id', async (req, res) => { - await management.delete(req, res); - }); app.get('/skills/:id/files', async (req, res) => { await management.listFiles(req, res); }); @@ -193,19 +187,6 @@ it('requires create capability for management mutations', async () => { .patch(`/skills/${skillId}`) .send({ expectedVersion: 1, body: 'Denied' }) .expect(403); - await request(app).delete(`/skills/${skillId}`).expect(403); -}); - -it('deletes an authorized inline Skill through the existing cleanup path', async () => { - const response = await request(app).delete(`/skills/${skillId}`).expect(200); - expect(response.body).toEqual({ id: skillId, deleted: true, cleanupComplete: true }); - expect(await inTenant(() => db.getSkillById(skillId))).toBeNull(); -}); - -it('requires DELETE permission without revealing inaccessible Skills', async () => { - allowDelete = false; - await request(app).delete(`/skills/${skillId}`).expect(404); - expect(await inTenant(() => db.getSkillById(skillId))).not.toBeNull(); }); it('lists and retrieves stable representations without ownership or storage internals', async () => { @@ -273,7 +254,6 @@ it.each([ it('hides inaccessible and cross-tenant Skills, including on file writes', async () => { allowView = false; allowEdit = false; - allowDelete = false; await request(app).get(`/skills/${skillId}`).expect(404); await request(app) .patch(`/skills/${skillId}`) @@ -283,7 +263,6 @@ it('hides inaccessible and cross-tenant Skills, including on file writes', async .put(`/skills/${skillId}/files/reference.md`) .send({ content: 'x' }) .expect(404); - await request(app).delete(`/skills/${skillId}`).expect(404); await request(app).delete(`/skills/${skillId}/files/reference.md`).expect(404); expect(saveFile).not.toHaveBeenCalled(); allowView = true; @@ -332,7 +311,6 @@ it('keeps synchronized Skill content and files read-only', async () => { .put(`/skills/${skillId}/files/reference.md`) .send({ content: 'x' }) .expect(403); - await request(app).delete(`/skills/${skillId}`).expect(403); await request(app).delete(`/skills/${skillId}/files/reference.md`).expect(403); expect(saveFile).not.toHaveBeenCalled(); }); diff --git a/packages/api/src/skills/management.ts b/packages/api/src/skills/management.ts index 152fcebee1..9d0d9bec96 100644 --- a/packages/api/src/skills/management.ts +++ b/packages/api/src/skills/management.ts @@ -10,7 +10,6 @@ import type { SkillFrontmatterValue, TCreateSkill, TDeleteSkillFileResponse, - TDeleteSkillResponse, TSkill, TUpdateSkillPayload, } from 'librechat-data-provider'; @@ -126,11 +125,6 @@ export const skillFileContentSchema: z.AnyZodObject = skillFileSchema.extend({ export const skillFileUpdateSchema: z.AnyZodObject = z .object({ content: z.string().max(1024 * 1024) }) .strict(); -export const skillDeleteResponseSchema: z.ZodType = z.object({ - id: idSchema, - deleted: z.literal(true), - cleanupComplete: z.boolean().optional(), -}); export const skillFileDeleteResponseSchema: z.ZodType = z.object({ skillId: idSchema, relativePath: z.string(), @@ -239,15 +233,7 @@ async function admitFileWrite(req: Request, res: Response, limiters: RequestHand export function createSkillManagementHandlers( deps: SkillManagementDeps, ): Record< - | 'list' - | 'create' - | 'get' - | 'update' - | 'delete' - | 'listFiles' - | 'getFile' - | 'updateFile' - | 'deleteFile', + 'list' | 'create' | 'get' | 'update' | 'listFiles' | 'getFile' | 'updateFile' | 'deleteFile', (request: Request, res: Response) => Promise > { async function canManageSkills(req: ManagementRequest): Promise { @@ -382,12 +368,6 @@ export function createSkillManagementHandlers( }, { mutation: true }, ), - delete: wrap( - PermissionBits.DELETE, - (req, res) => - runHandler(req, res, deps.handlers.delete, (body) => skillDeleteResponseSchema.parse(body)), - { mutation: true }, - ), listFiles: wrap(PermissionBits.VIEW, (req, res) => runHandler(req, res, deps.handlers.listFiles, (body) => ({ object: 'list', diff --git a/packages/api/src/storage/index.ts b/packages/api/src/storage/index.ts index 3639c5fa65..d6e6922efb 100644 --- a/packages/api/src/storage/index.ts +++ b/packages/api/src/storage/index.ts @@ -7,3 +7,4 @@ export * from './metadata'; export * from './url'; export * from './path'; export * from './local'; +export * from './ownedBlobDelete'; diff --git a/packages/api/src/storage/local-delete.spec.ts b/packages/api/src/storage/local-delete.spec.ts new file mode 100644 index 0000000000..6b07937882 --- /dev/null +++ b/packages/api/src/storage/local-delete.spec.ts @@ -0,0 +1,69 @@ +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import { createLocalFileDeleter } from './local'; + +describe('local file deletion', () => { + let root: string; + let req: { user: { id: string }; config: { paths: { publicPath: string; uploads: string } } }; + const deleteRagFile = jest.fn().mockResolvedValue(undefined); + const logger = { warn: jest.fn(), error: jest.fn() }; + const remove = createLocalFileDeleter({ deleteRagFile, logger }); + + beforeEach(() => { + jest.clearAllMocks(); + root = fs.mkdtempSync(path.join(os.tmpdir(), 'local-delete-')); + req = { + user: { id: 'user-1' }, + config: { + paths: { publicPath: path.join(root, 'public'), uploads: path.join(root, 'uploads') }, + }, + }; + }); + + afterEach(() => fs.rmSync(root, { recursive: true, force: true })); + + const uploadedFile = (name: string, owner = 'user-1') => { + const dir = path.join(root, 'uploads', owner); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(dir, name), 'contents'); + return { file_id: 'file-1', filepath: `/uploads/${owner}/${name}`, user: owner }; + }; + + it('removes uploaded bytes and RAG data', async () => { + const file = uploadedFile('knowledge.txt'); + await remove(req, file); + expect(fs.existsSync(path.join(root, 'uploads', 'user-1', 'knowledge.txt'))).toBe(false); + expect(deleteRagFile).toHaveBeenCalledWith({ userId: 'user-1', file }); + }); + + it('accepts missing storage but rejects other unlink failures', async () => { + const file = { file_id: 'file-1', filepath: '/uploads/user-1/missing.txt', user: 'user-1' }; + await expect(remove(req, file)).resolves.toBeUndefined(); + + const directory = path.join(root, 'uploads', 'user-1', 'not-a-file'); + fs.mkdirSync(directory, { recursive: true }); + const existing = { file_id: 'file-2', filepath: '/uploads/user-1/not-a-file', user: 'user-1' }; + await expect(remove(req, existing)).rejects.toThrow(); + expect(fs.existsSync(directory)).toBe(true); + }); + + it('uses the recorded owner for managers and rejects another owner namespace', async () => { + const file = uploadedFile('managed.txt', 'user-2'); + req.user.id = 'manager-1'; + await remove(req, file); + expect(deleteRagFile).toHaveBeenCalledWith({ userId: 'user-2', file }); + + const other = uploadedFile('protected.txt', 'user-2'); + other.user = 'user-3'; + deleteRagFile.mockClear(); + await expect(remove(req, other)).rejects.toThrow('Invalid file path'); + expect(deleteRagFile).not.toHaveBeenCalled(); + }); + + it('cleans up RAG data for a legacy agent record without a disk path', async () => { + const file = { file_id: 'file-1', filepath: 'agents', user: 'user-2' }; + await remove(req, file); + expect(deleteRagFile).toHaveBeenCalledWith({ userId: 'user-2', file }); + }); +}); diff --git a/packages/api/src/storage/local.ts b/packages/api/src/storage/local.ts index 5b9de9acb3..48fda30383 100644 --- a/packages/api/src/storage/local.ts +++ b/packages/api/src/storage/local.ts @@ -1,12 +1,83 @@ import path from 'path'; import crypto from 'node:crypto'; +import { EModelEndpoint } from 'librechat-data-provider'; import { mkdir, copyFile, rename, unlink, writeFile } from 'fs/promises'; +import { stripCacheBust } from './path'; export interface LocalStoragePaths { publicPath: string; uploads: string; } +interface LocalFile { + file_id: string; + filepath: string; + user?: { toString(): string }; +} + +interface LocalDeleteRequest { + user: { id: string }; + config: { paths: LocalStoragePaths }; +} + +interface LocalFileDeleteDependencies { + deleteRagFile: (args: { userId: string; file: LocalFile }) => Promise; + logger: { warn: (...args: unknown[]) => void; error: (...args: unknown[]) => void }; +} + +/** Deletes only within the persisted owner's namespace, and reports storage failures to callers. */ +export function createLocalFileDeleter({ deleteRagFile, logger }: LocalFileDeleteDependencies) { + const unlinkStoredFile = async (filepath: string) => { + try { + await unlink(filepath); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + logger.warn('Local file was already missing during delete:', error); + return; + } + logger.error('Error deleting file:', error); + throw error; + } + }; + + return async (req: LocalDeleteRequest, file: LocalFile): Promise => { + const { publicPath, uploads } = req.config.paths; + const ownerId = file.user?.toString() || req.user.id; + const cleanFilepath = stripCacheBust(file.filepath); + const uploadPrefix = `/uploads/${ownerId}/`; + + if (cleanFilepath.startsWith(uploadPrefix)) { + const userUploadDir = path.join(uploads, ownerId); + const filePath = path.join(userUploadDir, cleanFilepath.slice(uploadPrefix.length)); + if (!isStrictlyWithin(userUploadDir, filePath)) { + throw new Error(`Invalid file path: ${cleanFilepath}`); + } + await deleteRagFile({ userId: ownerId, file }); + await unlinkStoredFile(filePath); + return; + } + + const parts = cleanFilepath.split(path.sep); + const subfolder = parts[1]; + if (!subfolder && parts[0] === EModelEndpoint.agents) { + await deleteRagFile({ userId: ownerId, file }); + logger.warn(`Agent File ${file.file_id} is missing filepath, may have been deleted already`); + return; + } + const filePath = path.join(publicPath, cleanFilepath); + if (!subfolder || !isStrictlyWithin(path.join(publicPath, subfolder, ownerId), filePath)) { + throw new Error('Invalid file path'); + } + await deleteRagFile({ userId: ownerId, file }); + await unlinkStoredFile(filePath); + }; +} + +function isStrictlyWithin(directory: string, filePath: string): boolean { + const rel = path.relative(path.resolve(directory), path.resolve(filePath)); + return !!rel && !rel.startsWith('..') && !path.isAbsolute(rel) && !rel.includes(`..${path.sep}`); +} + /** * Fills a sibling temp file and renames it into place, so concurrent writers to one path each * land whole (last rename wins) and readers never see a partly written file. The temp name is diff --git a/packages/api/src/storage/ownedBlobDelete.spec.ts b/packages/api/src/storage/ownedBlobDelete.spec.ts new file mode 100644 index 0000000000..f8ca253cda --- /dev/null +++ b/packages/api/src/storage/ownedBlobDelete.spec.ts @@ -0,0 +1,60 @@ +import { + createOwnedBlobFileDeleter, + extractAzureBlobPath, + extractFirebaseBlobPath, +} from './ownedBlobDelete'; + +describe.each([ + { + source: 'Azure', + filepath: 'https://example.blob.core.windows.net/files/uploads/author-1/file.txt', + extractPath: (url: string) => extractAzureBlobPath(url, 'files'), + }, + { + source: 'Firebase', + filepath: 'https://firebasestorage.googleapis.com/v0/b/bucket/o/uploads%2Fauthor-1%2Ffile.txt', + extractPath: extractFirebaseBlobPath, + }, +])('$source owner-aware deletion', ({ filepath, extractPath }) => { + const deleteBlob = jest.fn().mockResolvedValue(undefined); + const deleteRagFile = jest.fn().mockResolvedValue(undefined); + const remove = createOwnedBlobFileDeleter({ + extractPath, + deleteBlob, + deleteRagFile, + isMissing: (error) => (error as { code?: string }).code === 'missing', + }); + const req = { user: { id: 'manager-1' } }; + + beforeEach(() => { + deleteBlob.mockReset().mockResolvedValue(undefined); + deleteRagFile.mockClear(); + }); + + it("deletes another author's file under the persisted owner", async () => { + const file = { filepath, user: 'author-1' }; + await remove(req, file); + expect(deleteBlob).toHaveBeenCalledWith('uploads/author-1/file.txt'); + expect(deleteRagFile).toHaveBeenCalledWith({ userId: 'author-1', file }); + }); + + it('rejects an owner mismatch before deleting either blob or RAG data', async () => { + await expect(remove(req, { filepath, user: 'author-2' })).rejects.toThrow('Invalid file path'); + expect(deleteBlob).not.toHaveBeenCalled(); + expect(deleteRagFile).not.toHaveBeenCalled(); + }); + + it('does not clean up RAG data when remote deletion fails', async () => { + deleteBlob.mockRejectedValueOnce(new Error('storage unavailable')); + await expect(remove(req, { filepath, user: 'author-1' })).rejects.toThrow( + 'storage unavailable', + ); + expect(deleteRagFile).not.toHaveBeenCalled(); + }); + + it('treats an already missing blob as deleted and still cleans up RAG data', async () => { + deleteBlob.mockRejectedValueOnce(Object.assign(new Error('gone'), { code: 'missing' })); + await remove(req, { filepath, user: 'author-1' }); + expect(deleteRagFile).toHaveBeenCalledWith({ userId: 'author-1', file: expect.any(Object) }); + }); +}); diff --git a/packages/api/src/storage/ownedBlobDelete.ts b/packages/api/src/storage/ownedBlobDelete.ts new file mode 100644 index 0000000000..ed2f92b050 --- /dev/null +++ b/packages/api/src/storage/ownedBlobDelete.ts @@ -0,0 +1,61 @@ +interface StoredBlobFile { + filepath: string; + user?: { toString(): string }; +} + +interface OwnedBlobDeleteDependencies { + extractPath: (url: string) => string; + deleteBlob: (path: string) => Promise; + isMissing: (error: unknown) => boolean; + deleteRagFile: (args: { userId: string; file: StoredBlobFile }) => Promise; +} + +/** Storage cleanup uses the persisted owner, not the authorized caller's identity. */ +export function createOwnedBlobFileDeleter({ + extractPath, + deleteBlob, + isMissing, + deleteRagFile, +}: OwnedBlobDeleteDependencies) { + return async (req: { user: { id: string } }, file: StoredBlobFile): Promise => { + const ownerId = file.user?.toString() || req.user.id; + const blobPath = extractPath(file.filepath); + const segments = blobPath.split('/'); + // saveBuffer stores at //; the exact owner segment + // must be adjacent to the file name, not merely a substring elsewhere in the URL. + if ( + !ownerId || + segments.length < 3 || + segments[segments.length - 2] !== ownerId || + !segments[segments.length - 1] + ) { + throw new Error('Invalid file path'); + } + + try { + await deleteBlob(blobPath); + } catch (error) { + if (!isMissing(error)) throw error; + } + await deleteRagFile({ userId: ownerId, file }); + }; +} + +export function extractAzureBlobPath(filepath: string, containerName: string): string { + const segments = new URL(filepath).pathname.split('/').filter(Boolean); + const containerIndex = segments.indexOf(containerName); + if (containerIndex < 0 || containerIndex === segments.length - 1) { + throw new Error('Invalid Azure blob path'); + } + return segments + .slice(containerIndex + 1) + .map(decodeURIComponent) + .join('/'); +} + +export function extractFirebaseBlobPath(filepath: string): string { + const pathname = new URL(filepath).pathname; + const objectIndex = pathname.indexOf('/o/'); + if (objectIndex < 0) throw new Error('Invalid Firebase blob path'); + return decodeURIComponent(pathname.slice(objectIndex + 3)); +}