Fix #1409, slug cannot be empty

This commit is contained in:
Louis Lam 2022-03-24 23:43:07 +08:00
parent 6c7a0ff7d3
commit 28d72fcd08
3 changed files with 28 additions and 5 deletions

View file

@ -218,6 +218,10 @@ class Database {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
static async migrateNewStatusPage() { 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"); let title = await setting("title");
if (title) { if (title) {

View file

@ -90,6 +90,8 @@ module.exports.statusPageSocketHandler = (socket) => {
socket.on("saveStatusPage", async (slug, config, imgDataUrl, publicGroupList, callback) => { socket.on("saveStatusPage", async (slug, config, imgDataUrl, publicGroupList, callback) => {
try { try {
checkSlug(config.slug);
checkLogin(socket); checkLogin(socket);
apicache.clear(); apicache.clear();
@ -227,11 +229,7 @@ module.exports.statusPageSocketHandler = (socket) => {
// lower case only // lower case only
slug = slug.toLowerCase(); slug = slug.toLowerCase();
// Check slug a-z, 0-9, - only checkSlug(slug);
// 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");
}
let statusPage = R.dispense("status_page"); let statusPage = R.dispense("status_page");
statusPage.slug = slug; 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");
}
}

View file

@ -518,6 +518,7 @@ export default {
save() { save() {
let startTime = new Date(); 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) => { this.$root.getSocket().emit("saveStatusPage", this.slug, this.config, this.imgDataUrl, this.$root.publicGroupList, (res) => {
if (res.ok) { if (res.ok) {