fix: documents.import writes into an unauthorized collection when nested (#13331)

* fix: documents.import writes into an unauthorized collection when a parentDocumentId is also given

* test: assert imported child resolves to the parent's collection
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-07 12:30:34 +00:00
1 parent 046526839c
commit eb07bfebec
2 files changed
+77 -27

No files matched your search

+72 -3
View File
@@ -3515,9 +3515,11 @@ describe("#documents.import", () => {
vi.spyOn(FileStorage, "store").mockResolvedValue(
undefined as unknown as string
);
vi.spyOn(DocumentImportTask.prototype, "schedule").mockResolvedValue({
finished: vi.fn().mockResolvedValue({ documentId: childDocument.id }),
} as unknown as Awaited<ReturnType<DocumentImportTask["schedule"]>>);
const schedule = vi
.spyOn(DocumentImportTask.prototype, "schedule")
.mockResolvedValue({
finished: vi.fn().mockResolvedValue({ documentId: childDocument.id }),
} as unknown as Awaited<ReturnType<DocumentImportTask["schedule"]>>);
const content = await readFile(
path.resolve(
@@ -3543,6 +3545,73 @@ describe("#documents.import", () => {
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(childDocument.id);
expect(schedule).toHaveBeenCalledWith(
expect.objectContaining({ collectionId: collection.id })
);
vi.restoreAllMocks();
});
it("should ignore a collectionId the user cannot write when a parent document is given", async () => {
const team = await buildTeam();
const author = await buildUser({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const ownCollection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const privateCollection = await buildCollection({
userId: author.id,
teamId: team.id,
permission: null,
});
const parentDocument = await buildDocument({
userId: user.id,
teamId: team.id,
collectionId: ownCollection.id,
});
const childDocument = await buildDocument({
userId: user.id,
teamId: team.id,
collectionId: ownCollection.id,
parentDocumentId: parentDocument.id,
});
vi.spyOn(FileStorage, "store").mockResolvedValue(
undefined as unknown as string
);
const schedule = vi
.spyOn(DocumentImportTask.prototype, "schedule")
.mockResolvedValue({
finished: vi.fn().mockResolvedValue({ documentId: childDocument.id }),
} as unknown as Awaited<ReturnType<DocumentImportTask["schedule"]>>);
const content = await readFile(
path.resolve(
__dirname,
"..",
"..",
"..",
"test",
"fixtures",
"markdown.md"
)
);
const form = new FormData();
form.append("file", content, "markdown.md");
form.append("token", user.getSessionToken());
form.append("collectionId", privateCollection.id);
form.append("parentDocumentId", parentDocument.id);
form.append("publish", "true");
const res = await server.post("/api/documents.import", {
headers: form.getHeaders(),
body: form,
});
expect(res.status).toEqual(200);
expect(schedule).toHaveBeenCalledWith(
expect.objectContaining({ collectionId: ownCollection.id })
);
vi.restoreAllMocks();
});
+5 -24
View File
@@ -1623,29 +1623,10 @@ router.post(
throw ValidationError("one of attachmentId or file is required");
}
let parentDocument: Document | null = null;
let collection: Collection | null = null;
if (parentDocumentId) {
parentDocument = await Document.findByPk(parentDocumentId, {
userId: user.id,
});
if (parentDocument?.collectionId) {
collection = await Collection.findByPk(parentDocument.collectionId, {
userId: user.id,
});
}
authorize(user, "createChildDocument", parentDocument, {
collection,
});
} else if (collectionId) {
collection = await Collection.findByPk(collectionId, {
userId: user.id,
});
authorize(user, "createDocument", collection);
}
const { collection } = await authorizeDocumentCreate(ctx, {
collectionId,
parentDocumentId,
});
let key: string;
let fileName: string;
@@ -1686,7 +1667,7 @@ router.post(
mimeType,
},
userId: user.id,
collectionId: collectionId ?? parentDocument?.collectionId,
collectionId: collection?.id,
parentDocumentId,
publish,
authType: ctx.state.auth.type,