Category: Security

Security is the dedicated cybersecurity category on orthogonal.info, covering everything from application-level secure coding practices to network-layer defenses and zero-trust architecture. In an era where a single misconfigured cloud bucket or unpatched dependency can lead to a headline-making breach, this category provides the practical, hands-on guidance that engineers need to build and maintain secure systems. Each article blends defensive theory with real commands, configurations, and code you can apply immediately.

With 21 posts spanning offensive and defensive security topics, this collection reflects a practitioner’s perspective — not checkbox compliance, but genuine risk reduction.

Key Topics Covered

Application security (AppSec) — Secure coding patterns, input validation, OWASP Top 10 mitigations, and static analysis with tools like Semgrep, Bandit, and CodeQL.
Network security and firewalls — Configuring OPNsense, pfSense, VLANs, WireGuard tunnels, and network segmentation strategies for home and production environments.
CVE analysis and vulnerability management — Dissecting real-world CVEs, understanding CVSS scoring, and building patch management workflows with Trivy, Grype, and OSV-Scanner.
Penetration testing and red teaming — Practical walkthroughs using Nmap, Burp Suite, Nuclei, and Metasploit to identify weaknesses before attackers do.
Zero-trust architecture — Implementing identity-aware proxies, mutual TLS, and least-privilege access using Cloudflare Access, Tailscale, and SPIFFE/SPIRE.
Container and Kubernetes security — Pod security standards, image scanning, runtime protection with Falco, and supply-chain security with Sigstore and cosign.
Secrets management — Storing and rotating secrets with HashiCorp Vault, SOPS, Sealed Secrets, and cloud-native key management services.
Compliance and hardening — CIS Benchmarks, STIGs, and automated compliance scanning for Linux hosts, containers, and cloud accounts.

Who This Content Is For
This category serves security engineers, DevSecOps practitioners, penetration testers, platform engineers, and system administrators who take security seriously without wanting to drown in vendor marketing. Whether you are hardening a homelab, preparing for a SOC 2 audit, or building a secure CI/CD pipeline, the guides here are written by and for people who ship code and defend infrastructure daily.

What You Will Learn
Readers of the Security category will gain the skills to identify and remediate vulnerabilities across the full stack — from source code to running containers to network perimeters. You will learn how to integrate security scanning into CI/CD pipelines, configure firewalls with defense-in-depth principles, analyze CVE disclosures to assess real-world impact, and implement zero-trust networking without crippling developer velocity. Every article prioritizes actionable steps over abstract theory.

