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 @@ -19,6 +19,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)

## [2.27.0] - 2024-04-01
Expand Down
13 changes: 11 additions & 2 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,19 @@ function App () {

Log.setLogLevel(config.logLevel);

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"];

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 (positions.indexOf(module.position) > -1 || 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
4 changes: 3 additions & 1 deletion js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Loader = (function () {
const loadedModuleFiles = [];
const loadedFiles = [];
const moduleObjects = [];
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"];
bugsounet marked this conversation as resolved.
Show resolved Hide resolved

/* Private Methods */

Expand Down Expand Up @@ -50,7 +51,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) && (positions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
bugsounet marked this conversation as resolved.
Show resolved Hide resolved
return AllModules;
};

/**
Expand Down
84 changes: 51 additions & 33 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.13.0",
"ansis": "^3.2.0",
"console-stamp": "^3.1.2",
"envsub": "^4.1.0",
Expand Down