allow non-standards fallback for all http.TimeFormats (#15662)

fixes #15645
This commit is contained in:
Harshavardhana 2022-09-07 07:24:54 -07:00 committed by GitHub
parent 52861d3aea
commit 228c6686f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 153 additions and 52 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/google/uuid"
"github.com/minio/minio-go/v7/pkg/tags"
"github.com/minio/minio/internal/amztime"
sse "github.com/minio/minio/internal/bucket/encryption"
"github.com/minio/minio/internal/bucket/lifecycle"
"github.com/minio/minio/internal/event"
@ -786,7 +787,7 @@ func parseRestoreObjStatus(restoreHdr string) (restoreObjStatus, error) {
if strings.TrimSpace(expiryTokens[0]) != "expiry-date" {
return restoreObjStatus{}, errRestoreHDRMalformed
}
expiry, err := time.Parse(http.TimeFormat, strings.Trim(expiryTokens[1], `"`))
expiry, err := amztime.ParseHeader(strings.Trim(expiryTokens[1], `"`))
if err != nil {
return restoreObjStatus{}, errRestoreHDRMalformed
}

View File

@ -38,6 +38,7 @@ import (
"time"
"github.com/djherbis/atime"
"github.com/minio/minio/internal/amztime"
"github.com/minio/minio/internal/config/cache"
"github.com/minio/minio/internal/crypto"
"github.com/minio/minio/internal/disk"
@ -132,17 +133,14 @@ func (m *cacheMeta) ToObjectInfo() (o ObjectInfo) {
} else {
o.StorageClass = globalMinioDefaultStorageClass
}
var (
t time.Time
e error
)
if exp, ok := meta["expires"]; ok {
if t, e = time.Parse(http.TimeFormat, exp); e == nil {
if t, e := amztime.ParseHeader(exp); e == nil {
o.Expires = t.UTC()
}
}
if mtime, ok := meta["last-modified"]; ok {
if t, e = time.Parse(http.TimeFormat, mtime); e == nil {
if t, e := amztime.ParseHeader(mtime); e == nil {
o.ModTime = t.UTC()
}
}

View File

@ -26,6 +26,7 @@ import (
"strings"
"time"
"github.com/minio/minio/internal/amztime"
"github.com/minio/minio/internal/bucket/replication"
"github.com/minio/minio/internal/hash/sha256"
xhttp "github.com/minio/minio/internal/http"
@ -127,17 +128,8 @@ func (fi FileInfo) ToObjectInfo(bucket, object string, versioned bool) ObjectInf
SuccessorModTime: fi.SuccessorModTime,
}
// Update expires
var (
t time.Time
e error
)
const nonStandardHTTPTimeFormat = "Mon, 2 Jan 2006 15:04:05 GMT"
if exp, ok := fi.Metadata["expires"]; ok {
if t, e = time.Parse(http.TimeFormat, exp); e == nil {
objInfo.Expires = t.UTC()
} else if t, e = time.Parse(nonStandardHTTPTimeFormat, exp); e == nil {
if t, err := amztime.ParseHeader(exp); err == nil {
objInfo.Expires = t.UTC()
}
}

View File

@ -23,12 +23,11 @@ import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
pathutil "path"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/minio/minio/internal/amztime"
xhttp "github.com/minio/minio/internal/http"
"github.com/minio/minio/internal/lock"
"github.com/minio/minio/internal/logger"
@ -175,12 +174,9 @@ func (m fsMetaV1) ToObjectInfo(bucket, object string, fi os.FileInfo) ObjectInfo
} else {
objInfo.StorageClass = globalMinioDefaultStorageClass
}
var (
t time.Time
e error
)
if exp, ok := m.Meta["expires"]; ok {
if t, e = time.Parse(http.TimeFormat, exp); e == nil {
if t, e := amztime.ParseHeader(exp); e == nil {
objInfo.Expires = t.UTC()
}
}

View File

@ -27,10 +27,11 @@ import (
"sync/atomic"
"time"
"github.com/dustin/go-humanize"
"github.com/minio/minio-go/v7/pkg/set"
xnet "github.com/minio/pkg/net"
"github.com/dustin/go-humanize"
"github.com/minio/minio/internal/amztime"
"github.com/minio/minio/internal/config/dns"
"github.com/minio/minio/internal/crypto"
xhttp "github.com/minio/minio/internal/http"
@ -241,17 +242,6 @@ func isAdminReq(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, adminPathPrefix)
}
// Supported amz date formats.
var amzDateFormats = []string{
// Do not change this order, x-amz-date format is usually in
// iso8601Format rest are meant for relaxed handling of other
// odd SDKs that might be out there.
iso8601Format,
time.RFC1123,
time.RFC1123Z,
// Add new AMZ date formats here.
}
// Supported Amz date headers.
var amzDateHeaders = []string{
// Do not chane this order, x-amz-date value should be
@ -260,24 +250,17 @@ var amzDateHeaders = []string{
"date",
}
// parseAmzDate - parses date string into supported amz date formats.
func parseAmzDate(amzDateStr string) (amzDate time.Time, apiErr APIErrorCode) {
for _, dateFormat := range amzDateFormats {
amzDate, err := time.Parse(dateFormat, amzDateStr)
if err == nil {
return amzDate, ErrNone
}
}
return time.Time{}, ErrMalformedDate
}
// parseAmzDateHeader - parses supported amz date headers, in
// supported amz date formats.
func parseAmzDateHeader(req *http.Request) (time.Time, APIErrorCode) {
for _, amzDateHeader := range amzDateHeaders {
amzDateStr := req.Header.Get(amzDateHeader)
if amzDateStr != "" {
return parseAmzDate(amzDateStr)
t, err := amztime.Parse(amzDateStr)
if err != nil {
return time.Time{}, ErrMalformedDate
}
return t, ErrNone
}
}
// Date header missing.

View File

@ -24,6 +24,7 @@ import (
"strconv"
"time"
"github.com/minio/minio/internal/amztime"
"github.com/minio/minio/internal/event"
"github.com/minio/minio/internal/hash"
xhttp "github.com/minio/minio/internal/http"
@ -78,7 +79,7 @@ func checkCopyObjectPreconditions(ctx context.Context, w http.ResponseWriter, r
// since the specified time otherwise return 412 (precondition failed).
ifModifiedSinceHeader := r.Header.Get(xhttp.AmzCopySourceIfModifiedSince)
if ifModifiedSinceHeader != "" {
if givenTime, err := time.Parse(http.TimeFormat, ifModifiedSinceHeader); err == nil {
if givenTime, err := amztime.ParseHeader(ifModifiedSinceHeader); err == nil {
if !ifModifiedSince(objInfo.ModTime, givenTime) {
// If the object is not modified since the specified time.
writeHeaders()
@ -92,7 +93,7 @@ func checkCopyObjectPreconditions(ctx context.Context, w http.ResponseWriter, r
// modified since the specified time, otherwise return a 412 (precondition failed).
ifUnmodifiedSinceHeader := r.Header.Get(xhttp.AmzCopySourceIfUnmodifiedSince)
if ifUnmodifiedSinceHeader != "" {
if givenTime, err := time.Parse(http.TimeFormat, ifUnmodifiedSinceHeader); err == nil {
if givenTime, err := amztime.ParseHeader(ifUnmodifiedSinceHeader); err == nil {
if ifModifiedSince(objInfo.ModTime, givenTime) {
// If the object is modified since the specified time.
writeHeaders()
@ -172,7 +173,7 @@ func checkPreconditions(ctx context.Context, w http.ResponseWriter, r *http.Requ
// otherwise return a 304 (not modified).
ifModifiedSinceHeader := r.Header.Get(xhttp.IfModifiedSince)
if ifModifiedSinceHeader != "" {
if givenTime, err := time.Parse(http.TimeFormat, ifModifiedSinceHeader); err == nil {
if givenTime, err := amztime.ParseHeader(ifModifiedSinceHeader); err == nil {
if !ifModifiedSince(objInfo.ModTime, givenTime) {
// If the object is not modified since the specified time.
writeHeaders()
@ -186,7 +187,7 @@ func checkPreconditions(ctx context.Context, w http.ResponseWriter, r *http.Requ
// time, otherwise return a 412 (precondition failed).
ifUnmodifiedSinceHeader := r.Header.Get(xhttp.IfUnmodifiedSince)
if ifUnmodifiedSinceHeader != "" {
if givenTime, err := time.Parse(http.TimeFormat, ifUnmodifiedSinceHeader); err == nil {
if givenTime, err := amztime.ParseHeader(ifUnmodifiedSinceHeader); err == nil {
if ifModifiedSince(objInfo.ModTime, givenTime) {
// If the object is modified since the specified time.
writeHeaders()

72
internal/amztime/parse.go Normal file
View File

@ -0,0 +1,72 @@
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package amztime implements AWS specific time parsing and deviations
package amztime
import (
"errors"
"net/http"
"time"
)
// Supported amz date formats.
var amzDateFormats = []string{
// Do not change this order, x-amz-date format is usually in
// iso8601Format rest are meant for relaxed handling of other
// odd SDKs that might be out there.
"20060102T150405Z",
time.RFC1123,
time.RFC1123Z,
// Add new AMZ date formats here.
}
// ErrMalformedDate always returned for dates that cannot be parsed.
var ErrMalformedDate = errors.New("malformed date")
// Parse parses date string via supported amz date formats.
func Parse(amzDateStr string) (time.Time, error) {
for _, dateFormat := range amzDateFormats {
amzDate, err := time.Parse(dateFormat, amzDateStr)
if err == nil {
return amzDate, nil
}
}
return time.Time{}, ErrMalformedDate
}
var httpTimeFormats = []string{
// Do not chagne this order, http time format dates
// are usually in http.TimeFormat however there are
// situations where for example aws-sdk-java doesn't
// send the correct format.
http.TimeFormat,
"Mon, 2 Jan 2006 15:04:05 GMT",
}
// ParseHeader parses http.TimeFormat with an acceptable
// extension for http.TimeFormat - return time might be zero
// if the timeStr is invalid.
func ParseHeader(timeStr string) (time.Time, error) {
for _, dateFormat := range httpTimeFormats {
t, err := time.Parse(dateFormat, timeStr)
if err == nil {
return t, nil
}
}
return time.Time{}, ErrMalformedDate
}

View File

@ -0,0 +1,58 @@
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package amztime implements AWS specific time parsing and deviations
package amztime
import (
"errors"
"testing"
"time"
)
func TestParse(t *testing.T) {
type testCase struct {
expectedErr error
expectedTime time.Time
timeStr string
}
testCases := []testCase{
{
ErrMalformedDate,
time.Time{},
"Tue Sep 6 07:10:23 PM PDT 2022",
},
{
nil,
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
"Tue, 10 Nov 2009 23:00:00 UTC",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.timeStr, func(t *testing.T) {
gott, goterr := Parse(testCase.timeStr)
if !errors.Is(goterr, testCase.expectedErr) {
t.Errorf("expected %v, got %v", testCase.expectedErr, goterr)
}
if !gott.Equal(testCase.expectedTime) {
t.Errorf("expected %v, got %v", testCase.expectedTime, gott)
}
})
}
}