Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 6e786d6

Browse files
weeman1337t3chguy
authored andcommitted
Prevent level order to be modified (#8821)
(cherry picked from commit b87c537)
1 parent d6c162a commit 6e786d6

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/settings/SettingsStore.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export const LEVEL_ORDER = [
8080
function getLevelOrder(setting: ISetting): SettingLevel[] {
8181
// Settings which support only a single setting level are inherently ordered
8282
if (setting.supportedLevelsAreOrdered || setting.supportedLevels.length === 1) {
83-
return setting.supportedLevels;
83+
// return a copy to prevent callers from modifying the array
84+
return [...setting.supportedLevels];
8485
}
8586
return LEVEL_ORDER;
8687
}
@@ -359,7 +360,7 @@ export default class SettingsStore {
359360
if (!levelOrder.includes(SettingLevel.DEFAULT)) levelOrder.push(SettingLevel.DEFAULT); // always include default
360361

361362
const minIndex = levelOrder.indexOf(level);
362-
if (minIndex === -1) throw new Error("Level " + level + " is not prioritized");
363+
if (minIndex === -1) throw new Error(`Level "${level}" for setting "${settingName}" is not prioritized`);
363364

364365
const handlers = SettingsStore.getHandlers(settingName);
365366

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import BasePlatform from "../../src/BasePlatform";
18+
import { SettingLevel } from "../../src/settings/SettingLevel";
19+
import SettingsStore from "../../src/settings/SettingsStore";
20+
import { mockPlatformPeg } from "../test-utils";
21+
22+
const TEST_DATA = [
23+
{
24+
name: "Electron.showTrayIcon",
25+
level: SettingLevel.PLATFORM,
26+
value: true,
27+
},
28+
];
29+
30+
describe("SettingsStore", () => {
31+
let platformSettings: object;
32+
33+
beforeAll(() => {
34+
jest.clearAllMocks();
35+
platformSettings = {};
36+
mockPlatformPeg({
37+
isLevelSupported: jest.fn().mockReturnValue(true),
38+
supportsSetting: jest.fn().mockReturnValue(true),
39+
setSettingValue: jest.fn().mockImplementation((settingName: string, value: any) => {
40+
platformSettings[settingName] = value;
41+
}),
42+
getSettingValue: jest.fn().mockImplementation((settingName: string) => {
43+
return platformSettings[settingName];
44+
}),
45+
} as unknown as BasePlatform);
46+
47+
TEST_DATA.forEach(d => {
48+
SettingsStore.setValue(d.name, null, d.level, d.value);
49+
});
50+
});
51+
52+
describe("getValueAt", () => {
53+
TEST_DATA.forEach(d => {
54+
it(`should return the value "${d.level}"."${d.name}"`, () => {
55+
expect(SettingsStore.getValueAt(d.level, d.name)).toBe(d.value);
56+
// regression test #22545
57+
expect(SettingsStore.getValueAt(d.level, d.name)).toBe(d.value);
58+
});
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)