﻿# ======================================================================
# SECURITY FIREWALL: PERMANENT BAN FOR MALICIOUS ATTACKER (ANURAG DUBEY)
# Incident Ref: CR-2026-1008-ANURAG-DUBEY
# Details: See SECURITY_BLOCKED_ATTACKER.md
# Subnet: 2409:4090:f051:2204::/64
# Specific: 2409:4090:f051:2204:25c4:7f04:b036:ff32
# ======================================================================
<IfModule mod_authz_core.c>
    <RequireAll>
        Require all granted
        Require not ip 2409:4090:f051:2204::/64
    </RequireAll>
</IfModule>
<IfModule !mod_authz_core.c>
    Order Allow,Deny
    Allow from all
    Deny from 2409:4090:f051:2204::/64
</IfModule>
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} ^2409:4090:f051:2204: [NC]
    RewriteRule ^ - [F,L]
</IfModule>
# ======================================================================

# ==============================================================================
# 1. SECURITY: DISABLE DIRECTORY BROWSING
# ==============================================================================
Options -Indexes

# ==============================================================================
# 2. MOD_REWRITE ENGINE RULES
# ==============================================================================
<IfModule mod_rewrite.c>
  RewriteEngine On

  # Force HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # Redirect www to non-www (Fixes SEO duplicate content)
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

  # Block hidden files and directories (e.g. .git, .env)
  RewriteRule (^\.|/\.) - [F]

  # Block malicious query strings (SQL Injection, XSS, Path Traversal)
  RewriteCond %{QUERY_STRING} (?:<script.*>|(\.\./|%2e%2e%2f)) [NC,OR]
  RewriteCond %{QUERY_STRING} (UNION.*SELECT|SELECT.*FROM|INSERT.*INTO|DROP.*TABLE) [NC]
  RewriteRule .* - [F,L]

  # Handle SPA Routing for React/Vite (PushState support)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.html [L]
</IfModule>

# ==============================================================================
# 3. SECURITY: BLOCK SENSITIVE FILE EXTENSIONS
# ==============================================================================
<FilesMatch "(^#.*#|\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|sw[op]|ts|tsx|pem|env|env\.example|lock|cert|key))$">
  Require all denied
</FilesMatch>

# ==============================================================================
# 4. HTTP SECURITY HEADERS
# ==============================================================================
<IfModule mod_headers.c>
  # Prevent MIME Sniffing attacks
  Header set X-Content-Type-Options "nosniff"

  # Basic Cross-Site Scripting filter
  Header set X-XSS-Protection "1; mode=block"

  # Prevent token & URL referrer leak to third parties
  Header set Referrer-Policy "strict-origin-when-cross-origin"

  # Remove server disclosure headers
  Header unset X-Powered-By
  Header always unset X-Powered-By
</IfModule>

# ==============================================================================
# 5. PERFORMANCE: GZIP COMPRESSION & CACHE CONTROL
# ==============================================================================
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json image/svg+xml
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType font/woff2 "access plus 1 year"
</IfModule>


