Prevent level order to be modified (#8821)

This commit is contained in:
Michael Weimann 2022-06-13 08:03:16 +02:00 committed by GitHub
parent 18d7f7c525
commit b87c537885
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View file

@ -80,7 +80,8 @@ export const LEVEL_ORDER = [
function getLevelOrder(setting: ISetting): SettingLevel[] { function getLevelOrder(setting: ISetting): SettingLevel[] {
// Settings which support only a single setting level are inherently ordered // Settings which support only a single setting level are inherently ordered
if (setting.supportedLevelsAreOrdered || setting.supportedLevels.length === 1) { if (setting.supportedLevelsAreOrdered || setting.supportedLevels.length === 1) {
return setting.supportedLevels; // return a copy to prevent callers from modifying the array
return [...setting.supportedLevels];
} }
return LEVEL_ORDER; return LEVEL_ORDER;
} }
@ -359,7 +360,7 @@ export default class SettingsStore {
if (!levelOrder.includes(SettingLevel.DEFAULT)) levelOrder.push(SettingLevel.DEFAULT); // always include default if (!levelOrder.includes(SettingLevel.DEFAULT)) levelOrder.push(SettingLevel.DEFAULT); // always include default
const minIndex = levelOrder.indexOf(level); const minIndex = levelOrder.indexOf(level);
if (minIndex === -1) throw new Error("Level " + level + " is not prioritized"); if (minIndex === -1) throw new Error(`Level "${level}" for setting "${settingName}" is not prioritized`);
const handlers = SettingsStore.getHandlers(settingName); const handlers = SettingsStore.getHandlers(settingName);

View file

@ -0,0 +1,61 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import BasePlatform from "../../src/BasePlatform";
import { SettingLevel } from "../../src/settings/SettingLevel";
import SettingsStore from "../../src/settings/SettingsStore";
import { mockPlatformPeg } from "../test-utils";
const TEST_DATA = [
{
name: "Electron.showTrayIcon",
level: SettingLevel.PLATFORM,
value: true,
},
];
describe("SettingsStore", () => {
let platformSettings: object;
beforeAll(() => {
jest.clearAllMocks();
platformSettings = {};
mockPlatformPeg({
isLevelSupported: jest.fn().mockReturnValue(true),
supportsSetting: jest.fn().mockReturnValue(true),
setSettingValue: jest.fn().mockImplementation((settingName: string, value: any) => {
platformSettings[settingName] = value;
}),
getSettingValue: jest.fn().mockImplementation((settingName: string) => {
return platformSettings[settingName];
}),
} as unknown as BasePlatform);
TEST_DATA.forEach(d => {
SettingsStore.setValue(d.name, null, d.level, d.value);
});
});
describe("getValueAt", () => {
TEST_DATA.forEach(d => {
it(`should return the value "${d.level}"."${d.name}"`, () => {
expect(SettingsStore.getValueAt(d.level, d.name)).toBe(d.value);
// regression test #22545
expect(SettingsStore.getValueAt(d.level, d.name)).toBe(d.value);
});
});
});
});