|
@@ -19,6 +19,15 @@ use Symfony\Bundle\SecurityBundle\Security;
|
|
|
*/
|
|
*/
|
|
|
final class DownloadRequestDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
|
|
final class DownloadRequestDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
|
|
|
{
|
|
{
|
|
|
|
|
+ // Internal ips allowed to access private files without being authenticated
|
|
|
|
|
+ const INTERNAL_IPS = [
|
|
|
|
|
+ '/^127\.0\.0\.[0-1]$/',
|
|
|
|
|
+ '/^localhost$/',
|
|
|
|
|
+ '/^10\.8\.0\.\d{1,3}$/', // VPN
|
|
|
|
|
+ '/^141\.94\.117\.[33-61]$/', // internal public ips
|
|
|
|
|
+ '/^172\.20\.\d{1,3}\.\d{1,3}$/', // docker
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
private FileRepository $fileRepository,
|
|
private FileRepository $fileRepository,
|
|
|
private FileManager $fileManager,
|
|
private FileManager $fileManager,
|
|
@@ -31,6 +40,21 @@ final class DownloadRequestDataProvider implements ItemDataProviderInterface, Re
|
|
|
return DownloadRequest::class === $resourceClass;
|
|
return DownloadRequest::class === $resourceClass;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns true if the client Ip is allowed to access restricted content without auth
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $clientIp
|
|
|
|
|
+ * @return bool
|
|
|
|
|
+ */
|
|
|
|
|
+ private function isInternalIp(string $clientIp): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ foreach (self::INTERNAL_IPS as $ipRule) {
|
|
|
|
|
+ if (preg_match($ipRule, $clientIp)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): Response | RedirectResponse
|
|
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): Response | RedirectResponse
|
|
|
{
|
|
{
|
|
|
$file = $this->fileRepository->find($id);
|
|
$file = $this->fileRepository->find($id);
|
|
@@ -41,8 +65,12 @@ final class DownloadRequestDataProvider implements ItemDataProviderInterface, Re
|
|
|
throw new \RuntimeException("File " . $id . " has " . $file->getStatus() . " status; abort.");
|
|
throw new \RuntimeException("File " . $id . " has " . $file->getStatus() . " status; abort.");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // This is a request from an authorized IP
|
|
|
|
|
+ $clientIp = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
+ $internalIp = $this->isInternalIp($clientIp);
|
|
|
|
|
+
|
|
|
// Read the file
|
|
// Read the file
|
|
|
- $token = $this->security->getToken();
|
|
|
|
|
|
|
+ $token = $internalIp ? null : $this->security->getToken();
|
|
|
$content = $this->fileManager->read($file, $token);
|
|
$content = $this->fileManager->read($file, $token);
|
|
|
|
|
|
|
|
// Build the response and attach the file to it
|
|
// Build the response and attach the file to it
|