@@ -150,16 +150,26 @@ async function _is_object_version_fit(req, predicate, value) {
150150 return res ;
151151}
152152
153+ /**
154+ * has_bucket_policy_permission validate the bucket policy principal
155+ *
156+ * @param {object } policy
157+ * @param {string[] | string } account
158+ * @param {string[] | string } method
159+ * @param {string } arn_path
160+ * @param {object } req
161+ */
153162async function has_bucket_policy_permission ( policy , account , method , arn_path , req ,
154163 { disallow_public_access = false , should_pass_principal = true } = { } ) {
155164 const [ allow_statements , deny_statements ] = _ . partition ( policy . Statement , statement => statement . Effect === 'Allow' ) ;
156165
157166 // the case where the permission is an array started in op get_object_attributes
158167 const method_arr = Array . isArray ( method ) ? method : [ method ] ;
168+ const account_arr = Array . isArray ( account ) ? account : [ account ] ;
159169
160170 // look for explicit denies
161171 const res_arr_deny = await is_statement_fit_of_method_array (
162- deny_statements , account , method_arr , arn_path , req , {
172+ deny_statements , account_arr , method_arr , arn_path , req , {
163173 disallow_public_access : false , // No need to disallow in "DENY"
164174 should_pass_principal
165175 }
@@ -168,7 +178,7 @@ async function has_bucket_policy_permission(policy, account, method, arn_path, r
168178
169179 // look for explicit allows
170180 const res_arr_allow = await is_statement_fit_of_method_array (
171- allow_statements , account , method_arr , arn_path , req , {
181+ allow_statements , account_arr , method_arr , arn_path , req , {
172182 disallow_public_access,
173183 should_pass_principal
174184 } ) ;
@@ -191,14 +201,14 @@ function _is_action_fit(method, statement) {
191201 return statement . Action ? action_fit : ! action_fit ;
192202}
193203
194- function _is_principal_fit ( account , statement , ignore_public_principal = false ) {
204+ function _is_principal_fit ( account_arr , statement , ignore_public_principal = false ) {
195205 let statement_principal = statement . Principal || statement . NotPrincipal ;
196206
197207 let principal_fit = false ;
198208 statement_principal = statement_principal . AWS ? statement_principal . AWS : statement_principal ;
199209 for ( const principal of _ . flatten ( [ statement_principal ] ) ) {
200- dbg . log1 ( 'bucket_policy: ' , statement . Principal ? 'Principal' : 'NotPrincipal' , ' fit?' , principal , account ) ;
201- if ( ( principal . unwrap ( ) === '*' ) || ( principal . unwrap ( ) === account ) ) {
210+ dbg . log1 ( 'bucket_policy: ' , statement . Principal ? 'Principal' : 'NotPrincipal' , ' fit?' , principal , account_arr ) ;
211+ if ( ( principal . unwrap ( ) === '*' ) || account_arr . includes ( principal . unwrap ( ) ) ) {
202212 if ( ignore_public_principal && principal . unwrap ( ) === '*' && statement . Principal ) {
203213 // Ignore the "fit" if ignore_public_principal is requested
204214 continue ;
@@ -226,19 +236,19 @@ function _is_resource_fit(arn_path, statement) {
226236 return statement . Resource ? resource_fit : ! resource_fit ;
227237}
228238
229- async function is_statement_fit_of_method_array ( statements , account , method_arr , arn_path , req ,
239+ async function is_statement_fit_of_method_array ( statements , account_arr , method_arr , arn_path , req ,
230240 { disallow_public_access = false , should_pass_principal = true } = { } ) {
231241 return Promise . all ( method_arr . map ( method_permission =>
232- _is_statements_fit ( statements , account , method_permission , arn_path , req , { disallow_public_access, should_pass_principal } ) ) ) ;
242+ _is_statements_fit ( statements , account_arr , method_permission , arn_path , req , { disallow_public_access, should_pass_principal } ) ) ) ;
233243}
234244
235- async function _is_statements_fit ( statements , account , method , arn_path , req ,
245+ async function _is_statements_fit ( statements , account_arr , method , arn_path , req ,
236246 { disallow_public_access = false , should_pass_principal = true } = { } ) {
237247 for ( const statement of statements ) {
238248 const action_fit = _is_action_fit ( method , statement ) ;
239249 // When evaluating IAM user inline policies, should_pass_principal is false since these policies
240250 // don't have a Principal field (the principal is implicitly the user)
241- const principal_fit = should_pass_principal ? _is_principal_fit ( account , statement , disallow_public_access ) : true ;
251+ const principal_fit = should_pass_principal ? _is_principal_fit ( account_arr , statement , disallow_public_access ) : true ;
242252 const resource_fit = _is_resource_fit ( arn_path , statement ) ;
243253 const condition_fit = await _is_condition_fit ( statement , req , method ) ;
244254
0 commit comments