Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KV limit exceeded cause uncaught exeception #53

Open
LaurenceJJones opened this issue Dec 23, 2024 · 0 comments · May be fixed by #54
Open

KV limit exceeded cause uncaught exeception #53

LaurenceJJones opened this issue Dec 23, 2024 · 0 comments · May be fixed by #54

Comments

@LaurenceJJones
Copy link
Contributor

LaurenceJJones commented Dec 23, 2024

const getRemediationForRequest = async (request, env) => {
console.log("Checking for decision against the IP")
const clientIP = request.headers.get("CF-Connecting-IP");
let value = await env.CROWDSECCFBOUNCERNS.get(clientIP);
if (value !== null) {
return value
}
console.log("Checking for decision against the IP ranges")
let actionByIPRange = await env.CROWDSECCFBOUNCERNS.get("IP_RANGES");
if (typeof actionByIPRange === "string") {
actionByIPRange = JSON.parse(actionByIPRange)
}
if (actionByIPRange !== null) {
const clientIPAddr = ipaddr.parse(clientIP);
for (const [range, action] of Object.entries(actionByIPRange)) {
if (clientIPAddr.match(ipaddr.parseCIDR(range))) {
return action
}
}
}
// Check for decision against the AS
const clientASN = request.cf.asn.toString();
value = await env.CROWDSECCFBOUNCERNS.get(clientASN);
if (value !== null) {
return value
}
// Check for decision against the country of the request
const clientCountry = request.cf.country.toLowerCase();
if (clientCountry !== null) {
value = await env.CROWDSECCFBOUNCERNS.get(clientCountry);
if (value !== null) {
return value
}
}
return null
}

Worker code can throw an uncaught exception if KV limit is exceeded but the request limit is not, we should wrap all .get functions call with a try catch directive that will log the message but ultimately return null at the end as the worker cannot do anything and should return the fetch call as cloudflare will handle the fail case? 🤷🏻

https://developers.cloudflare.com/kv/api/read-key-value-pairs/#get-method

@blotus blotus linked a pull request Dec 23, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant