Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash possibility if module: <name> is not defined and on mistake position: <position> #3445

Merged
merged 14 commits into from
Jun 24, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _This release is scheduled to be released on 2024-07-01._

### Fixed

- Fix crash possibility if `module: <name>` is not defined and on `postion: <positon>` misktake.
- [weather] Fixed precipitationProbability in forecast for provider openmeteo (#3446)
- [weather] Fixed type=daily for provider openmeteo having no data when running after 23:00 (#3449)
- [weather] Fixed type=daily for provider openmeteo showing nightly icons in forecast when current time is "nightly" (#3458)
Expand Down
11 changes: 9 additions & 2 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,15 @@ function App () {

let modules = [];
for (const module of config.modules) {
if (!modules.includes(module.module) && !module.disabled) {
modules.push(module.module);
if (module.disabled) continue;
if (module.module) {
if (Utils.moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
modules.push(module.module);
} else {
Log.warn("Invalid module position found for this configuration:", module);
}
} else {
Log.warn("No module name found for this configuration:", module);
}
}

Expand Down
66 changes: 66 additions & 0 deletions js/check_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const { Linter } = require("eslint");

const linter = new Linter();

const Ajv = require("ajv");

const ajv = new Ajv();

const rootPath = path.resolve(`${__dirname}/../`);
const Log = require(`${rootPath}/js/logger.js`);

Expand Down Expand Up @@ -59,6 +63,68 @@ function checkConfigFile () {
for (const error of errors) {
Log.error(`Line ${error.line} column ${error.column}: ${error.message}`);
}
return;
}

Log.info("Checking modules structure configuration... ");

// Make Ajv schema confguration of modules config
// only scan "module" and "position"
const schema = {
type: "object",
properties: {
modules: {
type: "array",
items: {
type: "object",
properties: {
module: {
type: "string"
},
position: {
type: "string",
enum: [
"top_bar",
"top_left",
"top_center",
"top_right",
"upper_third",
"middle_center",
"lower_third",
"bottom_left",
"bottom_center",
"bottom_right",
"bottom_bar",
"fullscreen_above",
"fullscreen_below"
]
}
},
required: ["module"]
}
}
}
};

// scan all modules
const validate = ajv.compile(schema);
const data = require(configFileName);

const valid = validate(data);
if (!valid) {
let module = validate.errors[0].instancePath.split("/")[2];
let position = validate.errors[0].instancePath.split("/")[3];

Log.error(colors.red("This module configuration contains errors:"));
Log.error(data.modules[module]);
if (position) {
Log.error(colors.red(`${position}: ${validate.errors[0].message}`));
Log.error(validate.errors[0].params.allowedValues);
} else {
Log.error(colors.red(validate.errors[0].message));
}
} else {
Log.info(colors.green("Your modules structure configuration doesn't contain errors :)"));
}
}

Expand Down
3 changes: 2 additions & 1 deletion js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const Loader = (function () {
* @returns {object[]} module data as configured in config
*/
const getAllModules = function () {
return config.modules;
const AllModules = config.modules.filter((module) => (module.module !== undefined) && (MM.getAvailableModulePositions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
return AllModules;
};

/**
Expand Down
11 changes: 7 additions & 4 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ const MM = (function () {
* an ugly top margin. By using this function, the top bar will be hidden if the
* update notification is not visible.
*/
const updateWrapperStates = function () {
const positions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"];
const modulePositions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"];

positions.forEach(function (position) {
const updateWrapperStates = function () {
modulePositions.forEach(function (position) {
const wrapper = selectWrapper(position);
const moduleWrappers = wrapper.getElementsByClassName("module");

Expand Down Expand Up @@ -701,7 +701,10 @@ const MM = (function () {
showModule (module, speed, callback, options) {
// do not change module.hidden yet, only if we really show it later
showModule(module, speed, callback, options);
}
},

// return all available module postions.
getAvailableModulePositions: modulePositions
};
}());

Expand Down
11 changes: 11 additions & 0 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,16 @@ module.exports = {
} catch (e) {
Log.error(e);
}
},

// return all available module positions
getAvailableModulePositions () {
return ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"];
},

// return if postion is on modulePositions Array (true/false)
moduleHasValidPosition (position) {
if (this.getAvailableModulePositions().indexOf(position) === -1) return false;
return true;
}
};
87 changes: 73 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"*.css": "stylelint --fix"
},
"dependencies": {
"ajv": "^8.16.0",
"ansis": "^3.2.0",
"console-stamp": "^3.1.2",
"envsub": "^4.1.0",
Expand Down