Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Prevent SSRF on import and channel sync

Chocobozzzebe3c1e92335
diff --git a/server/core/helpers/youtube-dl/youtube-dl-wrapper.ts b/server/core/helpers/youtube-dl/youtube-dl-wrapper.ts
index 71d4d0f0e2..5ec61f023a 100644
--- a/server/core/helpers/youtube-dl/youtube-dl-wrapper.ts
+++ b/server/core/helpers/youtube-dl/youtube-dl-wrapper.ts
@@ -6,6 +6,7 @@ import { readdir } from 'fs/promises'
import { dirname, join } from 'path'
import { inspect } from 'util'
import { isVideoFileExtnameValid } from '../custom-validators/videos.js'
+import { isResolvingToUnicastOnly } from '../dns.js'
import { t } from '../i18n.js'
import { logger, loggerTagsFactory } from '../logger.js'
import { generateVideoImportTmpPath } from '../utils.js'
@@ -98,6 +99,8 @@ export class YoutubeDLWrapper {
}): Promise<YoutubeDLInfo> {
const { userLanguage, youtubeDLArgs = [] } = options
+ await this.checkUnicastOrThrow(userLanguage)
+
const youtubeDL = await YoutubeDLCLI.safeGet()
try {
@@ -140,6 +143,8 @@ export class YoutubeDLWrapper {
}) {
const { userLanguage } = options
+ await this.checkUnicastOrThrow(userLanguage)
+
const youtubeDL = await YoutubeDLCLI.safeGet()
const list = await youtubeDL.getListInfo({
@@ -160,7 +165,11 @@ export class YoutubeDLWrapper {
return list.map(info => info.webpage_url)
}
- async getSubtitles (): Promise<YoutubeDLSubs> {
+ async getSubtitles (options: { userLanguage: string }): Promise<YoutubeDLSubs> {
+ const { userLanguage } = options
+
+ await this.checkUnicastOrThrow(userLanguage)
+
const cwd = CONFIG.STORAGE.TMP_DIR
const youtubeDL = await YoutubeDLCLI.safeGet()
@@ -187,7 +196,15 @@ export class YoutubeDLWrapper {
return subtitles
}
- async downloadVideo (fileExt: string, timeout: number): Promise<string> {
+ async downloadVideo (options: {
+ fileExt: string
+ timeout: number
+ userLanguage: string
+ }): Promise<string> {
+ const { fileExt, timeout, userLanguage } = options
+
+ await this.checkUnicastOrThrow(userLanguage)
+
// Leave empty the extension, youtube-dl will add it
const pathWithoutExtension = generateVideoImportTmpPath(this.url, '')
@@ -245,4 +262,13 @@ export class YoutubeDLWrapper {
return undefined
}
+
+ private async checkUnicastOrThrow (userLanguage: string) {
+ if (!await isResolvingToUnicastOnly(this.url)) {
+ throw new YoutubeDlImportError({
+ message: t(`URL {targetUrl} is not a unicast URL.`, userLanguage, { targetUrl: this.url }),
+ code: YoutubeDlImportErrorCode.NOT_ONLY_UNICAST_URL
+ })
+ }
+ }
}
diff --git a/server/core/lib/job-queue/handlers/video-import.ts b/server/core/lib/job-queue/handlers/video-import.ts
index ee77bdba38..db79d42036 100644
--- a/server/core/lib/job-queue/handlers/video-import.ts
+++ b/server/core/lib/job-queue/handlers/video-import.ts
@@ -92,33 +92,44 @@ export {
async function processTorrentImport (job: Job, videoImport: MVideoImportDefault, payload: VideoImportTorrentPayload) {
logger.info('Processing torrent video import in job %s.', job.id)
- const options = { type: payload.type, generateTranscription: payload.generateTranscription, videoImportId: payload.videoImportId }
-
const target = {
torrentName: videoImport.torrentName
? getSecureTorrentName(videoImport.torrentName)
: undefined,
uri: videoImport.magnetUri
}
- return processFile(() => downloadWebTorrentVideo(target, JOB_TTL['video-import']), videoImport, options)
+
+ const user = await UserModel.loadById(videoImport.userId)
+
+ return processFile({
+ downloader: () => downloadWebTorrentVideo(target, JOB_TTL['video-import']),
+ videoImport,
+ type: payload.type,
+ generateTranscription: payload.generateTranscription,
+ user
+ })
}
async function processYoutubeDLImport (job: Job, videoImport: MVideoImportDefault, payload: VideoImportYoutubeDLPayload) {
logger.info('Processing youtubeDL video import in job %s.', job.id)
- const options = { type: payload.type, generateTranscription: payload.generateTranscription, videoImportId: videoImport.id }
-
const youtubeDL = new YoutubeDLWrapper(
videoImport.targetUrl,
ServerConfigManager.Instance.getEnabledResolutions('vod'),
CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
)
- return processFile(
- () => youtubeDL.downloadVideo(payload.fileExt, JOB_TTL['video-import']),
+ const user = await UserModel.loadById(videoImport.userId)
+
+ return processFile({
+ downloader: () => {
+ return youtubeDL.downloadVideo({ fileExt: payload.fileExt, timeout: JOB_TTL['video-import'], userLanguage: user.getLanguage() })
+ },
videoImport,
- options
- )
+ type: payload.type,
+ generateTranscription: payload.generateTranscription,
+ user
+ })
}
async function getVideoImportOrDie (payload: VideoImportPayload) {
@@ -138,12 +149,15 @@ async function getVideoImportOrDie (payload: VideoImportPayload) {
return videoImport
}
-type ProcessFileOptions = {
+async function processFile (options: {
+ downloader: () => Promise<string>
+ videoImport: MVideoImportDefault
+ user: MUserId
type: VideoImportYoutubeDLPayloadType | VideoImportTorrentPayloadType
generateTranscription: boolean
- videoImportId: number
-}
-async function processFile (downloader: () => Promise<string>, videoImport: MVideoImportDefault, options: ProcessFileOptions) {
+}) {
+ const { downloader, videoImport, user, type, generateTranscription } = options
+
let tmpVideoPath: string
let videoFile: MVideoFile
@@ -153,7 +167,6 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
// Get information about this video
const stats = await stat(tmpVideoPath)
- const user = await UserModel.loadByVideoId(videoImport.videoId)
const isAble = await isUserQuotaValid({ channelUserId: user.id, uploadSize: stats.size })
if (isAble === false) {
@@ -172,7 +185,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
videoFile = await buildNewFile({ mode: 'web-video', ffprobe, path: tmpVideoPath })
videoFile.videoId = videoImport.videoId
- const hookName = options.type === 'youtube-dl'
+ const hookName = type === 'youtube-dl'
? 'filter:api.video.post-import-url.accept.result'
: 'filter:api.video.post-import-torrent.accept.result'
@@ -254,7 +267,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
video,
videoFile,
user: videoImport.User,
- generateTranscription: options.generateTranscription
+ generateTranscription
})
} finally {
videoFileLockReleaser()
diff --git a/server/core/lib/video-pre-import.ts b/server/core/lib/video-pre-import.ts
index db970e7496..596d7f4509 100644
--- a/server/core/lib/video-pre-import.ts
+++ b/server/core/lib/video-pre-import.ts
@@ -263,7 +263,7 @@ export async function buildYoutubeDLImport (options: {
})
// Get video subtitles
- await processYoutubeSubtitles(youtubeDL, targetUrl, video)
+ await processYoutubeSubtitles({ youtubeDL, targetUrl, video, userLanguage: user.getLanguage() })
let fileExt = `.${youtubeDLInfo.ext}`
if (!isVideoFileExtnameValid(fileExt)) fileExt = '.mp4'
@@ -316,9 +316,16 @@ async function processThumbnails (options: {
return []
}
-async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl: string, video: MVideo) {
+async function processYoutubeSubtitles (options: {
+ youtubeDL: YoutubeDLWrapper
+ targetUrl: string
+ video: MVideo
+ userLanguage: string
+}) {
+ const { youtubeDL, targetUrl, video, userLanguage } = options
+
try {
- const subtitles = await youtubeDL.getSubtitles()
+ const subtitles = await youtubeDL.getSubtitles({ userLanguage })
logger.info('Found %s subtitles candidates from youtube-dl import %s.', subtitles.length, targetUrl)