Skip to content

Commit

Permalink
Merge pull request #148 from 21TORR/asset-proxy-url-generator
Browse files Browse the repository at this point in the history
Asset proxy url generator
  • Loading branch information
apfelbox authored Nov 22, 2024
2 parents c90eb63 + 3c33cd0 commit edaad2f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
3.16.0
======

* (feature) Add `AssetProxyUrlGenerator` to rewrite asset URLs.
* (feature) Sign asset proxy URLs for more secure proxying.



3.15.0
======

Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@
"21torr/hosting": "^3.2 || ^4.0",
"psr/log": "^3.0",
"sebastian/diff": "^4.0.4 || ^5.0",
"symfony/config": "^7.1",
"symfony/console": "^7.1",
"symfony/dependency-injection": "^7.1",
"symfony/event-dispatcher": "^7.1",
"symfony/filesystem": "^7.1",
"symfony/finder": "^7.1",
"symfony/framework-bundle": "^7.1",
"symfony/http-client": "^7.1",
"symfony/http-foundation": "^7.1",
"symfony/http-kernel": "^7.1",
"symfony/lock": "^7.1",
"symfony/rate-limiter": "^7.1",
"symfony/routing": "^7.1",
"symfony/service-contracts": "^3.5",
"symfony/string": "^7.1",
"symfony/validator": "^7.1",
"ueberdosis/tiptap-php": "^1.3"
Expand Down Expand Up @@ -69,4 +79,4 @@
"vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi -v"
]
}
}
}
7 changes: 7 additions & 0 deletions src/Assets/Controller/AssetProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Torr\Storyblok\Assets\Proxy\AssetProxy;
use Torr\Storyblok\Assets\Url\AssetProxyUrlGenerator;

/**
* @final
Expand All @@ -15,10 +16,16 @@ class AssetProxyController extends AbstractController
{
public function proxyAsset (
AssetProxy $assetProxy,
AssetProxyUrlGenerator $proxyUrlGenerator,
Request $request,
string $path,
) : Response
{
if (!$proxyUrlGenerator->verifyProxyUrlRequest($request))
{
throw $this->createNotFoundException("Invalid request");
}

// check for valid URLs
if (!preg_match('~^\d*x\d*\/\w+\/[^\/]+$~D', $path))
{
Expand Down
45 changes: 45 additions & 0 deletions src/Assets/Url/AssetProxyUrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Torr\Storyblok\Assets\Url;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @final
*/
readonly class AssetProxyUrlGenerator
{
/**
*/
public function __construct (
private UrlGeneratorInterface $urlGenerator,
private UriSigner $uriSigner,
) {}

/**
* Rewrites a Storyblok asset URL to a proxied one
*/
public function rewriteAssetUrl (string $storyblokUrl) : string
{
// if it's not a storyblok URL, just return
if (!preg_match('~^https://a.storyblok.com/f/\d+/(?P<path>.+)$~D', $storyblokUrl, $matches))
{
return $storyblokUrl;
}

$url = $this->urlGenerator->generate("storyblok.asset-proxy", [
"path" => $matches['path'],
], UrlGeneratorInterface::ABSOLUTE_URL);

return $this->uriSigner->sign($url, null);
}

/**
*/
public function verifyProxyUrlRequest (Request $request) : bool
{
return $this->uriSigner->checkRequest($request);
}
}

0 comments on commit edaad2f

Please sign in to comment.