luizmachado.dev

PT EN

How to mitigate attacks on code agents in AWS

(e.g.: "Rules File Backdoor", which affects Copilot/Cursor)

AI-assisted code generation tools — Copilot, Cursor, Amazon Q, custom LLM agents, etc. — boost productivity but introduce a new attack surface: the agent itself can be "steered" into inserting backdoors or executing unwanted commands. In March 2025, researchers demonstrated the Rules File Backdoor: a single ".rules" file (with invisible Unicode characters) in the repository was enough to induce Copilot/Cursor to generate malicious code and, in certain IDEs, even execute it automatically.

Here are some best practices to avoid this on AWS, aiming to prevent, detect and respond to this type of attack within AWS.

---

1 • Verifying the layers

| Action | How to do it on AWS |

|--------|---------------------|

| Commit signing & mandatory review | • Enable branch protection rules in CodeCommit or GitHub. • Require two human reviewers for "dot" files (.rules, .vscode, .copilot) and CI/CD. |

| Dependency and secrets scanning | • Amazon CodeWhisperer security scans • Amazon Inspector (ECR / Lambda) + OSS tools (Trivy, Checkov). |

| SAST/IAST before merge | • AWS CodeBuild + CodeGuru Reviewer to detect insecure APIs generated by the agent. |

| CODEOWNERS rules | Assign mandatory owners for directories containing agent configurations (e.g. ".copilot/"). |

---

2 • Confining the agent

  1. Isolated environment per account

- Use a separate Dev Account in AWS Organizations.

- Apply read-only policies to repositories and artifacts; never use production keys.

  1. IAM always enforcing least privilege

- Create an exclusive role for the agent without write permissions to critical S3, CloudFormation or IAM.

- Block sensitive actions with Service Control Policies (SCPs). E.g.: deny iam:PutRolePolicy, s3:DeleteBucket.

  1. Network and runtime sandbox

- Run the agent/IDE in Cloud9, EC2 or a Fargate container in a private subnet.

- Allow outbound traffic only to required endpoints (Git, CodeArtifact).

- Use SSM (Systems Manager) Session Manager for auditable access, without SSH ports.

---

3 • Detecting malicious behavior

| Signal | Tool |

|--------|------|

| Automatic downloads or executions of unknown scripts | GuardDuty (EC2/Flow Logs) + VPC Traffic Mirroring for Suricata. |

| Pull requests containing obfuscated/invisible Unicode code | CodeGuru Reviewer & RegEx filters in CodeBuild. (Tip: reject if it contains invisible tags from the Unicode "Tags" block). |

| Surge of API calls that modify infrastructure | CloudTrail + CloudWatch Alarms; integrate findings into Security Hub. |

| Modifications to .rules, .editorconfig, .copilot | Lambda* triggered by CodeCommit/GitHub Webhook event → notifies Slack. |

---

4 • AI Guardrails

| Layer | Resource |

|-------|----------|

| Input | Validate prompts sent to the agent with Amazon Bedrock Guardrails or Lambda "lint" functions that remove dangerous commands (rm -rf /). |

| Output | Use the CodeWhisperer or CodeGuru Explainability API to comment on suggested diffs and flag insecurities. |

| Execution policies | In CodeCatalyst/CodePipeline, enable mandatory manual approval when the build contains AI-generated code (> xx% of changed lines). |

---

5 • Backups and resilience against repository encryption

  • CodeCommit: enable CloudWatch Events → external snapshot (S3 Object Lock + versioning).
  • EBS / EFS: immutable policies via AWS Backup with cross-account copy.
  • S3 Artifacts: enable versioning + Object Lock (Governance mode) to prevent overwrites.

---

6 • Rapid response

  1. Automatic quarantine

- EventBridge ➜ Lambda ➜ removes the agent's role and tags the EC2 instance with Quarantine=true.

  1. Code rollback

- Use automated git revert via pipeline; restore the latest immutable artifact version.

  1. Forensics

- Create an EBS snapshot; export SSM & CloudTrail logs; analyze the malicious rule file.

  1. Credential rotation

- Systems Manager ➜ secrets rotation for CI/CD tokens and CodeArtifact keys.

---

7 • Quick checklist

  • Separate AWS Account + SCP.
  • Agent-specific roles with MFA condition (aws:MultiFactorAuthPresent).
  • Repositories with branch protection and CODEOWNERS.
  • Pipeline: CodeGuru + Inspector + OSS SAST.
  • GuardDuty & Security Hub enabled.
  • Immutable backups in another account/region.
  • Tested incident playbook (game day).

---

Conclusion:

Attacks like the "Rules File Backdoor" show that the code agent must be treated as part of the attack surface. Mitigation combines supply-chain security, IAM governance, sandboxing, real-time detection and automated response. By applying the layers above, you drastically reduce the chance that a compromised agent reaches your AWS environments or silently injects vulnerabilities into your software.