urBackend icon indicating copy to clipboard operation
urBackend copied to clipboard

[Bug] dashboard-api: INCR and EXPIRE in dbExport are not atomic — export quota key can persist without TTL if process crashes

Open anshul23102 opened this issue 6 months ago • 6 comments

Describe the bug

In apps/dashboard-api/src/controllers/dbExport.controller.js, the daily export quota key is incremented and then given a TTL in two separate Redis operations:

const newCount = await redis.incr(key);
if (newCount === 1) {
    await redis.expire(key, 86400);
}

If the Node.js process crashes, is restarted, or loses the Redis connection between the INCR and the EXPIRE calls, the key is created but never given an expiry. The counter for that project/day persists indefinitely, permanently blocking the project from making further export requests on that day — or preventing exports forever if the TTL is never applied.

To Reproduce

  1. Send an export request that creates the counter key for the first time (newCount === 1).
  2. Simulate a crash between INCR and EXPIRE (e.g., kill -9 the process, or introduce a mock that throws before EXPIRE).
  3. Restart the server.
  4. Send export requests. The key still exists with value = 1 and no TTL, so the limit check passes normally — but if you kill before EXPIRE on every first request, the key has no TTL.
  5. Over multiple days the counter accumulates without resetting.

Expected behavior

The increment and TTL assignment should be atomic. Use SET key 1 EX 86400 NX for the first write, then INCR for subsequent ones. Or use the incrWithTtlAtomic helper already present in @urbackend/common:

const newCount = await incrWithTtlAtomic(redis, key, 86400, 1);
if (newCount > maxExports) {
    await redis.decr(key);
    return next(new AppError(429, `Daily export limit reached...`));
}

Additional context

File: apps/dashboard-api/src/controllers/dbExport.controller.js

Labels: bug, nsoc26, level2

anshul23102 avatar Jun 02 '26 11:06 anshul23102

pls assign this to me under gssoc'26

aaniya22 avatar Jun 05 '26 14:06 aaniya22

Hi @yash-pouranik! I am the author of this issue and would like to work on it under GSSoC '26. Could you please assign it to me?

/assign @yash-pouranik /claim

anshul23102 avatar Jun 28 '26 18:06 anshul23102

/assign

anshul23102 avatar Jul 02 '26 11:07 anshul23102

@coderabbitai Is this a valid issue??

yash-pouranik avatar Jul 31 '26 05:07 yash-pouranik

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

repo="geturbackend/urBackend"
issue="258"

echo "== Issue =="
gh issue view "$issue" --repo "$repo" --json number,title,body,state,author,labels,assignees,comments,url

echo
echo "== Linked pull requests =="
gh api "repos/$repo/issues/$issue/timeline" \
  -H "Accept: application/vnd.github+json" \
  --paginate \
  --jq '.[] | select(.event == "cross-referenced" or .event == "connected") | {event, source: .source.issue.html_url, title: .source.issue.title, state: .source.issue.state}' || true

Length of output: 4500


🏁 Script executed:

#!/bin/bash
set -euo pipefail

controller="apps/dashboard-api/src/controllers/dbExport.controller.js"

echo "== Controller outline =="
ast-grep outline "$controller" --items all || true

echo
echo "== Quota logic in controller =="
rg -n -C 12 -P '\b(redis\.(incr|expire|decr)|maxExports|export.*limit|quota)\b' "$controller" || true

echo
echo "== Atomic helper definitions and usages =="
rg -n -C 8 -P '\bincrWithTtlAtomic\s*\(' . \
  -g '!node_modules/**' -g '!dist/**' -g '!build/**' || true

Length of output: 11033


@yash-pouranik, no. Issue #258 is not valid against the current code.

apps/dashboard-api/src/controllers/dbExport.controller.js performs the quota update in one redis.eval() call. Its Lua script runs INCR and the first-key EXPIRE atomically. A Node.js crash cannot occur between those Redis commands.

The issue describes an older implementation with separate client-side INCR and EXPIRE calls. The current implementation already removes that failure window. Close the issue as already fixed or stale.

You are interacting with an AI system.

coderabbitai[bot] avatar Jul 31 '26 05:07 coderabbitai[bot]

ok Closing.

yash-pouranik avatar Jul 31 '26 05:07 yash-pouranik