Restrict cors and access policies - #64
Conversation
…64 directly and fixing deployment
…ssions to specific resources and actions. Update environment variable for Amplify branch.
There was a problem hiding this comment.
Pull Request Overview
This PR enhances security and fixes configuration issues in the AWS CDK infrastructure for a job search application. The changes implement least-privilege IAM permissions, fix CORS configuration to use dynamic Amplify app URLs, resolve environment variable issues, and correct typos in the codebase.
- Replaces wildcard IAM permissions with scoped resource-specific policies for all AWS services
- Moves Amplify app creation earlier in the stack to properly configure CORS with the actual app URL
- Removes unused
aws-lambdapackage dependency and cleans up stale JavaScript build artifacts - Fixes typos in agent prompts and removes unnecessary
await responseStream.finished()calls
Reviewed Changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/package.json | Adds aws-lambda package dependency |
| backend/package-lock.json | Updates lock file with aws-lambda and transitive dependencies, removes dev-only flags from shared dependencies |
| backend/lib/backend-stack.ts | Moves Amplify app creation earlier, replaces wildcard IAM policies with scoped permissions, fixes CORS and environment variables |
| backend/lib/backend-stack.js | Generated JavaScript output from TypeScript compilation |
| backend/lambda/agent-proxy/index.js | Removes unnecessary await responseStream.finished() calls and trailing whitespace |
| backend/bin/backend.js | Generated JavaScript output from TypeScript compilation |
| backend/JobSearchAgent/StrandsAgents.py | Fixes typos in job search prompt instructions |
Files not reviewed (1)
- backend/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "@aws-cdk/aws-amplify-alpha": "^2.216.0-alpha.0", | ||
| "@cdklabs/generative-ai-cdk-constructs": "^0.1.309", | ||
| "aws-cdk-lib": "^2.216.0", | ||
| "aws-lambda": "^1.0.7", |
There was a problem hiding this comment.
The aws-lambda package appears to be unused in the codebase. This package is typically for running Lambda functions locally, but the project uses AWS CDK with direct Lambda deployment. Consider removing this dependency as it adds unnecessary transitive dependencies (aws-sdk v2, commander, js-yaml, watchpack) to the production bundle.
| "aws-lambda": "^1.0.7", |
| actions: ["bedrock-agentcore:InvokeAgentRuntime"], | ||
| resources: ["*"], | ||
| resources: [ | ||
| `arn:aws:bedrock-agentcore:${aws_region}:${this.account}:runtime/*` |
There was a problem hiding this comment.
The IAM policy resource ARN pattern appears incorrect. Based on the action bedrock-agentcore:InvokeAgentRuntime, the correct resource pattern should likely be arn:aws:bedrock-agentcore:${aws_region}:${this.account}:agent/* or arn:aws:bedrock-agentcore:${aws_region}:${this.account}:agent-runtime/*, not runtime/*. This may cause authorization failures when invoking the agent.
| `arn:aws:bedrock-agentcore:${aws_region}:${this.account}:runtime/*` | |
| `arn:aws:bedrock-agentcore:${aws_region}:${this.account}:agent-runtime/*` |
| conditions: { | ||
| StringEquals: { | ||
| "cloudwatch:namespace": "AWS/Bedrock" | ||
| } | ||
| } |
There was a problem hiding this comment.
The IAM policy condition uses conditions as the key, but the correct AWS IAM policy syntax requires Condition (capital C). This will likely cause the policy statement to fail validation or be ignored, allowing broader permissions than intended for CloudWatch metrics.
| @@ -0,0 +1,791 @@ | |||
| "use strict"; | |||
There was a problem hiding this comment.
This appears to be a compiled JavaScript file from TypeScript source. Compiled/generated files should typically be excluded from version control by adding them to .gitignore. Consider adding backend/lib/**/*.js and backend/bin/**/*.js to .gitignore to prevent committing build artifacts.
| effect: iam.Effect.ALLOW, | ||
| actions: ["bedrock-agentcore:InvokeAgentRuntime"], | ||
| resources: [ | ||
| `arn:aws:bedrock-agentcore:${aws_region}:${this.account}:agent/*`, |
There was a problem hiding this comment.
The IAM policy includes both agent/* and agent-runtime/* resource patterns for the bedrock-agentcore:InvokeAgentRuntime action. Verify which resource pattern is correct for this action to avoid granting broader permissions than necessary. The TypeScript version at line 425 only uses runtime/* pattern, creating an inconsistency between the two files.
| `arn:aws:bedrock-agentcore:${aws_region}:${this.account}:agent/*`, |
| conditions: { | ||
| StringEquals: { | ||
| "cloudwatch:namespace": "AWS/Bedrock" | ||
| } | ||
| } |
There was a problem hiding this comment.
The IAM policy condition uses lowercase conditions as the key, but AWS IAM policy syntax requires Condition (capital C). This will cause the policy statement to fail validation or be ignored, allowing broader permissions than intended for CloudWatch metrics.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
|
@aaravmat1209 I've opened a new pull request, #65, to work on those changes. Once the pull request is ready, I'll request review from you. |
…mproved security and maintainability; streamline access for logs, ECR, X-Ray, and CloudWatch.
…enhance security and simplify policy management.
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
Files not reviewed (1)
- backend/package-lock.json: Language not supported
Comments suppressed due to low confidence (1)
backend/lib/backend-stack.ts:1
- The trailing comma was added to this line, but this appears to be within a string literal (part of a prompt), not actual JSON code. Adding a trailing comma in JSON strings that will be parsed could cause parsing errors if the consumer expects strict JSON. Verify that the system consuming this prompt can handle trailing commas in JSON examples.
import * as cdk from "aws-cdk-lib";
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Add environment variables to Amplify branch | ||
| mainBranch.addEnvironment('REACT_APP_AGENT_QUALIFIER', 'DEFAULT'); | ||
| mainBranch.addEnvironment('REACT_APP_AGENT_RUNTIME_ARN', 'MANUALLY ADD HERE'); | ||
| mainBranch.addEnvironment('REACT_APP_AGENT_RUNTIME_ARN', 's ADD HERE'); |
There was a problem hiding this comment.
Corrupted text in environment variable placeholder. The value should be 'MANUALLY ADD HERE' to match the pattern used in line 523.
| mainBranch.addEnvironment('REACT_APP_AGENT_RUNTIME_ARN', 's ADD HERE'); | |
| mainBranch.addEnvironment('REACT_APP_AGENT_RUNTIME_ARN', 'MANUALLY ADD HERE'); |
| "sms-voice:SendTextMessage" | ||
| ], | ||
| resources: [ | ||
| `arn:aws:sms-voice:${aws_region}:${this.account}:phone-number/${senderNumber.replace('+', '')}`, |
There was a problem hiding this comment.
The replace('+', '') method only replaces the first occurrence of '+'. If the phone number contains multiple '+' characters (which is unlikely but possible), they won't be removed. Use replaceAll('+', '') or a regular expression .replace(/\+/g, '') to ensure all '+' characters are removed.
| `arn:aws:sms-voice:${aws_region}:${this.account}:phone-number/${senderNumber.replace('+', '')}`, | |
| `arn:aws:sms-voice:${aws_region}:${this.account}:phone-number/${senderNumber.replace(/\+/g, '')}`, |
No description provided.