2022-07-18 16:00:03 +02:00
|
|
|
import assignWithDepth from './assignWithDepth';
|
2021-02-06 15:56:05 +05:30
|
|
|
import { log } from './logger';
|
2020-07-22 16:46:41 -04:00
|
|
|
import theme from './themes';
|
2020-07-29 18:38:59 +02:00
|
|
|
import config from './defaultConfig';
|
2020-07-22 16:46:41 -04:00
|
|
|
|
2020-07-29 18:38:59 +02:00
|
|
|
export const defaultConfig = Object.freeze(config);
|
2020-06-26 09:26:56 -04:00
|
|
|
|
2020-07-29 18:38:59 +02:00
|
|
|
let siteConfig = assignWithDepth({}, defaultConfig);
|
2021-01-27 22:23:33 +01:00
|
|
|
let configFromInitialize;
|
2020-07-29 18:38:59 +02:00
|
|
|
let directives = [];
|
|
|
|
let currentConfig = assignWithDepth({}, defaultConfig);
|
2020-06-26 09:26:56 -04:00
|
|
|
|
2020-07-29 18:38:59 +02:00
|
|
|
export const updateCurrentConfig = (siteCfg, _directives) => {
|
2020-08-16 21:49:36 +02:00
|
|
|
// start with config beeing the siteConfig
|
2020-07-29 18:38:59 +02:00
|
|
|
let cfg = assignWithDepth({}, siteCfg);
|
2020-08-16 21:49:36 +02:00
|
|
|
// let sCfg = assignWithDepth(defaultConfig, siteConfigDelta);
|
2020-06-26 09:26:56 -04:00
|
|
|
|
2020-08-16 21:49:36 +02:00
|
|
|
// Join directives
|
|
|
|
let sumOfDirectives = {};
|
2020-07-29 22:19:23 +02:00
|
|
|
for (let i = 0; i < _directives.length; i++) {
|
|
|
|
const d = _directives[i];
|
2020-07-29 18:38:59 +02:00
|
|
|
sanitize(d);
|
2020-08-16 21:49:36 +02:00
|
|
|
|
2022-06-27 12:34:28 +09:00
|
|
|
// Apply the data from the directive where the the overrides the themeVariables
|
2020-08-16 21:49:36 +02:00
|
|
|
sumOfDirectives = assignWithDepth(sumOfDirectives, d);
|
2020-07-29 22:19:23 +02:00
|
|
|
}
|
2020-08-16 21:49:36 +02:00
|
|
|
|
|
|
|
cfg = assignWithDepth(cfg, sumOfDirectives);
|
|
|
|
|
2022-04-29 17:46:19 -07:00
|
|
|
if (sumOfDirectives.theme && theme[sumOfDirectives.theme]) {
|
2021-01-27 22:23:33 +01:00
|
|
|
const tmpConfigFromInitialize = assignWithDepth({}, configFromInitialize);
|
2020-08-16 21:49:36 +02:00
|
|
|
const themeVariables = assignWithDepth(
|
2021-01-27 22:23:33 +01:00
|
|
|
tmpConfigFromInitialize.themeVariables || {},
|
2020-08-16 21:49:36 +02:00
|
|
|
sumOfDirectives.themeVariables
|
|
|
|
);
|
|
|
|
cfg.themeVariables = theme[cfg.theme].getThemeVariables(themeVariables);
|
2020-06-26 09:26:56 -04:00
|
|
|
}
|
2019-10-11 15:39:50 +02:00
|
|
|
|
2020-07-29 18:38:59 +02:00
|
|
|
currentConfig = cfg;
|
|
|
|
return cfg;
|
|
|
|
};
|
2021-11-06 19:36:06 -07:00
|
|
|
|
2020-06-26 09:26:56 -04:00
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ## setSiteConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | ------------- | ------------------------------------- | ----------- | --------------------------------------- |
|
|
|
|
* | setSiteConfig | Sets the siteConfig to desired values | Put Request | Any Values, except ones in secure array |
|
|
|
|
*
|
|
|
|
* **Notes:** Sets the siteConfig. The siteConfig is a protected configuration for repeat use. Calls
|
|
|
|
* to reset() will reset the currentConfig to siteConfig. Calls to reset(configApi.defaultConfig)
|
2021-11-11 10:31:08 -08:00
|
|
|
* will reset siteConfig and currentConfig to the defaultConfig Note: currentConfig is set in this
|
2022-03-21 21:45:45 +01:00
|
|
|
* function _Default value: At default, will mirror Global Config_
|
2021-11-10 08:41:52 +01:00
|
|
|
*
|
2021-11-11 10:31:08 -08:00
|
|
|
* @param conf - The base currentConfig to use as siteConfig
|
|
|
|
* @returns {object} - The siteConfig
|
2020-06-26 09:26:56 -04:00
|
|
|
*/
|
2021-07-15 11:35:12 +02:00
|
|
|
export const setSiteConfig = (conf) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
siteConfig = assignWithDepth({}, defaultConfig);
|
|
|
|
siteConfig = assignWithDepth(siteConfig, conf);
|
2020-07-30 19:18:18 +02:00
|
|
|
|
2022-04-29 17:46:19 -07:00
|
|
|
if (conf.theme && theme[conf.theme]) {
|
2020-07-30 19:18:18 +02:00
|
|
|
siteConfig.themeVariables = theme[conf.theme].getThemeVariables(conf.themeVariables);
|
|
|
|
}
|
|
|
|
|
2020-07-29 18:38:59 +02:00
|
|
|
currentConfig = updateCurrentConfig(siteConfig, directives);
|
|
|
|
return siteConfig;
|
|
|
|
};
|
2020-08-16 21:49:36 +02:00
|
|
|
|
2022-06-27 12:34:28 +09:00
|
|
|
export const saveConfigFromInitialize = (conf) => {
|
2021-01-27 22:23:33 +01:00
|
|
|
configFromInitialize = assignWithDepth({}, conf);
|
2020-08-16 21:49:36 +02:00
|
|
|
};
|
2021-01-27 22:23:33 +01:00
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
export const updateSiteConfig = (conf) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
siteConfig = assignWithDepth(siteConfig, conf);
|
|
|
|
updateCurrentConfig(siteConfig, directives);
|
|
|
|
|
|
|
|
return siteConfig;
|
2020-06-26 09:26:56 -04:00
|
|
|
};
|
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ## getSiteConfig
|
|
|
|
*
|
2021-11-11 10:31:08 -08:00
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | ------------- | ------------------------------------------------- | ----------- | -------------------------------- |
|
|
|
|
* | setSiteConfig | Returns the current siteConfig base configuration | Get Request | Returns Any Values in siteConfig |
|
2021-11-10 08:41:52 +01:00
|
|
|
*
|
|
|
|
* **Notes**: Returns **any** values in siteConfig.
|
|
|
|
*
|
2021-11-11 10:31:08 -08:00
|
|
|
* @returns {object} - The siteConfig
|
2020-06-26 09:26:56 -04:00
|
|
|
*/
|
|
|
|
export const getSiteConfig = () => {
|
|
|
|
return assignWithDepth({}, siteConfig);
|
|
|
|
};
|
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ## setConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | ------------- | ------------------------------------- | ----------- | --------------------------------------- |
|
|
|
|
* | setSiteConfig | Sets the siteConfig to desired values | Put Request | Any Values, except ones in secure array |
|
|
|
|
*
|
|
|
|
* **Notes**: Sets the currentConfig. The parameter conf is sanitized based on the siteConfig.secure
|
|
|
|
* keys. Any values found in conf with key found in siteConfig.secure will be replaced with the
|
|
|
|
* corresponding siteConfig value.
|
|
|
|
*
|
|
|
|
* @param {any} conf - The potential currentConfig
|
|
|
|
* @returns {any} - The currentConfig merged with the sanitized conf
|
2020-06-26 09:26:56 -04:00
|
|
|
*/
|
2021-07-15 11:35:12 +02:00
|
|
|
export const setConfig = (conf) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
// sanitize(conf);
|
|
|
|
// Object.keys(conf).forEach(key => {
|
|
|
|
// const manipulator = manipulators[key];
|
|
|
|
// conf[key] = manipulator ? manipulator(conf[key]) : conf[key];
|
|
|
|
// });
|
2020-07-16 23:39:38 +02:00
|
|
|
|
2020-06-26 09:26:56 -04:00
|
|
|
assignWithDepth(currentConfig, conf);
|
2020-07-29 18:38:59 +02:00
|
|
|
|
2020-06-26 09:26:56 -04:00
|
|
|
return getConfig();
|
|
|
|
};
|
2020-07-29 18:38:59 +02:00
|
|
|
|
2020-06-26 09:26:56 -04:00
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ## getConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Return Values |
|
|
|
|
* | --------- | ------------------------- | ----------- | ------------------------------ |
|
|
|
|
* | getConfig | Obtains the currentConfig | Get Request | Any Values from current Config |
|
|
|
|
*
|
|
|
|
* **Notes**: Returns **any** the currentConfig
|
|
|
|
*
|
|
|
|
* @returns {any} - The currentConfig
|
2020-06-26 09:26:56 -04:00
|
|
|
*/
|
|
|
|
export const getConfig = () => {
|
|
|
|
return assignWithDepth({}, currentConfig);
|
|
|
|
};
|
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ## sanitize
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | -------- | -------------------------------------- | ----------- | ------ |
|
|
|
|
* | sanitize | Sets the siteConfig to desired values. | Put Request | None |
|
|
|
|
*
|
|
|
|
* Ensures options parameter does not attempt to override siteConfig secure keys **Notes**: modifies
|
|
|
|
* options in-place
|
|
|
|
*
|
|
|
|
* @param {any} options - The potential setConfig parameter
|
2020-06-26 09:26:56 -04:00
|
|
|
*/
|
2021-07-15 11:35:12 +02:00
|
|
|
export const sanitize = (options) => {
|
2021-03-11 20:57:45 +01:00
|
|
|
// Checking that options are not in the list of excluded options
|
2021-07-15 11:35:12 +02:00
|
|
|
Object.keys(siteConfig.secure).forEach((key) => {
|
2020-06-26 09:26:56 -04:00
|
|
|
if (typeof options[siteConfig.secure[key]] !== 'undefined') {
|
|
|
|
// DO NOT attempt to print options[siteConfig.secure[key]] within `${}` as a malicious script
|
|
|
|
// can exploit the logger's attempt to stringify the value and execute arbitrary code
|
2021-02-06 15:56:05 +05:30
|
|
|
log.debug(
|
2020-06-26 09:26:56 -04:00
|
|
|
`Denied attempt to modify a secure key ${siteConfig.secure[key]}`,
|
|
|
|
options[siteConfig.secure[key]]
|
|
|
|
);
|
|
|
|
delete options[siteConfig.secure[key]];
|
|
|
|
}
|
|
|
|
});
|
2021-03-11 20:57:45 +01:00
|
|
|
|
|
|
|
// Check that there no attempts of prototype pollution
|
2021-07-15 11:35:12 +02:00
|
|
|
Object.keys(options).forEach((key) => {
|
2021-03-11 20:57:45 +01:00
|
|
|
if (key.indexOf('__') === 0) {
|
|
|
|
delete options[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Check that there no attempts of xss, there should be no tags at all in the directive
|
|
|
|
// blocking data urls as base64 urls can contain svgs with inline script tags
|
2021-07-15 11:35:12 +02:00
|
|
|
Object.keys(options).forEach((key) => {
|
2021-03-11 20:57:45 +01:00
|
|
|
if (typeof options[key] === 'string') {
|
|
|
|
if (
|
|
|
|
options[key].indexOf('<') > -1 ||
|
|
|
|
options[key].indexOf('>') > -1 ||
|
|
|
|
options[key].indexOf('url(data:') > -1
|
|
|
|
) {
|
|
|
|
delete options[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof options[key] === 'object') {
|
|
|
|
sanitize(options[key]);
|
|
|
|
}
|
|
|
|
});
|
2020-06-26 09:26:56 -04:00
|
|
|
};
|
2020-07-29 18:38:59 +02:00
|
|
|
|
2021-11-06 19:36:06 -07:00
|
|
|
/**
|
|
|
|
* Pushes in a directive to the configuration
|
2021-11-11 10:31:08 -08:00
|
|
|
*
|
|
|
|
* @param {object} directive The directive to push in
|
2021-11-06 19:36:06 -07:00
|
|
|
*/
|
2021-07-15 11:35:12 +02:00
|
|
|
export const addDirective = (directive) => {
|
2020-08-14 07:34:42 +02:00
|
|
|
if (directive.fontFamily) {
|
|
|
|
if (!directive.themeVariables) {
|
|
|
|
directive.themeVariables = { fontFamily: directive.fontFamily };
|
|
|
|
} else {
|
|
|
|
if (!directive.themeVariables.fontFamily) {
|
|
|
|
directive.themeVariables = { fontFamily: directive.fontFamily };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 18:38:59 +02:00
|
|
|
directives.push(directive);
|
|
|
|
updateCurrentConfig(siteConfig, directives);
|
|
|
|
};
|
|
|
|
|
2020-06-26 09:26:56 -04:00
|
|
|
/**
|
2021-11-10 08:41:52 +01:00
|
|
|
* ## reset
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Required | Values |
|
|
|
|
* | -------- | ---------------------------- | ----------- | -------- | ------ |
|
|
|
|
* | reset | Resets currentConfig to conf | Put Request | Required | None |
|
2020-07-14 18:11:36 -07:00
|
|
|
*
|
2021-11-15 09:07:08 +01:00
|
|
|
* ## conf
|
2020-07-14 18:11:36 -07:00
|
|
|
*
|
2022-05-31 23:18:45 +02:00
|
|
|
* | Parameter | Description | Type | Required | Values |
|
|
|
|
* | --------- | -------------------------------------------------------------- | ---------- | -------- | -------------------------------------------- |
|
|
|
|
* | conf | base set of values, which currentConfig could be **reset** to. | Dictionary | Required | Any Values, with respect to the secure Array |
|
2021-11-08 22:06:24 +01:00
|
|
|
*
|
2021-11-10 08:41:52 +01:00
|
|
|
* **Notes**: (default: current siteConfig ) (optional, default `getSiteConfig()`)
|
2020-06-26 09:26:56 -04:00
|
|
|
*/
|
2020-07-29 18:38:59 +02:00
|
|
|
export const reset = () => {
|
|
|
|
// Replace current config with siteConfig
|
|
|
|
directives = [];
|
|
|
|
updateCurrentConfig(siteConfig, directives);
|
2020-06-17 18:14:10 -04:00
|
|
|
};
|