From eb35eb3de542037e2b91ebff2dac065d4a942d5a Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 24 Oct 2023 20:15:11 +0200 Subject: [PATCH] Disable dashboard resources in safe mode (#18382) --- cast/src/receiver/layout/hc-main.ts | 2 +- package.json | 2 +- src/fake_data/demo_config.ts | 1 + .../resources/ha-config-lovelace-resources.ts | 4 ++-- src/panels/lovelace/common/load-resources.ts | 15 ++++++++++++--- src/panels/lovelace/ha-panel-lovelace.ts | 7 +++---- yarn.lock | 10 +++++----- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cast/src/receiver/layout/hc-main.ts b/cast/src/receiver/layout/hc-main.ts index bb84bbb39cf1..314e290df2a9 100644 --- a/cast/src/receiver/layout/hc-main.ts +++ b/cast/src/receiver/layout/hc-main.ts @@ -309,7 +309,7 @@ export class HcMain extends HassElement { ? await fetchResources(this.hass!.connection) : (this._lovelaceConfig as LegacyLovelaceConfig).resources; if (resources) { - loadLovelaceResources(resources, this.hass!.auth.data.hassUrl); + loadLovelaceResources(resources, this.hass!); } } diff --git a/package.json b/package.json index 8235d15295af..cc91677a8f2f 100644 --- a/package.json +++ b/package.json @@ -114,7 +114,7 @@ "fuse.js": "6.6.2", "google-timezones-json": "1.2.0", "hls.js": "1.4.12", - "home-assistant-js-websocket": "9.0.0", + "home-assistant-js-websocket": "9.1.0", "idb-keyval": "6.2.1", "intl-messageformat": "10.5.4", "js-yaml": "4.1.0", diff --git a/src/fake_data/demo_config.ts b/src/fake_data/demo_config.ts index 01f1a3317b92..caaf1bb55806 100644 --- a/src/fake_data/demo_config.ts +++ b/src/fake_data/demo_config.ts @@ -22,6 +22,7 @@ export const demoConfig: HassConfig = { allowlist_external_urls: [], config_source: "storage", recovery_mode: false, + safe_mode: false, state: STATE_RUNNING, internal_url: "http://homeassistant.local:8123", external_url: null, diff --git a/src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts b/src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts index aed6cd896761..a1733e094040 100644 --- a/src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts +++ b/src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts @@ -144,14 +144,14 @@ export class HaConfigLovelaceRescources extends LitElement { this._resources = this._resources!.concat(created).sort((res1, res2) => stringCompare(res1.url, res2.url, this.hass!.locale.language) ); - loadLovelaceResources([created], this.hass!.auth.data.hassUrl); + loadLovelaceResources([created], this.hass!); }, updateResource: async (values) => { const updated = await updateResource(this.hass!, resource!.id, values); this._resources = this._resources!.map((res) => res === resource ? updated : res ); - loadLovelaceResources([updated], this.hass!.auth.data.hassUrl); + loadLovelaceResources([updated], this.hass!); }, removeResource: async () => { if ( diff --git a/src/panels/lovelace/common/load-resources.ts b/src/panels/lovelace/common/load-resources.ts index 5b9fb6d3166d..27e1d8a416ea 100644 --- a/src/panels/lovelace/common/load-resources.ts +++ b/src/panels/lovelace/common/load-resources.ts @@ -1,5 +1,6 @@ import { loadCSS, loadJS, loadModule } from "../../../common/dom/load_resource"; import { LovelaceResource } from "../../../data/lovelace"; +import type { HomeAssistant } from "../../../types"; // CSS and JS should only be imported once. Modules and HTML are safe. const CSS_CACHE = {}; @@ -7,10 +8,17 @@ const JS_CACHE = {}; export const loadLovelaceResources = ( resources: NonNullable, - hassUrl: string -) => + hass: HomeAssistant +) => { + // Don't load ressources on safe mode + if (hass.config.safe_mode) { + return; + } resources.forEach((resource) => { - const normalizedUrl = new URL(resource.url, hassUrl).toString(); + const normalizedUrl = new URL( + resource.url, + hass.auth.data.hassUrl + ).toString(); switch (resource.type) { case "css": if (normalizedUrl in CSS_CACHE) { @@ -35,3 +43,4 @@ export const loadLovelaceResources = ( console.warn(`Unknown resource type specified: ${resource.type}`); } }); +}; diff --git a/src/panels/lovelace/ha-panel-lovelace.ts b/src/panels/lovelace/ha-panel-lovelace.ts index b011049ea14c..9fd8da1d9db8 100644 --- a/src/panels/lovelace/ha-panel-lovelace.ts +++ b/src/panels/lovelace/ha-panel-lovelace.ts @@ -229,10 +229,9 @@ export class LovelacePanel extends LitElement { } if (!resourcesLoaded) { resourcesLoaded = true; - (llWindow.llResProm || fetchResources(this.hass!.connection)).then( - (resources) => - loadLovelaceResources(resources, this.hass!.auth.data.hassUrl) - ); + const resources = await (llWindow.llResProm || + fetchResources(this.hass!.connection)); + loadLovelaceResources(resources, this.hass!); } if (this.urlPath !== null || !confProm) { diff --git a/yarn.lock b/yarn.lock index 0745de0694f8..66ca51d66a6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9789,7 +9789,7 @@ __metadata: gulp-rename: 2.0.0 gulp-zopfli-green: 6.0.1 hls.js: 1.4.12 - home-assistant-js-websocket: 9.0.0 + home-assistant-js-websocket: 9.1.0 html-minifier-terser: 7.2.0 husky: 8.0.3 idb-keyval: 6.2.1 @@ -9865,10 +9865,10 @@ __metadata: languageName: unknown linkType: soft -"home-assistant-js-websocket@npm:9.0.0": - version: 9.0.0 - resolution: "home-assistant-js-websocket@npm:9.0.0" - checksum: c1543483f55c1d135400b9bd6dce084b5942f57dc8098f4c84c6ece7b8289c558dc6578c45c94eecde99fec134041d8c4f458492e2573875bfb706df70ffd902 +"home-assistant-js-websocket@npm:9.1.0": + version: 9.1.0 + resolution: "home-assistant-js-websocket@npm:9.1.0" + checksum: 4692b856b1e89c05ec772e7e3590d6b0cd0f6ef711eac9fa888d62c21fe4f9c669ab7ae8b7b5c52af58e4821c7cecb4167ec8a617426bff17b8095fd44a9c8e4 languageName: node linkType: hard