yireo / Yireo_NextGenImages

45 stars 28 forks source link

Potential error risk with CDN Settings. #68

Closed hgati closed 1 year ago

hgati commented 1 year ago

AWS CloudFront CDN Domain Name

https://d3dxxxxxxg.cloudfront.net/

AWS CloudFront CDN Setting - Origin Path

Magento's CDN Setting

I am currently operating a website with the above configuration. In the Magento frontend HTML, the image URL is represented as follows:

<img src="https://d3dxxxxxxg.cloudfront.net/catalog/product/cache/0b92992ca6366f8cb05593cfbb60c917/s/e/seven-go-magazine-vol-0.jpg" />

The important thing to note is that I have already set the Origin Path to "/media" in the AWS CloudFront management panel.

Therefore, in the Magento CDN configuration, I should not include the "media" path when setting up the media URL. In other words, I should only set the pure domain name as follows:

https://d3dxxxxxxg.cloudfront.net/

When making a request like the one below from webbrowser to AWS CloudFront:

<img src="https://d3dxxxxxxg.cloudfront.net/catalog/product/cache/0b92992ca6366f8cb05593cfbb60c917/s/e/seventeen-going-magazine-vol-rn-0.jpg">

AWS CloudFront will automatically append the origin path (/media) that I have configured to the request when reaching out to the my origin server:

https://<Origin My Server>/media/catalog/product/cache/0b92992ca6366f8cb05593cfbb60c917/s/e/seventeen-going-magazine-vol-rn-0.jpg

In Magento, the media path can vary depending on Magento's configuration (Secure Base URL for User Media Files). If the following code is checking the captcha directory within the media path, comparing it as a constant value could potentially be a source of errors.

https://github.com/yireo/Yireo_NextGenImages/blob/c1f26f7f9fcc0cb949fa55a18a730ad5caf97b0a/Util/HtmlReplacer.php#L249

It would be better to dynamically retrieve the currently configured Magento media directory or URL using a function from the Magento core or its equivalent (getMediaUrl()) function and then compare it.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$captchaUrl = $mediaUrl.'captcha/';
if (strpos($imageUrl, $captchaUrl) !== false) {
//if (strpos($imageUrl, '/media/captcha/') !== false) {
       return false;
}