Merge branch 'master' into feature/download-file-cleanup

This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-07-29 23:13:29 +01:00
commit 3a689b379a
21 files changed
+632 -46

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;
+2 -3
View File
@@ -8,10 +8,9 @@ const { escapeArg } = require("../../packages/create/escape_arg");
const execFile = util.promisify(child_process.execFile);
async function exec(file, args) {
const command = [file, ...args.map(escapeArg)].join(" ");
const { stdout, stderr } = await execFile(
file,
args.map(escapeArg),
command,
{ shell: true, cwd: __dirname, env: { ...process.env, pct: "%"} }
);
return { stdout: JSON.parse(stdout), stderr };
+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);
});
});
});
});
+81 -1
View File
@@ -3,7 +3,7 @@ const assert = require("assert").strict;
const zlib = require("zlib");
const lib = require("@clusterio/lib");
const { ModPack } = lib;
const { ModInfo, ModPack, getInstalledModUpdates, applyModRecordAdvisories } = lib;
describe("lib/data/ModPack", function() {
@@ -421,4 +421,84 @@ describe("lib/data/ModPack", function() {
});
});
});
describe("applyModRecordAdvisories()", function() {
it("finds newer installed versions for the same Factorio version", function() {
const current = ModInfo.fromJSON({
name: "source",
version: "1.0.0",
factorio_version: "2.0",
});
const newer = ModInfo.fromJSON({
name: "source",
version: "1.1.0",
factorio_version: "2.0",
});
const otherFactorioVersion = ModInfo.fromJSON({
name: "source",
version: "2.0.0",
factorio_version: "1.1",
});
const updates = getInstalledModUpdates([
{ name: "source", enabled: true, version: "1.0.0", info: current },
], [current, newer, otherFactorioVersion]);
assert.deepEqual([...updates], [["source", "1.1.0"]]);
});
it("marks compatible disabled recommended dependencies", function() {
const sourceInfo = ModInfo.fromJSON({
name: "source",
version: "1.0.0",
dependencies: ["+ quality >= 2.0.0", "+ stale >= 2.0.0", "+ active"],
});
const otherInfo = ModInfo.fromJSON({
name: "other",
version: "1.0.0",
dependencies: ["+ quality"],
});
const mods = [
{ name: "source", enabled: true, version: "1.0.0", info: sourceInfo },
{ name: "other", enabled: true, version: "1.0.0", info: otherInfo },
{ name: "ignored", enabled: false, version: "1.0.0", info: otherInfo },
{ name: "quality", enabled: false, version: "2.0.0" },
{ name: "stale", enabled: false, version: "1.0.0" },
{ name: "active", enabled: true, version: "1.0.0" },
];
const annotated = applyModRecordAdvisories(mods);
assert.deepEqual(annotated[3].advisories, [
{ type: "recommended_dependency", sourceModName: "source" },
{ type: "recommended_dependency", sourceModName: "other" },
]);
assert.equal(annotated[4].advisories, undefined);
assert.equal(annotated[5].advisories, undefined);
assert.equal(mods[3].advisories, undefined);
});
it("marks only newer resolved updates and preserves other advisories", function() {
const sourceInfo = ModInfo.fromJSON({
name: "source",
version: "1.0.0",
dependencies: ["+ quality"],
});
const mods = [
{ name: "source", enabled: true, version: "1.0.0", info: sourceInfo },
{ name: "quality", enabled: false, version: "2.0.0" },
];
const annotated = applyModRecordAdvisories(mods, new Map([
["source", "1.0.0"],
["quality", "2.1.0"],
]));
assert.deepEqual(annotated[0].advisories, undefined);
assert.deepEqual(annotated[1].advisories, [
{ type: "recommended_dependency", sourceModName: "source" },
{ type: "update_available", version: "2.1.0" },
]);
});
});
});
+27
View File
@@ -16,6 +16,33 @@ describe("lib/data/messages_core", function() {
const instanceBr = new Address(Address.broadcast, Address.instance);
const controlBr = new Address(Address.broadcast, Address.control);
describe(".withoutRequestId()", () => {
it("returns the same instance when requestId is undefined", () => {
const address = new Address(1, 2);
assert.strictEqual(address.withoutRequestId(), address);
});
it("returns a new instance when requestId is present", () => {
const address = new Address(1, 2, 123);
const result = address.withoutRequestId();
assert.notStrictEqual(result, address);
assert.deepEqual(result, new Address(1, 2));
});
it("does not modify the original address", () => {
const address = new Address(1, 2, 123);
address.withoutRequestId();
assert.equal(address.type, 1);
assert.equal(address.id, 2);
assert.equal(address.requestId, 123);
});
});
it("Should correctly resolve addressedTo", function() {
assert(controller.addressedTo(controller));
assert(!controller.addressedTo(host1));
+17
View File
@@ -291,6 +291,23 @@ describe("lib/file_ops", function() {
);
});
it("should not leak credentials from the url when throwing", async function () {
// The mod portal takes the Factorio token as a query parameter, see #795.
const downloadPath = path.join(downloadDir, "credentials.txt");
const url = new URL("/not-found", baseUrl);
url.searchParams.set("username", "user");
url.searchParams.set("token", "secret");
await assert.rejects(
lib.downloadFile(url, downloadPath, "overwrite"),
err => {
assert.ok(!err.message.includes("secret"), `token leaked in: ${err.message}`);
assert.match(err.message, /token=REDACTED/);
assert.match(err.message, /username=user/);
return true;
}
);
});
it("should throw if fetch response body is missing", async function () {
const downloadPath = path.join(downloadDir, "not-content.txt");
await assert.rejects(
+39
View File
@@ -236,6 +236,45 @@ describe("lib/helpers", function() {
});
});
describe("redactUrl()", function() {
it("should redact the value of credential query parameters", function() {
assert.equal(
lib.redactUrl("https://mods.factorio.com/download/mod?username=user&token=secret"),
"https://mods.factorio.com/download/mod?username=user&token=REDACTED"
);
for (const name of ["access_token", "api_key", "apikey", "password", "secret", "token"]) {
assert.equal(
lib.redactUrl(`https://example.com/?${name}=secret`),
`https://example.com/?${name}=REDACTED`
);
}
});
it("should match credential parameter names case insensitively", function() {
assert.equal(lib.redactUrl("https://example.com/?Token=secret"), "https://example.com/?Token=REDACTED");
assert.equal(lib.redactUrl("https://example.com/?API_KEY=secret"), "https://example.com/?API_KEY=REDACTED");
});
it("should redact every occurrence of a repeated credential parameter", function() {
assert.equal(
lib.redactUrl("https://example.com/?token=first&token=second"),
"https://example.com/?token=REDACTED&token=REDACTED"
);
});
it("should accept a URL object", function() {
assert.equal(
lib.redactUrl(new URL("https://example.com/path?token=secret")),
"https://example.com/path?token=REDACTED"
);
});
it("should leave URLs without credentials untouched", function() {
assert.equal(lib.redactUrl("https://example.com/path"), "https://example.com/path");
assert.equal(lib.redactUrl("https://example.com/?page=2"), "https://example.com/?page=2");
});
it("should return unparsable input unchanged", function() {
assert.equal(lib.redactUrl("not a url?token=secret"), "not a url?token=secret");
assert.equal(lib.redactUrl(""), "");
});
});
function parse(input, attributes) {
const result = lib.parseSearchString(input, attributes);
if (!result.issues.length) {
+65
View File
@@ -614,6 +614,71 @@ describe("lib/subscriptions", function() {
), /unreachable case: invalid action/);
});
describe("addressing", function() {
// A subscribe arrives as a request, so the source address it is
// handled with carries the request id which should not be persisted
const connectorData = connectorSetupDate[0];
function requestSource(requestId) {
return new lib.Address(connectorData.dst.type, connectorData.dst.id, requestId);
}
function lastSentMessage() {
return getLink(connectorData.id).connector.sentMessages.at(-1);
}
it("should strip the request id from broadcast events", async function() {
const ownSubscriptions = new lib.SubscriptionController(mockController);
ownSubscriptions.handle(RegisteredEvent);
await ownSubscriptions.handleRequest(
getLink(connectorData.id),
new lib.SubscriptionRequest(RegisteredEvent.name, "subscribe"),
requestSource(213), connectorData.src
);
ownSubscriptions.broadcast(new RegisteredEvent());
await onceConnectorSend(connectorData.id);
const message = lastSentMessage();
assert.equal(message.name, RegisteredEvent.name);
assert.equal(message.dst.requestId, undefined, "event addressed to a request id");
assert.deepEqual(message.dst.toJSON(), connectorData.dst.toJSON());
});
it("should strip the request id from the replayed event", async function() {
const ownSubscriptions = new lib.SubscriptionController(mockController);
ownSubscriptions.handle(RegisteredEvent, async () => new RegisteredEvent());
await ownSubscriptions.handleRequest(
getLink(connectorData.id),
new lib.SubscriptionRequest(RegisteredEvent.name, "subscribe"),
requestSource(417), connectorData.src
);
const message = lastSentMessage();
assert.equal(message.name, RegisteredEvent.name);
assert.equal(message.dst.requestId, undefined, "replay addressed to a request id");
assert.deepEqual(message.dst.toJSON(), connectorData.dst.toJSON());
});
it("should strip the request id when replacing filters creates the subscription", async function() {
const ownSubscriptions = new lib.SubscriptionController(mockController);
ownSubscriptions.handle(RegisteredEvent);
await ownSubscriptions.handleRequest(
getLink(connectorData.id),
new lib.SubscriptionRequest(RegisteredEvent.name, "replace", 0, "foo"),
requestSource(918), connectorData.src
);
ownSubscriptions.broadcast(new RegisteredEvent(), "foo");
await onceConnectorSend(connectorData.id);
const message = lastSentMessage();
assert.equal(message.name, RegisteredEvent.name);
assert.equal(message.dst.requestId, undefined, "event addressed to a request id");
assert.deepEqual(message.dst.toJSON(), connectorData.dst.toJSON());
});
});
describe("subscribe", function() {
it("should create a new subscription", async function() {
const request = new lib.SubscriptionRequest(RegisteredEvent.name, "subscribe");