Skip to content
intermediate Phase 72 · Security Advanced

Security Patches

45m
1 problems
Topic Progress 0%

Patch Installation Methods

Composer Patches

# Install security patch via composer
composer require magento/security-patches:1.0.0

# Apply specific patch
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy

Manual Patch Application

# Download patch from Magento Security Center
wget https://magento.com/patches/MAGETO-2024-001.patch

# Apply patch
git apply MAGETO-2024-001.patch

# Or with rejection handling
git apply --reject MAGETO-2024-001.patch

# Verify patch applied
git status

Using cweagans/composer-patches

// composer.json
{
    "require": {
        "cweagans/composer-patches": "^1.7"
    },
    "extra": {
        "patches": {
            "magento/module-customer": {
                "MAGETO-2024-001": "patches/MAGETO-2024-001.patch"
            }
        }
    }
}

Key Points

  • Always backup before applying patches
  • Test patches in staging environment first
  • Use version control to track patch applications
  • Document which patches are applied

Security Bulletin Monitoring

Magento Security Center

// Subscribe to security alerts
// https:// magento.com/security

// Check current version
bin/magento --version

// Check for available updates
composer show magento/* --format=json | jq '.[].name'

Security Bulletin RSS

// Monitor security bulletins programmatically
class SecurityBulletinMonitor
{
    private $feedUrl = 'https://magento.com/security/feed';

    public function checkBulletins()
    {
        $feed = $this->fetchFeed($this->feedUrl);
        
        foreach ($feed['items'] as $item) {
            if ($this->isApplicableToCurrentVersion($item['title'])) {
                $this->alert($item);
            }
        }
    }

    private function isApplicableToCurrentVersion($bulletin)
    {
        $currentVersion = $this->getCurrentVersion();
        return strpos($bulletin, $currentVersion) !== false;
    }
}

Automated Update Checks

# Cron job for update checks
0 0 * * * cd /var/www/magento && bin/magento setup:check

# Composer update check
composer outdated magento/*

Key Points

  • Subscribe to Magento security announcements
  • Monitor CVSS scores for severity
  • Prioritize critical vulnerabilities (CVSS >= 9.0)
  • Document bulletin tracking process

Update Strategy

Staged Rollout Process

# .gitlab-ci.yml / .github/workflows/update.yml
stages:
  - test
  - staging
  - production

security-patch:
  stage: test
  script:
    - composer require magento/security-patches:1.0.0
    - bin/magento setup:upgrade
    - bin/magento setup:di:compile
    - php bin/magento module:enable --all
    - phpunit tests/
  only:
    - security-patches

deploy-staging:
  stage: staging
  script:
    - bin/magento setup:upgrade
    - bin/magento cache:clean
  environment:
    name: staging
  only:
    - security-patches

deploy-production:
  stage: production
  script:
    - bin/magento setup:upgrade
    - bin/magento cache:clean
  environment:
    name: production
  when: manual
  only:
    - security-patches

Rollback Plan

// Rollback procedure
public function rollback($patchVersion)
{
    // 1. Put site in maintenance mode
    exec('bin/magento maintenance:enable');
    
    // 2. Restore from backup
    exec('git checkout ' . $patchVersion);
    
    // 3. Run setup
    exec('bin/magento setup:upgrade');
    exec('bin/magento setup:di:compile');
    exec('bin/magento setup:static-content:deploy');
    
    // 4. Clear cache
    exec('bin/magento cache:clean');
    
    // 5. Disable maintenance mode
    exec('bin/magento maintenance:disable');
}

Key Points

  • Test patches in staging before production
  • Have rollback plan ready
  • Schedule patches during low-traffic periods
  • Monitor after deployment for issues

Testing Patches

Automated Testing

// Run full test suite after patch
use PHPUnit\Framework\TestCase;

class PatchVerificationTest extends TestCase
{
    public function testSecurityPatchApplied()
    {
        $composer = json_decode(file_get_contents('composer.json'), true);
        $patchVersion = $composer['require']['magento/security-patches'] ?? null;
        
        $this->assertNotNull($patchVersion, 'Security patch not installed');
        $this->assertStringStartsWith('1.0.', $patchVersion);
    }

    public function testVulnerabilityPatched()
    {
        // Test that specific vulnerability is fixed
        $response = $this->sendMaliciousRequest();
        $this->assertEquals(403, $response->getStatusCode());
    }
}

Manual Verification

# Verify patch files
find . -name "*.patch" -exec echo {} \;

# Check modified files
git diff --name-only HEAD~1

# Verify database schema
grep -r "ALTER TABLE" var/log/

# Run security scanner
bin/magento security:check

Security Scanning

// Post-patch security scan
class PostPatchScan
{
    public function scan()
    {
        $checks = [
            'file_permissions' => $this->checkFilePermissions(),
            'known_vulnerabilities' => $this->checkVulnerabilities(),
            'configuration' => $this->checkSecurityConfig(),
        ];

        return $checks;
    }
}

Key Points

  • Run full test suite after patching
  • Verify file permissions haven't changed
  • Check for new security warnings
  • Document verification results

Practice Problems

0 / 1 solved
Patch Management Script

Create a script that automates the patch application process including backup, testing, and deployment.

Solution
#!/bin/bash
set -e

BACKUP_DIR="/backups/$(date +%Y%m%d)"
PATCH_FILE=$1

if [ -z "$PATCH_FILE" ]; then
    echo "Usage: $0 <patch-file>"
    exit 1
fi

# Step 1: Backup
echo "Creating backup..."
mkdir -p $BACKUP_DIR
cp -r . $BACKUP_DIR/
db-dump > $BACKUP_DIR/database.sql

# Step 2: Apply patch
echo "Applying patch..."
git apply $PATCH_FILE

# Step 3: Test
echo "Running tests..."
php bin/magento setup:upgrade
php bin/magento setup:di:compile
phpunit tests/

if [ $? -ne 0 ]; then
    echo "Tests failed, rolling back..."
    cd $BACKUP_DIR
    cp -r . /var/www/magento/
    mysql magento < $BACKUP_DIR/database.sql
    exit 1
fi

# Step 4: Deploy
echo "Deploying to production..."
php bin/magento cache:clean
echo "Patch applied successfully!"

Quiz

1. What should you do BEFORE applying a security patch?

Question 1 options

2. What CVSS score indicates a critical vulnerability?

Question 2 options

3. What is the recommended patch deployment schedule?

Question 3 options

4. What command enables maintenance mode?

Question 4 options

Flashcards

Question

Patch testing order?

Answer

Staging → Production, always test first

Question

Critical CVSS score?

Answer

9-10, requires immediate attention

Question

Maintenance mode command?

Answer

bin/magento maintenance:enable

Question

Security bulletin monitoring?

Answer

Subscribe to Magento security announcements

Revision Notes

Key Takeaways

  • 1. Always test patches in staging before production
  • 2. Monitor Magento security bulletins for critical updates
  • 3. Have a rollback plan before applying patches
  • 4. Deploy during low-traffic periods

Interview Tips

  • Explain the patch testing workflow
  • Discuss how to monitor for security updates
  • Know rollback procedures

Cheat Sheet

Security Patches

  • Test: staging first
  • Monitor: security bulletins
  • Deploy: low-traffic periods
  • Always: backup + rollback plan
  • CVSS 9-10: critical, patch immediately