Merge pull request #2764 from thelounge/xpaw/safer-log-folders

Limit network name length and replace spaces in log folders
This commit is contained in:
Pavel Djundik 2018-09-09 15:03:55 +03:00 committed by GitHub
commit 72fe898e60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -31,8 +31,7 @@ class TextFileMessageStorage {
return; return;
} }
const networkFolderName = cleanFilename(`${network.name}-${network.uuid.substring(network.name.length + 1)}`); const logPath = path.join(Helper.getUserLogsPath(), this.client.name, TextFileMessageStorage.getNetworkFolderName(network));
const logPath = path.join(Helper.getUserLogsPath(), this.client.name, networkFolderName);
try { try {
fsextra.ensureDirSync(logPath); fsextra.ensureDirSync(logPath);
@ -114,6 +113,14 @@ class TextFileMessageStorage {
canProvideMessages() { canProvideMessages() {
return false; return false;
} }
static getNetworkFolderName(network) {
// Limit network name in the folder name to 23 characters
// So we can still fit 12 characters of the uuid for de-duplication
const networkName = cleanFilename(network.name.substring(0, 23).replace(/ /g, "-"));
return `${networkName}-${network.uuid.substring(networkName.length + 1)}`;
}
} }
module.exports = TextFileMessageStorage; module.exports = TextFileMessageStorage;

View File

@ -0,0 +1,34 @@
"use strict";
const expect = require("chai").expect;
const TextFileMessageStorage = require("../../src/plugins/messageStorage/text");
describe("TextFileMessageStorage", function() {
it("should combine network name and uuid into a safe name", function() {
expect(TextFileMessageStorage.getNetworkFolderName({
name: "Freenode",
uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628",
})).to.equal("freenode-4016-45e0-a8a8-d378fb252628");
});
it("network name should be cleaned up and lowercased", function() {
expect(TextFileMessageStorage.getNetworkFolderName({
name: "@ TeSt ../..\\<>:\"/\\|?*",
uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628",
})).to.equal("@-test-.._..--45e0-a8a8-d378fb252628");
});
it("folder name may contain two dashes if on boundary", function() {
expect(TextFileMessageStorage.getNetworkFolderName({
name: "Freenod",
uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628",
})).to.equal("freenod--4016-45e0-a8a8-d378fb252628");
});
it("should limit network name length", function() {
expect(TextFileMessageStorage.getNetworkFolderName({
name: "This network name is longer than the uuid itself but it should be limited",
uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628",
})).to.equal("this-network-name-is-lo-d378fb252628");
});
});