My Cloudflare App worker seems to be conflicting with a users custom worker. Basically when they have their worker installed, and install my app their site does not respond at all.
I'm not exactly sure what could be causing it. My worker isn't modifying the request at all. Theirs is but I don't see anything obvious.
I posted his worker here with his permission.
Their worker:
let securityHeaders = {
"Content-Security-Policy" : "upgrade-insecure-requests",
"Strict-Transport-Security" : "max-age=1000",
"X-Xss-Protection" : "1; mode=block",
"X-Frame-Options" : "DENY",
"X-Content-Type-Options" : "nosniff",
"Referrer-Policy" : "strict-origin-when-cross-origin",
}
let sanitiseHeaders = {
"Plan" : "Dental",
"Batman" : "Scientist",
"Lisa-Needs" : "Braces"
}
let removeHeaders = [
"x-goog-generation",
"x-goog-hash",
"x-goog-metageneration",
"x-goog-meta-goog-reserved-file-mtime",
"x-goog-stored-content-encoding",
"x-goog-storage-class",
"x-goog-stored-content-length",
"X-GUploader-UploadID"
]
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[name]);
})
Object.keys(sanitiseHeaders).map(function(name, index) {
newHdrs.set(name, sanitiseHeaders[name]);
})
removeHeaders.forEach(function(name){
newHdrs.delete(name)
})
return new Response(response.body , {
status: response.status,
statusText: response.statusText,
headers: newHdrs
})
}
My worker (installed via my Cloudflare App):
function buildLogEntry(request, response) {
const options = INSTALL_OPTIONS
const logDefs = {
rMeth: request.method,
rUrl: request.url,
uAgent: request.headers.get("user-agent"),
cfRay: request.headers.get("cf-ray"),
cIP: request.headers.get("cf-connecting-ip"),
statusCode: response.status,
contentLength: response.headers.get("content-legth"),
cfCacheStatus: response.headers.get("cf-cache-status"),
contentType: response.headers.get("content-type"),
responseConnection: response.headers.get("connection"),
requestConnection: request.headers.get("connection"),
cacheControl: response.headers.get("cache-control"),
acceptRanges: response.headers.get("accept-ranges"),
expectCt: response.headers.get("expect-ct"),
expires: response.headers.get("expires"),
lastModified: response.headers.get("last-modified"),
vary: response.headers.get("vary"),
server: response.headers.get("server"),
etag: response.headers.get("etag"),
date: response.headers.get("date"),
transferEncoding: response.headers.get("transfer-encoding"),
}
const logArray = []
options.metadata.forEach(entry => {
logArray.push(logDefs[entry.field])
})
const logEntry = logArray.join(" | ")
return logEntry
}
async function handleRequest(event) {
const { request } = event
const response = await fetch(request)
const rHost = request.headers.get("host")
const options = INSTALL_OPTIONS
const sourceKey = options.source
const apiKey = options.logflare.api_key
const logEntry = buildLogEntry(request, response)
const init = {
method: "POST",
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
"User-Agent": `Cloudflare Worker via ${rHost}`,
},
body: JSON.stringify({ source: sourceKey, log_entry: logEntry }),
}
event.waitUntil(fetch("https://logflare.app/api/logs", init))
// console.log(cIP)
return response
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event))
})
This sounds likely to be a bug deeper in the system, not your fault. Can you e-mail me (kenton at cloudflare) and we'll try to track down what's going on here?