Skip to content

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

Closed
@dustin-lennon

Description

@dustin-lennon

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

Metadata

Metadata

Labels

bugThis issue is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions