Files
outline/server/routes/api/middlewares/editor.ts
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

31 lines
925 B
TypeScript

import { Context, Next } from "koa";
import semver from "semver";
import EDITOR_VERSION from "@shared/editor/version";
import { EditorUpdateError } from "@server/errors";
export default function editor() {
/**
* Middleware to prevent connections from clients with an outdated editor
* version. See the equivalent logic for collab server in:
* /server/collaboration/EditorVersionExtension.ts
*/
return async function editorMiddleware(ctx: Context, next: Next) {
const clientVersion = ctx.headers["x-editor-version"];
if (clientVersion) {
const parsedClientVersion = semver.parse(clientVersion as string);
const parsedCurrentVersion = semver.parse(EDITOR_VERSION);
if (
parsedClientVersion &&
parsedCurrentVersion &&
parsedClientVersion.major < parsedCurrentVersion.major
) {
throw EditorUpdateError();
}
}
return next();
};
}