auto(agent): Improved error handling and input validation in addresses route

This commit is contained in:
OpenClaw 2026-03-06 17:05:29 +00:00
parent ed38467091
commit c2dd24f1b3

View file

@ -10,9 +10,24 @@ const router = Router();
const hashCode = (code) => createHash('sha256').update(code).digest('hex'); const hashCode = (code) => createHash('sha256').update(code).digest('hex');
router.post('/change-request', requireAuth, async (req, res) => { 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() }); 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 verificationCode = String(randomInt(100000, 999999));
const verificationCodeHash = hashCode(verificationCode); const verificationCodeHash = hashCode(verificationCode);