@@ -79,13 +79,29 @@ function _length_max_check_input(max_length, input_value, parameter_name) {
7979 */
8080function _validate_json_policy_document ( input_policy_document ) {
8181 try {
82- JSON . parse ( input_policy_document ) ;
82+ return JSON . parse ( input_policy_document ) ;
8383 } catch ( error ) {
84- const message_with_details = `Syntax errors in policy` ;
84+ const message_with_details = `Syntax errors in policy. ` ;
8585 throw new RpcError ( 'MALFORMED_POLICY_DOCUMENT' , message_with_details ) ;
8686 }
8787}
8888
89+ /**
90+ * _validate_policy_document_iam_no_principal will validate the policy document basic structure
91+ * this function purpose is to create better error
92+ * @param {object } policy_document
93+ */
94+
95+ function _validate_policy_document_iam_no_principal ( policy_document ) {
96+ for ( const statement of policy_document . Statement ) {
97+ const statement_principal = statement . Principal || statement . NotPrincipal ;
98+ if ( statement_principal ) {
99+ const message_with_details = `Policy document should not specify a principal.` ;
100+ throw new RpcError ( 'MALFORMED_POLICY_DOCUMENT' , message_with_details ) ;
101+ }
102+ }
103+ }
104+
89105/**
90106 * validate_username will validate:
91107 * 1. type
@@ -175,7 +191,8 @@ function validate_policy_document(input_policy_document, parameter_name = iam_co
175191 throw new RpcError ( 'MALFORMED_POLICY_DOCUMENT' , message_with_details ) ;
176192 }
177193 // valid JSON check
178- _validate_json_policy_document ( input_policy_document ) ;
194+ const policy_document = _validate_json_policy_document ( input_policy_document ) ;
195+ _validate_policy_document_iam_no_principal ( policy_document ) ;
179196 return true ;
180197}
181198
0 commit comments