Skip to content

Commit d47c980

Browse files
fix(ui): url encode imageCacheTag for media on dashboard (#11164)
### What? URL encodes the imageCacheTag query param used to render Media on the Admin Dashboard ### Why? The format of the timestamp used as the `imageCacheTag` is causing an `InvalidQueryStringException` when hosting with Cloudfront + Lambda (SST/OpenNext) [See issue](#11163) ### How? Uses `encodeURIComponent` on instances where the `imageCacheTag` is being formatted for the request URL. (In EditUpload, Thumbnail, and PreviewSizes) Fixes #11163 --------- Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com>
1 parent 7f124cf commit d47c980

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

packages/ui/src/elements/EditUpload/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export const EditUpload: React.FC<EditUploadProps> = ({
175175
setFocalPosition({ x: xCenter, y: yCenter })
176176
}
177177

178-
const fileSrcToUse = imageCacheTag ? `${fileSrc}?${imageCacheTag}` : fileSrc
178+
const fileSrcToUse = imageCacheTag ? `${fileSrc}?${encodeURIComponent(imageCacheTag)}` : fileSrc
179179

180180
return (
181181
<div className={baseClass}>

packages/ui/src/elements/PreviewSizes/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const PreviewSizes: React.FC<PreviewSizesProps> = ({ doc, imageCacheTag,
9595
return null
9696
}
9797
if (doc.url) {
98-
return `${doc.url}${imageCacheTag ? `?${imageCacheTag}` : ''}`
98+
return `${doc.url}${imageCacheTag ? `?${encodeURIComponent(imageCacheTag)}` : ''}`
9999
}
100100
}
101101
useEffect(() => {

packages/ui/src/elements/Thumbnail/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const Thumbnail: React.FC<ThumbnailProps> = (props) => {
5050
*/
5151
if (fileSrc) {
5252
const queryChar = fileSrc?.includes('?') ? '&' : '?'
53-
src = imageCacheTag ? `${fileSrc}${queryChar}${imageCacheTag}` : fileSrc
53+
src = imageCacheTag ? `${fileSrc}${queryChar}${encodeURIComponent(imageCacheTag)}` : fileSrc
5454
}
5555

5656
return (
@@ -100,7 +100,7 @@ export function ThumbnailComponent(props: ThumbnailComponentProps) {
100100
*/
101101
if (fileSrc) {
102102
const queryChar = fileSrc?.includes('?') ? '&' : '?'
103-
src = imageCacheTag ? `${fileSrc}${queryChar}${imageCacheTag}` : fileSrc
103+
src = imageCacheTag ? `${fileSrc}${queryChar}${encodeURIComponent(imageCacheTag)}` : fileSrc
104104
}
105105

106106
return (

test/uploads/e2e.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ import { RESTClient } from '../helpers/rest.js'
2222
import { TEST_TIMEOUT_LONG } from '../playwright.config.js'
2323
import {
2424
adminThumbnailFunctionSlug,
25-
adminThumbnailWithSearchQueries,
26-
mediaWithoutCacheTagsSlug,
2725
adminThumbnailSizeSlug,
26+
adminThumbnailWithSearchQueries,
2827
animatedTypeMedia,
2928
audioSlug,
3029
customFileNameMediaSlug,
30+
customUploadFieldSlug,
3131
focalOnlySlug,
3232
mediaSlug,
33+
mediaWithoutCacheTagsSlug,
3334
relationPreviewSlug,
3435
relationSlug,
3536
withMetadataSlug,
3637
withOnlyJPEGMetadataSlug,
3738
withoutMetadataSlug,
38-
customUploadFieldSlug,
3939
} from './shared.js'
4040
import { startMockCorsServer } from './startMockCorsServer.js'
4141
const filename = fileURLToPath(import.meta.url)
@@ -500,9 +500,9 @@ describe('Uploads', () => {
500500
/**
501501
* Regex matcher for date cache tags.
502502
*
503-
* @example it will match `?2022-01-01T00:00:00.000Z`
503+
* @example it will match `?2022-01-01T00%3A00%3A00.000Z` (`?2022-01-01T00:00:00.000Z` encoded)
504504
*/
505-
const cacheTagPattern = /\?\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/
505+
const cacheTagPattern = /\?\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}\.\d{3}Z/
506506

507507
expect(src).not.toMatch(cacheTagPattern)
508508
})
@@ -530,9 +530,9 @@ describe('Uploads', () => {
530530
/**
531531
* Regex matcher for date cache tags.
532532
*
533-
* @example it will match `?2022-01-01T00:00:00.000Z`
533+
* @example it will match `?2022-01-01T00%3A00%3A00.000Z` (`?2022-01-01T00:00:00.000Z` encoded)
534534
*/
535-
const cacheTagPattern = /\?\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/
535+
const cacheTagPattern = /\?\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}\.\d{3}Z/
536536

537537
expect(src).toMatch(cacheTagPattern)
538538
})
@@ -857,7 +857,7 @@ describe('Uploads', () => {
857857
})
858858

859859
describe('remote url fetching', () => {
860-
beforeAll(async () => {
860+
beforeAll(() => {
861861
mockCorsServer = startMockCorsServer()
862862
})
863863

0 commit comments

Comments
 (0)