Hi, Cloudflare Community! We want to implement security headers through the cloudflare workers. I have written workers script for that working perfectly, but some of our subdomains like real-time chat system uses websockets to communicate between client’s browser and servers.
Now the problem here is that those websocket connections get failed and when I start Log Stream to check the logs of workers then following logs are shown
Exception
GET
https:/mydomain.com/socket.io/?EIO=4&transport=websocket
{
"outcome": "exception",
"scriptName": null,
"exceptions": [
{
"name": "RangeError",
"message": "Responses may only be constructed with status codes in the range 200 to 599, inclusive.",
"timestamp":
}
],
"logs": [],
"eventTimestamp": ,
"event": {
"request": {
"url": "https://mydomain.com/socket.io/?EIO=4&transport=websocket",
"method": "GET",
"headers": {
"accept-encoding": "gzip",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"cf-connecting-ip": "",
"cf-ipcountry": "",
"cf-ray": "",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Upgrade",
"host": "",
"origin": "",
"pragma": "no-cache",
"sec-websocket-extensions": "permessage-deflate; client_max_window_bits",
"sec-websocket-key": "REDACTED",
"sec-websocket-version": "13",
"upgrade": "websocket",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36",
"x-forwarded-proto": "https",
"x-real-ip": ""
},
"cf": {
"clientTcpRtt": 644,
"longitude": "",
"latitude": "",
"tlsCipher": "AEAD-AES128-GCM-SHA256",
"continent": "AS",
"asn": 9541,
"clientAcceptEncoding": "gzip, deflate, br",
"country": "",
"tlsClientAuth": {
"certIssuerDNLegacy": "",
"certIssuerSKI": "",
"certSubjectDNRFC2253": "",
"certSubjectDNLegacy": "",
"certFingerprintSHA256": "",
"certNotBefore": "",
"certSKI": "",
"certSerial": "",
"certIssuerDN": "",
"certVerified": "NONE",
"certNotAfter": "",
"certSubjectDN": "",
"certPresented": "0",
"certRevoked": "0",
"certIssuerSerial": "",
"certIssuerDNRFC2253": "",
"certFingerprintSHA1": ""
},
"tlsExportedAuthenticator": {
"clientFinished": "f525ec3a95466687d87b5464471209e3def152eabbb538d66e3d4e7fcd369c9c",
"clientHandshake": "02347b4197cbf65a81e61c9e31ce65f26a23e4474826e2fdfcc603f6b3bedf69",
"serverHandshake": "0952aa3b1a12269ad55e48491fdcebbcd9",
"serverFinished": "8aa2696ba4b723019db592335717117df3c"
},
"tlsVersion": "TLSv1.3",
"colo": "MRS",
"timezone": "",
"city": "",
"edgeRequestKeepAliveStatus": 1,
"requestPriority": "",
"httpProtocol": "HTTP/1.1",
"region": "",
"regionCode": "",
"asOrganization": "",
"postalCode": ""
}
}
},
"id": 79
As you know that websockets works using 101 status code. Then I read about websockets supports through the CF-Workers. I used the following JS code to test the websocket’s communication
async function handleRequest(request) {
const upgradeHeader = request.headers.get("Upgrade")
if (!upgradeHeader || upgradeHeader !== "websocket") {
return new Response("Expected Upgrade: websocket", { status: 426 })
}
const webSocketPair = new WebSocketPair()
const [client, server] = Object.values(webSocketPair)
server.accept()
server.addEventListener("message", event => {
console.log(event.data)
})
return new Response(null, {
status: 101,
webSocket: client
})
}
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
});
and mentioned the following in “Triggers”
mydomain.com/socket.io/*
And started Monitoring Logs but I am unable to see any logs or hit. Actually, We want to implement security headers through cloudflare workers and it should not affect any websocket communication being used in different subdomains.
Please guide how it is possible through Cloudflare Workers.