]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/oembed.ts
Improve regexp performance
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / oembed.ts
CommitLineData
41fb13c3 1import express from 'express'
c8861d5d 2import { query } from 'express-validator'
d8755eed 3import { join } from 'path'
868fce62 4import { loadVideo } from '@server/lib/model-loaders'
6fad8e51
C
5import { VideoPlaylistModel } from '@server/models/video/video-playlist'
6import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
c0e8b12e 7import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
da854ddd 8import { isTestInstance } from '../../helpers/core-utils'
d4a8e7a6 9import { isIdOrUUIDValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
da854ddd 10import { logger } from '../../helpers/logger'
6dd9de95 11import { WEBSERVER } from '../../initializers/constants'
10363c74 12import { areValidationErrors } from './shared'
d8755eed 13
a1eda903
C
14const playlistPaths = [
15 join('videos', 'watch', 'playlist'),
16 join('w', 'p')
17]
18
19const videoPaths = [
20 join('videos', 'watch'),
21 'w'
22]
23
24function buildUrls (paths: string[]) {
25 return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/')
26}
27
28const startPlaylistURLs = buildUrls(playlistPaths)
29const startVideoURLs = buildUrls(videoPaths)
6fad8e51 30
57731286 31const watchRegex = /\/([^/]+)$/
d8755eed
C
32const isURLOptions = {
33 require_host: true,
34 require_tld: true
35}
36
37// We validate 'localhost', so we don't have the top level domain
38if (isTestInstance()) {
39 isURLOptions.require_tld = false
40}
41
42const oembedValidator = [
43 query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
44 query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
45 query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
46 query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
47
a2431b7d 48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
d8755eed
C
49 logger.debug('Checking oembed parameters', { parameters: req.query })
50
a2431b7d
C
51 if (areValidationErrors(req, res)) return
52
53 if (req.query.format !== undefined && req.query.format !== 'json') {
76148b27
RK
54 return res.fail({
55 status: HttpStatusCode.NOT_IMPLEMENTED_501,
56 message: 'Requested format is not implemented on server.',
57 data: {
58 format: req.query.format
59 }
60 })
a2431b7d
C
61 }
62
67c604ae
C
63 const url = req.query.url as string
64
e93ee838
C
65 let urlPath: string
66
67 try {
68 urlPath = new URL(url).pathname
69 } catch (err) {
70 return res.fail({
71 status: HttpStatusCode.BAD_REQUEST_400,
72 message: err.message,
73 data: {
74 url
75 }
76 })
77 }
78
a1eda903
C
79 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
80 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
6fad8e51
C
81
82 const startIsOk = isVideo || isPlaylist
83
e93ee838 84 const matches = watchRegex.exec(urlPath)
67c604ae 85
a2431b7d 86 if (startIsOk === false || matches === null) {
76148b27
RK
87 return res.fail({
88 status: HttpStatusCode.BAD_REQUEST_400,
89 message: 'Invalid url.',
90 data: {
91 url
92 }
93 })
a2431b7d 94 }
d8755eed 95
d4a8e7a6 96 const elementId = toCompleteUUID(matches[1])
6fad8e51 97 if (isIdOrUUIDValid(elementId) === false) {
76148b27 98 return res.fail({ message: 'Invalid video or playlist id.' })
a2431b7d 99 }
d8755eed 100
6fad8e51 101 if (isVideo) {
868fce62 102 const video = await loadVideo(elementId, 'all')
6fad8e51
C
103
104 if (!video) {
76148b27
RK
105 return res.fail({
106 status: HttpStatusCode.NOT_FOUND_404,
107 message: 'Video not found'
108 })
6fad8e51 109 }
d8755eed 110
6fad8e51 111 if (video.privacy !== VideoPrivacy.PUBLIC) {
76148b27
RK
112 return res.fail({
113 status: HttpStatusCode.FORBIDDEN_403,
114 message: 'Video is not public'
115 })
6fad8e51
C
116 }
117
118 res.locals.videoAll = video
119 return next()
120 }
121
122 // Is playlist
123
124 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
125 if (!videoPlaylist) {
76148b27
RK
126 return res.fail({
127 status: HttpStatusCode.NOT_FOUND_404,
128 message: 'Video playlist not found'
129 })
6fad8e51
C
130 }
131
132 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
76148b27
RK
133 return res.fail({
134 status: HttpStatusCode.FORBIDDEN_403,
135 message: 'Playlist is not public'
136 })
6fad8e51
C
137 }
138
139 res.locals.videoPlaylistSummary = videoPlaylist
a2431b7d 140 return next()
d8755eed 141 }
6fad8e51 142
d8755eed
C
143]
144
145// ---------------------------------------------------------------------------
146
147export {
148 oembedValidator
149}