Encrypting the origin URL
fredshevek opened this issue · 5 comments
Please provide the following information and someone from @imgix/imgix-sdk-team will respond to your issue shortly.
Before you submit:
- Please search through the [existing issues](existing issues) to see if your question has already been discussed.
Question
I'm using a web proxy source which I'm able to generate secure URLs using the token, and it works. But the original source requires an API_KEY
to be passed on the URL. The current method only signs the original URL which becomes the imgix URL encoded and appended an s
, leaving the API_KEY
in the open.
I was wondering if there is a way to actually encrypt the URL instead?
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=...'
Hi @ericdeansanchez,
Sorry for the lack of clarity in the original question, an example would have helped.
Yes, that's exactly the situation.
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.
- 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/
. - 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 urlhttps://proxydomain.com/image.png?key=123
is encrypted to928nriorsotne
. It is then added to an imgix webproxy urlfredshevekapi.imgix.net
and the resulting image URL ishttps://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
- Then, the browser will request this “image” from imgix at https://fredshevekapi.imgix.net/928nriorsotne.
- 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
- 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)
})
- imgix sees this as a normal image (and has no idea what happened behind the scenes)
- 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!
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!
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!