Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
secured runtime config to only allow select config values to be set
Ronald A. Richardson6697b791858e
diff --git a/console/app/app.js b/console/app/app.jsindex f55f3c21..1665251d 100644--- a/console/app/app.js+++ b/console/app/app.js@@ -4,25 +4,7 @@ import loadInitializers from 'ember-load-initializers';import config from '@fleetbase/console/config/environment';import loadExtensions from '@fleetbase/ember-core/utils/load-extensions';import mapEngines from '@fleetbase/ember-core/utils/map-engines';-import { set } from '@ember/object';-import { debug } from '@ember/debug';--async function loadRuntimeConfig() {- try {- const response = await fetch(`/fleetbase.config.json?_t=${Date.now()}`, { cache: 'no-cache' });- if (!response.ok) {- debug('No fleetbase.config.json found, using built-in config defaults');- return;- }-- const runtimeConfig = await response.json();- for (const [path, value] of Object.entries(runtimeConfig)) {- set(config, path, value);- }- } catch (e) {- debug(`Failed to load runtime config : ${e.message}`);- }-}+import loadRuntimeConfig from '@fleetbase/console/utils/runtime-config';export default class App extends Application {modulePrefix = config.modulePrefix;@@ -46,4 +28,4 @@ document.addEventListener('DOMContentLoaded', async () => {let fleetbase = App.create();fleetbase.deferReadiness();fleetbase.boot();-});\ No newline at end of file+});diff --git a/console/app/utils/runtime-config.js b/console/app/utils/runtime-config.jsnew file mode 100644index 00000000..d37207ec--- /dev/null+++ b/console/app/utils/runtime-config.js@@ -0,0 +1,80 @@+import config from '@fleetbase/console/config/environment';+import toBoolean from '@fleetbase/ember-core/utils/to-boolean';+import { set } from '@ember/object';+import { debug } from '@ember/debug';++/**+ * Maps allowed runtime keys to internal config paths.+ */+const RUNTIME_CONFIG_MAP = {+ API_HOST: 'API.host',+ API_NAMESPACE: 'API.namespace',+ SOCKETCLUSTER_PATH: 'socket.path',+ SOCKETCLUSTER_HOST: 'socket.hostname',+ SOCKETCLUSTER_SECURE: 'socket.secure',+ SOCKETCLUSTER_PORT: 'socket.port',+ OSRM_HOST: 'osrm.host',+ EXTENSIONS: 'APP.extensions',+};++/**+ * Coerce and sanitize runtime config values based on key.+ *+ * @param {String} key+ * @param {*} value+ * @return {*}+ */+function coerceValue(key, value) {+ switch (key) {+ case 'SOCKETCLUSTER_PORT':+ return parseInt(value, 10);++ case 'SOCKETCLUSTER_SECURE':+ return toBoolean(value);++ case 'EXTENSIONS':+ return typeof value === 'string' ? value.split(',') : [value];++ default:+ return value;+ }+}++/**+ * Apply runtime config overrides based on strict allowlist mapping.+ *+ * @param {Object} rawConfig+ */+export function applyRuntimeConfig(rawConfig = {}) {+ Object.entries(rawConfig).forEach(([key, value]) => {+ const configPath = RUNTIME_CONFIG_MAP[key];++ if (configPath) {+ const coercedValue = coerceValue(key, value);+ set(config, configPath, coercedValue);+ } else {+ debug(`[runtime-config] Ignored unknown key: ${key}`);+ }+ });+}++/**+ * Load and apply runtime config.+ *+ * @export+ * @return {void}+ */+export default async function loadRuntimeConfig() {+ try {+ const response = await fetch(`/fleetbase.config.json?_t=${Date.now()}`, { cache: 'no-cache' });+ if (!response.ok) {+ debug('No fleetbase.config.json found, using built-in config defaults');+ return;+ }++ const runtimeConfig = await response.json();+ applyRuntimeConfig(runtimeConfig);+ } catch (e) {+ debug(`Failed to load runtime config : ${e.message}`);+ }+}diff --git a/console/tests/unit/utils/runtime-config-test.js b/console/tests/unit/utils/runtime-config-test.jsnew file mode 100644index 00000000..781f7fed--- /dev/null+++ b/console/tests/unit/utils/runtime-config-test.js@@ -0,0 +1,10 @@+import runtimeConfig from '@fleetbase/console/utils/runtime-config';+import { module, test } from 'qunit';++module('Unit | Utility | runtime-config', function () {+ // TODO: Replace this with your real tests.+ test('it works', function (assert) {+ let result = runtimeConfig();+ assert.ok(result);+ });+});