Giter Club home page Giter Club logo

Comments (5)

fredshevek avatar fredshevek commented on August 27, 2024 1

Hi @ericdeansanchez,

Sorry for the lack of clarity in the original question, an example would have helped.

Yes, that's exactly the situation.

from js-core.

frederickfogerty avatar frederickfogerty commented on August 27, 2024 1

Hey @fredshevek - Fred from imgix here 👋 We talked about this a bit internally and I came up with a crazy idea that we want to share with you.

  1. Create an imgix webproxy source (e.g fredshevekapi.imgix.com) which points TO the server where you are running this code, e.g. https://fredshevekapi.netlify.com/image-api/.
  2. When creating imgix urls with encrypted API keys; instead of creating imgix webproxy urls with the embedded api key, encrypt the original url using a private key, and create an imgix webproxy url to the server itself with the imgix domain and this encrypted url.
    e.g. the original url https://proxydomain.com/image.png?key=123 is encrypted to 928nriorsotne. It is then added to an imgix webproxy url fredshevekapi.imgix.net and the resulting image URL is https://fredshevekapi.imgix.net/928nriorsotne.
    In JS:
const KEY = 'super_secret_private_key';
const url = 'https://proxydomain.com/image.png?key=123`
const encryptedUrl = encrypt(url, KEY)
const ub = new ImgixClient({domain: 'fredshevekapi.imgix.net', secureUrlToken: 'abc'});
const imageUrl = ub.buildURL(encryptedUrl) // pass this to the client.
// this will create an image url like this: https://fredshevekapi.imgix.net/928nriorsotne
  1. Then, the browser will request this “image” from imgix at https://fredshevekapi.imgix.net/928nriorsotne.
  2. Because of the webproxy, imgix will go back to the same server as in (2) to request this “image”, in this case: https://fredshevekapi.netlifty.com/image-api/928nriorsotne
  3. The same server should then decrypt the “image key” into the original image URL and do a 302 redirect (imgix follows redirects).
    In JS:
app.get('/image-api/:encryptedUrl', (request, response) => {
  const encryptedUrl = request.params.encryptedUrl;
  if (!encryptedUrl) {
    response.status(404)
  }
  const decryptedUrl = decrypt(encryptedUrl, KEY); // same key as in (1)
  response.redirect(decryptedUrl)
})
  1. imgix sees this as a normal image (and has no idea what happened behind the scenes)
  2. image is rendered on the browser and bob’s your uncle

Although this is quite a complicated solution/suggestion, I think it is within the realms of reasonably complexity if executed correctly. For instance, if the server fails to respond in (5), imgix will just retry at a later time. In terms of performance, the initial image request might take slightly longer due to the extra request step of (5) compared to a "normal" request, every request after this will be cached. imgix also has an origin cache so that the image doesn't have to be refetched when imgix parameters change.

Another option

This could also be stripped down to not have the encryption, and instead of passing around the encrypted url, it could just pass around the image url to proxy without the api key, and then the server could add the api key back in (4). i.e in (2) the server creates an image url of https://fredshevekapi.imgix.net/image.png, and then in (5) the script should add the api key ?key=123 to the image request.


Let me know what you think and if either of these options work out for you!

from js-core.

ericdeansanchez avatar ericdeansanchez commented on August 27, 2024

Hey @fredshevek 👋 thanks for opening this issue! Could you clarify what you mean by your API_KEY being left "in the open?"

Is this your situation?

var ub = new ImgixClient({domain: 'domain.imgix.net', secureUrlToken: 'abc'});
var url = ub.buildURL('https://proxydomain.com/image.png?key=123');
console.log(url);
// https://domain.imgix.net/https%3A%2F%2Fproxydomain.com%2Fimage.png%3Fkey=123?s=...'

from js-core.

fredshevek avatar fredshevek commented on August 27, 2024

Hi @frederickfogerty,

Those options make sense. In fact, I'm already on Netlify, so I can do this with a Netlify Function easily.
I think adding the API key on Netlify and dropping encryption all together is the best option.

Thanks a lot for following this up!

from js-core.

frederickfogerty avatar frederickfogerty commented on August 27, 2024

Great, glad we could help @fredshevek! I'm going to mark this is as closed for not as I think we've progressed far enough on the original question, but let us know how it goes and if you have any more questions!

from js-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.