From a866a05db7b6aebabbf115a45b67852f5cc1fd66 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sun, 27 Aug 2023 23:59:55 +0900 Subject: [PATCH 1/3] fix: fix error when msg.space is empty --- src/api.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index 70b9369..172307a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -66,14 +66,21 @@ router.get('/api/messages/:hash', async (req, res) => { }); router.post('/', async (req, res) => { + const msg = req.body.data.message; + if (!req.body.data || !req.body.data.message) { return res.status(400).json({ error: 'Invalid format request' }); } + if (!req.body.data.types.Space && !msg.settings && !msg.space) { + return res.status(400).json({ + error: 'Missing space' + }); + } + try { - const msg = req.body.data.message; const msgHash = snapshot.utils.getHash(req.body.data); const address = getAddress(req.body.address); const env = 'livenet'; From 80aa16e5da33751699e223ec47394a8e659121b4 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Mon, 28 Aug 2023 00:17:15 +0900 Subject: [PATCH 2/3] fix: validate message before reading --- src/api.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index 172307a..802031f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -66,14 +66,13 @@ router.get('/api/messages/:hash', async (req, res) => { }); router.post('/', async (req, res) => { - const msg = req.body.data.message; - if (!req.body.data || !req.body.data.message) { return res.status(400).json({ error: 'Invalid format request' }); } + const msg = req.body.data.message; if (!req.body.data.types.Space && !msg.settings && !msg.space) { return res.status(400).json({ error: 'Missing space' From 3da0e18e3a4955eb3d569ebed2355dfa3a36c1fc Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:46:14 +0900 Subject: [PATCH 3/3] chore: refactoring --- src/api.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index f6604aa..6e394ab 100644 --- a/src/api.ts +++ b/src/api.ts @@ -66,13 +66,14 @@ router.get('/api/messages/:hash', async (req, res) => { }); router.post('/', async (req, res) => { - if (!req.body.data || !req.body.data.message) { + const msg = req.body.data?.message; + + if (!msg) { return res.status(400).json({ error: 'Invalid format request' }); } - const msg = req.body.data.message; if (!req.body.data.types.Space && !msg.settings && !msg.space) { return res.status(400).json({ error: 'Missing space'