serverless-image-handler icon indicating copy to clipboard operation
serverless-image-handler copied to clipboard

FR: implement URL tampering encoding (v3/Thumbor)

Open timkelty opened this issue 7 years ago • 9 comments

In 3.x, I generated safe URLs by setting a SECURITY_KEY env var, which is used by Thumbor to create the encoded url: https://github.com/thumbor/thumbor/wiki/security

I could then use this same SECURITY_KEY to generate a valid url with another backend-system (eg PHP.

How can I do something similar with 4.x/Sharp?

Related: https://github.com/awslabs/serverless-image-handler/issues/106

timkelty avatar Jun 21 '19 15:06 timkelty

…I see, it looks like its just the base64 encoded JSON: https://github.com/awslabs/serverless-image-handler/blob/master/source/image-handler/image-request.js#L197

So it doesn't really have the same level of URL tampering that Thumbor has?

timkelty avatar Jun 21 '19 16:06 timkelty

I have the same issue Orz

kewang avatar Jun 26 '19 06:06 kewang

As Sharp doesn't really concern itself with the serving of anything, this would need to be implemented in this project, I believe.

timkelty avatar Jun 26 '19 10:06 timkelty

@timkelty Thanks for adding this request, I'm copying it into our feature backlog to be looked into and addressed in a future release. Your feedback has been super helpful in the improvement of this solution!

hayesry avatar Jun 26 '19 14:06 hayesry

Agree safe urls HMAC signed are essential as provided by the last version with Thumbor. Malicious users could decode the image key then automate requests for other assets. Lucky the Thumbor implementation is still working.

abalsekar avatar Jul 17 '19 22:07 abalsekar

Due to the fact that AWS have updated the lambda execution environment, which caused our old thumbor implementation to break, we had to implement this ourselves. If anyone is interested, this is the code we have so far (this is not tested in production yet, so no guarantees...but maybe helpful to someone)

     /**
     * verifies that the security hash sent in the path is valid
     * @param {Object} event - The request body.
     * @param {String} security_key - The security key from environment variable.*     
     */
    verifySecurityHash(event, security_key) {
        var crypto = require('crypto');

        var path = event.path;
        const pathParts = path.split('/');
        var securityHash = pathParts[securityHashPathIndex];
        path = path.replace('/' + securityHash + '/', '');
        var hash = crypto.createHmac('sha1', security_key).update(path).digest('base64').replace(/\+/g, "-").replace(/\//g, "_");

        if (securityHash !== hash) {
            throw new Error('ThumborMapping::VerifySecurityHash::InvalidSecurityHash');
        }
        return true;
    }

securityHashPathIndex (for us at least) is 1. We get security_key from the environment variables (ie. process.env.SECURITY_KEY) and set that in the AWS lambda environment vars.

Here are a couple of unit tests we wrote for it, with the keys and paths removed:

// ----------------------------------------------------------------------------
// verifySecurityHash()
// ----------------------------------------------------------------------------
describe('verifySecurityHash()', function () {
    describe('001/validHash', function () {
        it(`Should pass if the security hash is being validated correctly`, function () {

                // Arrange
                var security_key = '{[[insert your security key here]]';
                var securityHash = '[[insert your 'correct' hash of the security key here]]';

                const event = {
                    path: '/' + securityHash +'[[insert your path which results in the above hash here]]'
                }
                // Act
                const thumborMapping = new ThumborMapping();
                var result = thumborMapping.verifySecurityHash(event, security_key);
                // Assert
                const expectedResult = true;
                assert.deepEqual(result, expectedResult);
            });
    });
    describe('002/InvalidHash', function () {
        it(`Should pass if it is throwing error for invalid security hash`, function () {

            // Arrange
			var security_key = '{[[insert your security key here]]';
			var securityHash = '[[insert your 'incorrect' hash of the security key here]]';

			const event = {
				path: '/' + securityHash +'[[insert your path which does not result in the above hash here]]'
			}
			
            // Act
            const thumborMapping = new ThumborMapping();
            // Assert
            assert.throws(function () {
                thumborMapping.verifySecurityHash(event, security_key);
            }, Error, 'ThumborMapping::VerifySecurityHash::InvalidSecurityHash');
        });
    });
});

soupy1976 avatar Jul 18 '19 07:07 soupy1976

This is also critical for us. I will implement this locally and do some production testing.

powertoaster avatar Aug 06 '19 12:08 powertoaster

@timkelty Thanks for adding this request, I'm copying it into our feature backlog to be looked into and addressed in a future release. Your feedback has been super helpful in the improvement of this solution!

Is this future gonna be released soon? or you have decided to not add it.. and leave it to the user zone?

rakk7 avatar Dec 12 '19 14:12 rakk7

Thanks for waiting, all. We've added a security feature to v5.1.0 to meet the requirement. Please take a look at the latest version.

beomseoklee avatar Nov 19 '20 19:11 beomseoklee