Hardening your headers and secure your site - Special Thanks to pascalandy
Goto Worker's and then Launch Editor copy the code - Once done add route and link to your script


let securityHeaders = {
"Content-Security-Policy" : "upgrade-insecure-requests",
"Strict-Transport-Security" : "max-age=31536000; includeSubDomains; preload",
"X-Xss-Protection" : "1; mode=block",
"X-Frame-Options" : "DENY",
"X-Content-Type-Options" : "nosniff",
"Referrer-Policy" : "strict-origin-when-cross-origin",
"Feature-Policy" : "accelerometer 'none' ; ambient-light-sensor 'none' ; autoplay 'self' ; camera 'none' ; encrypted-media 'none' ; fullscreen 'self' ; geolocation 'none' ; gyroscope 'none' ; magnetometer 'none' ; microphone 'none' ; midi 'none' ; payment 'self' ; picture-in-picture * ; speaker 'self' ; sync-xhr 'none' ; usb 'none' ; notifications 'self' ; vibrate 'self' ; push 'self' ; vr 'none'",
"Cache-Control" : "public, max-age=0, must-revalidate",
"Content-Type" : "text/html; charset=UTF-8",
}
let sanitiseHeaders = {
"Server" : "headers override",
}
let removeHeaders = [
"Public-Key-Pins",
"X-Powered-By",
"X-AspNet-Version",
]
addEventListener('fetch', event => {
event.respondWith(addHeaders(event.request))
})
async function addHeaders(req) {
let response = await fetch(req)
let newHdrs = new Headers(response.headers)
if (newHdrs.has("Content-Type") && !newHdrs.get("Content-Type").includes("text/html")) {
return new Response(response.body , {
status: response.status,
statusText: response.statusText,
headers: newHdrs
})
}
Object.keys(securityHeaders).map(function(name, index) {
newHdrs.set(name, securityHeaders);
})
Object.keys(sanitiseHeaders).map(function(name, index) {
newHdrs.set(name, sanitiseHeaders);
})
removeHeaders.forEach(function(name){
newHdrs.delete(name)
})
return new Response(response.body , {
status: response.status,
statusText: response.statusText,
headers: newHdrs
})
}
You can check your headers right away by going to https://securityheaders.com/
Enjoyed this article?
Show your appreciation with a clap
0claps
SK
You might also like
View all
Cloud & Infrastructure
Automate Virtual Machine Creation in Proxmox with Cloud-Init: Complete Guide
SKSohaib Khan

Cloud & Infrastructure
How to Install Bitninja for Cloudpanel Control for Server Security
SKSohaib Khan
U
Cloud & Infrastructure
Unleashing the Power of Hetzner Cloud with Bash and API Magic
SKSohaib Khan
Comments (0)
No comments yet. Be the first to comment!
