Skip to content

[SECURITY] Proteksi .env & Konfigurasi Variabel Lingkungan (Sensitive Infromation Exposure)#982

Open
pandigresik wants to merge 4 commits intorilis-devfrom
dev-967
Open

[SECURITY] Proteksi .env & Konfigurasi Variabel Lingkungan (Sensitive Infromation Exposure)#982
pandigresik wants to merge 4 commits intorilis-devfrom
dev-967

Conversation

@pandigresik
Copy link
Contributor

Perbaikan issue #967

Monitoring & Alerting untuk Akses .env

Overview

Dokumen ini menjelaskan cara monitoring dan alerting untuk mendeteksi upaya akses ke file .env atau dotfile sensitif lainnya.

⚠️ Keamanan Credential

File .env.example berisi nilai default (testing_db, root, secret) yang HANYA untuk testing (GitHub Actions).

Untuk Production:

  • Ganti semua credential dengan nilai yang aman
  • Gunakan password yang kuat
  • Jangan gunakan user root untuk database
  • Review semua nilai sebelum deploy

Proteksi yang Diterapkan

1. Apache (.htaccess)

File public/.htaccess sudah dikonfigurasi dengan 3 layer proteksi:

# Layer 1: RewriteRule - Return 404
RewriteCond %{REQUEST_URI} ^/\.env [NC]
RewriteRule ^ - [R=404,L]

# Layer 2: FilesMatch - Apache 2.4+
<FilesMatch "^\.env">
    Require all denied
</FilesMatch>

# Layer 3: Fallback - Apache 2.2
<IfModule !mod_authz_core.c>
    <FilesMatch "^\.env">
        order allow,deny
        deny from all
        Satisfy All
    </FilesMatch>
</IfModule>

Proteksi:

  • Return 404 untuk semua request ke /.env
  • Block akses ke semua dotfiles (.git, .htaccess, .htpasswd, dll)
  • Kompatibel dengan Apache 2.2 dan 2.4+

2. Nginx (Alternatif)

Jika menggunakan Nginx, tambahkan konfigurasi berikut di server block:

# Block semua dotfiles
location ~ /\. {
    deny all;
    return 404;
}

# Atau spesifik untuk .env
location ~ /\.env {
    deny all;
    return 404;
}

Monitoring

Apache Access Log

Untuk monitoring, periksa access log Apache:

# Lihat request ke .env
tail -f /var/log/apache2/access.log | grep "\.env"

# Atau gunakan grep untuk filter lebih spesifik
grep "\.env" /var/log/apache2/access.log

Format Log Entry (Apache)

192.168.1.100 - - [11/Mar/2026:10:30:00 +0000] "GET /.env HTTP/1.1" 404 1234

Informasi yang tercatat:

  • IP address
  • Timestamp
  • Request method dan URI
  • HTTP response code (404/403)

Nginx Access Log

# Lihat request ke .env
tail -f /var/log/nginx/access.log | grep "\.env"

# Lihat error log untuk request yang di-deny
tail -f /var/log/nginx/error.log | grep "\.env"

Alerting

Script Monitoring Sederhana

Script scripts/monitor-env-access.sh sudah tersedia untuk monitoring:

#!/bin/bash

# Monitoring script untuk akses .env
# Dapat dijalankan via cron setiap 5 menit

LOG_FILE="/var/log/apache2/access.log"
THRESHOLD=5  # Jumlah attempt dalam periode tertentu

# Hitung jumlah attempt akses .env
COUNT=$(grep -c "\.env" "$LOG_FILE" 2>/dev/null || echo "0")

if [ "$COUNT" -ge "$THRESHOLD" ]; then
    echo "ALERT: Terdeteksi $COUNT upaya akses .env!"
    # Kirim notifikasi (email, Slack, Telegram, dll)
fi

Integrasi Telegram Alert

#!/bin/bash

TELEGRAM_BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"

send_alert() {
    local message="$1"
    curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
         -d "chat_id=${CHAT_ID}" \
         -d "text=${message}" \
         -d "parse_mode=Markdown"
}

# Cek access log
COUNT=$(grep -c "\.env" /var/log/apache2/access.log 2>/dev/null || echo "0")

if [ "$COUNT" -gt 0 ]; then
    send_alert "🚨 *Security Alert*

Terdeteksi *$COUNT* upaya akses file .env

Segera periksa log untuk detail:
- IP Address
- User Agent
- Timestamp"
fi

Cron Job Setup

# Tambahkan ke crontab (crontab -e)
# Monitoring setiap 5 menit
*/5 * * * * /path/to/OpenKab/scripts/monitor-env-access.sh

# Atau langsung ke script monitoring
*/5 * * * * grep -c "\.env" /var/log/apache2/access.log | \
    awk '{if($1 > 0) print "ALERT: " $1 " attempts detected"}' | \
    mail -s "Security Alert" admin@example.com

Testing

Verifikasi Proteksi

# Test manual - harus return 404 atau 403
curl -I http://localhost/.env
curl -I http://localhost/.env.example
curl -I http://localhost/.git/config
curl -I http://localhost/.htaccess

# Semua request di atas harus return HTTP 404 atau 403

Automated Testing

Gunakan script scripts/test-env-protection.sh:

# Jalankan test (default: http://localhost)
./scripts/test-env-protection.sh

# Atau dengan base URL spesifik
./scripts/test-env-protection.sh http://your-domain.com

Verifikasi Log

# Apache
tail /var/log/apache2/access.log | grep "\.env"

# Nginx
tail /var/log/nginx/access.log | grep "\.env"

Troubleshooting

.env Masih Dapat Diakses

  1. Pastikan mod_rewrite aktif (Apache):

    a2enmod rewrite
    systemctl restart apache2
  2. Pastikan AllowOverride aktif (Apache):

    <Directory /var/www/html>
        AllowOverride All
    </Directory>
  3. Clear cache browser dan CDN

  4. Restart web server:

    systemctl restart apache2
    # atau
    systemctl restart nginx

Log Tidak Muncul

  1. Pastikan log level sesuai
  2. Cek konfigurasi log web server
  3. Pastikan permission log file benar

Best Practices

  1. Gunakan HTTPS untuk semua traffic production
  2. Block dotfiles di level web server (bukan hanya aplikasi)
  3. Monitor access log secara berkala
  4. Setup alerting untuk suspicious activity
  5. Gunakan firewall untuk limit akses
  6. Regular security audit dan penetration testing

Checklist Security

  • .env tidak accessible via web
  • .env.example sudah disanitasi (tanpa credential asli)
  • Proteksi di level web server (Apache/Nginx)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant