Files
outline/server/collaboration/EditorVersionExtension.ts
T
Tom Moor 0ec6440506 Prevent outdated clients from connecting to collaboration server (#8751)
* Move editor version check to collaboration server connection

* connected -> onConnect

* docs

* Remove hardcoded event codes
2025-03-22 14:35:45 -07:00

41 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Extension, onConnectPayload } from "@hocuspocus/server";
import semver from "semver";
import { EditorUpdateError } from "@shared/collaboration/CloseEvents";
import EDITOR_VERSION from "@shared/editor/version";
import Logger from "@server/logging/Logger";
import { trace } from "@server/logging/tracing";
import { withContext } from "./types";
@trace()
export class EditorVersionExtension implements Extension {
/**
* On connect hook prevents connections from clients with an outdated editor
* version. See the equivalent logic for API in /server/routes/api/middlewares/editor.ts
*
* @param data The connect payload
* @returns Promise, resolving will allow the connection, rejecting will drop.
*/
onConnect({ requestParameters }: withContext<onConnectPayload>) {
const clientVersion = requestParameters.get("editorVersion");
if (clientVersion) {
const parsedClientVersion = semver.parse(clientVersion);
const parsedServerVersion = semver.parse(EDITOR_VERSION);
if (
parsedClientVersion &&
parsedServerVersion &&
parsedClientVersion.major < parsedServerVersion.major
) {
Logger.debug(
"multiplayer",
`Dropping connection due to outdated editor version: ${clientVersion} < ${EDITOR_VERSION}`
);
return Promise.reject(EditorUpdateError);
}
}
return Promise.resolve();
}
}