samvera / serverless-iiif

IIIF Image API 2.1 & 3.0 server in an AWS Serverless Application
https://samvera.github.io/serverless-iiif/
Apache License 2.0
69 stars 21 forks source link

CloudFront caching interfering with CORS responses #111

Closed mbklein closed 11 months ago

mbklein commented 1 year ago

When the CorsAllowOrigin setting is set to REFLECT_ORIGIN and CloudFront caching is enabled, the first Origin to request a particular resource causes the Access-Control-Allow-Origin response header to be cached, causing CORS errors for any other origin that tries to request the same resource.

Two possible solutions come to mind, but both introduce breaking changes:

In the end, the best compromise solution might be (pseudocode):

if CorsAllowOrigin == "REFLECT_ORIGIN" && CachePolicyID == DEFAULT
  create CorsCachePolicy that includes the Origin in the cache key
  use CorsCachePolicy ID as the default behavior's cache policy
else
  use CachePolicyID  as the default behavior's cache policy
end

Then we just need to update the documentation to stress that, if using a non-default cache policy and REFLECT_ORIGIN, the cache policy must include the Origin request header as part of the key.

mbklein commented 11 months ago

With the CloudFront enabled version being deprecated, this is now an issue for implementers. It might be something to document, but not something we can address directly.