From feb0e46aab3c2b84c96cac0a670d38b072548157 Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Thu, 23 Jul 2026 20:38:09 +0000 Subject: [PATCH 01/10] Redact credentials from URLs in download errors The mod portal takes the Factorio account token as a query parameter on the download URL. When a download failed, downloadFile put the whole URL into the error message, which ModStore then logged verbatim, printing the token into the log at error level on every failed mod download. Add a redactUrl helper that replaces the value of credential-carrying query parameters with REDACTED, and use it when downloadFile builds its error. Parameter names and the rest of the URL are kept so the message stays useful for diagnosing which request failed; username is deliberately left intact as it is not a secret and identifies the account in use. Fixes #795 Co-Authored-By: Claude Opus 4.8 (1M context) --- packages/lib/src/file_ops.ts | 4 +++- packages/lib/src/helpers.ts | 44 ++++++++++++++++++++++++++++++++++++ test/lib/file_ops.js | 17 ++++++++++++++ test/lib/helpers.js | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/packages/lib/src/file_ops.ts b/packages/lib/src/file_ops.ts index d1dbf11f..6b54548d 100644 --- a/packages/lib/src/file_ops.ts +++ b/packages/lib/src/file_ops.ts @@ -7,6 +7,8 @@ import fs from "node:fs/promises"; import path from "path"; import stream from "stream"; +import { redactUrl } from "./helpers"; + /** * Returns the newest file in a directory @@ -311,7 +313,7 @@ export async function downloadFile( writeStream.end(); await events.once(writeStream, "close"); await fs.unlink(tempFilePath); - throw new Error(`Failed to download ${url}: ${response.status} ${response.statusText}`); + throw new Error(`Failed to download ${redactUrl(url)}: ${response.status} ${response.statusText}`); } const writer = stream.Writable.toWeb(writeStream); await response.body.pipeTo(writer); diff --git a/packages/lib/src/helpers.ts b/packages/lib/src/helpers.ts index 4d98d7a1..051339c7 100644 --- a/packages/lib/src/helpers.ts +++ b/packages/lib/src/helpers.ts @@ -268,6 +268,50 @@ export function formatBytes(bytes: number, prefixes: "si" | "binary" = "si") { return significant.toFixed(fractionDigits) + units[exponent]; } +/** + * Query parameters whose values are credentials and must never be logged. + * + * Matched case insensitively against the full parameter name. + */ +const sensitiveSearchParams = new Set([ + "access_token", + "api_key", + "apikey", + "password", + "secret", + "token", +]); + +/** + * Replace credentials in a URL with a placeholder + * + * Returns the URL as a string with the values of any query parameter + * carrying a credential replaced by `REDACTED`, so that the URL can + * safely be included in log output and error messages. The parameter + * names are kept intact to preserve the diagnostic value of the URL. + * + * Returns the input unchanged if it is a string which does not parse as + * a URL, as there are then no query parameters to redact. + * + * @param url - URL to redact credentials from. + * @returns url with credentials replaced by `REDACTED`. + */ +export function redactUrl(url: URL | string) { + let parsed; + try { + parsed = new URL(url); + } catch { + return String(url); + } + + for (const name of [...parsed.searchParams.keys()]) { + if (sensitiveSearchParams.has(name.toLowerCase())) { + parsed.searchParams.set(name, "REDACTED"); + } + } + return parsed.toString(); +} + function skipWhitespace(pos: number, input: string) { // whitespace = 1*" " while (pos < input.length && input.charAt(pos) === " ") { diff --git a/test/lib/file_ops.js b/test/lib/file_ops.js index 7e503326..428fd493 100644 --- a/test/lib/file_ops.js +++ b/test/lib/file_ops.js @@ -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( diff --git a/test/lib/helpers.js b/test/lib/helpers.js index b38e50f7..f8632cf0 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -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" + ); + }); + 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) { From 4a9c241eee15d4e3489a5ce4ff511683d1a94031 Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Thu, 23 Jul 2026 22:49:04 +0000 Subject: [PATCH 02/10] Stop addressing subscription events to a request id A subscribe arrives as a request, so the source address the subscription controller handles it with carries the request id used to route the response back to the caller. That address was stored verbatim as the destination for the subscription, so every event broadcast to it, and the replay event sent when the subscription is created, went out addressed to a request that had long since been answered. A request id identifies one request in flight and has no meaning on an event. The receiving link keys response routing off it, so events carrying one are put through paths meant for response traffic. Strip the request id when taking the subscription's destination from the request that created it. Add Address.withoutRequestId for that, as the same address is used in three places here. The subscription map is keyed on addressIndex, which already excludes the request id, so subscriptions were never duplicated by this and only the address events are sent to was wrong. Fixes #847 Co-Authored-By: Claude Opus 4.8 (1M context) --- packages/lib/src/data/messages_core.ts | 18 +++++++ packages/lib/src/subscriptions.ts | 13 +++-- test/lib/subscriptions.test.js | 66 ++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/packages/lib/src/data/messages_core.ts b/packages/lib/src/data/messages_core.ts index 305905e4..c4145a47 100644 --- a/packages/lib/src/data/messages_core.ts +++ b/packages/lib/src/data/messages_core.ts @@ -114,6 +114,24 @@ export class Address { return `${this.type}:${this.id}` as const; } + /** + * Returns this address with any request id removed + * + * A request id identifies one request in flight and is set on the + * source address of a request so that its response can be matched back + * up with it. It is meaningless on anything else, so an address taken + * from a request has to be stripped of it before it is used as the + * destination of an event. + * + * @returns this address without a request id. + */ + withoutRequestId() { + if (this.requestId === undefined) { + return this; + } + return new Address(this.type, this.id); + } + /** * Returns true if this address targets the given destination * @param dst - Destination to check diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index 72b2b24b..7e047f34 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -345,6 +345,9 @@ export class SubscriptionController { } const addressIndex = src.addressIndex(); const subscriber = eventData.subscriptions.get(addressIndex); + // src is the source of a request and carries its request id, which + // has no meaning on the events sent back to the subscriber. + const subscriberDst = src.withoutRequestId(); switch (request.action) { case "unsubscribe": if (!subscriber) { @@ -358,7 +361,9 @@ export class SubscriptionController { case "subscribe": if (!subscriber) { - eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters }); + eventData.subscriptions.set( + addressIndex, { link: link, dst: subscriberDst, filters: request.filters } + ); } else { subscriber.filters.union(request.filters); } @@ -371,7 +376,9 @@ export class SubscriptionController { } eventData.subscriptions.delete(addressIndex); } else if (!subscriber) { - eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters }); + eventData.subscriptions.set( + addressIndex, { link: link, dst: subscriberDst, filters: request.filters } + ); } else { subscriber.filters = request.filters; } @@ -384,7 +391,7 @@ export class SubscriptionController { if (eventData.subscriptionUpdate) { const eventReplay = await eventData.subscriptionUpdate(request, src, dst); if (eventReplay) { - link.sendTo(src, eventReplay); + link.sendTo(subscriberDst, eventReplay); return true; } } diff --git a/test/lib/subscriptions.test.js b/test/lib/subscriptions.test.js index 313b2968..eb4659ed 100644 --- a/test/lib/subscriptions.test.js +++ b/test/lib/subscriptions.test.js @@ -520,6 +520,72 @@ describe("lib/subscriptions", function() { }); }); + describe("subscriber addressing", function() { + // A subscribe arrives as a request, so the source address it is + // handled with carries the request id used to route the response + // back to the caller. That id identifies one request in flight and + // means nothing on an event, see #847. These use a controller of + // their own because a subscription is only ever addressed when it + // is first created, and the shared one is already subscribed. + 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); + + assert.equal(lastSentMessage().name, RegisteredEvent.name); + assert.equal(lastSentMessage().dst.requestId, undefined, "event addressed to a request id"); + assert.deepEqual(lastSentMessage().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 + ); + + assert.equal(lastSentMessage().name, RegisteredEvent.name); + assert.equal(lastSentMessage().dst.requestId, undefined, "replay addressed to a request id"); + assert.deepEqual(lastSentMessage().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); + + assert.equal(lastSentMessage().name, RegisteredEvent.name); + assert.equal(lastSentMessage().dst.requestId, undefined, "event addressed to a request id"); + assert.deepEqual(lastSentMessage().dst.toJSON(), connectorData.dst.toJSON()); + }); + }); + describe(".unsubscribeLink() / .unsubscribeAddress()", function() { beforeEach(function() { subscriptions.handle(RegisteredEvent); From 70305b79370ce7c946ab3d8613952ea91d00da34 Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Thu, 23 Jul 2026 23:02:37 +0000 Subject: [PATCH 03/10] Silence the deprecation warning on install execFile was called with shell: true and an array of arguments, which Node deprecates in DEP0190 because it concatenates the arguments by space without escaping anything, giving the impression they have been made safe. The warning was printed to the console on every install. The arguments here are already escaped for the shell by escapeArg, so join them into the command instead of handing them to execFile to concatenate. Nothing about what ends up being run changes. Point the escaping test at the same form so it covers what the installer actually does, and stops emitting the warning itself. Its several hundred round trip cases still pass, which is what confirms the escaping is unaffected. Fixes #851 Co-Authored-By: Claude Opus 4.8 (1M context) --- packages/create/create.js | 11 +++++++---- test/create/escape_arg.js | 8 +++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/create/create.js b/packages/create/create.js index e7f668fc..236df97b 100755 --- a/packages/create/create.js +++ b/packages/create/create.js @@ -118,12 +118,15 @@ async function safeOutputFile(file, data, options={}) { } async function execFile(cmd, args) { - const escaped = args.map(escapeArg); - logger.verbose(`executing ${cmd} ${escaped.join(" ")}`); + // Arguments are escaped here and joined into the command rather than + // passed to execFile. With shell: true it would only concatenate them by + // space without escaping anything, which is what it is deprecated for + // (DEP0190) as it gives the impression the arguments are made safe. + const command = [cmd, ...args.map(escapeArg)].join(" "); + logger.verbose(`executing ${command}`); return new Promise((resolve, reject) => { let child = child_process.execFile( - cmd, - escaped, + command, { shell: true, diff --git a/test/create/escape_arg.js b/test/create/escape_arg.js index 496737e3..720fc27a 100644 --- a/test/create/escape_arg.js +++ b/test/create/escape_arg.js @@ -8,10 +8,12 @@ const { escapeArg } = require("../../packages/create/escape_arg"); const execFile = util.promisify(child_process.execFile); async function exec(file, args) { + // Escape the arguments and join them into the command the same way + // create.js does, rather than passing them to execFile which would only + // concatenate them by space without escaping (DEP0190). + 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 }; From 870e1f2125ebf00da59ef2271bb538c2a35f549b Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sat, 25 Jul 2026 14:51:14 +0100 Subject: [PATCH 04/10] Remove unneeded comments --- packages/create/create.js | 4 ---- test/create/escape_arg.js | 3 --- 2 files changed, 7 deletions(-) diff --git a/packages/create/create.js b/packages/create/create.js index 236df97b..3d0557af 100755 --- a/packages/create/create.js +++ b/packages/create/create.js @@ -118,10 +118,6 @@ async function safeOutputFile(file, data, options={}) { } async function execFile(cmd, args) { - // Arguments are escaped here and joined into the command rather than - // passed to execFile. With shell: true it would only concatenate them by - // space without escaping anything, which is what it is deprecated for - // (DEP0190) as it gives the impression the arguments are made safe. const command = [cmd, ...args.map(escapeArg)].join(" "); logger.verbose(`executing ${command}`); return new Promise((resolve, reject) => { diff --git a/test/create/escape_arg.js b/test/create/escape_arg.js index 720fc27a..593b1a3f 100644 --- a/test/create/escape_arg.js +++ b/test/create/escape_arg.js @@ -8,9 +8,6 @@ const { escapeArg } = require("../../packages/create/escape_arg"); const execFile = util.promisify(child_process.execFile); async function exec(file, args) { - // Escape the arguments and join them into the command the same way - // create.js does, rather than passing them to execFile which would only - // concatenate them by space without escaping (DEP0190). const command = [file, ...args.map(escapeArg)].join(" "); const { stdout, stderr } = await execFile( command, From cc92e851ad9a72cb52d59b3e118b2f71051f149a Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sat, 25 Jul 2026 15:10:57 +0100 Subject: [PATCH 05/10] Rename `subscriber.dst` to `subscriber.address` --- packages/lib/src/subscriptions.ts | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index 7e047f34..1b3311b6 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -238,7 +238,7 @@ export class SubscriptionRequest { type Subscriber = { link: Link, - dst: Address, + address: Address, filters: SubscriptionFilters, } @@ -296,7 +296,7 @@ export class SubscriptionController { continue; } if (subscriber.filters.intersects(broadcastFilters)) { - subscriber.link.sendTo(subscriber.dst, event); + subscriber.link.sendTo(subscriber.address, event); } } } @@ -343,11 +343,9 @@ export class SubscriptionController { if (!eventData) { throw new Error(`Event ${request.eventName} is not a registered as subscribable`); } - const addressIndex = src.addressIndex(); - const subscriber = eventData.subscriptions.get(addressIndex); - // src is the source of a request and carries its request id, which - // has no meaning on the events sent back to the subscriber. - const subscriberDst = src.withoutRequestId(); + const index = src.addressIndex(); + const address = src.withoutRequestId(); + const subscriber = eventData.subscriptions.get(index); switch (request.action) { case "unsubscribe": if (!subscriber) { @@ -355,15 +353,13 @@ export class SubscriptionController { } subscriber.filters.subtract(request.filters); if (subscriber.filters.isEmpty()) { - eventData.subscriptions.delete(addressIndex); + eventData.subscriptions.delete(index); } break; case "subscribe": if (!subscriber) { - eventData.subscriptions.set( - addressIndex, { link: link, dst: subscriberDst, filters: request.filters } - ); + eventData.subscriptions.set(index, { link, address, filters: request.filters }); } else { subscriber.filters.union(request.filters); } @@ -374,11 +370,9 @@ export class SubscriptionController { if (!subscriber) { return false; } - eventData.subscriptions.delete(addressIndex); + eventData.subscriptions.delete(index); } else if (!subscriber) { - eventData.subscriptions.set( - addressIndex, { link: link, dst: subscriberDst, filters: request.filters } - ); + eventData.subscriptions.set(index, { link, address, filters: request.filters }); } else { subscriber.filters = request.filters; } @@ -391,7 +385,7 @@ export class SubscriptionController { if (eventData.subscriptionUpdate) { const eventReplay = await eventData.subscriptionUpdate(request, src, dst); if (eventReplay) { - link.sendTo(subscriberDst, eventReplay); + link.sendTo(address, eventReplay); return true; } } From 9fe1819ea5844d4ab68f5922144cb51ce60915ad Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sat, 25 Jul 2026 15:23:34 +0100 Subject: [PATCH 06/10] Fix tests --- test/lib/data/messages_core.js | 27 +++++++ test/lib/subscriptions.test.js | 131 ++++++++++++++++----------------- 2 files changed, 92 insertions(+), 66 deletions(-) diff --git a/test/lib/data/messages_core.js b/test/lib/data/messages_core.js index 3ff28509..2e06c673 100644 --- a/test/lib/data/messages_core.js +++ b/test/lib/data/messages_core.js @@ -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)); diff --git a/test/lib/subscriptions.test.js b/test/lib/subscriptions.test.js index eb4659ed..bd556487 100644 --- a/test/lib/subscriptions.test.js +++ b/test/lib/subscriptions.test.js @@ -520,72 +520,6 @@ describe("lib/subscriptions", function() { }); }); - describe("subscriber addressing", function() { - // A subscribe arrives as a request, so the source address it is - // handled with carries the request id used to route the response - // back to the caller. That id identifies one request in flight and - // means nothing on an event, see #847. These use a controller of - // their own because a subscription is only ever addressed when it - // is first created, and the shared one is already subscribed. - 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); - - assert.equal(lastSentMessage().name, RegisteredEvent.name); - assert.equal(lastSentMessage().dst.requestId, undefined, "event addressed to a request id"); - assert.deepEqual(lastSentMessage().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 - ); - - assert.equal(lastSentMessage().name, RegisteredEvent.name); - assert.equal(lastSentMessage().dst.requestId, undefined, "replay addressed to a request id"); - assert.deepEqual(lastSentMessage().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); - - assert.equal(lastSentMessage().name, RegisteredEvent.name); - assert.equal(lastSentMessage().dst.requestId, undefined, "event addressed to a request id"); - assert.deepEqual(lastSentMessage().dst.toJSON(), connectorData.dst.toJSON()); - }); - }); - describe(".unsubscribeLink() / .unsubscribeAddress()", function() { beforeEach(function() { subscriptions.handle(RegisteredEvent); @@ -680,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"); From dfe52df7bbadc03ee9ca030838170d9c12d1d661 Mon Sep 17 00:00:00 2001 From: Loi Nguyen <1948922+lntutor@users.noreply.github.com> Date: Mon, 20 Jul 2026 02:04:35 +0700 Subject: [PATCH 07/10] Show informational mod recommendations --- packages/lib/src/data/ModPack.ts | 91 ++++++++++++++++++- packages/lib/src/data/index.ts | 4 +- .../web_ui/src/components/ModPackViewPage.tsx | 67 +++++++++----- test/lib/data/ModPack.js | 82 ++++++++++++++++- 4 files changed, 218 insertions(+), 26 deletions(-) diff --git a/packages/lib/src/data/ModPack.ts b/packages/lib/src/data/ModPack.ts index 4968980d..1db62177 100644 --- a/packages/lib/src/data/ModPack.ts +++ b/packages/lib/src/data/ModPack.ts @@ -12,7 +12,7 @@ import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo"; import { PartialVersion, PartialVersionSchema, integerPartialVersion, - SourceVersion, SourceVersionSchema, normaliseFullVersion, + SourceVersion, SourceVersionSchema, normaliseFullVersion, integerSourceVersion, } from "./version"; @@ -32,6 +32,10 @@ export interface ModSetting { value: boolean | number | string | ModSettingColor; } +export type ModRecordAdvisory = + | { type: "recommended_dependency", sourceModName: string } + | { type: "update_available", version: SourceVersion }; + const ModSettingJsonSchema = Type.Object({ "value": Type.Union([Type.Boolean(), Type.Number(), Type.String(), ModSettingColor]), }); @@ -56,6 +60,91 @@ export interface ModRecord { warning?: ModDependencyUnsatisfiedReason | "wrong_factorio_version", /** Used inside packages\web_ui\src\components\ModPackViewPage.tsx when there is no error. */ info?: ModInfo, + /** Non-blocking recommendations displayed by the Web UI. */ + advisories?: ModRecordAdvisory[], +} + +type AnnotatedModRecord = ModRecord & { advisories: ModRecordAdvisory[] }; + +/** + * Finds newer versions already installed on the controller for mods in a pack. + */ +export function getInstalledModUpdates(mods: ModRecord[], installedMods: Iterable) { + const updates = new Map(); + + for (const mod of mods) { + if (!mod.info) { + continue; + } + + for (const installedMod of installedMods) { + if ( + installedMod.name !== mod.name + || installedMod.factorioVersion !== mod.info.factorioVersion + || installedMod.integerVersion <= integerSourceVersion(mod.version) + ) { + continue; + } + + const existingUpdate = updates.get(mod.name); + if (!existingUpdate || installedMod.integerVersion > integerSourceVersion(existingUpdate)) { + updates.set(mod.name, installedMod.version); + } + } + } + + return updates; +} + +export function applyModRecordAdvisories( + mods: ModRecord[], + availableUpdates: ReadonlyMap = new Map(), +) { + const annotatedMods: AnnotatedModRecord[] = mods.map(mod => ({ ...mod, advisories: [] })); + const modsByName = new Map(annotatedMods.map(mod => [mod.name, mod])); + + for (const sourceMod of annotatedMods) { + if (!sourceMod.enabled || !sourceMod.info) { + continue; + } + + for (const dependency of sourceMod.info.dependencies) { + if (!dependency.recommended) { + continue; + } + + const targetMod = modsByName.get(dependency.name); + if ( + !targetMod + || targetMod.enabled + || dependency.version && !dependency.version.testVersion(targetMod.version) + ) { + continue; + } + + if (!targetMod.advisories.some( + advisory => advisory.type === "recommended_dependency" + && advisory.sourceModName === sourceMod.name + )) { + targetMod.advisories.push({ + type: "recommended_dependency", + sourceModName: sourceMod.name, + }); + } + } + } + + for (const mod of annotatedMods) { + const availableVersion = availableUpdates.get(mod.name); + if (availableVersion && integerSourceVersion(availableVersion) > integerSourceVersion(mod.version)) { + mod.advisories.push({ type: "update_available", version: availableVersion }); + } + if (mod.advisories.length === 0) { + delete (mod as ModRecord).advisories; + } + } + + return annotatedMods; } const ModRecordJsonSchema = Type.Object({ diff --git a/packages/lib/src/data/index.ts b/packages/lib/src/data/index.ts index d93dc7d2..a8ca8a96 100644 --- a/packages/lib/src/data/index.ts +++ b/packages/lib/src/data/index.ts @@ -5,8 +5,8 @@ */ export { default as ExportManifest } from "./ExportManifest"; export { default as ModInfo, ModDependency } from "./ModInfo"; -export { ModSettingColor } from "./ModPack"; -export type { ModSetting, ModRecord } from "./ModPack"; +export { ModSettingColor, getInstalledModUpdates, applyModRecordAdvisories } from "./ModPack"; +export type { ModSetting, ModRecord, ModRecordAdvisory } from "./ModPack"; export { default as ModPack } from "./ModPack"; export { default as ModuleInfo } from "./ModuleInfo"; export { default as Permission } from "./Permission"; diff --git a/packages/web_ui/src/components/ModPackViewPage.tsx b/packages/web_ui/src/components/ModPackViewPage.tsx index 937450e2..c00a4315 100644 --- a/packages/web_ui/src/components/ModPackViewPage.tsx +++ b/packages/web_ui/src/components/ModPackViewPage.tsx @@ -11,6 +11,7 @@ import { import { ExportOutlined, FileUnknownOutlined, FileExclamationOutlined, FileSyncOutlined, CloseOutlined, DeleteOutlined, ToolOutlined, PlusOutlined, CloudSyncOutlined, CloudDownloadOutlined, + LikeOutlined, } from "@ant-design/icons"; import type { SorterResult, FilterValue, TableCurrentDataSource } from "antd/es/table/interface"; @@ -545,7 +546,7 @@ function ModsTable(props: ModsTableProps) { } } - const mods = [...props.modPack.mods.values(), ...deletedMods.values()].map( + let mods = [...props.modPack.mods.values(), ...deletedMods.values()].map( (mod: lib.ModRecord): lib.ModRecord => { if (props.builtInModNames.includes(mod.name)) { return { @@ -590,6 +591,7 @@ function ModsTable(props: ModsTableProps) { } } } + mods = lib.applyModRecordAdvisories(mods, lib.getInstalledModUpdates(mods, modInfos.values())); async function fixDependencyIssues(mod: lib.ModRecord) { if (!mod.info) { @@ -766,27 +768,48 @@ function ModsTable(props: ModsTableProps) { { title: "Name", key: "name", - render: (_, mod: lib.ModRecord) => <> - {mod.error === "missing" && - {" "} - } - {mod.error === "bad_checksum" && - {" "} - } - {mod.warning === "incompatible" && - {" "} - } - {mod.warning === "missing_dependency" && - {" "} - } - {mod.warning === "wrong_version" && - {" "} - } - {mod.warning === "wrong_factorio_version" && - {" "} - } - {mod.info?.title || mod.name} - , + render: (_, mod: lib.ModRecord) => { + const recommendedBy = mod.advisories + ?.filter(advisory => advisory.type === "recommended_dependency") + .map(advisory => advisory.sourceModName) ?? []; + const update = mod.advisories + ?.find(advisory => advisory.type === "update_available"); + return <> + {mod.error === "missing" && + {" "} + } + {mod.error === "bad_checksum" && + {" "} + } + {mod.warning === "incompatible" && + {" "} + } + {mod.warning === "missing_dependency" && + {" "} + } + {mod.warning === "wrong_version" && + {" "} + } + {mod.warning === "wrong_factorio_version" && + {" "} + } + {recommendedBy.length > 0 && + {" "} + } + {update?.type === "update_available" && + {" "} + } + {mod.info?.title || mod.name} + ; + }, defaultSortOrder: "ascend", sorter: (a, b) => strcmp(a.name, b.name), }, diff --git a/test/lib/data/ModPack.js b/test/lib/data/ModPack.js index 5000845b..b813374b 100644 --- a/test/lib/data/ModPack.js +++ b/test/lib/data/ModPack.js @@ -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" }, + ]); + }); + }); }); From 74bd19c0632b6e336581333e0ee97cdba28757af Mon Sep 17 00:00:00 2001 From: Loi Nguyen Date: Sun, 26 Jul 2026 17:26:17 +0700 Subject: [PATCH 08/10] Remove redundant recommended-dependency duplicate guard Address review feedback: modsByName is built from annotatedMods so the advisories array always exists, and each source mod is iterated once with no duplicate dependencies, so the existence check can never fail. Push the recommended_dependency advisory directly. Co-Authored-By: Claude Opus 4.8 (1M context) Claude-Session: https://claude.ai/code/session_01G8b9dEaJtpvydzz4ttM7JH --- packages/lib/src/data/ModPack.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/lib/src/data/ModPack.ts b/packages/lib/src/data/ModPack.ts index 1db62177..33251845 100644 --- a/packages/lib/src/data/ModPack.ts +++ b/packages/lib/src/data/ModPack.ts @@ -122,15 +122,10 @@ export function applyModRecordAdvisories( continue; } - if (!targetMod.advisories.some( - advisory => advisory.type === "recommended_dependency" - && advisory.sourceModName === sourceMod.name - )) { - targetMod.advisories.push({ - type: "recommended_dependency", - sourceModName: sourceMod.name, - }); - } + targetMod.advisories.push({ + type: "recommended_dependency", + sourceModName: sourceMod.name, + }); } } From 08651b591746e7e3c86db2900329ce4ca4cc2bc4 Mon Sep 17 00:00:00 2001 From: Loi Nguyen Date: Sun, 26 Jul 2026 18:20:22 +0700 Subject: [PATCH 09/10] fix: prevent restart downgrades (#951) Co-authored-by: Loi Nguyen <1948922+lntutor@users.noreply.github.com> --- packages/controller/package.json | 2 + packages/controller/src/ControlConnection.ts | 9 +++ packages/controller/src/Controller.ts | 26 +++++++++ packages/host/src/Host.ts | 34 +++++++++++ test/controller/ControlConnection.test.js | 42 ++++++++++++++ test/controller/Controller.js | 17 ++++++ test/host.js | 60 +++++++++++++++++++- 7 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 test/controller/ControlConnection.test.js diff --git a/packages/controller/package.json b/packages/controller/package.json index 4ef85bce..cc070c83 100644 --- a/packages/controller/package.json +++ b/packages/controller/package.json @@ -43,6 +43,7 @@ "finalhandler": "^1.3.1", "jsonwebtoken": "catalog:", "jszip": "catalog:", + "semver": "^7.7.1", "winston": "catalog:", "winston-daily-rotate-file": "catalog:", "ws": "catalog:", @@ -57,6 +58,7 @@ "@types/finalhandler": "catalog:", "@types/jsonwebtoken": "catalog:", "@types/node": "catalog:", + "@types/semver": "^7.5.0", "@types/ws": "catalog:", "@types/yargs": "^17.0.33", "react": "catalog:", diff --git a/packages/controller/src/ControlConnection.ts b/packages/controller/src/ControlConnection.ts index bb0d9812..6baf2fe4 100644 --- a/packages/controller/src/ControlConnection.ts +++ b/packages/controller/src/ControlConnection.ts @@ -189,6 +189,15 @@ export default class ControlConnection extends BaseConnection { if (!this._controller.canRestart) { throw new lib.RequestError("Cannot restart, controller does not have a process monitor to restart it."); } + const downgrade = await this._controller.checkRestartDowngrade(); + if (downgrade) { + const { installedVersion, runningVersion } = downgrade; + throw new lib.RequestError( + `Cannot restart controller with installed Clusterio version ${installedVersion} because it ` + + `is older than running version ${runningVersion}. ` + + "Stop the controller before starting the older version manually." + ); + } this._controller.shouldRestart = true; this._controller.stop(); } diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 8051a0cc..bbf571d8 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -12,6 +12,7 @@ import http from "http"; import https from "https"; import jwt from "jsonwebtoken"; import path from "path"; +import semver from "semver"; import stream from "stream"; import * as lib from "@clusterio/lib"; @@ -654,6 +655,31 @@ export default class Controller { return false; } + async checkRestartDowngrade() { + try { + const runningVersion = this.config.get("controller.version"); + const packageJsonPath = require.resolve("@clusterio/controller/package.json"); + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8")); + const installedVersion = packageJson.version; + + if (!semver.valid(runningVersion) || !semver.valid(installedVersion)) { + logger.warn( + `Unable to compare running controller version ${runningVersion} ` + + `with installed version ${installedVersion}.` + ); + return null; + } + + if (semver.lt(installedVersion, runningVersion)) { + return { installedVersion, runningVersion }; + } + } catch (err: any) { + logger.warn(`Failed to check controller version before restart:\n${err.stack ?? err.message}`); + } + + return null; + } + onAutosaveIntervalChanged() { if (this.autosaveInterval) { clearInterval(this.autosaveInterval); diff --git a/packages/host/src/Host.ts b/packages/host/src/Host.ts index b99392dc..9c9d5abe 100644 --- a/packages/host/src/Host.ts +++ b/packages/host/src/Host.ts @@ -2,6 +2,7 @@ import fs from "node:fs/promises"; import path from "path"; import events from "events"; import pidusage from "pidusage"; +import semver from "semver"; import setBlocking from "set-blocking"; import stream from "stream"; import util from "util"; @@ -496,6 +497,14 @@ export default class Host extends lib.Link { if (!this.canRestart) { throw new lib.RequestError("Cannot restart, host does not have a process monitor to restart it."); } + const downgrade = await this.checkRestartDowngrade(); + if (downgrade) { + const { installedVersion, runningVersion } = downgrade; + throw new lib.RequestError( + `Cannot restart host with installed Clusterio version ${installedVersion} because it is older than ` + + `running version ${runningVersion}. Stop the host before starting the older version manually.` + ); + } process.exitCode = 1; this.shutdown(); } @@ -875,6 +884,31 @@ export default class Host extends lib.Link { return false; } + async checkRestartDowngrade() { + try { + const runningVersion = this.config.get("host.version"); + const packageJsonPath = require.resolve("@clusterio/host/package.json"); + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8")); + const installedVersion = packageJson.version; + + if (!semver.valid(runningVersion) || !semver.valid(installedVersion)) { + logger.warn( + `Unable to compare running host version ${runningVersion} ` + + `with installed version ${installedVersion}.` + ); + return null; + } + + if (semver.lt(installedVersion, runningVersion)) { + return { installedVersion, runningVersion }; + } + } catch (err: any) { + logger.warn(`Failed to check host version before restart:\n${err.stack ?? err.message}`); + } + + return null; + } + async handleHostMetricsRequest() { let requests: Promise>[] = []; for (let instanceConnection of this.instanceConnections.values()) { diff --git a/test/controller/ControlConnection.test.js b/test/controller/ControlConnection.test.js new file mode 100644 index 00000000..90483a08 --- /dev/null +++ b/test/controller/ControlConnection.test.js @@ -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); + }); + }); +}); diff --git a/test/controller/Controller.js b/test/controller/Controller.js index 7b0d4127..62c1c264 100644 --- a/test/controller/Controller.js +++ b/test/controller/Controller.js @@ -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; diff --git a/test/host.js b/test/host.js index ad7a16aa..4308c189 100644 --- a/test/host.js +++ b/test/host.js @@ -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); + }); + }); }); }); From 417ee874229fa36a423519344a0f14e067dfc805 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Wed, 29 Jul 2026 22:56:19 +0100 Subject: [PATCH 10/10] Maintain repeated credential parameters --- packages/lib/src/helpers.ts | 9 +++++---- test/lib/helpers.js | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/lib/src/helpers.ts b/packages/lib/src/helpers.ts index 051339c7..b14d8e2a 100644 --- a/packages/lib/src/helpers.ts +++ b/packages/lib/src/helpers.ts @@ -304,11 +304,12 @@ export function redactUrl(url: URL | string) { return String(url); } - for (const name of [...parsed.searchParams.keys()]) { - if (sensitiveSearchParams.has(name.toLowerCase())) { - parsed.searchParams.set(name, "REDACTED"); - } + const searchParams = new URLSearchParams(); + for (const [name, value] of parsed.searchParams) { + searchParams.append(name, sensitiveSearchParams.has(name.toLowerCase()) ? "REDACTED" : value); } + + parsed.search = searchParams.toString(); return parsed.toString(); } diff --git a/test/lib/helpers.js b/test/lib/helpers.js index f8632cf0..27486882 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -256,7 +256,7 @@ describe("lib/helpers", function() { 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" + "https://example.com/?token=REDACTED&token=REDACTED" ); }); it("should accept a URL object", function() {