From 28d72fcd089bf443498ee9d2cc22883bcce617f5 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 24 Mar 2022 23:43:07 +0800 Subject: [PATCH] Fix #1409, slug cannot be empty --- server/database.js | 4 +++ .../status-page-socket-handler.js | 28 +++++++++++++++---- src/pages/StatusPage.vue | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/server/database.js b/server/database.js index 7b0b26c38..5cd57c9d8 100644 --- a/server/database.js +++ b/server/database.js @@ -218,6 +218,10 @@ class Database { * @returns {Promise} */ static async migrateNewStatusPage() { + + // Fix 1.13.0 empty slug bug + await R.exec("UPDATE status_page SET slug = 'empty-slug-recover' WHERE TRIM(slug) = ''"); + let title = await setting("title"); if (title) { diff --git a/server/socket-handlers/status-page-socket-handler.js b/server/socket-handlers/status-page-socket-handler.js index 606e84814..55a70d711 100644 --- a/server/socket-handlers/status-page-socket-handler.js +++ b/server/socket-handlers/status-page-socket-handler.js @@ -90,6 +90,8 @@ module.exports.statusPageSocketHandler = (socket) => { socket.on("saveStatusPage", async (slug, config, imgDataUrl, publicGroupList, callback) => { try { + checkSlug(config.slug); + checkLogin(socket); apicache.clear(); @@ -227,11 +229,7 @@ module.exports.statusPageSocketHandler = (socket) => { // lower case only slug = slug.toLowerCase(); - // Check slug a-z, 0-9, - only - // Regex from: https://stackoverflow.com/questions/22454258/js-regex-string-validation-for-slug - if (!slug.match(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/)) { - throw new Error("Invalid Slug"); - } + checkSlug(slug); let statusPage = R.dispense("status_page"); statusPage.slug = slug; @@ -302,3 +300,23 @@ module.exports.statusPageSocketHandler = (socket) => { } }); }; + +/** + * Check slug a-z, 0-9, - only + * Regex from: https://stackoverflow.com/questions/22454258/js-regex-string-validation-for-slug + */ +function checkSlug(slug) { + if (typeof slug !== "string") { + throw new Error("Slug must be string"); + } + + slug = slug.trim(); + + if (!slug) { + throw new Error("Slug cannot be empty"); + } + + if (!slug.match(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/)) { + throw new Error("Invalid Slug"); + } +} diff --git a/src/pages/StatusPage.vue b/src/pages/StatusPage.vue index abd5ca1a1..2695b10d2 100644 --- a/src/pages/StatusPage.vue +++ b/src/pages/StatusPage.vue @@ -518,6 +518,7 @@ export default { save() { let startTime = new Date(); + this.config.slug = this.config.slug.trim().toLowerCase(); this.$root.getSocket().emit("saveStatusPage", this.slug, this.config, this.imgDataUrl, this.$root.publicGroupList, (res) => { if (res.ok) {