forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideoHandler.ts
90 lines (82 loc) · 3.4 KB
/
videoHandler.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import fs = require('fs')
import { type Request, type Response } from 'express'
import challengeUtils = require('../lib/challengeUtils')
import config from 'config'
import * as utils from '../lib/utils'
const pug = require('pug')
const challenges = require('../data/datacache').challenges
const themes = require('../views/themes/themes').themes
const Entities = require('html-entities').AllHtmlEntities
const entities = new Entities()
exports.getVideo = () => {
return (req: Request, res: Response) => {
const path = videoPath()
const stat = fs.statSync(path)
const fileSize = stat.size
const range = req.headers.range
if (range) {
const parts = range.replace(/bytes=/, '').split('-')
const start = parseInt(parts[0], 10)
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1
const chunksize = (end - start) + 1
const file = fs.createReadStream(path, { start, end })
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Location': '/assets/public/videos/owasp_promo.mp4',
'Content-Type': 'video/mp4'
}
res.writeHead(206, head)
file.pipe(res)
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4'
}
res.writeHead(200, head)
fs.createReadStream(path).pipe(res)
}
}
}
exports.promotionVideo = () => {
return (req: Request, res: Response) => {
fs.readFile('views/promotionVideo.pug', function (err, buf) {
if (err != null) throw err
let template = buf.toString()
const subs = getSubsFromFile()
challengeUtils.solveIf(challenges.videoXssChallenge, () => { return utils.contains(subs, '</script><script>alert(`xss`)</script>') })
const theme = themes[config.get<string>('application.theme')]
template = template.replace(/_title_/g, entities.encode(config.get('application.name')))
template = template.replace(/_favicon_/g, favicon())
template = template.replace(/_bgColor_/g, theme.bgColor)
template = template.replace(/_textColor_/g, theme.textColor)
template = template.replace(/_navColor_/g, theme.navColor)
template = template.replace(/_primLight_/g, theme.primLight)
template = template.replace(/_primDark_/g, theme.primDark)
const fn = pug.compile(template)
let compiledTemplate = fn()
compiledTemplate = compiledTemplate.replace('<script id="subtitle"></script>', '<script id="subtitle" type="text/vtt" data-label="English" data-lang="en">' + subs + '</script>')
res.send(compiledTemplate)
})
}
function favicon () {
return utils.extractFilename(config.get('application.favicon'))
}
}
function getSubsFromFile () {
const subtitles = config.get<string>('application.promotion.subtitles') ?? 'owasp_promo.vtt'
const data = fs.readFileSync('frontend/dist/frontend/assets/public/videos/' + subtitles, 'utf8')
return data.toString()
}
function videoPath () {
if (config.get<string>('application.promotion.video') !== null) {
const video = utils.extractFilename(config.get<string>('application.promotion.video'))
return 'frontend/dist/frontend/assets/public/videos/' + video
}
return 'frontend/dist/frontend/assets/public/videos/owasp_promo.mp4'
}