mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 16:13:12 +09:00
Merge branch 'master' into feature/download-file-cleanup
This commit is contained in:
21 files changed
+632
-46
No files matched your search
@@ -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" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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));
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user