🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🩹 fix(database): enforce document history ownership and pagination
Inneib9c4b87a904d
diff --git a/packages/database/src/models/__tests__/documentHistory.test.ts b/packages/database/src/models/__tests__/documentHistory.test.tsindex cd6c27d64af..b6b052e13e4 100644--- a/packages/database/src/models/__tests__/documentHistory.test.ts+++ b/packages/database/src/models/__tests__/documentHistory.test.ts@@ -110,10 +110,32 @@ describe('DocumentHistoryModel', () => {}),).rejects.toThrow();});++ it('should reject history rows for documents owned by another user', async () => {+ const otherDocumentId = await createTestDocument(documentModel2, fileModel2, 'Other content');++ await expect(+ historyModel.create({+ documentId: otherDocumentId,+ payload: { editorData: { blocks: [] } },+ saveSource: 'manual',+ savedAt: new Date('2026-04-11T00:00:00.000Z'),+ storageKind: 'snapshot',+ version: 1,+ }),+ ).rejects.toThrow('Document not found');++ const stored = await serverDB+ .select()+ .from(documentHistories)+ .where(eq(documentHistories.documentId, otherDocumentId));++ expect(stored).toHaveLength(0);+ });});describe('list', () => {- it('should return document history rows ordered by savedAt descending', async () => {+ it('should return document history rows ordered by version descending', async () => {const documentId = await createTestDocument(documentModel, fileModel, 'Initial content');await historyModel.create({@@ -179,6 +201,45 @@ describe('DocumentHistoryModel', () => {expect(anchored).toHaveLength(1);expect(anchored[0]?.version).toBe(2);});++ it('should keep pagination stable when savedAt order differs from version order', async () => {+ const documentId = await createTestDocument(documentModel, fileModel, 'Initial content');++ await historyModel.create({+ documentId,+ payload: { editorData: { version: 1 } },+ saveSource: 'autosave',+ savedAt: new Date('2026-04-11T00:00:03.000Z'),+ storageKind: 'snapshot',+ version: 1,+ });+ await historyModel.create({+ documentId,+ payload: { editorData: { version: 2 } },+ saveSource: 'manual',+ savedAt: new Date('2026-04-11T00:00:01.000Z'),+ storageKind: 'patch',+ version: 2,+ });+ await historyModel.create({+ documentId,+ payload: { editorData: { version: 3 } },+ saveSource: 'restore',+ savedAt: new Date('2026-04-11T00:00:02.000Z'),+ storageKind: 'snapshot',+ version: 3,+ });++ const firstPage = await historyModel.list({ documentId, limit: 2 });+ const secondPage = await historyModel.list({+ beforeVersion: firstPage.at(-1)?.version,+ documentId,+ limit: 2,+ });++ expect(firstPage.map((row) => row.version)).toEqual([3, 2]);+ expect(secondPage.map((row) => row.version)).toEqual([1]);+ });});describe('findByDocumentIdAndVersion', () => {diff --git a/packages/database/src/models/documentHistory.ts b/packages/database/src/models/documentHistory.tsindex 9f286e24793..657be965874 100644--- a/packages/database/src/models/documentHistory.ts+++ b/packages/database/src/models/documentHistory.ts@@ -1,7 +1,7 @@import { and, desc, eq, lt } from 'drizzle-orm';import type { DocumentHistoryItem, NewDocumentHistory } from '../schemas';-import { documentHistories } from '../schemas';+import { documentHistories, documents } from '../schemas';import type { LobeChatDatabase } from '../type';export interface QueryDocumentHistoryParams {@@ -20,6 +20,16 @@ export class DocumentHistoryModel {}create = async (params: Omit<NewDocumentHistory, 'userId'>): Promise<DocumentHistoryItem> => {+ const [document] = await this.db+ .select({ id: documents.id })+ .from(documents)+ .where(and(eq(documents.id, params.documentId), eq(documents.userId, this.userId)))+ .limit(1);++ if (!document) {+ throw new Error('Document not found');+ }+const [result] = await this.db.insert(documentHistories).values({ ...params, userId: this.userId })@@ -113,8 +123,8 @@ export class DocumentHistoryModel {.from(documentHistories).where(and(...conditions)).orderBy(- desc(documentHistories.savedAt),desc(documentHistories.version),+ desc(documentHistories.savedAt),desc(documentHistories.id),).limit(limit);