fix: prevent restart downgrades (#951)

Co-authored-by: Loi Nguyen <1948922+lntutor@users.noreply.github.com>
This commit is contained in:
Loi NguyenandLoi Nguyen authored and GitHub committed 2026-07-26 12:20:22 +01:00
1 parent 9ff090c30b
commit 08651b5917
7 files changed
+189 -1

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
"use strict";
const assert = require("assert").strict;
const { ControlConnection } = require("@clusterio/controller");
describe("controller/src/ControlConnection", function() {
describe(".handleControllerRestartRequest()", function() {
let mockController;
beforeEach(function() {
mockController = {
canRestart: true,
shouldRestart: false,
stopped: false,
async checkRestartDowngrade() { return null; },
stop() { this.stopped = true; },
};
});
async function restart() {
await ControlConnection.prototype.handleControllerRestartRequest.call({
_controller: mockController,
});
}
it("restarts when the installed version is not older", async function() {
await restart();
assert.equal(mockController.shouldRestart, true);
assert.equal(mockController.stopped, true);
});
it("rejects a downgrade without stopping the controller", async function() {
mockController.checkRestartDowngrade = async () => ({
installedVersion: "1.0.0",
runningVersion: "2.0.0",
});
await assert.rejects(restart, /Stop the controller before starting the older version manually/);
assert.equal(mockController.shouldRestart, false);
assert.equal(mockController.stopped, false);
});
});
});
+17
View File
@@ -252,6 +252,23 @@ describe("controller/src/Controller", function() {
assert.equal(controller.config.restartRequired, true);
});
});
describe(".checkRestartDowngrade()", function() {
it("returns null when the installed version matches", async function() {
controller.config.set("controller.version", controllerVersion);
assert.equal(await controller.checkRestartDowngrade(), null);
});
it("returns null when the installed version is newer", async function() {
controller.config.set("controller.version", "0.0.0");
assert.equal(await controller.checkRestartDowngrade(), null);
});
it("returns both versions when the installed version is older", async function() {
controller.config.set("controller.version", "999.0.0");
assert.deepEqual(await controller.checkRestartDowngrade(), {
installedVersion: controllerVersion,
runningVersion: "999.0.0",
});
});
});
describe(".factorioVersions", function() {
const _fetch = global.fetch;
+59 -1
View File
@@ -118,9 +118,9 @@ describe("Host testing", function() {
});
describe(".checkRestartRequired()", function() {
let host;
const hostVersion = require("@clusterio/host/package.json").version;
beforeEach(function() {
// This can be used more generally, but i did not want to interfere with handleSyncUserListsEvent
const hostVersion = require("@clusterio/host/package.json").version;
const pluginInfos = [{
name: "global_chat",
requirePath: path.dirname(require.resolve("@clusterio/host/package.json")),
@@ -174,5 +174,63 @@ describe("Host testing", function() {
assert.equal(host.config.restartRequired, true);
});
});
describe(".checkRestartDowngrade()", function() {
let host;
const hostVersion = require("@clusterio/host/package.json").version;
beforeEach(function() {
const hostConfig = new lib.HostConfig("host", { "host.version": hostVersion });
const hostConnector = new HostConnector(hostConfig, []);
host = new Host(hostConnector, hostConfig, []);
});
it("returns null when the installed version matches", async function() {
assert.equal(await host.checkRestartDowngrade(), null);
});
it("returns null when the installed version is newer", async function() {
host.config.set("host.version", "0.0.0");
assert.equal(await host.checkRestartDowngrade(), null);
});
it("returns both versions when the installed version is older", async function() {
host.config.set("host.version", "999.0.0");
assert.deepEqual(await host.checkRestartDowngrade(), {
installedVersion: hostVersion,
runningVersion: "999.0.0",
});
});
});
describe(".handleHostRestartRequest()", function() {
let mockHost;
beforeEach(function() {
process.exitCode = undefined;
mockHost = {
canRestart: true,
shutdownCalled: false,
async checkRestartDowngrade() { return null; },
shutdown() { this.shutdownCalled = true; },
handleHostRestartRequest: Host.prototype.handleHostRestartRequest,
};
});
afterEach(function() {
process.exitCode = undefined;
});
it("restarts when the installed version is not older", async function() {
await mockHost.handleHostRestartRequest();
assert.equal(process.exitCode, 1);
assert.equal(mockHost.shutdownCalled, true);
});
it("rejects a downgrade without shutting down the host", async function() {
mockHost.checkRestartDowngrade = async () => ({
installedVersion: "1.0.0",
runningVersion: "2.0.0",
});
await assert.rejects(
() => mockHost.handleHostRestartRequest(),
/Stop the host before starting the older version manually/
);
assert.equal(process.exitCode, undefined);
assert.equal(mockHost.shutdownCalled, false);
});
});
});
});