Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions routes/deliveryEstimate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/

import * as models from '../models/index'
import { type Request, type Response, type NextFunction } from 'express'
import path from 'path'
import fs from 'fs'

const NOTES_ROOT = path.resolve('ftp/notes')

// Delivery estimate for a given region, plus the courier's published notes.
module.exports = function deliveryEstimate () {
return (req: Request, res: Response, next: NextFunction) => {
const region = String(req.query.region ?? '')
models.sequelize.query(
'SELECT etaDays FROM Deliveries WHERE region = :region',
{ replacements: { region }, type: models.sequelize.QueryTypes.SELECT }
).then((rows: any) => {
const note = path.basename(String(req.query.note ?? 'default.txt'))
const resolved = path.join(NOTES_ROOT, note)
if (!resolved.startsWith(NOTES_ROOT + path.sep)) {
res.status(400).send('Invalid note.')
return
}
fs.readFile(resolved, 'utf8', (error, text) => {
if (error != null) {
next(error)
return
}
res.json({ region, rows, note: text.slice(0, 200) })
})
}).catch((error: Error) => {
next(error)
})
}
}