amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

🚨 Error: [AMP cache] access-control-allow-origin CORS errors

Open saurabh230197 opened this issue 5 years ago • 15 comments

Details: Images/SVGs are allowing access to only one website and not other if its used in other domain. In our case https://visualstories.com is a tool to create web stories in users own domain. Tool allows user to use Images and SVGs for free. https://pets-visualstories-com.cdn.ampproject.org/v/s/pets.visualstories.com/reasons-why-cats-are-better-than-dogs?amp_js_v=0.1#origin=https%3A%2F%2Fvisualstories.com&viewerUrl=https%3A%2F%2Fvisualstories.com%2F&cap=navigateTo%2Cswipe See the attached story URL this will throw CORS errors now hit https://media-visualstories-com.cdn.ampproject.org/i/s/media.visualstories.com/uploads/svges/9304945.svg this SVG URL in new tab of the browser when you are hitting open the network tab from developer tool to see request/response headers, You'll see access-control-allow-origin is allowing to only one website it means when an another website is accessing the same SVG this will throw CORS error to that web story. Currently I'm facing this error when I see same SVGs used in different domains. This may affect on rankings.

Frequency: Daily

Reporting this issue from @visualstories to improve AMP technology.

saurabh230197 avatar Feb 22 '21 18:02 saurabh230197

@newmuis @gmajoulet Can you please look into this. The bug is causing us issues when using Google AMP cache in our feeds like

  • https://visualstories.com/?app=pwa
  • https://visualstories.com/feed/quizgame
  • https://visualstories.com/feed/showcase

saurabh230197 avatar Apr 22 '21 05:04 saurabh230197

Can you try adding a crossorigin attribute on your images/svgs? I think that should fix it. cf: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-crossorigin

gmajoulet avatar Apr 22 '21 15:04 gmajoulet

@gmajoulet Ok we we will add crossorigin attribute and let you know if it works.

saurabh230197 avatar Apr 22 '21 17:04 saurabh230197

@gmajoulet

We already added access-control-allow-origin: * and I verified again. PFA screenshots and see crossorigin_allsite this is original path and crossorigin_onesite this is AMP cache URL where access-control-allow-origin: https://pets-visualstories-com.cdn.ampproject.org This may get update if a person see a story from different website where this this SVG is used.

We used same SVG for image masking in various websites.

See below stories from original path all works as expected. images are visible in both stories. https://pets.visualstories.com/reasons-why-cats-are-better-than-dogs#page=page8 8th slide

https://travel.visualstories.com/most-beautiful-medieval-towns-that-still-exist#page=page6 6th slide

See same stories from AMP cache you'll see image in either first or second story. https://pets-visualstories-com.cdn.ampproject.org/v/s/pets.visualstories.com/reasons-why-cats-are-better-than-dogs?amp_js_v=0.1#origin=https://visualstories.com&viewerUrl=https://visualstories.com/&cap=navigateTo,swipe&page=page8 8th slide

https://travel-visualstories-com.cdn.ampproject.org/v/s/travel.visualstories.com/most-beautiful-medieval-towns-that-still-exist?amp_js_v=0.1#origin=https%3A%2F%2Fvisualstories.com&viewerUrl=https%3A%2F%2Fvisualstories.com%2F&cap=navigateTo%2Cswipe&page=page6 6th slide

This is happening in our hundreds of web stories. To check see https://visualstories.com/?app=pwa

saurabh230197 avatar Apr 23 '21 06:04 saurabh230197

@gmajoulet Hi please help me with this issue.

saurabh230197 avatar Apr 25 '21 19:04 saurabh230197

The response headers are great but the request headers are not. Please try adding a crossorigin parameter to your svg/img, that will fix it. Repeating the link from above if you want to learn more about how this works: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-crossorigin

gmajoulet avatar Apr 26 '21 20:04 gmajoulet

@gmajoulet Hi Gabriel,

There is one SVG being used by 2 stories:

  • Original story 1: https://pets.visualstories.com/reasons-why-cats-are-better-than-dogs
  • Original story 2: https://travel.visualstories.com/most-beautiful-medieval-towns-that-still-exist
  • Original SVG: https://media.visualstories.com/uploads/svges/9304945.svg
  • original SVG has access-control-allow-origin: *

While it works fine on original story URLs, AMP cache version of the stories are facing this problem:

In the AMP cache, one story is getting access to the SVG but other is not.

Notice how AMP cache version of the SVG is restricting access-control-allow-origin only to pets-visualstories-com.cdn.ampproject.org. So when the story on travel-visualstories-com.cdn.ampproject.org is trying to use the SVG it gives this error:

cors-error

It means the story that gets crawled first is getting the access.

AutadeJS avatar Apr 27 '21 23:04 AutadeJS

CCing myself to this thread, I'm going to look into it tonight.

jridgewell avatar Apr 27 '21 23:04 jridgewell

Investigated this, and it's an Google AMP Cache issue. It's easily visible by doing:

  1. Open new tab: https://thrilling--bubble--feast-glitch-me.cdn.ampproject.org/c/s/thrilling-bubble-feast.glitch.me
  2. Hard refresh
  3. Observe the page has a black circle with white text, and that the 9304945.svg image is cached in network panel with access-control-allow-origin: https://thrilling--bubble--feast-glitch-me.cdn.ampproject.org
  4. Navigate to: https://mixolydian--humorous--boa-glitch-me.cdn.ampproject.org/c/s/mixolydian-humorous-boa.glitch.me
  5. Observe the page is blank, and there's a blocked request to the same 9304945.svg.

This is because we've tightened the ACAO header from * to https://thrilling--bubble--feast-glitch-me.cdn.ampproject.org in during the initial request to 9304945.svg, which is then cached. When you open the second page, it is making a request to the same image, and the browser reuses the cached response. That response does not allow https://mixolydian--humorous--boa-glitch-me.cdn.ampproject.org Origin, and so the response request is rejected.

Unfortunately when we added this tightening, we failed to consider the request from multiple Cached Origins, and that the response would be reused across those multiple origins. I'm opening an internal bug to have this fixed.

The good news is that the fix is simple (we need to add a Vary: Origin to our response, which will inform the browser it needs to re-request the image since it's requesting with a new Origin header when it's on the second page). The bad news is the fix won't be able to roll out until next Thursday at the earliest.

jridgewell avatar Apr 28 '21 01:04 jridgewell

Internal bug: b/186585381

jridgewell avatar Apr 28 '21 01:04 jridgewell

@jridgewell Thanks for helping to understand issue.

saurabh230197 avatar Apr 28 '21 03:04 saurabh230197

cl/370994902 (Google internal pull request) has been merged, so we should see this fix roll out next Thurs.

jridgewell avatar Apr 28 '21 23:04 jridgewell

@jridgewell It seems this issue haven't fixed yet. Open the attached page URL and open console you will see some ACAO header issues.

https://animals-visualstories-com.cdn.ampproject.org/v/s/animals.visualstories.com/why-are-flamingos-pink?amp_js_v=0.1

saurabh230197 avatar May 12 '21 10:05 saurabh230197

The server push that was supposed to happen last week was reverted due to an unrelated bug. We're still waiting for this week's push, which should happen again on Thursday.

jridgewell avatar May 12 '21 17:05 jridgewell

The server push is live now, and my tests pages are passing.

jridgewell avatar May 17 '21 17:05 jridgewell