From 7b54a81cccf6b4c12269e9d6897d608b1a99537a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 6 Jan 2022 11:16:35 +0100 Subject: Prevent video import on non unicast ips --- server/middlewares/validators/videos/video-imports.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index 640139c73..e4b54283f 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts @@ -13,6 +13,7 @@ import { CONFIG } from '../../../initializers/config' import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' import { areValidationErrors, doesVideoChannelOfAccountExist } from '../shared' import { getCommonVideoEditAttributes } from './videos' +import { isValid as isIPValid, parse as parseIP } from 'ipaddr.js' const videoImportAddValidator = getCommonVideoEditAttributes().concat([ body('channelId') @@ -71,6 +72,23 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' }) } + if (req.body.targetUrl) { + const hostname = new URL(req.body.targetUrl).hostname + + if (isIPValid(hostname)) { + const parsed = parseIP(hostname) + + if (parsed.range() !== 'unicast') { + cleanUpReqFiles(req) + + return res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Cannot use non unicast IP as targetUrl.' + }) + } + } + } + if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) return next() -- cgit v1.2.3