Penerapan Content Security Policy Agar Server Lebih Aman

Content Security Policy atau CSP adalah pengaturan tambahan yang diterapkan melalui respon HTTP Headers. kegunaan dari CSP sendiri adalah untuk mencegah eksploitasi XSS maupun serangan dengan kode injeksi lainnya pada server. Dengan Content Security Policy kita bisa megatur sumber mana saja yang diijinkan untuk diload di server kita sehingga jika attacker mencoba menyisipkan kode exploit dari sumber yang tidak dipercaya makan browser akan memblokir.


Parameter yang Didukung

Ada beberapa petunjuk yang tersedia untuk pemilik website yang ingin menerapkan Content Security Policy. Sebuah server juga dapat mendefinisikan beberapa arahan dalam header keamanan CSP. Berikut beberapa daftar yang disediakan oleh owasp.org:
  • default-src : Define loading policy for all resources type in case of a resource type dedicated directive is not defined (fallback),
  • script-src : Define which scripts the protected resource can execute,
  • object-src : Define from where the protected resource can load plugins,
  • style-src : Define which styles (CSS) the user applies to the protected resource,
  • img-src : Define from where the protected resource can load images,
  • media-src : Define from where the protected resource can load video and audio,
  • frame-src : Define from where the protected resource can embed frames,
  • frame-ancestors : Specifies valid parents that may embed a page using <frame>, <iframe>, <object>, <embed>, or <applet>.
  • font-src : Define from where the protected resource can load fonts,
  • connect-src : Define which URIs the protected resource can load using script interfaces,
  • form-action : Define which URIs can be used as the action of HTML form elements,
  • sandbox : Specifies an HTML sandbox policy that the user agent applies to the protected resource,
  • script-nonce : Define script execution by requiring the presence of the specified nonce on script elements,
  • plugin-types : Define the set of plugins that can be invoked by the protected resource by limiting the types of resources that can be embedded,
  • reflected-xss : Instructs a user agent to activate or deactivate any heuristics used to filter or block reflected cross-site scripting attacks, equivalent to the effects of the non-standard X-XSS-Protection header,
  • report-uri : Specifies a URI to which the user agent sends reports about policy violation

Content Security Policy

Oke kita langsung beri contohnya aja ya. Biar gak bingung.
Contoh 1
Content-Security-Policy: default-src https:
Kode CSP diatas untuk "menyuruh" browser hanya memuat resource dari situs yang sudah https, dan memblokir source dati non https.

Contoh 2
Content-Security-Policy: script-src 'self' www.google-analytics.com linuxsec.org
CSP ini memungkinkan script yang diijinkan dieksekusi dari domain saat ini (didefinisikan dengan 'self'), google-analytics.com dan linuxsec.org.

Contoh 3
Content-Security-Policy: default-src 'self' *.zonawibu.net
CSP juga mendukung wildcard. Kode diatas memungkinkan script untuk dimuat dari situs saat ini dan semua subdomain zonawibu.net.

Contoh Penerapan di Nginx dan Apache


Disini untuk penerapannya sebagai berikut.
Nginx
Edit file konfigurasi nginx kalian lalu masukkan CSP yang sudah kalian tentukan.
add_header Content-Security-Policy-Report-Only "default-src 'none'; script-src https://zonawibu.net";
Apache
Edit file konfigurasi apache kalian lalu masukkan CSP yang sudah kalian tentukan.
Header set Content-Security-Policy-Report-Only "default-src 'none'; script-src https://zonawibu.net;"
Menerapkan Content Security Policy memang tidak menjamin sepenuhnya situs kalian aman dari peretasan. Namun tidak ada salahnya melakukan tindakan pencegahan untuk mengurangi resiko peretasan tersebut.

Sekian tutorial sederhana kali ini, jika ada yang kurang jelas silahkan bertanya.

3 komentar untuk "Penerapan Content Security Policy Agar Server Lebih Aman"

Comment Author Avatar
CSP kalo nginx gimana bro ?
Comment Author Avatar
Duh sakit mata bro baca yang kode bg warna hitam, apakah bisa dibuat lebih kontras biar gampang bacanya, btw nice tuts bro

Silahkan tinggalkan komentar jika ada masukan, pertanyaan, kritik ataupun dukungan. Namun pastikan untuk berkomentar secara sopan.