DEV Community

Dom Derrien
Dom Derrien

Posted on

Secure Code Review: Branch Protection and Automated Security Scanning

This is the third article in our series "From Chaos to Control: GitHub Rule Sets and Workflows for Safer AWS Deployments." In our previous articles, we explored why secure deployment pipelines matter and how to enforce quality through status checks. Now we tackle the critical human element: meaningful code reviews that complement your automated validation.


Picture this: your CI pipeline is green, all tests pass, security scans show no vulnerabilities, and the code deploys successfully. Three weeks later, you discover a critical business logic error that automated tools missed entirely. The function worked perfectly—it just solved the wrong problem.

This scenario highlights why human oversight remains irreplaceable in our automated world. While machines excel at catching syntax errors and known vulnerabilities, they struggle with context, business logic, and architectural decisions that could haunt your system for years.

Why Reviews Matter: Beyond Automation

Automated CI checks validate that your code builds, tests pass, and follows security patterns. But they can't catch:

  • Business logic errors: A function that works but solves the wrong problem
  • Architectural drift: Code that works but doesn't align with system design
  • Context-specific security: Patterns that are technically secure but inappropriate for your use case
  • Maintainability issues: Code that works today but will be painful to modify tomorrow

Here's how reviews integrate with your existing validation pipeline:

PR Creation & Validation Flow
┌─────────────────┐
│ PR Created      │
│        ↓        │
│ ✅ CI Checks    │ ← Automated: builds, tests, linting
│ ✅ Security     │ ← Automated: vulnerability scanning
│ ✅ Infra Diff   │ ← Automated: infrastructure preview
│        ↓        │
│ 👤 Code Review  │ ← Human: logic, architecture, context
│   Required      │
│        ↓        │
│ ✅ Approved     │
│        ↓        │
│ 🚀 Deploy       │
└─────────────────┘
Enter fullscreen mode Exit fullscreen mode

Review Requirements Rule Set

The GitHub Rule Set below enforces human approval while maintaining flexibility for emergency situations:

{
  "name": "Review Requirements",
  "target": "branch",
  "enforcement": "active",
  "conditions": {
    "ref_name": {
      "include": ["refs/heads/main", "refs/heads/develop"],
      "exclude": []
    }
  },
  "rules": [
    {
      "type": "pull_request",
      "parameters": {
        "required_approving_review_count": 1,
        "dismiss_stale_reviews_on_push": true,
        "require_code_owner_review": true,
        "require_last_push_approval": false,
        "required_review_thread_resolution": false,
        "automatic_copilot_code_review_enabled": false,
        "allowed_merge_methods": ["merge", "squash", "rebase"]
      }
    }
  ],
  "bypass_actors": [
    {
      "actor_type": "RepositoryRole",
      "actor_id": 5
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Key parameters explained:

  • required_approving_review_count: 1: Minimum one approval required
  • dismiss_stale_reviews_on_push: true: New commits invalidate previous approvals
  • require_code_owner_review: true: Specific owners must approve changes to their areas
  • require_last_push_approval: false: Allows minor fixes without re-approval
  • bypass_actors: Repository admins can override in emergencies

Strategic Code Ownership with CODEOWNERS

The CODEOWNERS file creates clear accountability while distributing review load efficiently. Here's a strategic approach:

# CODEOWNERS - Strategic ownership for efficient reviews

# Global fallback - ensures no code goes unreviewed
* @admin-1 @admin-2

# Infrastructure & DevOps - high-impact, specialized knowledge required
/.github @devops-1 @admin-1
/iac/ @devops-1 @admin-1

# Backend Services - business logic and data handling
/serverless/ @backend-1 @backend-2
/interfaces/ @backend-1 @frontend-1  # Shared contracts need both perspectives

# Frontend - user experience and client-side logic
/webapp/ @frontend-1 @frontend-2
/cdn/ @frontend-1 @devops-1          # Static assets need deployment knowledge

# Security-sensitive areas - multiple eyes required
/iac/security/ @devops-1 @security-lead @admin-1
/serverless/auth/ @backend-1 @security-lead
Enter fullscreen mode Exit fullscreen mode

This setup ensures that infrastructure changes get specialized review while maintaining flexibility for team evolution. The commented entries allow you to gradually assign ownership as your team grows and roles become more defined.

Review Workflow Integration

Review Process Flow
┌─────────────────┐
│ PR Submitted    │
│        ↓        │
│ ⏱️ CI Running   │ ← Reviewer can start early review
│        ↓        │
│ ✅ CI Passed    │ ← Automated checks complete
│        ↓        │
│ 🔍 Code Review  │ ← Focused human review
│   • Logic       │
│   • Architecture│
│   • Security    │
│   • Maintenance │
│        ↓        │
│ ✅ Approved     │
│        ↓        │
│ 🔄 Merge        │ ← Triggers deployment
└─────────────────┘
Enter fullscreen mode Exit fullscreen mode

Balancing Speed and Thoroughness

Repository administrators can bypass reviews in genuine emergencies using the bypass_actors configuration. This should be used sparingly and documented properly:

  1. Use the bypass capability only for critical fixes
  2. Document the reason in the merge commit
  3. Create a follow-up PR for proper review
  4. Conduct post-incident review of the bypass decision

Implementation Checklist

  • [ ] Deploy the Review Requirements rule set
  • [ ] Create comprehensive CODEOWNERS file
  • [ ] Train team on review focus areas
  • [ ] Establish review time expectations
  • [ ] Document emergency bypass procedures
  • [ ] Set up review assignment automation
  • [ ] Monitor review bottlenecks and adjust ownership

What's Next

With review gates protecting your main branches, you've created a solid foundation for secure deployments. But what happens when external contributors want to help? In our next article, we'll tackle one of the most challenging aspects of secure CI/CD: handling forked workflows safely.

When someone forks your repository and submits a PR, they shouldn't have access to your AWS credentials or be able to modify your infrastructure. Yet you still want to preview their changes safely. We'll explore how to build trust boundaries that allow collaboration without compromising security.

In the next article: "The Trust Challenge: Safe Infrastructure Previews in Forked Workflows"

Top comments (0)