Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Invalid RFC-7231 date-time value when sending HeadObjectCommand #3813

Closed
dustin-lennon opened this issue Jul 20, 2022 · 2 comments · Fixed by #3814
Closed

TypeError: Invalid RFC-7231 date-time value when sending HeadObjectCommand #3813

dustin-lennon opened this issue Jul 20, 2022 · 2 comments · Fixed by #3814
Assignees
Labels
bug This issue is a bug.

Comments

@dustin-lennon
Copy link
Contributor

dustin-lennon commented Jul 20, 2022

Describe the bug

Got error TypeError: Invalid RFC-7231 date-time value when sending command HeadObjectCommand from S3Client.

JSON object being sent is

{
    "bucketName": "test-prod",
    "fileName": "library/images/standard/MelikWilliams.jpeg"
}

Expected Behavior

Error not thrown and file deleted from S3 bucket

Current Behavior

Error is thrown and file is not deleted from S3 bucket

Reproduction Steps

const { S3Client, HeadBucketCommand, HeadObjectCommand, GetObjectCommand,
    PutObjectCommand, DeleteObjectCommand, CopyObjectCommand } = require("@aws-sdk/client-s3")
const s3 = new S3Client({ region: 'us-east-1' })

exports.deleteHandler = async (req, res) => {
    let b = req.body.bucketName
    let f = req.body.fileName

    let options = {
        Bucket: b,
        Key: f
    }

    if (await bucketExists(b)) {
        if (await fileExists(b, f)) {
            const command = new DeleteObjectCommand(options)
            try {
                await s3.send(command)
                sendJson('Success', res)
            } catch (error) {
                console.log(error)
                res.status(500).send('Internal Server Error')
            }
        } else {
            res.status(404).send('File Not Found')
        }
    } else {
        res.status(404).send('Bucket Not Found')
    }
}

const bucketExists = async b => {
    const options = {
        Bucket: b,
    }
    const command = new HeadBucketCommand(options)

    try {
        await s3.send(command)
        return true
    } catch (error) {
        if (error.name === 'NotFound') {
            return false
        } else {
            throw error
        }
    }
}

const fileExists = async (b, f) => {
    const options = {
        Bucket: b,
        Key: f
    }
    const command = new HeadObjectCommand(options)

    try {
        const response = await s3.send(command)
        return true
    } catch (error) {
        if (error.name === 'NotFound') {
            return false
        } else {
            throw error
        }
    }
}

Possible Solution

In @aws-sdk/smithy-client/dist-cjs/date-utils.js change line 41-43 from

const IMF_FIXDATE = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/);
const RFC_850_DATE = new RegExp(/^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/);
const ASC_TIME = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( [1-9]|\d{2}) (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))? (\d{4})$/);

to

const IMF_FIXDATE = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/);
const RFC_850_DATE = new RegExp(/^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/);
const ASC_TIME = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( [1-9]|\d{2}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? (\d{4})$/);

in order to account for a time being 04:46:49 GMT or 4:46:41 GMT

Additional Information/Context

No response

SDK version used

3.130.0

Environment details (OS name and version, etc.)

node:16 Docker image

@dustin-lennon dustin-lennon added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 20, 2022
@dustin-lennon dustin-lennon changed the title (short issue description) TypeError: Invalid RFC-7231 date-time value Jul 20, 2022
@dustin-lennon dustin-lennon changed the title TypeError: Invalid RFC-7231 date-time value TypeError: Invalid RFC-7231 date-time value when sending DeleteObjectCommand Jul 20, 2022
@dustin-lennon dustin-lennon changed the title TypeError: Invalid RFC-7231 date-time value when sending DeleteObjectCommand TypeError: Invalid RFC-7231 date-time value when sending HeadObjectCommand Jul 20, 2022
@yenfryherrerafeliz yenfryherrerafeliz self-assigned this Jul 20, 2022
@yenfryherrerafeliz
Copy link
Contributor

Hi @dustin-lennon, thanks for opening this issue. I have reviewed your PR and I have left a message there.

Thanks!

@yenfryherrerafeliz yenfryherrerafeliz removed the needs-triage This issue or PR still needs to be triaged. label Jul 21, 2022
@github-actions
Copy link

github-actions bot commented Aug 9, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants