From 6079a0ae210ea0648d4a9ca44eafe7df8f40db99 Mon Sep 17 00:00:00 2001 From: chrislu Date: Tue, 9 Dec 2025 00:36:41 -0800 Subject: Address code review feedback - Fix unsafe type assertions in GetObjectHandler and HeadObjectHandler when getting identity from context (properly handle type assertion failure) - Extract getConditionContextValue helper to eliminate duplicated logic between EvaluateConditions and EvaluateConditionsLegacy - Ensure consistent handling of missing condition keys (always return empty slice) --- weed/s3api/s3api_object_handlers.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'weed/s3api/s3api_object_handlers.go') diff --git a/weed/s3api/s3api_object_handlers.go b/weed/s3api/s3api_object_handlers.go index 034710c3c..e9145aca0 100644 --- a/weed/s3api/s3api_object_handlers.go +++ b/weed/s3api/s3api_object_handlers.go @@ -636,7 +636,17 @@ func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) // Re-check bucket policy with object entry for tag-based conditions (e.g., s3:ExistingObjectTag) if objectEntryForSSE != nil { - identity, _ := s3_constants.GetIdentityFromContext(r).(*Identity) + identityRaw := s3_constants.GetIdentityFromContext(r) + var identity *Identity + if identityRaw != nil { + var ok bool + identity, ok = identityRaw.(*Identity) + if !ok { + glog.Errorf("GetObjectHandler: unexpected identity type in context for %s/%s", bucket, object) + s3err.WriteErrorResponse(w, r, s3err.ErrInternalError) + return + } + } principal := buildPrincipalARN(identity) if errCode, _ := s3a.checkPolicyWithEntry(r, bucket, object, string(s3_constants.ACTION_READ), principal, objectEntryForSSE.Extended); errCode != s3err.ErrNone { s3err.WriteErrorResponse(w, r, errCode) @@ -2198,7 +2208,17 @@ func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request } // Re-check bucket policy with object entry for tag-based conditions (e.g., s3:ExistingObjectTag) - identity, _ := s3_constants.GetIdentityFromContext(r).(*Identity) + identityRaw := s3_constants.GetIdentityFromContext(r) + var identity *Identity + if identityRaw != nil { + var ok bool + identity, ok = identityRaw.(*Identity) + if !ok { + glog.Errorf("HeadObjectHandler: unexpected identity type in context for %s/%s", bucket, object) + s3err.WriteErrorResponse(w, r, s3err.ErrInternalError) + return + } + } principal := buildPrincipalARN(identity) if errCode, _ := s3a.checkPolicyWithEntry(r, bucket, object, string(s3_constants.ACTION_READ), principal, objectEntryForSSE.Extended); errCode != s3err.ErrNone { s3err.WriteErrorResponse(w, r, errCode) -- cgit v1.2.3