s3-proxy icon indicating copy to clipboard operation
s3-proxy copied to clipboard

s3Key with url like `/?foo=bar` with query on `/` path, leads to error when reading .createReadStream() after also trying to use defaultKey

Open jplwood opened this issue 4 years ago • 0 comments

In getObject() the function starts with the following code:

    // This will get everything in the path following the mountpath
    var s3Key = decodeURIComponent(req.originalUrl.substr(req.baseUrl.length + 1));

    // If the key is empty (this occurs if a request comes in for a url ending in '/'), and there is a defaultKey
    // option present on options, use the default key
    // E.g. if someone wants to route '/' to '/index.html'
    if ( s3Key === '' && options.defaultKey ) {
        s3Key = options.defaultKey;
    }

    // Chop off the querystring, it causes problems with SDK.
    var queryIndex = s3Key.indexOf('?');
    if (queryIndex !== -1) {
      s3Key = s3Key.substr(0, queryIndex);
    }

Say I have a defaultKey such as index.html, and I request a / route with a query string, e.g., /?foo=bar. If i do this, that first if statement

    if ( s3Key === '' && options.defaultKey ) {
        s3Key = options.defaultKey;
    }

my s3Key will be ?foo=bar and my defaultKey will not be used. The code continues and removes the query string from the s3Key making it s3Key = ''. If my s3Key is passed to the s3Params as a blank string, which is passed to s3Request and finally when we call s3Request.createReadStream(), a 500 error is thrown:

UriParameterError: Expected uri parameter to have length >= 1, but found "" for params.Key
    at ParamValidator.fail (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:50:37)
    at ParamValidator.validateUri (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:165:14)
    at ParamValidator.validateString (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:158:12)
    at ParamValidator.validateScalar (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:130:21)
    at ParamValidator.validateMember (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:94:21)
    at ParamValidator.validateStructure (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:75:14)
    at ParamValidator.validateMember (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:88:21)
    at ParamValidator.validate (C:\my-app\node_modules\aws-sdk\lib\param_validator.js:34:10)
    at Request.VALIDATE_PARAMETERS (C:\my-app\node_modules\aws-sdk\lib\event_listeners.js:132:42)
    at Request.callListeners (C:\my-app\node_modules\aws-sdk\lib\sequential_executor.js:106:20)

In my own testing, I believe the fix is as simple as reordering the code so the removal of the query string right after creating the s3Key. I'll put in a PR and let you review this...

Thanks!

jplwood avatar Sep 22 '20 01:09 jplwood