juice-shop
42 строки · 2.3 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import fs = require('fs')
7import { type Request, type Response, type NextFunction } from 'express'
8import logger from '../lib/logger'
9
10import { UserModel } from '../models/user'
11import * as utils from '../lib/utils'
12const security = require('../lib/insecurity')
13const request = require('request')
14
15module.exports = function profileImageUrlUpload () {
16return (req: Request, res: Response, next: NextFunction) => {
17if (req.body.imageUrl !== undefined) {
18const url = req.body.imageUrl
19if (url.match(/(.)*solve\/challenges\/server-side(.)*/) !== null) req.app.locals.abused_ssrf_bug = true
20const loggedInUser = security.authenticatedUsers.get(req.cookies.token)
21if (loggedInUser) {
22const imageRequest = request
23.get(url)
24.on('error', function (err: unknown) {
25UserModel.findByPk(loggedInUser.data.id).then(async (user: UserModel | null) => { return await user?.update({ profileImage: url }) }).catch((error: Error) => { next(error) })
26logger.warn(`Error retrieving user profile image: ${utils.getErrorMessage(err)}; using image link directly`)
27})
28.on('response', function (res: Response) {
29if (res.statusCode === 200) {
30const ext = ['jpg', 'jpeg', 'png', 'svg', 'gif'].includes(url.split('.').slice(-1)[0].toLowerCase()) ? url.split('.').slice(-1)[0].toLowerCase() : 'jpg'
31imageRequest.pipe(fs.createWriteStream(`frontend/dist/frontend/assets/public/images/uploads/${loggedInUser.data.id}.${ext}`))
32UserModel.findByPk(loggedInUser.data.id).then(async (user: UserModel | null) => { return await user?.update({ profileImage: `/assets/public/images/uploads/${loggedInUser.data.id}.${ext}` }) }).catch((error: Error) => { next(error) })
33} else UserModel.findByPk(loggedInUser.data.id).then(async (user: UserModel | null) => { return await user?.update({ profileImage: url }) }).catch((error: Error) => { next(error) })
34})
35} else {
36next(new Error('Blocked illegal activity by ' + req.socket.remoteAddress))
37}
38}
39res.location(process.env.BASE_PATH + '/profile')
40res.redirect(process.env.BASE_PATH + '/profile')
41}
42}
43