Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: media library image signedUrl cache busting issue #18541

Merged

Conversation

thanos982
Copy link
Contributor

@thanos982 thanos982 commented Oct 23, 2023

What does it do?

Adds a new property called isUrlSigned to the admin upload file object, within the signFileUrls method. This property is later utilised in the ImageAssetCard component to determine whether cache-busting is required for the thumbnail URL.

Why is it needed?

Currently, when using the AWS S3 provider with AWS Signature Version 4 authentication with query params, images in the Asset Media Library appear broken. This is due to a "SignatureDoesNotMatch" error caused by the presence of the "updatedAt" parameter, which is used for cache busting. This error occurs because the "updatedAt" parameter is not included in the canonical URL for generating the AWS S3 signature.

This pull request adds the updatedAt cache busting parameter only when the thumbnail URLs are not signed, which fixes the issue.

How to test it?

  • Configure media library S3 provider to use v4 authentication, then upload an image in the media library. The image shouldn't appear broken.
  • Add images with and without the v4 S3 provider. Check the thumbnail urls - only the ones uploaded without S3 should have the ?updatedAt query param.

Related issue(s)/PR(s)

Related to #18443

@strapi-cla
Copy link

strapi-cla commented Oct 23, 2023

CLA assistant check
All committers have signed the CLA.

@thanos982 thanos982 changed the title media library image signedUrl cache busting issue (for issue #18443) media library image signedUrl cache busting issue Oct 23, 2023
@thanos982 thanos982 changed the title media library image signedUrl cache busting issue Fix: media library image signedUrl cache busting issue Oct 23, 2023
@joshuaellis joshuaellis self-requested a review October 24, 2023 15:04
@joshuaellis joshuaellis self-assigned this Oct 24, 2023
@thanos982 thanos982 marked this pull request as draft October 26, 2023 07:38
@thanos982 thanos982 marked this pull request as ready for review October 29, 2023 10:39
Copy link

@Fryuni Fryuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally and it fixes the problem for us :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marc-Roig can you review this change please 👍🏼

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me,
my question about this is, is there another way of not appending an updated by attribute in the image url?

This is a fix that might work for s3 signed urls, but many other providers might fail if you append invalid attributes like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marc-Roig I may be wrong but isn't the signFileUrls method of this file the default method which retrieves the signed urls from any (default or custom) provider? If it is, then the isUrlSigned property will be set correctly, regardless of which the current provider is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean is that other providers, without using any url signing, could fail loading an image with an "updatedAt" parameter .

this is completely unrelated to your work, rather to the fact that we inject the udpatedAt parameter into the image url !

Copy link
Contributor

@Marc-Roig Marc-Roig Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, so far we have only seen this issue when using signed urls.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marc-Roig I may be wrong but isn't the signFileUrls method of this file the default method which retrieves the signed urls from any (default or custom) provider? If it is, then the isUrlSigned property will be set correctly, regardless of which the current provider is.

Indeed, this is happening right now for the azure blob storage strapi upload plugin. It's causing double "?" parameters in the url and causes 403 forbidden signed errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinvugts can you open another issue (or share it if you have already done this)
cc @joshuaellis

Copy link
Member

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with the FE change, I need Marc to approve too though.

@joshuaellis joshuaellis added source: core:upload Source is core/upload package pr: fix This PR is fixing a bug flag: don't merge This PR should not be merged at the moment labels Oct 31, 2023
@joshuaellis joshuaellis added this to the 4.15.2 milestone Oct 31, 2023
@joshuaellis joshuaellis removed the flag: don't merge This PR should not be merged at the moment label Oct 31, 2023
@joshuaellis joshuaellis linked an issue Oct 31, 2023 that may be closed by this pull request
@joshuaellis joshuaellis merged commit 524af80 into strapi:main Oct 31, 2023
96 of 99 checks passed
@alexandrebodin alexandrebodin modified the milestones: 4.15.2, 4.15.3 Nov 3, 2023
joshuaellis pushed a commit that referenced this pull request Nov 8, 2023
* media library image signedUrl cache busting issue references #18443

* date query param typo

* add asset isUrlSigned property, used as asset url cache busting condition

* removed cacheBustUrl function, added inline comment to describe functionality
@alexandrebodin alexandrebodin modified the milestones: 4.15.3, 4.15.2 Nov 8, 2023
@joshuaellis joshuaellis modified the milestones: 4.15.2, 4.15.5 Nov 13, 2023
@kevinvugts
Copy link

This pull request is currently causing issues when using uploads with @strapi

When uploading images with a sass token to Azure Blob Storage using the strapi-provider-upload-azure-storage plugin this will cause a double "?" parameter in the url which is causing a 403 forbidden (signature invalid) issue on the Azure side.

This is caused by the new cacheBusting feature that you guys introduced to prevents the browser from caching the URL for all sizes in combination with react-query.

To be specific I have added the code below. This should be fixed or at least there should be a way to opt out of this.
File: https://github.com/strapi/strapi/blob/develop/packages/core/upload/admin/src/components/AssetCard/ImageAssetCard.jsx


import { CardAsset } from '@strapi/design-system';
import PropTypes from 'prop-types';

import { AssetCardBase } from './AssetCardBase';

export const ImageAssetCard = ({ height, width, thumbnail, size, alt, ...props }) => {
  // Prevents the browser from caching the URL for all sizes and allow react-query to make a smooth update
  // instead of a full refresh
  const urlWithCacheBusting = props.updatedAt ? `${thumbnail}?${props.updatedAt}` : thumbnail;

  return (
    <AssetCardBase {...props} subtitle={height && width && ` - ${width}✕${height}`} variant="Image">
      <CardAsset src={urlWithCacheBusting} size={size} alt={alt} />
    </AssetCardBase>
  );
};

ImageAssetCard.defaultProps = {
  height: undefined,
  width: undefined,
  selected: false,
  onEdit: undefined,
  onSelect: undefined,
  onRemove: undefined,
  size: 'M',
  updatedAt: undefined,
};

ImageAssetCard.propTypes = {
  alt: PropTypes.string.isRequired,
  extension: PropTypes.string.isRequired,
  height: PropTypes.number,
  name: PropTypes.string.isRequired,
  onEdit: PropTypes.func,
  onSelect: PropTypes.func,
  onRemove: PropTypes.func,
  width: PropTypes.number,
  thumbnail: PropTypes.string.isRequired,
  selected: PropTypes.bool,
  size: PropTypes.oneOf(['S', 'M']),
  updatedAt: PropTypes.string,
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: fix This PR is fixing a bug source: core:upload Source is core/upload package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Media Library] Thumbnail auto append datetime for img src
7 participants