cloudinary / js-url-gen

Cloudinary's base javascript library, including URL generation.
https://www.cloudinary.com
MIT License
47 stars 9 forks source link

Double encoding of comma in createCloudinaryURL() #580

Closed Edjevw12 closed 10 months ago

Edjevw12 commented 10 months ago

In the createCloudinaryURL method, the comma , character in the publicID is being encoded twice.

This results in %2C being transformed into %252C, which causes issues when trying to access images with a comma in their publicID.

This issue is causing 404 errors when trying to access images with a comma in their publicID, as the double-encoded comma does not match the actual publicID of the image.

https://github.com/cloudinary/js-url-gen/blob/master/src/assets/CloudinaryFile.ts

the issue is in these lines of code

const publicID = this.publicID
  .replace(/,/g, '%2C');

const safeURL = encodeURI(url)
  .replace(/\?/g, '%3F')
  .replace(/=/g, '%3D')

A fix could be to replace the double encoded characters so single encoded

const safeURL = encodeURI(url)
  .replace(/\?/g, '%3F')
  .replace(/=/g, '%3D')
  .replace(/%252C/g, '%2C');

I've tested this fix in my application(s) with multiple characters, commas, and slashes, and it seems to work correctly.

Edjevw12 commented 10 months ago

note: seems like this is expected behaviour. But it causes 404's in our application(s)?

image

tommyg-cld commented 10 months ago

@Edjevw12 thanks for raising this, it's a known issue and we will update you when it's fixed.

For now, I would recommend adding the .replaceAll('%252C', '%2C').replaceAll('%252C', '%2C') fix to your code after you get the delivery URL.

Edjevw12 commented 10 months ago

@Edjevw12 thanks for raising this, it's a known issue and we will update you when it's fixed.

For now, I would recommend adding the .replaceAll('%252C', '%2C').replaceAll('%252C', '%2C') fix to your code after you get the delivery URL.

thanks!

tommyg-cld commented 10 months ago

@Edjevw12 this has been fixed per the latest version 1.12.1 so please update it to this and let us know if you get any issues.