Commit 2389111
authored
fix(api): rate limit stats refresh bypass (JhaSourav07#3278)
## Description
Fixes JhaSourav07#3196
This adds the same manual refresh protections used by `/api/github` to
the public `/api/stats` endpoint. Before this, `/api/stats?refresh=true`
passed `bypassCache: true` straight into `fetchGitHubContributions`, so
repeated stats refreshes could force fresh GitHub contribution fetches
without checking shared quota, client refresh limits, or per-user
cooldown.
## Pillar
- [x] 🛠️ Other (Bug fix, refactoring, docs)
## What changed
- Added low-quota blocking for stats refreshes.
- Added client IP refresh rate limiting for `/api/stats?refresh=true`.
- Added per-user refresh cooldown fallback so repeated refreshes use
cached stats instead of bypassing cache.
- Added `X-Refresh-Status` for `Fresh`, `Cached`, and
`Cooldown-Served-Cached` responses.
- Added regression tests for fresh refresh, cooldown fallback, rate
limit rejection, and low quota rejection.
## GSSoC 2026
This PR is raised as part of GSSoC 2026. The linked issue is assigned to
me but still has no labels after the earlier `/addlabel` command, so
please add the relevant GSSoC/type labels if they are not applied
automatically.
## Visual Preview
N/A - API behavior/header change only.
## Validation
```bash
npm run test -- app/api/stats/route.test.ts
npm run format
npm run format:check
npm run lint
npm run typecheck
```
`npm run lint` passes with existing warnings outside this change. I also
tried `npm run build` locally, but this environment only returned Next's
generic `Build failed because of webpack errors` message without
file-level diagnostics; previous GitHub CI production builds have been
the reliable signal for this repo.
## Checklist before requesting a review:
- [x] I have read the `CONTRIBUTING.md` file.
- [x] I have tested these changes locally
(`localhost:3000/api/streak?user=YOUR_USERNAME`).
- [x] I have run `npm run format` and `npm run lint` locally and
resolved all errors.
- [x] My commits follow the Conventional Commits format.
- [x] I have updated `README.md` if I added a new theme or URL
parameter.
- [x] I have starred the repo.
- [x] I have made sure that I have only one commit to merge in this PR.
- [x] The SVG output matches the CommitPulse quality standard.2 files changed
Lines changed: 125 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
31 | 37 | | |
32 | 38 | | |
33 | 39 | | |
34 | 40 | | |
35 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
36 | 44 | | |
37 | 45 | | |
38 | 46 | | |
39 | 47 | | |
40 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
41 | 52 | | |
42 | 53 | | |
43 | 54 | | |
| |||
110 | 121 | | |
111 | 122 | | |
112 | 123 | | |
| 124 | + | |
113 | 125 | | |
114 | 126 | | |
115 | 127 | | |
| |||
143 | 155 | | |
144 | 156 | | |
145 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
146 | 199 | | |
147 | 200 | | |
148 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
6 | 21 | | |
7 | 22 | | |
8 | 23 | | |
| |||
20 | 35 | | |
21 | 36 | | |
22 | 37 | | |
| 38 | + | |
23 | 39 | | |
24 | 40 | | |
25 | 41 | | |
| |||
47 | 63 | | |
48 | 64 | | |
49 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
50 | 114 | | |
51 | 115 | | |
52 | 116 | | |
| |||
59 | 123 | | |
60 | 124 | | |
61 | 125 | | |
62 | | - | |
| 126 | + | |
63 | 127 | | |
64 | 128 | | |
65 | 129 | | |
66 | 130 | | |
67 | 131 | | |
68 | 132 | | |
69 | 133 | | |
70 | | - | |
| 134 | + | |
71 | 135 | | |
72 | 136 | | |
73 | 137 | | |
74 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
75 | 143 | | |
76 | 144 | | |
77 | 145 | | |
| |||
0 commit comments