Labs / Review the Pull Request
CodeMedium

Review the Pull Request

Farhan, a junior developer at ChaiCorp, has opened pull request #142. It adds a login route, a receipt download and a payment configuration file. You are the code reviewer.

Read the diff (lines starting with + are new) and the npm audit output. Use the code review checklist from the lesson. For each problem, choose the change you would ask Farhan to make.

Answer every question correctly to get the flag.

pull-request-142.diff

Pull request #142: "Add login, order receipts and payments"
Author: farhan (junior developer)   Target branch: main
The branch has already been pushed to the shared company repository.

--- a/src/routes/auth.js
+++ b/src/routes/auth.js
+async function login(req, res) {
+  const { email, password } = req.body;
+  logger.info('login attempt', req.body);
+  const [rows] = await db.query(
+    "SELECT id, password_hash FROM customers WHERE email = '" + email + "'"
+  );
+  const user = rows[0];
+  const ok = user && (await bcrypt.compare(password, user.password_hash));
+  if (!ok) return res.status(401).json({ error: 'Invalid email or password' });
+
+  const sessionId = crypto.randomBytes(32).toString('hex');
+  await sessions.create(sessionId, user.id);
+  res.cookie('sid', sessionId, { maxAge: 30 * 60 * 1000 });
+  res.json({ ok: true });
+}

--- a/src/routes/orders.js
+++ b/src/routes/orders.js
+// GET /api/orders/:id/receipt
+// The "Download receipt" button is only shown on the user's own orders.
+async function downloadReceipt(req, res) {
+  const [rows] = await db.execute(
+    'SELECT * FROM orders WHERE id = ?',
+    [req.params.id]
+  );
+  if (rows.length === 0) return res.status(404).end();
+  res.type('pdf').send(await renderReceipt(rows[0]));
+}
+router.get('/:id/receipt', requireLogin, downloadReceipt);

--- a/src/config.js
+++ b/src/config.js
+const PAYMENT_API_KEY = 'pay_7f3K9qLx2Rv8TmZ4wE1nB6yH';
+module.exports = { PAYMENT_API_KEY };

--- a/package.json
+++ b/package.json
+    "receipt-pdf-kit": "2.1.0",
1. In login(), the email is joined into the SQL text. What change do you request?
2. The session cookie is set with only maxAge. What should the cookie options also include?
3. login() writes req.body to the log on every attempt. What do you ask for?
4. The "Download receipt" button is only shown on the user's own orders. Is downloadReceipt safe?
5. config.js contains a payment API key, and the branch is already in the shared repository. What do you ask for?
6. npm audit reports a high severity issue in receipt-pdf-kit 2.1.0, fixed in 2.3.4. What is the right decision?

Submit the flag