mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* util: SHA-256 stands in for MD5 and SHA-1 under fips140=only GODEBUG=fips140=only panics on MD5 and SHA-1. NewMd5 and NewSha1 return those hashes as before, and under fips140=only SHA-256 cut to the same size, so digests keep their length. Md5, HashStringToLong and HashToInt32 use NewMd5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer stores: hash directory keys with NewMd5 and NewSha1 leveldb2, leveldb3, rocksdb and arangodb hash directory keys with MD5, tikv with SHA-1, which GODEBUG=fips140=only refuses, so the filer panicked at startup. They now use util.NewMd5 and util.NewSha1. Keys made under fips140=only differ from MD5 keys, so leveldb2 and leveldb3 mark an empty database opened under fips140=only and refuse a database whose keys are of the other kind instead of showing it empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * operation, volume: no Content-MD5 checks under fips140=only Under GODEBUG=fips140=only the uploader's digest is not an MD5, so it is no longer sent as Content-MD5, which volume servers outside that mode would reject; the digest still becomes the chunk ETag. A Go volume server in that mode keeps a client's Content-MD5 unchecked instead of panicking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api, filer: ETags and content digests with util.NewMd5 ETags, entry Md5 attributes and chunk digests now use util.Md5 and util.NewMd5, so under GODEBUG=fips140=only they are SHA-256 cut to 16 bytes instead of panicking. That digest cannot check a client's Content-MD5, so in that mode Content-MD5 is accepted unchecked, and the filer stops returning Content-MD5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api, iam, master: SHA-1 names with util.NewSha1 Multipart upload ids, IAM policy ids, the OIDC provider, audit and session revocation file names, and the master's read-only copy dir hash names with SHA-1. They now use util.NewSha1, which GODEBUG=fips140=only turns into truncated SHA-256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * postgres, oidc, update: refuse MD5 and SHA-1 checks under fips140=only PostgreSQL md5 authentication, OIDC TLS thumbprints (SHA-1 by definition) and weed update's MD5 check of the download cannot run under GODEBUG=fips140=only. They now fail with an error saying so instead of panicking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: no empty-body MD5 when Content-MD5 is not checked Under fips140=only a bodyless signed request still reached md5.Sum(nil) before the nil check and panicked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: empty copy part ETag from the digest it stores Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: skip the native MD5 test under fips140=only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3sink: no Content-MD5 under fips140=only The entry's Md5 attribute is truncated SHA-256 there, which an S3 destination checking Content-MD5 would reject. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: move OIDC provider and revocation records to this mode's name The record names hash the ARN or JTI with util.NewSha1, which differs under GODEBUG=fips140=only. On the first use of a filer, each store renames records whose name does not match the one derived from their content, so lookups, revocation checks and deletes find records written in the other mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: test the rename with the names each fips140 mode derives Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: rename a record only if it is unchanged, under its new name's lock Two processes renaming after a mode switch could both list a record; if one renamed it and the provider was then deleted, the other wrote it back. The rename is now one filer transaction locked on the new name, like the stores' conditional writes, that requires the old entry to be unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
354 lines
12 KiB
Go
354 lines
12 KiB
Go
package s3api
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"hash"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
func isExtractZip(r *http.Request) bool {
|
|
if r.URL.Query().Get("extract") == "zip" ||
|
|
r.Header.Get(s3_constants.SeaweedFSExtract) == "zip" {
|
|
return true
|
|
}
|
|
// AWS Snowball auto-extract: header presence requests extraction.
|
|
if v := r.Header.Get(s3_constants.AmzMetaSnowballAutoExtract); v != "" && v != "false" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func hasExplicitSseHeaders(r *http.Request) bool {
|
|
return r.Header.Get(s3_constants.AmzServerSideEncryption) != "" ||
|
|
r.Header.Get(s3_constants.AmzServerSideEncryptionAwsKmsKeyId) != "" ||
|
|
r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm) != ""
|
|
}
|
|
|
|
type extractMemberResult struct {
|
|
Name string `json:"name"`
|
|
Size int64 `json:"size,omitempty"`
|
|
ETag string `json:"etag,omitempty"`
|
|
}
|
|
|
|
type extractZipResult struct {
|
|
Extracted []*extractMemberResult `json:"extracted"`
|
|
Errors []string `json:"errors,omitempty"`
|
|
}
|
|
|
|
// extractZipObject writes a PutObject zip body as one object per member under
|
|
// the object's directory, like AWS Snowball's x-amz-meta-snowball-auto-extract.
|
|
// The archive object itself is not stored.
|
|
func (s3a *S3ApiServer) extractZipObject(w http.ResponseWriter, r *http.Request, bucket, object string, dataReader io.Reader) {
|
|
if hasExplicitSseHeaders(r) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
return
|
|
}
|
|
|
|
versioningState, err := s3a.getVersioningState(bucket)
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
if !s3a.handleAutoCreateBucket(w, r, bucket, "PutObjectHandler") {
|
|
return
|
|
}
|
|
versioningState, err = s3a.getVersioningState(bucket)
|
|
}
|
|
if err != nil {
|
|
glog.Errorf("extractZipObject: versioning state for bucket %s: %v", bucket, err)
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
return
|
|
}
|
|
}
|
|
versioningConfigured := versioningState != ""
|
|
|
|
r, aclCode := s3a.preparePutObjectACL(r, bucket)
|
|
if aclCode != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, aclCode)
|
|
return
|
|
}
|
|
|
|
objectLockEnabled, lockErr := s3a.isObjectLockEnabled(bucket)
|
|
if lockErr != nil && !errors.Is(lockErr, filer_pb.ErrNotFound) {
|
|
glog.Errorf("extractZipObject: object lock state for bucket %s: %v", bucket, lockErr)
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
return
|
|
}
|
|
// Malformed or unauthorized lock headers fail the request outright, the
|
|
// same as a standalone PutObject; per-member permission re-checks happen in
|
|
// authorizeMember.
|
|
if vErr := s3a.validateObjectLockHeaders(r, bucket, object, objectLockEnabled); vErr != nil {
|
|
s3err.WriteErrorResponse(w, r, mapValidationErrorToS3Error(vErr))
|
|
return
|
|
}
|
|
|
|
// Checksums describing the archive body are verified while it spools, then
|
|
// scrubbed so members are validated against their own bytes instead.
|
|
requestQuery := parseRequestQuery(r)
|
|
archiveMd5 := r.Header.Get("Content-Md5")
|
|
var archiveMd5Hash hash.Hash
|
|
if archiveMd5 != "" && !fips140Enforced() {
|
|
archiveMd5Hash = md5.New()
|
|
dataReader = io.TeeReader(dataReader, archiveMd5Hash)
|
|
}
|
|
archiveChecksumAlgo, archiveChecksumHeader, checksumCode := detectRequestedChecksumAlgorithmQ(r, requestQuery)
|
|
if checksumCode != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, checksumCode)
|
|
return
|
|
}
|
|
var archiveChecksumHash hash.Hash
|
|
expectedArchiveChecksum := ""
|
|
if archiveChecksumAlgo != ChecksumAlgorithmNone {
|
|
if archiveChecksumHash = getCheckSumWriter(archiveChecksumAlgo); archiveChecksumHash != nil {
|
|
dataReader = io.TeeReader(dataReader, archiveChecksumHash)
|
|
}
|
|
expectedArchiveChecksum = lookupHeaderOrQuery(r, requestQuery, archiveChecksumHeader)
|
|
}
|
|
scrubArchiveChecksums(r)
|
|
// Content-Encoding describes the archive body on the wire; the members it
|
|
// fans out to are stored as-is.
|
|
r.Header.Del("Content-Encoding")
|
|
|
|
ra, size, cleanup, err := filer.SpoolZipReader(dataReader, 0)
|
|
if err != nil {
|
|
if errors.Is(err, filer.ErrZipTooLarge) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrEntityTooLarge)
|
|
} else if strings.Contains(err.Error(), s3err.ErrMsgContentSha256Mismatch) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrContentSHA256Mismatch)
|
|
} else {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
}
|
|
return
|
|
}
|
|
defer cleanup()
|
|
if errCode := verifyArchiveChecksums(archiveMd5, archiveMd5Hash, expectedArchiveChecksum, archiveChecksumHash); errCode != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
|
return
|
|
}
|
|
zr, err := zip.NewReader(ra, size)
|
|
if err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
return
|
|
}
|
|
if err = filer.CheckZipLimits(zr); err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
return
|
|
}
|
|
|
|
root := object
|
|
if !strings.HasSuffix(root, "/") {
|
|
root = path.Dir(root)
|
|
}
|
|
root = strings.TrimSuffix(root, "/")
|
|
if root == "." {
|
|
root = ""
|
|
}
|
|
|
|
identity, _ := s3_constants.GetIdentityFromContext(r).(*Identity)
|
|
result := &extractZipResult{}
|
|
|
|
join := func(rel string) string {
|
|
if root == "" {
|
|
return rel
|
|
}
|
|
return root + "/" + rel
|
|
}
|
|
// authorizeMember applies to a member key the same checks a standalone
|
|
// PutObject of that key would face: IAM, table layout, bucket policy,
|
|
// object-lock header permissions, and existing-entry protections.
|
|
authorizeMember := func(memberKey string) s3err.ErrorCode {
|
|
if code := s3a.iam.AuthorizeObjectWrite(r, identity, bucket, memberKey); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
if err := s3a.validateTableBucketObjectPath(bucket, memberKey); err != nil {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
if code := s3a.checkVersionFileWrite(bucket, memberKey, "extractZipObject"); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
if code, _ := s3a.checkPolicyWithEntry(r, bucket, memberKey, string(s3_constants.ACTION_WRITE), "", nil); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
// Lock headers validated for the request still need the dedicated
|
|
// permissions against the member's own key.
|
|
if r.Header.Get(s3_constants.AmzObjectLockMode) != "" || r.Header.Get(s3_constants.AmzObjectLockRetainUntilDate) != "" {
|
|
if !s3a.checkObjectLockHeaderPermission(r, bucket, memberKey, s3_constants.ACTION_PUT_OBJECT_RETENTION) {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
}
|
|
if r.Header.Get(s3_constants.AmzObjectLockLegalHold) != "" {
|
|
if !s3a.checkObjectLockHeaderPermission(r, bucket, memberKey, s3_constants.ACTION_PUT_OBJECT_LEGAL_HOLD) {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
}
|
|
if !versioningConfigured {
|
|
if lockErr := s3a.enforceObjectLockProtections(r, bucket, memberKey, "", s3a.evaluateGovernanceBypassRequest(r, bucket, memberKey)); lockErr != nil {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
}
|
|
return s3err.ErrNone
|
|
}
|
|
writeMember := func(rel string, memberReader io.Reader, memberSize int64) {
|
|
memberKey := join(rel)
|
|
if authCode := authorizeMember(memberKey); authCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(authCode).Code)
|
|
return
|
|
}
|
|
memberContentType := mime.TypeByExtension(path.Ext(memberKey))
|
|
if memberContentType == "" {
|
|
memberContentType = "application/octet-stream"
|
|
}
|
|
// Write helpers read the object from route vars, e.g. for the SSE-KMS
|
|
// encryption context ARN; point them at the member, not the archive.
|
|
memberReq := mux.SetURLVars(r, map[string]string{
|
|
"bucket": bucket,
|
|
"object": memberKey,
|
|
})
|
|
memberReq.Header.Set("Content-Type", memberContentType)
|
|
memberReq.ContentLength = memberSize
|
|
var etag string
|
|
var errCode s3err.ErrorCode
|
|
switch versioningState {
|
|
case s3_constants.VersioningEnabled:
|
|
_, etag, errCode, _ = s3a.putVersionedObject(memberReq, bucket, memberKey, memberReader, memberContentType)
|
|
case s3_constants.VersioningSuspended:
|
|
etag, errCode, _ = s3a.putSuspendedVersioningObject(memberReq, bucket, memberKey, memberReader, memberContentType)
|
|
default:
|
|
filePath := s3a.toFilerPath(bucket, memberKey)
|
|
lifecycleTTLSec := s3a.lifecycleTTLForObjectWrite(bucket, memberKey, memberSize)
|
|
etag, errCode, _ = s3a.putToFiler(memberReq, filePath, memberReader, bucket, memberKey, 1, lifecycleTTLSec, nil, false, "")
|
|
}
|
|
if errCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(errCode).Code)
|
|
return
|
|
}
|
|
result.Extracted = append(result.Extracted, &extractMemberResult{Name: memberKey, Size: memberSize, ETag: etag})
|
|
}
|
|
mkdirMember := func(memberKey string) {
|
|
if authCode := authorizeMember(memberKey); authCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(authCode).Code)
|
|
return
|
|
}
|
|
dirPath := s3a.toFilerPath(bucket, memberKey)
|
|
dirName, entryName := path.Dir(dirPath), path.Base(dirPath)
|
|
// mkdir replaces an existing marker outright, so a lock recorded on the
|
|
// entry itself is checked under the same lock the replacement takes,
|
|
// mirroring the explicit-directory branch of PutObjectHandler.
|
|
markerCode := s3a.withObjectWriteLock(bucket, memberKey, func() s3err.ErrorCode {
|
|
if !objectLockEnabled {
|
|
return s3err.ErrNone
|
|
}
|
|
// mkdir replaces the entry on the default filer, so the lock check
|
|
// must read there too — a routed read can miss a marker the owner
|
|
// has not received yet.
|
|
existing, existErr := s3a.getEntry(dirName, entryName)
|
|
if existErr != nil {
|
|
return s3err.ErrNone
|
|
}
|
|
if lockErr := s3a.enforceObjectLockOnEntry(existing, bucket, memberKey, "", s3a.evaluateGovernanceBypassRequest(r, bucket, memberKey)); lockErr != nil {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
return s3err.ErrNone
|
|
}, func() s3err.ErrorCode {
|
|
mkErr := s3a.mkdir(dirName, entryName, func(entry *filer_pb.Entry) {
|
|
entry.Attributes.Mime = s3_constants.FolderMimeType
|
|
s3a.setObjectOwnerFromRequest(r, bucket, entry)
|
|
applyPutObjectACL(r, entry)
|
|
if lockErr := s3a.extractObjectLockMetadataFromRequest(r, entry); lockErr != nil {
|
|
glog.Errorf("extractZipObject: object lock metadata for %s/%s: %v", bucket, memberKey, lockErr)
|
|
}
|
|
})
|
|
return filerErrorToS3Error(mkErr)
|
|
})
|
|
if markerCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(markerCode).Code)
|
|
}
|
|
}
|
|
|
|
for _, zf := range zr.File {
|
|
if ctxErr := r.Context().Err(); ctxErr != nil {
|
|
result.Errors = append(result.Errors, ctxErr.Error())
|
|
break
|
|
}
|
|
rel, ok := filer.SanitizeZipMemberName(zf)
|
|
if !ok {
|
|
result.Errors = append(result.Errors, zf.Name+": unsafe member name")
|
|
continue
|
|
}
|
|
if zf.FileInfo().IsDir() {
|
|
mkdirMember(join(rel))
|
|
continue
|
|
}
|
|
memberReader, openErr := zf.Open()
|
|
if openErr != nil {
|
|
result.Errors = append(result.Errors, rel+": "+openErr.Error())
|
|
continue
|
|
}
|
|
writeMember(rel, memberReader, int64(zf.UncompressedSize64))
|
|
memberReader.Close()
|
|
}
|
|
|
|
w.Header().Set(s3_constants.SeaweedFSExtractedCount, strconv.Itoa(len(result.Extracted)))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(result)
|
|
}
|
|
|
|
// verifyArchiveChecksums compares the archive's supplied Content-Md5 and
|
|
// x-amz-checksum-* values against the bytes consumed while spooling. A nil
|
|
// hash means the client supplied nothing to check.
|
|
func verifyArchiveChecksums(expectedMd5 string, md5Hash hash.Hash, expectedChecksum string, checksumHash hash.Hash) s3err.ErrorCode {
|
|
if md5Hash != nil {
|
|
expected, err := base64.StdEncoding.DecodeString(expectedMd5)
|
|
if err != nil {
|
|
return s3err.ErrInvalidDigest
|
|
}
|
|
if sum := md5Hash.Sum(nil); !bytes.Equal(expected, sum) {
|
|
return s3err.ErrBadDigest
|
|
}
|
|
}
|
|
if checksumHash != nil && expectedChecksum != "" {
|
|
if sum := base64.StdEncoding.EncodeToString(checksumHash.Sum(nil)); expectedChecksum != sum {
|
|
return s3err.ErrBadDigest
|
|
}
|
|
}
|
|
return s3err.ErrNone
|
|
}
|
|
|
|
// scrubArchiveChecksums removes request checksums that describe the archive
|
|
// body; members are validated against their own bytes.
|
|
func scrubArchiveChecksums(r *http.Request) {
|
|
isChecksumHeader := func(k string) bool {
|
|
k = strings.ToLower(k)
|
|
return k == "content-md5" || k == "x-amz-trailer" || k == "x-amz-content-sha256" ||
|
|
strings.HasPrefix(k, "x-amz-checksum") || strings.HasPrefix(k, "x-amz-sdk-checksum")
|
|
}
|
|
for k := range r.Header {
|
|
if isChecksumHeader(k) {
|
|
delete(r.Header, k)
|
|
}
|
|
}
|
|
query := r.URL.Query()
|
|
for k := range query {
|
|
if isChecksumHeader(k) {
|
|
query.Del(k)
|
|
}
|
|
}
|
|
r.URL.RawQuery = query.Encode()
|
|
}
|