Skip to content

Commit

Permalink
Restrict hostnames for cover proxy. (#3385)
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Feb 6, 2024
1 parent f9ab536 commit 345d00f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions config/vufind/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,13 @@ coverimagesCache = true
; BrowZine DOI icons:
coverproxyCache[] = "/assets\.thirdiron\.com/"

; This setting controls which hosts are allowed to provide cover images through
; the built-in cover proxy. Images from hostnames that do not match the regular
; expressions below will not be proxied. Be sure to define regular expressions
; using an end of string ($) marker to prevent abuse.
coverproxyAllowedHosts[] = "/assets\.thirdiron\.com$/"
coverproxyAllowedHosts[] = "/\.summon\.serialssolutions\.com$/"

; This setting controls how cover image URLs are loaded. They could be loaded as
; part of main request, or asynchronously. Asynchronous loading is disabled by
; default; to enable it, just uncomment the line below.
Expand Down
35 changes: 33 additions & 2 deletions module/VuFind/src/VuFind/Controller/CoverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,31 @@ class CoverController extends \Laminas\Mvc\Controller\AbstractActionController
*/
protected $sessionSettings = null;

/**
* Configuration settings ([Content] section of config.ini)
*
* @var array
*/
protected $config;

/**
* Constructor
*
* @param Loader $loader Cover loader
* @param CachingProxy $proxy Proxy loader
* @param SessionSettings $ss Session settings
* @param array $config Configuration settings
*/
public function __construct(
Loader $loader,
CachingProxy $proxy,
SessionSettings $ss
SessionSettings $ss,
array $config = []
) {
$this->loader = $loader;
$this->proxy = $proxy;
$this->sessionSettings = $ss;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -115,6 +125,27 @@ protected function getImageParams()
];
}

/**
* Is the provided URL included on the configured allow list?
*
* @param string $url URL to check
*
* @return bool
*/
protected function proxyAllowedForUrl(string $url): bool
{
$host = parse_url($url, PHP_URL_HOST);
if (!$host) {
return false;
}
foreach ((array)($this->config['coverproxyAllowedHosts'] ?? []) as $regEx) {
if (preg_match($regEx, $host)) {
return true;
}
}
return false;
}

/**
* Send image data for display in the view
*
Expand All @@ -126,7 +157,7 @@ public function showAction()

// Special case: proxy a full URL:
$url = $this->params()->fromQuery('proxy');
if (!empty($url)) {
if (!empty($url) && $this->proxyAllowedForUrl($url)) {
try {
$image = $this->proxy->fetch($url);
$contentType = $image?->getHeaders()?->get('content-type')?->getFieldValue() ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory.');
}
$config = $container->get(\VuFind\Config\PluginManager::class)->get('config');
$configArray = $config?->Content?->toArray() ?? [];
return new $requestedName(
$container->get(\VuFind\Cover\Loader::class),
$container->get(\VuFind\Cover\CachingProxy::class),
$container->get(\VuFind\Session\Settings::class)
$container->get(\VuFind\Session\Settings::class),
$configArray
);
}
}

0 comments on commit 345d00f

Please sign in to comment.