From c2dd24f1b3db96b45b37e725d22b33a716e7a983 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Fri, 6 Mar 2026 17:05:29 +0000 Subject: [PATCH] auto(agent): Improved error handling and input validation in addresses route --- backend/src/routes/addresses.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/addresses.js b/backend/src/routes/addresses.js index 3cf7fac..33927f9 100644 --- a/backend/src/routes/addresses.js +++ b/backend/src/routes/addresses.js @@ -10,9 +10,24 @@ const router = Router(); const hashCode = (code) => createHash('sha256').update(code).digest('hex'); router.post('/change-request', requireAuth, async (req, res) => { - const parsed = z.object({ newAddress: z.string().min(10) }).safeParse(req.body); + const parsed = z.object({ newAddress: z.string().min(10).max(500) }).safeParse(req.body); if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() }); + // Check if user already has an address + try { + const [existingRows] = await pool.query( + `SELECT id FROM addresses WHERE user_id = ? LIMIT 1`, + [req.user.userId] + ); + + if (existingRows.length === 0) { + return res.status(400).json({ error: 'User must have an existing address to request a change' }); + } + } catch (err) { + console.error('Error checking existing address:', err); + return res.status(500).json({ error: 'Internal server error' }); + } + const verificationCode = String(randomInt(100000, 999999)); const verificationCodeHash = hashCode(verificationCode);