Skip to content

Commit 57885e4

Browse files
sdstolworthyakshbhu
authored andcommitted
fix: null check for custom IAM policies Action array (#12346)
1 parent e5b2842 commit 57885e4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/amplify-cli-core/src/__tests__/customPoliciesUtils.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createDefaultCustomPoliciesFile, generateCustomPoliciesInTemplate } from '../customPoliciesUtils';
22
import { printer } from '@aws-amplify/amplify-prompts';
3-
import { JSONUtilities } from '..';
3+
import { AmplifyError, JSONUtilities } from '..';
44
import { pathManager, PathConstants, stateManager } from '../state-manager';
55
import path from 'path';
66
import { Template, Fn } from 'cloudform-types';
@@ -136,4 +136,17 @@ describe('Custom policies util test', () => {
136136

137137
expect(template.Resources?.CustomLambdaExecutionPolicy).toBeUndefined();
138138
});
139+
test('test generateCustomPoliciesInTemplate with empty action array', () => {
140+
(stateManager.getCustomPolicies as jest.Mock).mockReturnValueOnce([
141+
{
142+
Action: undefined,
143+
Resource: [],
144+
},
145+
]);
146+
try {
147+
generateCustomPoliciesInTemplate({}, 'lambdaResourceName', 'Lambda', 'function');
148+
} catch (e) {
149+
expect((e as AmplifyError).name).toBe('CustomPoliciesFormatError');
150+
}
151+
});
139152
});

packages/amplify-cli-core/src/customPoliciesUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function resourceHasCustomPolicies(customPolicies: CustomIAMPolicies): boolean {
163163
const customPolicy = _.first(customPolicies);
164164

165165
// if either there are no custom policies in the array or the defined policy is the default
166-
if (!customPolicy || (customPolicy && customPolicy.Action.length === 0 && customPolicy.Resource.length == 0)) {
166+
if (!customPolicy || (customPolicy && customPolicy.Action?.length === 0 && customPolicy.Resource.length == 0)) {
167167
return false;
168168
}
169169

0 commit comments

Comments
 (0)