Labs / Find the Injectable Function
Find the Injectable Function
You have joined the ChaiCorp shop team. Before the next release, your team lead asks you to review three files of the Node.js API. The app uses Express, the mysql2 library for the database (db.query and db.execute) and execFile from child_process for two admin tools.
Most of the code follows safe patterns. But exactly one function lets a value from the request change the meaning of a SQL query or a shell command. Read every function and find it.
Flag format: SK{functionName}, written exactly as in the code (same capital letters). For example, if the bug were in a function called deleteProduct, the flag would be SK{deleteProduct}.
src/routes/orders.js
// ChaiCorp shop API: order routes (Express + mysql2)
const express = require('express');
const db = require('../db');
const { requireLogin } = require('../middleware/auth');
const router = express.Router();
const ALLOWED_SORT = ['created_at', 'total'];
// GET /api/orders?sort=total
async function listMyOrders(req, res) {
const sortColumn = ALLOWED_SORT.includes(req.query.sort) ? req.query.sort : 'created_at';
const [rows] = await db.execute(
`SELECT id, reference, total, status FROM orders
WHERE customer_id = ? ORDER BY ${sortColumn} DESC`,
[req.user.id]
);
res.json(rows);
}
// GET /api/orders/:id
async function getOrder(req, res) {
const [rows] = await db.execute(
'SELECT id, reference, total, status FROM orders WHERE id = ? AND customer_id = ?',
[req.params.id, req.user.id]
);
if (rows.length === 0) return res.status(404).json({ error: 'Not found' });
res.json(rows[0]);
}
// GET /api/orders/search?ref=CC-2026
async function searchOrders(req, res) {
const ref = String(req.query.ref || '');
const [rows] = await db.query(
"SELECT id, reference, total, status FROM orders " +
"WHERE customer_id = ? AND reference LIKE '%" + ref + "%'",
[req.user.id]
);
res.json(rows);
}
router.get('/', requireLogin, listMyOrders);
router.get('/search', requireLogin, searchOrders);
router.get('/:id', requireLogin, getOrder);
module.exports = router;