2022-10-12 10:09:02 -04:00
|
|
|
import express from 'express'
|
|
|
|
import { query } from 'express-validator'
|
|
|
|
import LRUCache from 'lru-cache'
|
|
|
|
import { basename, dirname } from 'path'
|
2023-02-27 03:44:03 -05:00
|
|
|
import { exists, isSafePeerTubeFilenameWithoutExtension, isUUIDValid, toBooleanOrNull } from '@server/helpers/custom-validators/misc'
|
2022-10-12 10:09:02 -04:00
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
import { LRU_CACHE } from '@server/initializers/constants'
|
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoFileModel } from '@server/models/video/video-file'
|
2022-10-19 04:43:53 -04:00
|
|
|
import { MStreamingPlaylist, MVideoFile, MVideoThumbnail } from '@server/types/models'
|
2022-10-12 10:09:02 -04:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
|
|
|
import { areValidationErrors, checkCanAccessVideoStaticFiles } from './shared'
|
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
type LRUValue = {
|
|
|
|
allowed: boolean
|
|
|
|
video?: MVideoThumbnail
|
|
|
|
file?: MVideoFile
|
|
|
|
playlist?: MStreamingPlaylist }
|
|
|
|
|
|
|
|
const staticFileTokenBypass = new LRUCache<string, LRUValue>({
|
2022-10-12 10:09:02 -04:00
|
|
|
max: LRU_CACHE.STATIC_VIDEO_FILES_RIGHTS_CHECK.MAX_SIZE,
|
|
|
|
ttl: LRU_CACHE.STATIC_VIDEO_FILES_RIGHTS_CHECK.TTL
|
|
|
|
})
|
|
|
|
|
|
|
|
const ensureCanAccessVideoPrivateWebTorrentFiles = [
|
|
|
|
query('videoFileToken').optional().custom(exists),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const token = extractTokenOrDie(req, res)
|
|
|
|
if (!token) return
|
|
|
|
|
|
|
|
const cacheKey = token + '-' + req.originalUrl
|
|
|
|
|
|
|
|
if (staticFileTokenBypass.has(cacheKey)) {
|
2022-10-19 04:43:53 -04:00
|
|
|
const { allowed, file, video } = staticFileTokenBypass.get(cacheKey)
|
|
|
|
|
|
|
|
if (allowed === true) {
|
|
|
|
res.locals.onlyVideo = video
|
|
|
|
res.locals.videoFile = file
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
return next()
|
|
|
|
}
|
2022-10-12 10:09:02 -04:00
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
const result = await isWebTorrentAllowed(req, res)
|
|
|
|
|
|
|
|
staticFileTokenBypass.set(cacheKey, result)
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
if (result.allowed !== true) return
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
res.locals.onlyVideo = result.video
|
|
|
|
res.locals.videoFile = result.file
|
2022-10-12 10:09:02 -04:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const ensureCanAccessPrivateVideoHLSFiles = [
|
2022-12-02 08:47:21 -05:00
|
|
|
query('videoFileToken')
|
|
|
|
.optional()
|
|
|
|
.custom(exists),
|
|
|
|
|
|
|
|
query('reinjectVideoFileToken')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toBooleanOrNull)
|
|
|
|
.isBoolean().withMessage('Should be a valid reinjectVideoFileToken boolean'),
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2023-02-27 03:44:03 -05:00
|
|
|
query('playlistName')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(isSafePeerTubeFilenameWithoutExtension),
|
|
|
|
|
2022-10-12 10:09:02 -04:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const videoUUID = basename(dirname(req.originalUrl))
|
|
|
|
|
|
|
|
if (!isUUIDValid(videoUUID)) {
|
|
|
|
logger.debug('Path does not contain valid video UUID to serve static file %s', req.originalUrl)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = extractTokenOrDie(req, res)
|
|
|
|
if (!token) return
|
|
|
|
|
|
|
|
const cacheKey = token + '-' + videoUUID
|
|
|
|
|
|
|
|
if (staticFileTokenBypass.has(cacheKey)) {
|
2022-10-19 04:43:53 -04:00
|
|
|
const { allowed, file, playlist, video } = staticFileTokenBypass.get(cacheKey)
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
if (allowed === true) {
|
|
|
|
res.locals.onlyVideo = video
|
|
|
|
res.locals.videoFile = file
|
|
|
|
res.locals.videoStreamingPlaylist = playlist
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
2022-10-12 10:09:02 -04:00
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
const result = await isHLSAllowed(req, res, videoUUID)
|
|
|
|
|
|
|
|
staticFileTokenBypass.set(cacheKey, result)
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
if (result.allowed !== true) return
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
res.locals.onlyVideo = result.video
|
|
|
|
res.locals.videoFile = result.file
|
|
|
|
res.locals.videoStreamingPlaylist = result.playlist
|
2022-10-12 10:09:02 -04:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
export {
|
|
|
|
ensureCanAccessVideoPrivateWebTorrentFiles,
|
|
|
|
ensureCanAccessPrivateVideoHLSFiles
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function isWebTorrentAllowed (req: express.Request, res: express.Response) {
|
|
|
|
const filename = basename(req.path)
|
|
|
|
|
|
|
|
const file = await VideoFileModel.loadWithVideoByFilename(filename)
|
|
|
|
if (!file) {
|
|
|
|
logger.debug('Unknown static file %s to serve', req.originalUrl, { filename })
|
|
|
|
|
|
|
|
res.sendStatus(HttpStatusCode.FORBIDDEN_403)
|
2022-10-19 04:43:53 -04:00
|
|
|
return { allowed: false }
|
2022-10-12 10:09:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
const video = await VideoModel.load(file.getVideo().id)
|
2022-10-12 10:09:02 -04:00
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
return {
|
|
|
|
file,
|
|
|
|
video,
|
|
|
|
allowed: await checkCanAccessVideoStaticFiles({ req, res, video, paramId: video.uuid })
|
|
|
|
}
|
2022-10-12 10:09:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function isHLSAllowed (req: express.Request, res: express.Response, videoUUID: string) {
|
2022-10-19 04:43:53 -04:00
|
|
|
const filename = basename(req.path)
|
|
|
|
|
|
|
|
const video = await VideoModel.loadWithFiles(videoUUID)
|
2022-10-12 10:09:02 -04:00
|
|
|
|
|
|
|
if (!video) {
|
|
|
|
logger.debug('Unknown static file %s to serve', req.originalUrl, { videoUUID })
|
|
|
|
|
|
|
|
res.sendStatus(HttpStatusCode.FORBIDDEN_403)
|
2022-10-19 04:43:53 -04:00
|
|
|
return { allowed: false }
|
2022-10-12 10:09:02 -04:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:43:53 -04:00
|
|
|
const file = await VideoFileModel.loadByFilename(filename)
|
|
|
|
|
|
|
|
return {
|
|
|
|
file,
|
|
|
|
video,
|
|
|
|
playlist: video.getHLSPlaylist(),
|
|
|
|
allowed: await checkCanAccessVideoStaticFiles({ req, res, video, paramId: video.uuid })
|
|
|
|
}
|
2022-10-12 10:09:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function extractTokenOrDie (req: express.Request, res: express.Response) {
|
|
|
|
const token = res.locals.oauth?.token.accessToken || req.query.videoFileToken
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return res.fail({
|
|
|
|
message: 'Bearer token is missing in headers or video file token is missing in URL query parameters',
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return token
|
|
|
|
}
|