Explore the posts below to strengthen your security posture today.

  • Zero Trust for Developers: Secure Systems by Design

    Zero Trust for Developers: Secure Systems by Design

    Why Zero Trust is Non-Negotiable for Developers

    📌 TL;DR: Why Zero Trust is Non-Negotiable for Developers Picture this: It’s a late Friday afternoon, and you’re prepping for the weekend when an alert comes through. An internal service has accessed sensitive customer data without authorization.
    🎯 Quick Answer: Zero Trust architecture requires developers to authenticate and authorize every service call, even internal ones. Implement mutual TLS between services, validate every API request with short-lived tokens, and enforce least-privilege access at the code level—never trust the network perimeter alone.

    I adopted Zero Trust principles after discovering that a misconfigured service account in my Kubernetes cluster had been silently accessing data it shouldn’t have—for weeks. As a security engineer running production infrastructure and trading systems, I learned the hard way that implicit trust is a vulnerability. Here’s how to build Zero Trust into your code from the start.

    Zero Trust Fundamentals Every Developer Should Know

    🔍 From production: A service in my cluster was using a shared service account token to access both a public API and an internal database. When I applied least-privilege Zero Trust policies, I discovered that service had been making database queries it never needed—leftover from a feature that was removed 6 months ago. Removing that access closed an attack surface I didn’t know existed.

    At its heart, Zero Trust operates on one core principle: “Never trust, always verify.” This means that no user, device, or application is trusted by default—not even those inside the network. Every access request must be authenticated, authorized, and continuously validated.

    Key Principles of Zero Trust

    • Least Privilege Access: Grant only the minimum permissions necessary for a task. For example, a service responsible for reading data from a database should not have write or delete permissions.
    • Micro-Segmentation: Break down your application into isolated components or zones. This limits the blast radius of potential breaches.
    • Continuous Monitoring: Access and behavior should be continuously monitored. Anomalies—such as a service suddenly requesting access to sensitive data—should trigger alerts or automated actions.
    • Identity-Centric Security: Verify both user and machine identities. Use strong authentication mechanisms like OAuth2, SAML, or OpenID Connect.
    Warning: Default configurations in many tools and platforms are overly permissive and violate Zero Trust principles. Always review and customize these settings before deployment.

    Zero Trust in Action: Real-World Example

    Imagine a microservices-based application where one service handles authentication and another handles user data. Here’s how Zero Trust can be applied:

    // Example: Token-based authentication in a Node.js API
    const express = require('express');
    const jwt = require('jsonwebtoken');
    const app = express();
    
    function authenticateToken(req, res, next) {
     const token = req.headers['authorization'];
     if (!token) return res.status(401).json({ message: 'Access denied' });
    
     jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
     if (err) return res.status(403).json({ message: 'Invalid token' });
     req.user = user;
     next();
     });
    }
    
    app.get('/user-data', authenticateToken, (req, res) => {
     if (!req.user.permissions.includes('read:user_data')) {
     return res.status(403).json({ message: 'Insufficient permissions' });
     }
     res.json({ message: 'Secure user data' });
    });
    

    In this example, every request to the /user-data endpoint is authenticated and authorized. Tokens are verified against a secret key, and user permissions are checked before granting access.

    Making Zero Trust Developer-Friendly

    Let’s be honest: developers are already juggling tight deadlines, feature requests, and bug fixes. Adding security to the mix can feel overwhelming. The key to successful Zero Trust implementation is to integrate it smoothly into your development workflows.

    Strategies for Developer-Friendly Zero Trust

    • Use Established Tools: Use tools like Open Policy Agent (OPA) for policy enforcement and HashiCorp Vault for secrets management.
    • Automate Repetitive Tasks: Automate security checks using CI/CD tools like Snyk, Trivy, or Checkov to scan for vulnerabilities in dependencies and configurations.
    • Provide Clear Guidelines: Ensure your team has access to actionable, easy-to-understand documentation on secure coding practices and Zero Trust principles.
    Pro Tip: Integrate policy-as-code tools like OPA into your pipelines. This allows you to enforce security policies early in the development cycle.

    Common Pitfalls to Avoid

    • Overcomplicating Security: Avoid adding unnecessary complexity. Start with the basics—like securing your APIs and authenticating all requests—and iterate from there.
    • Skipping Monitoring: Without real-time monitoring, you’re flying blind. Use tools like Datadog or Splunk to track access patterns and detect anomalies.
    • Ignoring Developer Feedback: If security measures disrupt workflows, developers may find ways to bypass them. Collaborate with your team to ensure solutions are practical and efficient.

    Practical Steps to Implement Zero Trust

    🔧 Why I enforce this everywhere: My infrastructure handles trading automation with brokerage API credentials. Every service that can access those credentials is a potential breach vector. Zero Trust ensures that even if one container is compromised, the blast radius is limited to exactly what that service needs—nothing more.

    Here’s how you can start applying Zero Trust principles in your projects today:

    1. Secure APIs and Microservices

    Use token-based authentication and enforce strict access controls. For instance, in Python with Flask:

    # Flask API example with JWT authentication
    from flask import Flask, request, jsonify
    import jwt
    
    app = Flask(__name__)
    SECRET_KEY = 'your_secret_key'
    
    def authenticate_token(token):
     try:
     return jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
     except jwt.ExpiredSignatureError:
     return None
    
    @app.route('/secure-endpoint', methods=['GET'])
    def secure_endpoint():
     token = request.headers.get('Authorization')
     if not token:
     return jsonify({'message': 'Access denied'}), 401
    
     user = authenticate_token(token)
     if not user or 'read:data' not in user['permissions']:
     return jsonify({'message': 'Insufficient permissions'}), 403
    
     return jsonify({'message': 'Secure data'})
    

    2. Enforce Role-Based Access Control (RBAC)

    Use tools like Kubernetes RBAC or AWS IAM to define roles and permissions. Avoid granting wildcard permissions like s3:* or admin roles to applications or users.

    3. Secure Your CI/CD Pipeline

    Your CI/CD pipeline is a critical part of your development workflow and a prime target for attackers. Ensure it’s secured by:

    • Signing all artifacts to prevent tampering.
    • Scanning dependencies for vulnerabilities using tools like Snyk or Trivy.
    • Restricting access to pipeline secrets and environment variables.
    Warning: Compromised CI/CD tools can lead to devastating supply chain attacks. Secure them as rigorously as your production systems.

    4. Implement Continuous Monitoring

    Set up centralized logging and monitoring for all services. Tools like ELK Stack, Splunk, or Datadog can help you track access patterns and flag suspicious behavior.

    Collaboration is Key: Developers and Security Teams

    Zero Trust is not just a technical framework—it’s a cultural shift. Developers and security teams must work together to make it effective.

    • Shared Responsibility: Security is everyone’s job. Developers should be empowered to make security-conscious decisions during development.
    • Feedback Loops: Regularly review security incidents and update policies based on lessons learned.
    • Continuous Education: Offer training sessions and resources to help developers understand Zero Trust principles and best practices.
    Pro Tip: Organize regular threat modeling sessions with cross-functional teams. These sessions can uncover hidden vulnerabilities and improve overall security awareness.

    Quick Summary

    • Zero Trust is about continuously verifying every access request—no assumptions, no exceptions.
    • Developers play a key role in securing systems by implementing Zero Trust principles in their workflows.
    • Use tools, automation, and clear guidelines to make Zero Trust practical and scalable.
    • Collaboration between developers and security teams is essential for long-term success.

    Start with a service account audit—list every credential in your cluster and verify each one needs the access it has. Remove anything that’s “just in case.” That single exercise will close more attack surface than any tool you can buy.

    🛠 Recommended Resources:

    Tools and books mentioned in (or relevant to) this article:

    📋 Disclosure: Some links are affiliate links. If you purchase through these links, I earn a small commission at no extra cost to you. I only recommend products I have personally used or thoroughly evaluated.


    📚 Related Articles

    📊 Free AI Market Intelligence

    Join Alpha Signal — AI-powered market research delivered daily. Narrative detection, geopolitical risk scoring, sector rotation analysis.

    Join Free on Telegram →

    Pro with stock conviction scores: $5/mo

    Get Weekly Security & DevOps Insights

    Join 500+ engineers getting actionable tutorials on Kubernetes security, homelab builds, and trading automation. No spam, unsubscribe anytime.

    Subscribe Free →

    Delivered every Tuesday. Read by engineers at Google, AWS, and startups.

    Frequently Asked Questions

    What is Zero Trust for Developers: Secure Systems by Design about?

    Why Zero Trust is Non-Negotiable for Developers Picture this: It’s a late Friday afternoon, and you’re prepping for the weekend when an alert comes through. An internal service has accessed sensitive

    Who should read this article about Zero Trust for Developers: Secure Systems by Design?

    Anyone interested in learning about Zero Trust for Developers: Secure Systems by Design and related topics will find this article useful.

    What are the key takeaways from Zero Trust for Developers: Secure Systems by Design?

    Panic sets in as you dig through logs, only to discover that a misconfigured access control policy has been quietly exposing data for weeks. This nightmare scenario is exactly what Zero Trust is desig

    References

  • .htaccess Upload Exploit in PHP: How Attackers Bypass File Validation (and How I Stopped It)

    .htaccess Upload Exploit in PHP: How Attackers Bypass File Validation (and How I Stopped It)

    Why File Upload Security Should Top Your Priority List

    📌 TL;DR: Why File Upload Security Should Top Your Priority List Picture this: Your users are happily uploading files to your PHP application—perhaps profile pictures, documents, or other assets. Everything seems to be working perfectly until one day you discover your server has been compromised.
    🎯 Quick Answer: PHP file uploads are vulnerable when attackers upload malicious .htaccess files that reconfigure Apache to execute arbitrary code. Fix this by storing uploads outside the web root, validating MIME types server-side, renaming files to random hashes, and disabling .htaccess overrides with `AllowOverride None` in your Apache config.

    A malicious .htaccess file uploaded to your PHP application can turn an innocent file-upload form into a remote code execution backdoor. Attackers exploit weak file validation to sneak in Apache directives that re-enable PHP execution inside upload directories—and most developers never check for it.

    we’ll explore how attackers exploit .htaccess files in file uploads, how to harden your application against such attacks, and the best practices that every PHP developer should implement.

    Understanding .htaccess: A Double-Edged Sword

    The .htaccess file is a potent configuration tool used by the Apache HTTP server. It allows developers to define directory-level rules, such as custom error pages, redirects, or file handling behavior. For PHP applications, it can even determine which file extensions are treated as executable PHP scripts.

    Here’s an example of an .htaccess directive that instructs Apache to treat .php5 and .phtml files as PHP scripts:

    AddType application/x-httpd-php .php .php5 .phtml

    While this flexibility is incredibly useful, it also opens doors for attackers. If your application allows users to upload files without proper restrictions, an attacker could weaponize .htaccess to bypass security measures or even execute arbitrary code.

    Pro Tip: If you’re not actively using .htaccess files for specific directory-level configurations, consider disabling their usage entirely via your Apache configuration. Use the AllowOverride None directive to block .htaccess files within certain directories.

    How Attackers Exploit .htaccess Files in PHP Applications

    When users are allowed to upload files to your server, you’re essentially granting them permission to place content in your directory structure. Without proper controls in place, this can lead to some dangerous scenarios. Here are the most common types of attacks Using .htaccess:

    1. Executing Arbitrary Code

    An attacker could upload a file named malicious.jpg that contains embedded PHP code. By adding their own .htaccess file with the following line:

    AddType application/x-httpd-php .jpg

    Apache will treat all .jpg files in that directory as PHP scripts. The attacker can then execute the malicious code by accessing https://yourdomain.com/uploads/malicious.jpg.

    Warning: Even if you restrict uploads to specific file types like images, attackers can embed PHP code in those files and use .htaccess to manipulate how the server interprets them.

    2. Enabling Directory Indexing

    If directory indexing is disabled globally on your server (as it should be), attackers can override this by uploading an .htaccess file containing:

    Options +Indexes

    This exposes the contents of the upload directory to anyone who knows its URL. Sensitive files stored there could be publicly accessible, posing a significant risk.

    3. Overriding Security Rules

    Even if you’ve configured your server to block PHP execution in upload directories, an attacker can re-enable it by uploading a malicious .htaccess file with the following directive:

    php_flag engine on

    This effectively nullifies your security measures and reintroduces the risk of code execution.

    Best Practices for Securing File Uploads

    Now that you understand how attackers exploit .htaccess, let’s look at actionable steps to secure your file uploads.

    1. Disable PHP Execution

    The most critical step is to disable PHP execution in your upload directory. Create an .htaccess file in the upload directory with the following content:

    php_flag engine off

    Alternatively, if you’re using Nginx, you can achieve the same result by adding this to your server block configuration:

    location /uploads/ {
     location ~ \.php$ {
     deny all;
     }
     }
    Pro Tip: For an extra layer of security, store uploaded files outside of your web root and use a script to serve them dynamically after validation.

    2. Restrict Allowed File Types

    Only allow the upload of file types that your application explicitly requires. For example, if you only need to accept images, ensure that only common image MIME types are permitted:

    $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
     $file_type = mime_content_type($_FILES['uploaded_file']['tmp_name']);
    
     if (!in_array($file_type, $allowed_types)) {
     die('Invalid file type.');
     }

    Also, verify file extensions and ensure they match the MIME type to prevent spoofing.

    3. Sanitize File Names

    To avoid directory traversal attacks and other exploits, sanitize file names before saving them:

    $filename = basename($_FILES['uploaded_file']['name']);
     $sanitized_filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $filename);
    
     move_uploaded_file($_FILES['uploaded_file']['tmp_name'], '/path/to/uploads/' . $sanitized_filename);

    4. Isolate Uploaded Files

    Consider serving user-uploaded files from a separate domain or subdomain. This isolates the upload directory and minimizes the impact of XSS or other attacks.

    5. Monitor Upload Activity

    Regularly audit your upload directories for suspicious activity. Tools like Tripwire or OSSEC can notify you of unauthorized file changes, including the presence of unexpected .htaccess files.

    Testing and Troubleshooting Your Configuration

    Before deploying your application, thoroughly test your upload functionality and security measures. Here’s a checklist:

    • Attempt to upload a PHP file and verify that it cannot be executed.
    • Test file type validation by uploading unsupported formats.
    • Check that directory indexing is disabled.
    • Ensure your .htaccess settings are correctly applied.

    If you encounter issues, check your server logs for misconfigurations or errors. Common pitfalls include:

    • Incorrect permissions on the upload directory, allowing overwrites.
    • Failure to validate both MIME type and file extension.
    • Overlooking nested .htaccess files in subdirectories.

    A Real-World Upload Vulnerability I Found

    During a security audit at a previous job, I found that a file upload endpoint accepted .phtml files. Combined with a misconfigured .htaccess that had AddType application/x-httpd-php .phtml, it was a full remote code execution vulnerability. An attacker could upload a PHP web shell disguised with a .phtml extension and gain complete control of the server.

    The attack chain, step by step:

    • Attacker discovers the upload endpoint accepts files by extension whitelist, but .phtml was not on the blocklist
    • Attacker uploads shell.phtml containing <?php system($_GET['cmd']); ?>
    • Apache’s existing .htaccess treats .phtml as executable PHP
    • Attacker visits /uploads/shell.phtml?cmd=whoami and gets command execution
    • From there: read config files for database credentials, pivot to internal services, exfiltrate data

    The fix was defense in depth — no single check, but multiple layers that each independently block the attack:

    <?php
    /**
     * Secure file upload handler with defense-in-depth validation.
     * Each check independently prevents a different attack vector.
     */
    function secureUpload(array $file, string $uploadDir): array {
        $errors = [];
    
        // Layer 1: Validate against a strict ALLOW-list of extensions
        $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'pdf'];
        $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        if (!in_array($extension, $allowedExtensions, true)) {
            $errors[] = "Blocked extension: .{$extension}";
        }
    
        // Layer 2: Check for double extensions (.php.jpg, .phtml.png)
        $nameParts = explode('.', $file['name']);
        $dangerousExtensions = ['php', 'phtml', 'php5', 'php7', 'phar', 'shtml', 'htaccess'];
        foreach ($nameParts as $part) {
            if (in_array(strtolower($part), $dangerousExtensions, true)) {
                $errors[] = "Dangerous extension found in filename: {$part}";
            }
        }
    
        // Layer 3: Verify MIME type matches claimed extension
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $detectedMime = $finfo->file($file['tmp_name']);
        $mimeMap = [
            'jpg' => ['image/jpeg'],
            'jpeg' => ['image/jpeg'],
            'png' => ['image/png'],
            'gif' => ['image/gif'],
            'webp' => ['image/webp'],
            'pdf' => ['application/pdf'],
        ];
        if (isset($mimeMap[$extension]) && !in_array($detectedMime, $mimeMap[$extension], true)) {
            $errors[] = "MIME mismatch: expected {$mimeMap[$extension][0]}, got {$detectedMime}";
        }
    
        // Layer 4: For images, verify the file is actually a valid image
        if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp'], true)) {
            $imageInfo = @getimagesize($file['tmp_name']);
            if ($imageInfo === false) {
                $errors[] = "File is not a valid image despite having image extension";
            }
        }
    
        // Layer 5: Check file size (prevent DoS via huge uploads)
        $maxSize = 10 * 1024 * 1024; // 10MB
        if ($file['size'] > $maxSize) {
            $errors[] = "File too large: {$file['size']} bytes (max: {$maxSize})";
        }
    
        if (!empty($errors)) {
            return ['success' => false, 'errors' => $errors];
        }
    
        // Layer 6: Rename file to a random name (breaks attacker URL prediction)
        $newFilename = bin2hex(random_bytes(16)) . '.' . $extension;
        $destination = rtrim($uploadDir, '/') . '/' . $newFilename;
    
        if (!move_uploaded_file($file['tmp_name'], $destination)) {
            return ['success' => false, 'errors' => ['Failed to move uploaded file']];
        }
    
        return ['success' => true, 'filename' => $newFilename, 'path' => $destination];
    }
    
    // Usage:
    $result = secureUpload($_FILES['avatar'], '/var/www/storage/uploads/');
    if (!$result['success']) {
        http_response_code(400);
        echo json_encode(['errors' => $result['errors']]);
        exit;
    }

    The critical lesson: never use a blocklist for file extensions. Always use an allowlist. Blocklists are guaranteed to miss something — there are dozens of PHP-executable extensions across different server configurations (.php, .phtml, .php5, .php7, .phar, .inc). An allowlist of known-safe extensions is the only reliable approach.

    Nginx vs Apache: Upload Security Differences

    Everything we have discussed about .htaccess exploits is Apache-specific. If you are running Nginx, the attack surface is fundamentally different — and in many ways, smaller. I migrated my homelab from Apache to Nginx specifically because .htaccess overrides were a security liability. With Nginx, there are no per-directory config overrides that an attacker can upload.

    Nginx location blocks for upload directories: Instead of .htaccess files, Nginx uses centralized configuration. Here is how to lock down an upload directory:

    # Nginx: Secure upload directory configuration
    server {
        listen 443 ssl;
        server_name app.example.com;
    
        # Upload directory -- serve files as static content only
        location /uploads/ {
            # Never execute PHP in the uploads directory
            location ~ \.php$ {
                deny all;
                return 403;
            }
    
            # Block all script-like extensions
            location ~* \.(phtml|php5|php7|phar|shtml|cgi|pl|py)$ {
                deny all;
                return 403;
            }
    
            # Prevent .htaccess from being served
            location ~ /\.ht {
                deny all;
            }
    
            # Force downloads instead of rendering (prevents XSS via SVG/HTML)
            add_header Content-Disposition "attachment" always;
            add_header X-Content-Type-Options "nosniff" always;
            add_header Content-Security-Policy "default-src 'none'" always;
    
            # Serve from a directory outside the application root
            alias /var/www/storage/uploads/;
        }
    }

    Comparing the two approaches:

    • Apache + .htaccess: Per-directory overrides are powerful but dangerous. Any uploaded .htaccess file can override server settings. You must explicitly disable overrides with AllowOverride None in the server config to prevent this. The flexibility is a liability.
    • Nginx: No per-directory config file concept. All configuration is centralized in server/location blocks. An attacker cannot upload a config file to change server behavior. This is inherently more secure for upload directories.
    • Performance: Nginx does not check for .htaccess files on every request, making it faster for serving static uploaded content. Apache checks every directory in the path for .htaccess files unless AllowOverride None is set.
    • Migration complexity: Moving from Apache to Nginx requires translating .htaccess rules into Nginx config blocks. The logic is the same; the syntax is different. Online converter tools can help with common directives.

    If you are starting a new project, I strongly recommend Nginx for any application that handles file uploads. If you are stuck on Apache, the single most important thing you can do is add AllowOverride None to your upload directory in the main server config — not in an .htaccess file, which can itself be overridden.

    Automated Security Testing for File Uploads

    I run this test suite against every upload endpoint before it goes live. Manual testing is not enough — you need automated tests that try every known bypass technique so you do not miss an edge case during a code review.

    # upload_security_test.py
    # Automated upload endpoint security tester.
    # Tests common bypass techniques against a file upload endpoint.
    # Run in CI/CD to catch regressions before they reach production.
    import requests
    import sys
    
    class UploadSecurityTester:
        def __init__(self, upload_url, auth_token=None):
            self.upload_url = upload_url
            self.headers = {}
            if auth_token:
                self.headers['Authorization'] = f'Bearer {auth_token}'
            self.results = []
    
        def test_upload(self, filename, content, content_type, description):
            files = {'file': (filename, content, content_type)}
            try:
                resp = requests.post(
                    self.upload_url, files=files,
                    headers=self.headers, timeout=10
                )
                accepted = resp.status_code in (200, 201)
                self.results.append({
                    'test': description,
                    'filename': filename,
                    'status': resp.status_code,
                    'accepted': accepted,
                })
                return accepted
            except Exception as e:
                self.results.append({'test': description, 'error': str(e)})
                return False
    
        def run_all_tests(self):
            php_payload = b'<?php echo "VULNERABLE"; ?>'
            gif_header = b'GIF89a' + php_payload
    
            # Test 1: Direct PHP upload
            self.test_upload('shell.php', php_payload,
                'application/x-php', 'Direct PHP upload')
    
            # Test 2: Double extension bypass
            self.test_upload('shell.php.jpg', php_payload,
                'image/jpeg', 'Double extension (php.jpg)')
    
            # Test 3: Alternative PHP extensions
            for ext in ['phtml', 'php5', 'php7', 'phar', 'inc', 'phps']:
                self.test_upload(f'shell.{ext}', php_payload,
                    'application/octet-stream',
                    f'Alternative extension (.{ext})')
    
            # Test 4: .htaccess upload
            htaccess = b'AddType application/x-httpd-php .jpg'
            self.test_upload('.htaccess', htaccess,
                'text/plain', '.htaccess upload attempt')
    
            # Test 5: Content-type spoofing
            self.test_upload('avatar.php', php_payload,
                'image/jpeg', 'Content-type spoofing')
    
            # Test 6: GIF header bypass
            self.test_upload('image.php.gif', gif_header,
                'image/gif', 'GIF magic bytes with PHP payload')
    
            # Test 7: Case variation bypass
            self.test_upload('shell.PhP', php_payload,
                'application/octet-stream', 'Case variation (.PhP)')
    
            # Test 8: Null byte injection
            self.test_upload('shell.php%00.jpg', php_payload,
                'image/jpeg', 'Null byte injection')
    
            # Test 9: Oversized file (DoS test)
            self.test_upload('huge.jpg', b'A' * (11 * 1024 * 1024),
                'image/jpeg', 'Oversized file upload (11MB)')
    
        def print_report(self):
            print("\n=== Upload Security Test Report ===\n")
            failures = 0
            for r in self.results:
                if 'error' in r:
                    status = "ERROR"
                elif r['accepted']:
                    status = "FAIL - ACCEPTED"
                    failures += 1
                else:
                    status = "PASS - REJECTED"
                print(f"  [{status}] {r['test']}")
            total = len(self.results)
            passed = total - failures
            print(f"\n{'PASSED' if failures == 0 else 'FAILED'}: {passed}/{total}")
            return 0 if failures == 0 else 1
    
    if __name__ == '__main__':
        url = sys.argv[1] if len(sys.argv) > 1 else 'https://app.example.com/api/upload'
        token = sys.argv[2] if len(sys.argv) > 2 else None
        tester = UploadSecurityTester(url, token)
        tester.run_all_tests()
        sys.exit(tester.print_report())

    Integrating with CI/CD: Add this as a step in your deployment pipeline. The script returns a non-zero exit code if any malicious upload is accepted, which fails the build:

    # .github/workflows/security-tests.yml (excerpt)
      upload-security:
        runs-on: ubuntu-latest
        needs: deploy-staging
        steps:
          - uses: actions/checkout@v4
          - name: Run upload security tests
            run: |
              pip install requests
              python upload_security_test.py \
                "${{ secrets.STAGING_URL }}/api/upload" \
                "${{ secrets.STAGING_TOKEN }}"

    Testing double extensions, null bytes, content-type spoofing, and alternative PHP extensions covers the most common bypass techniques. I update this test suite whenever I encounter a new bypass in the wild or read about one in security advisories. The goal is that no upload vulnerability makes it past staging — ever.

    Quick Summary

    • Disable PHP execution in upload directories to mitigate code execution risks.
    • Restrict uploads to specific file types and validate both MIME type and file name.
    • Isolate uploaded files by using a separate domain or storing them outside the web root.
    • Regularly monitor and audit your upload directories for suspicious activity.
    • Thoroughly test your configuration in a staging environment before going live.

    You can significantly reduce the risk of .htaccess-based attacks and ensure your PHP application remains secure. Have additional tips or techniques? Share them below!

    🛠 Recommended Resources:

    Tools and books mentioned in (or relevant to) this article:

    📋 Disclosure: Some links are affiliate links. If you purchase through these links, I earn a small commission at no extra cost to you. I only recommend products I have personally used or thoroughly evaluated.


    📚 Related Articles

    📊 Free AI Market Intelligence

    Join Alpha Signal — AI-powered market research delivered daily. Narrative detection, geopolitical risk scoring, sector rotation analysis.

    Join Free on Telegram →

    Pro with stock conviction scores: $5/mo

    Get Weekly Security & DevOps Insights

    Join 500+ engineers getting actionable tutorials on Kubernetes security, homelab builds, and trading automation. No spam, unsubscribe anytime.

    Subscribe Free →

    Delivered every Tuesday. Read by engineers at Google, AWS, and startups.

    Frequently Asked Questions

    What is Securing PHP File Uploads: .htaccess Exploits Fixed about?

    Why File Upload Security Should Top Your Priority List Picture this: Your users are happily uploading files to your PHP application—perhaps profile pictures, documents, or other assets. Everything see

    Who should read this article about Securing PHP File Uploads: .htaccess Exploits Fixed?

    Anyone interested in learning about Securing PHP File Uploads: .htaccess Exploits Fixed and related topics will find this article useful.

    What are the key takeaways from Securing PHP File Uploads: .htaccess Exploits Fixed?

    Malicious scripts are running, sensitive data is exposed, and your application is behaving erratically. A seemingly innocent .htaccess file uploaded by an attacker to your server. This is not a rare o

    References

Also by us: StartCaaS — AI Company OS · Hype2You — AI Tech Trends