Make the watch route generate a full YouTube URL (fixes #402)

This commit is contained in:
Pierre Rudloff 2022-02-27 23:32:08 +01:00
parent 7f28275fb0
commit 732baccd63
3 changed files with 23 additions and 4 deletions

View file

@ -94,7 +94,7 @@ class App extends \Slim\App
$this->any( $this->any(
'/watch', '/watch',
[$frontController, 'info'] [$frontController, 'watch']
); );
$this->any( $this->any(

View file

@ -169,10 +169,8 @@ abstract class BaseController
*/ */
protected function getVideoPageUrl(Request $request): string protected function getVideoPageUrl(Request $request): string
{ {
$url = $request->getQueryParam('url') ?: $request->getQueryParam('v');
// Prevent SSRF attacks. // Prevent SSRF attacks.
$parts = Url::validateUrl($url, new Options()); $parts = Url::validateUrl($request->getQueryParam('url'), new Options());
return $parts['url']; return $parts['url'];
} }

View file

@ -14,6 +14,7 @@ use Alltube\Middleware\CspMiddleware;
use Exception; use Exception;
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException; use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
use Slim\Http\StatusCode; use Slim\Http\StatusCode;
use Slim\Http\Uri;
use stdClass; use stdClass;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Throwable; use Throwable;
@ -345,4 +346,24 @@ class FrontController extends BaseController
return $this->displayError($request, $response, $message); return $this->displayError($request, $response, $message);
} }
} }
/**
* Route that mimics YouTube video URLs ("/watch?v=foo")
*
* @param Request $request
* @param Response $response
* @return Response
*/
public function watch(Request $request, Response $response): Response
{
// We build a full YouTube URL from the video ID.
$youtubeUri = Uri::createFromString('https://www.youtube.com/watch')
->withQuery(http_build_query(['v' => $request->getQueryParam('v')]));
// Then pass it to the info route.
return $response->withRedirect(
Uri::createFromString($this->router->pathFor('info'))
->withQuery(http_build_query(['url' => strval($youtubeUri)]))
);
}
} }