@@ -11,6 +11,7 @@ const GoogleStorage = require('../../util/google_storage_wrap');
1111
1212const P = require ( '../../util/promise' ) ;
1313const dbg = require ( '../../util/debug_module' ) ( __filename ) ;
14+ const SensitiveString = require ( '../../util/sensitive_string' ) ;
1415const config = require ( '../../../config' ) ;
1516const MDStore = require ( '../object_services/md_store' ) . MDStore ;
1617const BucketStatsStore = require ( '../analytic_services/bucket_stats_store' ) . BucketStatsStore ;
@@ -512,12 +513,38 @@ async function get_bucket_policy(req) {
512513 } ;
513514}
514515
516+ /*
517+ validate ARN principle
518+ 1. validate basic ARN, like arn prefix `arn:aws:iam::`
519+ 2. If principal ARN end with `root sufix its a account and get account with id eg : aws:arn:${account_id}:root
520+ 3. if principle ARN contains `user` its a IAM user and get account with username and id
521+ eg : aws:arn:${account_id}:user/${iam_path}/${use_name}
522+ account email = ${iam_user_name}:${account_id}
523+ */
524+ async function principal_validation_handler ( principal ) {
525+ const principal_as_string = principal instanceof SensitiveString ? principal . unwrap ( ) : principal ;
526+ const arn_prefix = 'arn:aws:iam::' ;
527+ const root_sufix = 'root' ;
528+ const user_sufix = 'user' ;
529+ const arn_parts = principal_as_string . split ( ':' ) ;
530+ if ( ! principal_as_string . startsWith ( arn_prefix ) || arn_parts . length < 6 ) {
531+ return ;
532+ }
533+ const account_id = arn_parts [ 4 ] ;
534+ if ( principal_as_string . endsWith ( root_sufix ) ) {
535+ return system_store . data . accounts . find ( account => account . _id . toString ( ) === account_id ) ;
536+ } else if ( principal_as_string . includes ( user_sufix ) ) {
537+ const arn_path_parts = principal_as_string . split ( '/' ) ;
538+ const iam_user_name = arn_path_parts [ arn_path_parts . length - 1 ] ;
539+ return system_store . get_account_by_email ( new SensitiveString ( `${ iam_user_name } :${ account_id } ` ) ) ;
540+ }
541+ }
515542
516543async function put_bucket_policy ( req ) {
517544 dbg . log0 ( 'put_bucket_policy:' , req . rpc_params ) ;
518545 const bucket = find_bucket ( req , req . rpc_params . name ) ;
519546 await bucket_policy_utils . validate_s3_policy ( req . rpc_params . policy , bucket . name ,
520- principal => system_store . get_account_by_email ( principal ) ) ;
547+ principal => principal_validation_handler ( principal ) ) ;
521548
522549 if (
523550 bucket . public_access_block ?. block_public_policy &&
0 commit comments