-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Expand file tree
/
Copy pathprofileImageUrlUpload.ts
More file actions
51 lines (48 loc) · 2.22 KB
/
Copy pathprofileImageUrlUpload.ts
File metadata and controls
51 lines (48 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) 2014-2026 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import fs from 'node:fs'
import { Readable } from 'node:stream'
import { finished } from 'node:stream/promises'
import { type Request, type Response, type NextFunction } from 'express'
import * as security from '../lib/insecurity'
import { UserModel } from '../models/user'
import * as utils from '../lib/utils'
import logger from '../lib/logger'
export function profileImageUrlUpload () {
return async (req: Request, res: Response, next: NextFunction) => {
if (req.body.imageUrl !== undefined) {
const url = req.body.imageUrl
if (url.match(/(.)*solve\/challenges\/server-side(.)*/) !== null) req.app.locals.abused_ssrf_bug = true
const loggedInUser = security.authenticatedUsers.get(req.cookies.token)
if (loggedInUser) {
try {
const response = await fetch(url)
if (!response.ok || !response.body) {
throw new Error('url returned a non-OK status code or an empty body')
}
const ext = ['jpg', 'jpeg', 'png', 'svg', 'gif'].includes(url.split('.').slice(-1)[0].toLowerCase()) ? url.split('.').slice(-1)[0].toLowerCase() : 'jpg'
const fileStream = fs.createWriteStream(`frontend/dist/frontend/assets/public/images/uploads/${loggedInUser.data.id}.${ext}`, { flags: 'w' })
await finished(Readable.fromWeb(response.body as any).pipe(fileStream))
const user = await UserModel.findByPk(loggedInUser.data.id)
await user?.update({ profileImage: `/assets/public/images/uploads/${loggedInUser.data.id}.${ext}` })
} catch (error) {
try {
const user = await UserModel.findByPk(loggedInUser.data.id)
await user?.update({ profileImage: url })
logger.warn(`Error retrieving user profile image: ${utils.getErrorMessage(error)}; using image link directly`)
} catch (error) {
next(error)
return
}
}
} else {
next(new Error('Blocked illegal activity by ' + req.socket.remoteAddress))
return
}
}
res.location(process.env.BASE_PATH + '/profile')
res.redirect(process.env.BASE_PATH + '/profile')
}
}