mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +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>
175 lines
4.6 KiB
Go
175 lines
4.6 KiB
Go
package s3api
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
var (
|
|
errRequestBodyTooLarge = errors.New("request body too large")
|
|
errContentSha256Mismatch = errors.New(s3err.ErrMsgContentSha256Mismatch)
|
|
errContentMd5Mismatch = errors.New("the Content-MD5 you specified did not match what was received")
|
|
)
|
|
|
|
func readRequestBody(r *http.Request, limit int64) ([]byte, error) {
|
|
if r.Body == nil {
|
|
return nil, nil
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(r.Body, limit+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int64(len(body)) > limit {
|
|
return nil, errRequestBodyTooLarge
|
|
}
|
|
return body, nil
|
|
}
|
|
|
|
// signedPayloadBody hands out a signed request body only after all of it has
|
|
// been read and matched against the payload hash the signature covered and
|
|
// any Content-MD5, so a handler that stops reading early never acts on
|
|
// unverified bytes.
|
|
type signedPayloadBody struct {
|
|
body io.ReadCloser
|
|
size int64
|
|
limit int64
|
|
sha256 []byte
|
|
md5 []byte
|
|
data *bytes.Reader
|
|
err error
|
|
}
|
|
|
|
func (b *signedPayloadBody) Read(p []byte) (int, error) {
|
|
if b.data == nil && b.err == nil {
|
|
b.data, b.err = b.load()
|
|
}
|
|
if b.err != nil {
|
|
return 0, b.err
|
|
}
|
|
return b.data.Read(p)
|
|
}
|
|
|
|
func (b *signedPayloadBody) load() (*bytes.Reader, error) {
|
|
if b.size > b.limit {
|
|
return nil, errRequestBodyTooLarge
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(b.body, b.limit+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int64(len(data)) > b.limit {
|
|
return nil, errRequestBodyTooLarge
|
|
}
|
|
if b.sha256 != nil {
|
|
if sum := sha256.Sum256(data); !bytes.Equal(sum[:], b.sha256) {
|
|
return nil, errContentSha256Mismatch
|
|
}
|
|
}
|
|
if b.md5 != nil {
|
|
if sum := md5.Sum(data); !bytes.Equal(sum[:], b.md5) {
|
|
return nil, errContentMd5Mismatch
|
|
}
|
|
}
|
|
return bytes.NewReader(data), nil
|
|
}
|
|
|
|
func (b *signedPayloadBody) Close() error {
|
|
return b.body.Close()
|
|
}
|
|
|
|
func (b *signedPayloadBody) wrap(body *io.ReadCloser) {
|
|
b.body = *body
|
|
*body = b
|
|
}
|
|
|
|
// unwrapSignedPayload puts back the raw body of a signedPayloadBody nothing has
|
|
// read yet, for readers that verify it as it streams, and returns the hash it
|
|
// was bound to.
|
|
func unwrapSignedPayload(body *io.ReadCloser) []byte {
|
|
if b, ok := (*body).(*signedPayloadBody); ok && b.data == nil && b.err == nil {
|
|
*body = b.body
|
|
return b.sha256
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// bindSignedPayload ties the request body to hashedPayload, the payload hash a
|
|
// V4 signature covered (UNSIGNED-PAYLOAD for V2), and to any Content-MD5.
|
|
// Uploads read through getRequestDataReader, which verifies the body as it
|
|
// streams instead.
|
|
func bindSignedPayload(r *http.Request, hashedPayload string) s3err.ErrorCode {
|
|
if bound, ok := r.Body.(*signedPayloadBody); ok {
|
|
if bound.sha256 != nil || bound.md5 != nil {
|
|
return s3err.ErrNone
|
|
}
|
|
// streamHashRequestBody buffered it and signed its hash; only
|
|
// Content-MD5 is left to check.
|
|
hashedPayload = unsignedPayload
|
|
}
|
|
switch hashedPayload {
|
|
case streamingContentSHA256, streamingContentSHA256Trailer, streamingUnsignedPayload:
|
|
return s3err.ErrNone
|
|
}
|
|
expected, valid := decodeContentSha256(hashedPayload)
|
|
hasBody := r.Body != nil && r.Body != http.NoBody && r.ContentLength != 0
|
|
if !valid {
|
|
if hasBody {
|
|
return s3err.ErrInvalidArgument
|
|
}
|
|
return s3err.ErrNone
|
|
}
|
|
var contentMd5 []byte
|
|
if _, present := r.Header["Content-Md5"]; present {
|
|
decoded, err := validateContentMd5(r.Header)
|
|
if err != nil || len(decoded) != md5.Size {
|
|
return s3err.ErrInvalidDigest
|
|
}
|
|
if !fips140Enforced() {
|
|
contentMd5 = decoded
|
|
}
|
|
}
|
|
if expected == nil && contentMd5 == nil {
|
|
return s3err.ErrNone
|
|
}
|
|
if !hasBody {
|
|
if emptySum := sha256.Sum256(nil); expected != nil && !bytes.Equal(expected, emptySum[:]) {
|
|
return s3err.ErrContentSHA256Mismatch
|
|
}
|
|
if contentMd5 != nil {
|
|
if emptySum := md5.Sum(nil); !bytes.Equal(contentMd5, emptySum[:]) {
|
|
return s3err.ErrBadDigest
|
|
}
|
|
}
|
|
return s3err.ErrNone
|
|
}
|
|
signed := &signedPayloadBody{
|
|
size: r.ContentLength,
|
|
limit: s3_constants.GetRequestBodyLimitFromContext(r, iamRequestBodyLimit),
|
|
sha256: expected,
|
|
md5: contentMd5,
|
|
}
|
|
signed.wrap(&r.Body)
|
|
return s3err.ErrNone
|
|
}
|
|
|
|
// requestBodyErrorCode names the check a failed body read tripped, or returns
|
|
// fallback for any other read error.
|
|
func requestBodyErrorCode(err error, fallback s3err.ErrorCode) s3err.ErrorCode {
|
|
switch {
|
|
case errors.Is(err, errContentSha256Mismatch):
|
|
return s3err.ErrContentSHA256Mismatch
|
|
case errors.Is(err, errContentMd5Mismatch):
|
|
return s3err.ErrBadDigest
|
|
case errors.Is(err, errRequestBodyTooLarge):
|
|
return s3err.ErrEntityTooLarge
|
|
}
|
|
return fallback
|
|
}
|