aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.05k stars 573 forks source link

createPresignedPost -> RangeError: Invalid time value #4355

Closed wahab-khurram-dev closed 1 year ago

wahab-khurram-dev commented 1 year ago

Checkboxes for prior research

Describe the bug

I am getting the following error

RangeError: Invalid time value at Date.toISOString () at iso8601 (D:\proj\node_modules\@aws-sdk\s3-presigned-post\dist-cjs\createPresignedPost.js:65:32)

65th code line on @aws-sdk\s3-presigned-post\dist-cjs\createPresignedPost.js is const iso8601 = (date) => date.toISOString().replace(/.\d{3}Z$/, "Z");

SDK version number

@aws-sdk/s3-presigned-post@3.254.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v14.19.3

Reproduction Steps

import { S3Client, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { createPresignedPost } from "@aws-sdk/s3-presigned-post";

let options = {
    //apiVersion: '2012-08-10',
    region: process.env.region
}

let s3Client = new S3Client(options);

(async function() {

const params = {
  Bucket: 'dummy',
  Fields: {
    key: 'dummy_key',
    'content-type': 'video/mp4',
    'content-length': '3682374'
  },
Expires: 600
};

 try {
            const rsltDat = await createPresignedPost(s3Client, params );
        } catch(err) {
console.log(err);

        }

})();

Observed Behavior

RangeError: Invalid time value at Date.toISOString () at iso8601 (D:\proj\node_modules\@aws-sdk\s3-presigned-post\dist-cjs\createPresignedPost.js:65:32)

Expected Behavior

There should be no RangeError: Invalid time value error

Possible Solution

No response

Additional Information/Context

No response

yenfryherrerafeliz commented 1 year ago

Hi @wahabfun, thanks for opening this issue. Could you please let me know if this issue is persistent or happens randomly?, I just tried on my machine and I got not issues, however, there are some scenarios where this problem could happen, and one of them is if the date being parsed for some reason is undefined. After looking at the code I found two places, inside of the presigned post implementation, from where this issue could occur:

One is here:

const signingDate = iso8601(now).replace(/[-:]/g, "");

We will get an error from the line above if the parameter given "now" is undefined, but how could that variable be undefined, well the only way I see for this to happen is if "systemClockOffset" is also undefined. I see that systemClockOffset is resolved from the client config so that we would need to investigate any case where this configuration "systemClockOffset" will be resolved as undefined. Lets see after here:

const { systemClockOffset, base64Encoder, utf8Decoder, sha256 } = client.config; const now = new Date(Date.now() + systemClockOffset);

And the other place is here, however, if "now" is undefined, it should never reach this point.

So, I will be awaiting for your response to see if this issue is sporadically or persistent, and in case is persistent we could then check other aspect from your environment that could help us reproducing this issue.

Steps I took to reproduce:

let options = { region: process.env.TEST_REGION } let s3Client = new S3Client(options); (async function() { const params = { Bucket: process.env.TEST_BUCKET, Key: process.env.TEST_KEY, Fields: { key: process.env.TEST_KEY, 'content-type': 'video/mp4', 'content-length': '3682374' }, Expires: 600 };

try {
    const response = await createPresignedPost(s3Client, params);
    console.log(response)
} catch(err) {
    console.log(err);

}

})();


- Run the code: ``` node src/index.js```

Thanks!
wahab-khurram-dev commented 1 year ago

@yenfryherrerafeliz Thank you for the reply. Yes the error is persistent.

wahab-khurram-dev commented 1 year ago

@yenfryherrerafeliz Also would you be so kind if you can tell me the temporary fixing of the bug so I will update the code and will continue the development of my project.

Thank You!

wahab-khurram-dev commented 1 year ago

@yenfryherrerafeliz

image I have consoled log your investigated variable and from the log it seems that the client.config object only have the "update" property.

Date.now is working fine.

Here is the log output

Aws client.config output { update: [Function: update] }
Date.now output 1674498549009
yenfryherrerafeliz commented 1 year ago

@wahabfun that is really weird, you should really get a lot of things more, when logging the client's config. For example, check my output:

```js Aws client.config output: { apiVersion: '2006-03-01', base64Decoder: [Function: fromBase64], base64Encoder: [Function: toBase64], disableHostPrefix: false, endpointProvider: [Function: defaultEndpointResolver], logger: NoOpLogger {}, serviceId: 'S3', signerConstructor: [class SignatureV4MultiRegion], signingEscapePath: false, urlParser: [Function: parseUrl], useArnRegion: [AsyncFunction (anonymous)], region: [AsyncFunction: region], runtime: 'node', defaultsMode: [AsyncFunction (anonymous)], bodyLengthChecker: [Function: calculateBodyLength], credentialDefaultProvider: [Function (anonymous)], defaultUserAgentProvider: [AsyncFunction (anonymous)], eventStreamSerdeProvider: [Function: eventStreamSerdeProvider], getAwsChunkedEncodingStream: [Function: getAwsChunkedEncodingStream], maxAttempts: [AsyncFunction (anonymous)], md5: [Function: bound Hash], requestHandler: NodeHttpHandler { metadata: { handlerProtocol: 'http/1.1' }, configProvider: Promise { } }, retryMode: [AsyncFunction (anonymous)], sdkStreamMixin: [Function: sdkStreamMixin], sha1: [Function: bound Hash], sha256: [Function: bound Hash], streamCollector: [Function: streamCollector], streamHasher: [Function: readableStreamHasher], useDualstackEndpoint: [AsyncFunction (anonymous)], useFipsEndpoint: [AsyncFunction: useFipsEndpoint], utf8Decoder: [Function: fromUtf8], utf8Encoder: [Function: toUtf8], useAccelerateEndpoint: false, useGlobalEndpoint: false, disableMultiregionAccessPoints: false, defaultSigningName: 's3', endpoint: undefined, tls: true, isCustomEndpoint: false, retryStrategy: [AsyncFunction: retryStrategy], systemClockOffset: 0, credentials: [AsyncFunction (anonymous)], signer: [AsyncFunction: signer], forcePathStyle: false, customUserAgent: undefined, eventStreamMarshaller: EventStreamMarshaller { universalMarshaller: EventStreamMarshaller { eventStreamCodec: [EventStreamCodec], utfEncoder: [Function: toUtf8] } } } { apiVersion: '2006-03-01', base64Decoder: [Function: fromBase64], base64Encoder: [Function: toBase64], disableHostPrefix: false, endpointProvider: [Function: defaultEndpointResolver], logger: NoOpLogger {}, serviceId: 'S3', signerConstructor: [class SignatureV4MultiRegion], signingEscapePath: false, urlParser: [Function: parseUrl], useArnRegion: [AsyncFunction (anonymous)], region: [AsyncFunction: region], runtime: 'node', defaultsMode: [AsyncFunction (anonymous)], bodyLengthChecker: [Function: calculateBodyLength], credentialDefaultProvider: [Function (anonymous)], defaultUserAgentProvider: [AsyncFunction (anonymous)], eventStreamSerdeProvider: [Function: eventStreamSerdeProvider], getAwsChunkedEncodingStream: [Function: getAwsChunkedEncodingStream], maxAttempts: [AsyncFunction (anonymous)], md5: [Function: bound Hash], requestHandler: NodeHttpHandler { metadata: { handlerProtocol: 'http/1.1' }, configProvider: Promise { [Object] } }, retryMode: [AsyncFunction (anonymous)], sdkStreamMixin: [Function: sdkStreamMixin], sha1: [Function: bound Hash], sha256: [Function: bound Hash], streamCollector: [Function: streamCollector], streamHasher: [Function: readableStreamHasher], useDualstackEndpoint: [AsyncFunction (anonymous)], useFipsEndpoint: [AsyncFunction: useFipsEndpoint], utf8Decoder: [Function: fromUtf8], utf8Encoder: [Function: toUtf8], useAccelerateEndpoint: false, useGlobalEndpoint: false, disableMultiregionAccessPoints: false, defaultSigningName: 's3', endpoint: undefined, tls: true, isCustomEndpoint: false, retryStrategy: [AsyncFunction: retryStrategy], systemClockOffset: 0, credentials: [AsyncFunction (anonymous)], signer: [AsyncFunction: signer], forcePathStyle: false, customUserAgent: undefined, eventStreamMarshaller: EventStreamMarshaller { universalMarshaller: EventStreamMarshaller { eventStreamCodec: [EventStreamCodec], utfEncoder: [Function: toUtf8] } } } ```

Would you be able to create an empty project, just to test, by using the repro steps that I put here, and let me know if you also have issues there.

Thanks!

wahab-khurram-dev commented 1 year ago

@yenfryherrerafeliz Thank you so much for the help!

Sorry were busy. I just tried it with the empty and it is working fine. So for my original project I reinstalled the s3 nodejs modules and everything is working smooth!

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.