Don't allow view URL to be a number (#26313)

This commit is contained in:
karwosts 2025-07-29 12:33:58 -07:00 committed by GitHub
parent 073ba22233
commit 641e406502
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View File

@ -29,6 +29,7 @@ declare global {
}
const VALID_PATH_REGEX = /^[a-zA-Z0-9_-]+$/;
const INTEGER_REGEX = /^[0-9]+$/;
@customElement("hui-view-editor")
export class HuiViewEditor extends LitElement {
@ -165,14 +166,21 @@ export class HuiViewEditor extends LitElement {
delete config.top_margin;
}
const slugifyTitle = (title: string | undefined) => {
const slug = slugify(title || "", "-");
if (INTEGER_REGEX.test(slug)) {
return `view-${slug}`;
}
return slug;
};
if (
this.isNew &&
!this._suggestedPath &&
this._config.path === config.path &&
(!this._config.path ||
config.path === slugify(this._config.title || "", "-"))
(!this._config.path || config.path === slugifyTitle(this._config.title))
) {
config.path = slugify(config.title || "", "-");
config.path = slugifyTitle(config.title);
}
let valid = true;
@ -180,6 +188,9 @@ export class HuiViewEditor extends LitElement {
if (config.path && !VALID_PATH_REGEX.test(config.path)) {
valid = false;
this._error = { path: "error_invalid_path" };
} else if (config.path && INTEGER_REGEX.test(config.path)) {
valid = false;
this._error = { path: "error_number" };
}
fireEvent(this, "view-config-changed", { valid, config });

View File

@ -7035,6 +7035,7 @@
"saving_failed": "Saving failed",
"error_same_url": "You cannot save a view with the same URL as a different existing view.",
"error_invalid_path": "URL contains invalid/reserved characters. Please enter a simple string only for the path of this view.",
"error_number": "URL may not be a number.",
"move_to_dashboard": "Move to dashboard"
},
"edit_view_header": {