Facebook
From Jhoon good, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 190
  1. <?php
  2. // Any other existing functions or code here
  3.  
  4. // Security headers functions
  5. function add_security_headers() {
  6.     // Strict-Transport-Security (HSTS)
  7.     header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
  8.  
  9.     // Content-Security-Policy (CSP) with nonce
  10.     $nonce = wp_create_nonce('my-nonce');
  11.     header("Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-$nonce' 'strict-dynamic'; style-src 'self' 'unsafe-inline';");
  12.  
  13.     // Feature-Policy
  14.     header('Feature-Policy: accelerometer none; camera none; geolocation none; gyroscope none; magnetometer none; microphone none; payment none; usb none;');
  15.  
  16.     // Permissions-Policy
  17.     header('Permissions-Policy: accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()');
  18.  
  19.     // X-Content-Type-Options
  20.     header('X-Content-Type-Options: nosniff');
  21.  
  22.     // X-Frame-Options
  23.     header('X-Frame-Options: DENY');
  24.  
  25.     // Referer-Policy
  26.     header('Referrer-Policy: strict-origin-when-cross-origin');
  27.  
  28.     // Other security policies
  29.     header('Interest-Cohort-Report-Only: "require-corp"');
  30.     header('Public-Key-Pins: max-age=31536000; includeSubDomains');
  31.     header('Screen-Wake-Lock: src self');
  32.     header('Sync-Xhr-Mode: deny');
  33.     header('Autoplay: none');
  34.     header('Serial: none');
  35.     header('Browsing-Topics: none');
  36.  
  37.     // Other security policies or directives
  38.     header('Clipboard-Write: write');
  39.  
  40.     // CORS policies
  41.     header('Access-Control-Allow-Origin: *');
  42.     header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
  43.     header('Access-Control-Allow-Headers: X-Requested-With');
  44.     header('Access-Control-Allow-Credentials: true');
  45.  
  46.     // Expect-CT
  47.     header('Expect-CT: enforce, max-age=604800');
  48. }
  49.  
  50. // Add action before sending headers
  51. add_action('send_headers', 'add_security_headers');
  52. ?>
  53.