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/create/create.js b/packages/create/create.js index e7f668fc..3d0557af 100755 --- a/packages/create/create.js +++ b/packages/create/create.js @@ -118,12 +118,11 @@ async function safeOutputFile(file, data, options={}) { } async function execFile(cmd, args) { - const escaped = args.map(escapeArg); - logger.verbose(`executing ${cmd} ${escaped.join(" ")}`); + 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/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/packages/lib/src/data/ModPack.ts b/packages/lib/src/data/ModPack.ts index 4968980d..33251845 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,86 @@ 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; + } + + 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/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/file_ops.ts b/packages/lib/src/file_ops.ts index fbaaff5b..55d2bda7 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 @@ -332,12 +334,11 @@ export async function downloadFile( } // Fetch stream bytes. Anything that goes wrong from here until the - // body has been written leaves a temporary file that is of no use to - // anyone, including the request never reaching the server at all. + // body has been written leaves a temporary file that needs to be removed try { const response = await fetch(url); if (!response.ok || !response.body) { - 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..b14d8e2a 100644 --- a/packages/lib/src/helpers.ts +++ b/packages/lib/src/helpers.ts @@ -268,6 +268,51 @@ 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); + } + + 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(); +} + function skipWhitespace(pos: number, input: string) { // whitespace = 1*" " while (pos < input.length && input.charAt(pos) === " ") { diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index 72b2b24b..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,8 +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); + const index = src.addressIndex(); + const address = src.withoutRequestId(); + const subscriber = eventData.subscriptions.get(index); switch (request.action) { case "unsubscribe": if (!subscriber) { @@ -352,13 +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: src, filters: request.filters }); + eventData.subscriptions.set(index, { link, address, filters: request.filters }); } else { subscriber.filters.union(request.filters); } @@ -369,9 +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: src, filters: request.filters }); + eventData.subscriptions.set(index, { link, address, filters: request.filters }); } else { subscriber.filters = request.filters; } @@ -384,7 +385,7 @@ export class SubscriptionController { if (eventData.subscriptionUpdate) { const eventReplay = await eventData.subscriptionUpdate(request, src, dst); if (eventReplay) { - link.sendTo(src, eventReplay); + link.sendTo(address, eventReplay); return true; } } 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/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/create/escape_arg.js b/test/create/escape_arg.js index 496737e3..593b1a3f 100644 --- a/test/create/escape_arg.js +++ b/test/create/escape_arg.js @@ -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 }; 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); + }); + }); }); }); 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" }, + ]); + }); + }); }); 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/file_ops.js b/test/lib/file_ops.js index acff0eb6..581cff94 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..27486882 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&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) { diff --git a/test/lib/subscriptions.test.js b/test/lib/subscriptions.test.js index 313b2968..bd556487 100644 --- a/test/lib/subscriptions.test.js +++ b/test/lib/subscriptions.test.js @@ -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");