mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
* feat: Re-authorize collaboration connections when access changes Collaboration connections are authorized once, when the websocket is first established, so a connection outlives changes to the access it was granted. Add a processor that publishes access changes to the collaboration servers over Redis. Each server closes the connections it holds within the scope of the change and clients reconnect, at which point they are authorized as normal. Authorization stays in one place, the published scope only has to cover the connections that may be affected. The API update subscriber now shares a single Redis connection rather than opening its own. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor: Log an access change once rather than per connection Every closed connection is already logged by LoggerExtension, so record the reason and the number of connections affected once instead. The count also makes a scope that matches nothing visible. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
199 lines
6.5 KiB
TypeScript
199 lines
6.5 KiB
TypeScript
import type {
|
|
Hocuspocus,
|
|
onConfigurePayload,
|
|
onDestroyPayload,
|
|
} from "@hocuspocus/server";
|
|
import { AuthorizationChanged } from "@shared/collaboration/CloseEvents";
|
|
import { CollectionPermission } from "@shared/types";
|
|
import { sleep } from "@shared/utils/timers";
|
|
import { UserMembership } from "@server/models";
|
|
import type User from "@server/models/User";
|
|
import RedisAdapter from "@server/storage/redis";
|
|
import {
|
|
buildAdmin,
|
|
buildCollection,
|
|
buildDocument,
|
|
buildGroup,
|
|
buildGroupUser,
|
|
buildTeam,
|
|
buildUser,
|
|
} from "@server/test/factories";
|
|
import { APIUpdateExtension } from "./APIUpdateExtension";
|
|
import AuthenticationExtension from "./AuthenticationExtension";
|
|
|
|
const buildConnection = (user: User) => ({
|
|
context: { user },
|
|
readOnly: false,
|
|
close: vi.fn(),
|
|
});
|
|
|
|
type Connections = Record<string, ReturnType<typeof buildConnection>[]>;
|
|
|
|
describe("AuthenticationExtension", () => {
|
|
let extension: AuthenticationExtension;
|
|
|
|
const configure = (connections: Connections) => {
|
|
const instance = {
|
|
documents: new Map(
|
|
Object.entries(connections).map(([documentId, forDocument]) => [
|
|
`document.${documentId}`,
|
|
{ getConnections: () => forDocument },
|
|
])
|
|
),
|
|
} as unknown as Hocuspocus;
|
|
|
|
return extension.onConfigure({ instance } as onConfigurePayload);
|
|
};
|
|
|
|
/** Build a private collection that a member has been given access to. */
|
|
const buildPrivateCollection = async () => {
|
|
const team = await buildTeam();
|
|
const admin = await buildAdmin({ teamId: team.id });
|
|
const user = await buildUser({ teamId: team.id });
|
|
const collection = await buildCollection({
|
|
teamId: team.id,
|
|
userId: admin.id,
|
|
permission: null,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: team.id,
|
|
userId: admin.id,
|
|
collectionId: collection.id,
|
|
});
|
|
const membership = await UserMembership.create({
|
|
collectionId: collection.id,
|
|
userId: user.id,
|
|
createdById: admin.id,
|
|
permission: CollectionPermission.ReadWrite,
|
|
});
|
|
|
|
return { admin, collection, document, membership, team, user };
|
|
};
|
|
|
|
beforeEach(() => {
|
|
extension = new AuthenticationExtension();
|
|
});
|
|
|
|
afterEach(() => extension.onDestroy({} as onDestroyPayload));
|
|
|
|
it("should disconnect when an invalidation is published", async () => {
|
|
const { document, user } = await buildPrivateCollection();
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await AuthenticationExtension.invalidate({ userIds: [user.id] });
|
|
await sleep(100);
|
|
|
|
expect(connection.close).toHaveBeenCalledWith(AuthorizationChanged);
|
|
});
|
|
|
|
it("should ignore messages for other collaboration channels", async () => {
|
|
const { document, user } = await buildPrivateCollection();
|
|
const apiUpdates = new APIUpdateExtension();
|
|
await apiUpdates.onConfigure({} as onConfigurePayload);
|
|
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
// Both extensions share a single subscriber, so a message for one must not
|
|
// be handled by the other.
|
|
await RedisAdapter.collaborationClient.publish(
|
|
`collaboration:api-update:${document.id}`,
|
|
JSON.stringify({ actorId: user.id })
|
|
);
|
|
await sleep(100);
|
|
|
|
expect(connection.close).not.toHaveBeenCalled();
|
|
|
|
await AuthenticationExtension.invalidate({ userIds: [user.id] });
|
|
await sleep(100);
|
|
|
|
expect(connection.close).toHaveBeenCalledWith(AuthorizationChanged);
|
|
await apiUpdates.onDestroy({} as onDestroyPayload);
|
|
});
|
|
|
|
it("should disconnect connections for documents in the collection", async () => {
|
|
const { collection, document, user } = await buildPrivateCollection();
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await extension.disconnect({
|
|
userIds: [user.id],
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
expect(connection.close).toHaveBeenCalledWith(AuthorizationChanged);
|
|
});
|
|
|
|
it("should disconnect connections for members of the group", async () => {
|
|
const { document, team, user } = await buildPrivateCollection();
|
|
const group = await buildGroup({ teamId: team.id });
|
|
await buildGroupUser({
|
|
teamId: team.id,
|
|
groupId: group.id,
|
|
userId: user.id,
|
|
});
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await extension.disconnect({ groupId: group.id });
|
|
|
|
expect(connection.close).toHaveBeenCalledWith(AuthorizationChanged);
|
|
});
|
|
|
|
it("should not disconnect connections belonging to another user", async () => {
|
|
const { collection, document, team, user } = await buildPrivateCollection();
|
|
const other = await buildUser({ teamId: team.id });
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await extension.disconnect({
|
|
userIds: [other.id],
|
|
collectionId: collection.id,
|
|
});
|
|
|
|
expect(connection.close).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should not disconnect connections for documents in another collection", async () => {
|
|
const { document, team, user } = await buildPrivateCollection();
|
|
const other = await buildCollection({ teamId: team.id });
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await extension.disconnect({ collectionId: other.id });
|
|
|
|
expect(connection.close).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should not disconnect connections for users outside the group", async () => {
|
|
const { document, team, user } = await buildPrivateCollection();
|
|
const group = await buildGroup({ teamId: team.id });
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await extension.disconnect({ groupId: group.id });
|
|
|
|
expect(connection.close).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should refuse to publish an empty scope", async () => {
|
|
await expect(AuthenticationExtension.invalidate({})).rejects.toThrow(
|
|
"must not be empty"
|
|
);
|
|
await expect(
|
|
AuthenticationExtension.invalidate({ userIds: undefined })
|
|
).rejects.toThrow("must not be empty");
|
|
});
|
|
|
|
it("should not disconnect connections for other documents", async () => {
|
|
const { document, user } = await buildPrivateCollection();
|
|
const connection = buildConnection(user);
|
|
await configure({ [document.id]: [connection] });
|
|
|
|
await extension.disconnect({ documentIds: ["other-document-id"] });
|
|
|
|
expect(connection.close).not.toHaveBeenCalled();
|
|
});
|
|
});
|