Skip to content

Commit

Permalink
Disable dashboard resources in safe mode (#18382)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Oct 24, 2023
1 parent 8350d71 commit eb35eb3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cast/src/receiver/layout/hc-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/fake_data/demo_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
15 changes: 12 additions & 3 deletions src/panels/lovelace/common/load-resources.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
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 = {};
const JS_CACHE = {};

export const loadLovelaceResources = (
resources: NonNullable<LovelaceResource[]>,
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) {
Expand All @@ -35,3 +43,4 @@ export const loadLovelaceResources = (
console.warn(`Unknown resource type specified: ${resource.type}`);
}
});
};
7 changes: 3 additions & 4 deletions src/panels/lovelace/ha-panel-lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit eb35eb3

Please sign in to comment.