1
0
mirror of https://github.com/home-assistant/frontend.git synced 2026-04-17 15:45:43 +01:00

Escape device names in energy dashboard (#27425)

This commit is contained in:
Petar Petrov
2025-10-10 11:25:21 +03:00
committed by GitHub
parent 5cfc34b020
commit 1b6c33efd4
5 changed files with 19 additions and 7 deletions

8
src/common/util/xss.ts Normal file
View File

@@ -0,0 +1,8 @@
import xss from "xss";
export const filterXSS = (html: string) =>
xss(html, {
whiteList: {},
stripIgnoreTag: true,
stripIgnoreTagBody: true,
});

View File

@@ -27,6 +27,7 @@ import type { HomeAssistant } from "../../types";
import { isMac } from "../../util/is_mac";
import "../chips/ha-assist-chip";
import "../ha-icon-button";
import { filterXSS } from "../../common/util/xss";
import { formatTimeLabel } from "./axis-label";
import { downSampleLineData } from "./down-sample";
@@ -811,7 +812,8 @@ export class HaChartBase extends LitElement {
};
}
}
return { ...s, data };
const name = filterXSS(String(s.name ?? s.id ?? ""));
return { ...s, name, data };
});
return series as ECOption["series"];
}

View File

@@ -9,6 +9,7 @@ import { ResizeController } from "@lit-labs/observers/resize-controller";
import type { HomeAssistant } from "../../types";
import type { ECOption } from "../../resources/echarts";
import { measureTextWidth } from "../../util/text";
import { filterXSS } from "../../common/util/xss";
import "./ha-chart-base";
import { NODE_SIZE } from "../trace/hat-graph-const";
import "../ha-alert";
@@ -92,12 +93,12 @@ export class HaSankeyChart extends LitElement {
: data.value;
if (data.id) {
const node = this.data.nodes.find((n) => n.id === data.id);
return `${params.marker} ${node?.label ?? data.id}<br>${value}`;
return `${params.marker} ${filterXSS(node?.label ?? data.id)}<br>${value}`;
}
if (data.source && data.target) {
const source = this.data.nodes.find((n) => n.id === data.source);
const target = this.data.nodes.find((n) => n.id === data.target);
return `${source?.label ?? data.source}${target?.label ?? data.target}<br>${value}`;
return `${filterXSS(source?.label ?? data.source)}${filterXSS(target?.label ?? data.target)}<br>${value}`;
}
return null;
};

View File

@@ -27,6 +27,7 @@ import {
} from "../../../../../common/datetime/format_date";
import { formatTime } from "../../../../../common/datetime/format_time";
import type { ECOption } from "../../../../../resources/echarts";
import { filterXSS } from "../../../../../common/util/xss";
export function getSuggestedMax(dayDifference: number, end: Date): number {
let suggestedMax = new Date(end);
@@ -201,7 +202,7 @@ function formatTooltip(
countNegative++;
}
}
return `${param.marker} ${param.seriesName}: ${value} ${unit}`;
return `${param.marker} ${filterXSS(param.seriesName!)}: ${value} ${unit}`;
})
.filter(Boolean);
let footer = "";

View File

@@ -6,6 +6,7 @@ import { classMap } from "lit/directives/class-map";
import memoizeOne from "memoize-one";
import type { BarSeriesOption } from "echarts/charts";
import type { ECElementEvent } from "echarts/types/dist/shared";
import { filterXSS } from "../../../../common/util/xss";
import { getGraphColorByIndex } from "../../../../common/color/colors";
import { formatNumber } from "../../../../common/number/format_number";
import "../../../../components/chart/ha-chart-base";
@@ -96,9 +97,8 @@ export class HuiEnergyDevicesGraphCard
}
private _renderTooltip(params: any) {
const title = `<h4 style="text-align: center; margin: 0;">${this._getDeviceName(
params.value[1]
)}</h4>`;
const deviceName = filterXSS(this._getDeviceName(params.value[1]));
const title = `<h4 style="text-align: center; margin: 0;">${deviceName}</h4>`;
const value = `${formatNumber(
params.value[0] as number,
this.hass.locale,