@@ -495,6 +495,10 @@ function _prepare_auth_request(req) {
495495 req . has_bucket_action_permission = async function ( bucket , action , bucket_path , req_query ) {
496496 return has_bucket_action_permission ( bucket , req . account , action , req_query , bucket_path ) ;
497497 } ;
498+
499+ req . has_list_bucket_permission = function ( bucket ) {
500+ return has_list_bucket_permission ( bucket , req . account , req . system ) ;
501+ } ;
498502}
499503
500504function _get_auth_info ( account , system , authorized_by , role , extra ) {
@@ -519,6 +523,51 @@ function _get_auth_info(account, system, authorized_by, role, extra) {
519523 return response ;
520524}
521525
526+ /**
527+ * is_system_owner checks if the account is the system owner
528+ * @param {Record<string, any> } system
529+ * @param {Record<string, any> } account
530+ * @returns {boolean }
531+ */
532+ function is_system_owner ( system , account ) {
533+ return system ?. owner ?. email ?. unwrap ( ) === account ?. email ?. unwrap ( ) ;
534+ }
535+
536+ /**
537+ * is_bucket_owner checks if the account owns the bucket
538+ * @param {Record<string, any> } bucket
539+ * @param {Record<string, any> } account
540+ * @returns {boolean }
541+ */
542+ function is_bucket_owner ( bucket , account ) {
543+ // check direct ownership
544+ if ( bucket ?. owner_account ?. email ?. unwrap ( ) === account ?. email ?. unwrap ( ) ) return true ;
545+
546+ // check bucket claim ownership (OBC)
547+ return account ?. bucket_claim_owner ?. name ?. unwrap ( ) === bucket ?. name ?. unwrap ( ) ;
548+ }
549+
550+ /**
551+ * has_list_bucket_permission returns true if the account can list the bucket in ListBuckets operation
552+ * this is a synchronous function that only checks system ownership and bucket ownership
553+ *
554+ * @param {Record<string, any> } bucket
555+ * @param {Record<string, any> } account
556+ * @param {Record<string, any> } system
557+ * @returns {boolean }
558+ */
559+ function has_list_bucket_permission ( bucket , account , system ) {
560+ // system owner can list all the buckets
561+ if ( is_system_owner ( bucket . system , account ) ) return true ;
562+
563+ // bucket owner can list their buckets only
564+ if ( is_bucket_owner ( bucket , account ) ) return true ;
565+
566+ // TODO: Add IAM policy support for s3:ListAllMyBuckets action
567+
568+ return false ;
569+ }
570+
522571/**
523572 * has_bucket_action_permission returns true if the requesting account has permission to perform
524573 * the given action on the given bucket.
@@ -536,20 +585,16 @@ function _get_auth_info(account, system, authorized_by, role, extra) {
536585 * @returns {Promise<boolean> } true if the account has permission to perform the action on the bucket
537586 */
538587async function has_bucket_action_permission ( bucket , account , action , req_query , bucket_path = "" ) {
539- if ( ! account || ! account . email ) {
540- dbg . warn ( 'has_bucket_action_permission: account or account.email is missing' ) ;
541- return false ;
542- }
543588 dbg . log1 ( 'has_bucket_action_permission:' , bucket . name , account . email , bucket . owner_account . email ) ;
544589
545- // If the system owner account wants to access the bucket, allow it
546- if ( bucket . system . owner . email . unwrap ( ) === account . email . unwrap ( ) ) return true ;
590+ // system owner can access all buckets
591+ if ( is_system_owner ( bucket . system , account ) ) return true ;
547592
548- const is_owner = ( bucket . owner_account . email . unwrap ( ) === account . email . unwrap ( ) ) ||
549- ( account . bucket_claim_owner && account . bucket_claim_owner . name . unwrap ( ) === bucket . name . unwrap ( ) ) ;
593+ // check bucket ownership
594+ const owner = is_bucket_owner ( bucket , account ) ;
550595 const bucket_policy = bucket . s3_policy ;
551596
552- if ( ! bucket_policy ) return is_owner ;
597+ if ( ! bucket_policy ) return owner ;
553598 if ( ! action ) {
554599 throw new Error ( 'has_bucket_action_permission: action is required' ) ;
555600 }
@@ -563,9 +608,8 @@ async function has_bucket_action_permission(bucket, account, action, req_query,
563608 ) ;
564609
565610 if ( result === 'DENY' ) return false ;
566- if ( result === 'ALLOW' ) return true ;
567611
568- return is_owner ;
612+ return owner || result === 'ALLOW' ;
569613}
570614
571615/**
0 commit comments