@@ -146,18 +146,31 @@ async function _is_object_version_fit(req, predicate, value) {
146
146
return res ;
147
147
}
148
148
149
- async function has_bucket_policy_permission ( policy , account , method , arn_path , req ) {
149
+ async function has_bucket_policy_permission ( policy , account , method , arn_path , req , account_identifier_type = 'id' ) {
150
150
const [ allow_statements , deny_statements ] = _ . partition ( policy . Statement , statement => statement . Effect === 'Allow' ) ;
151
151
152
152
// the case where the permission is an array started in op get_object_attributes
153
153
const method_arr = Array . isArray ( method ) ? method : [ method ] ;
154
154
155
155
// look for explicit denies
156
- const res_arr_deny = await is_statement_fit_of_method_array ( deny_statements , account , method_arr , arn_path , req ) ;
156
+ const res_arr_deny = await is_statement_fit_of_method_array ( deny_statements ,
157
+ account ,
158
+ method_arr ,
159
+ arn_path ,
160
+ req ,
161
+ account_identifier_type
162
+ ) ;
157
163
if ( res_arr_deny . every ( item => item ) ) return 'DENY' ;
158
164
159
165
// look for explicit allows
160
- const res_arr_allow = await is_statement_fit_of_method_array ( allow_statements , account , method_arr , arn_path , req ) ;
166
+ const res_arr_allow = await is_statement_fit_of_method_array (
167
+ allow_statements ,
168
+ account ,
169
+ method_arr ,
170
+ arn_path ,
171
+ req ,
172
+ account_identifier_type
173
+ ) ;
161
174
if ( res_arr_allow . every ( item => item ) ) return 'ALLOW' ;
162
175
163
176
// implicit deny
@@ -177,9 +190,8 @@ function _is_action_fit(method, statement) {
177
190
return statement . Action ? action_fit : ! action_fit ;
178
191
}
179
192
180
- function _is_principal_fit ( account , statement ) {
193
+ function _is_principal_fit ( account , statement , account_identifier_type ) {
181
194
let statement_principal = statement . Principal || statement . NotPrincipal ;
182
-
183
195
let principal_fit = false ;
184
196
statement_principal = statement_principal . AWS ? statement_principal . AWS : statement_principal ;
185
197
for ( const principal of _ . flatten ( [ statement_principal ] ) ) {
@@ -189,7 +201,23 @@ function _is_principal_fit(account, statement) {
189
201
break ;
190
202
}
191
203
}
192
- return statement . Principal ? principal_fit : ! principal_fit ;
204
+
205
+ if ( statement . NotPrincipal ) {
206
+ // If the account is categorized as "Not Principal," we need to ALLOW operations
207
+ // for that account under the associated principal.
208
+ //
209
+ // To maintain backward compatibility, we also support name-based comparison for principal matching.
210
+ //
211
+ // - If `account_identifier_type` is "id" but the `account` parameter contains a name,
212
+ // we return `false` to proceed/bypass to the next step, where we check for a name match.
213
+ //
214
+ // - In the next step, if `account_identifier_type` is "name" and the `account` also contains a name,
215
+ // we return the actual value.
216
+
217
+ return account_identifier_type === 'id' ? principal_fit : ! principal_fit ;
218
+ }
219
+
220
+ return principal_fit ;
193
221
}
194
222
195
223
function _is_resource_fit ( arn_path , statement ) {
@@ -207,15 +235,15 @@ function _is_resource_fit(arn_path, statement) {
207
235
return statement . Resource ? resource_fit : ! resource_fit ;
208
236
}
209
237
210
- async function is_statement_fit_of_method_array ( statements , account , method_arr , arn_path , req ) {
238
+ async function is_statement_fit_of_method_array ( statements , account , method_arr , arn_path , req , account_identifier_type ) {
211
239
return Promise . all ( method_arr . map ( method_permission =>
212
- _is_statements_fit ( statements , account , method_permission , arn_path , req ) ) ) ;
240
+ _is_statements_fit ( statements , account , method_permission , arn_path , req , account_identifier_type ) ) ) ;
213
241
}
214
242
215
- async function _is_statements_fit ( statements , account , method , arn_path , req ) {
243
+ async function _is_statements_fit ( statements , account , method , arn_path , req , account_identifier_type ) {
216
244
for ( const statement of statements ) {
217
245
const action_fit = _is_action_fit ( method , statement ) ;
218
- const principal_fit = _is_principal_fit ( account , statement ) ;
246
+ const principal_fit = _is_principal_fit ( account , statement , account_identifier_type ) ;
219
247
const resource_fit = _is_resource_fit ( arn_path , statement ) ;
220
248
const condition_fit = await _is_condition_fit ( statement , req , method ) ;
221
249
0 commit comments