Skip to content

Commit 6667c50

Browse files
authored
fix(cache): reject oversized keys to prevent memory bloat JhaSourav07#1403 (JhaSourav07#2040)
## Description Added strict length validation to the `TTLCache` `set` method to reject extremely large cache keys (e.g., `'a'.repeat(20000)`). This prevents potential memory bloat and database strain. Also updated the corresponding Vitest suite in `lib/cache.test.ts` to assert this new boundary behavior and removed an older, conflicting test. Fixes JhaSourav07#1403 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (Backend cache logic update) ## 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 (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [x] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started 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 "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents b3407c6 + ad02c3c commit 6667c50

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

lib/cache.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,16 +673,18 @@ describe('TTLCache', () => {
673673
cache.destroy();
674674
});
675675

676-
it('handles oversized cache keys safely', () => {
676+
// FIX: New test targeting oversized cache keys for Issue #1403
677+
it('rejects oversized cache keys to prevent memory bloat (Variation 2)', () => {
677678
const cache = new TTLCache<string>();
678-
679679
const oversizedKey = 'a'.repeat(20000);
680680

681+
// Assert that setting a massive key throws an error to prevent memory bloat
681682
expect(() => {
682683
cache.set(oversizedKey, 'large-key-value', 60_000);
683-
}).not.toThrow();
684+
}).toThrow();
684685

685-
expect(cache.get(oversizedKey)).toBe('large-key-value');
686+
// Verify the key was not saved
687+
expect(cache.has(oversizedKey)).toBe(false);
686688

687689
cache.destroy();
688690
});

lib/cache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ export class TTLCache<T> {
159159
if (key === '') throw new Error('Cache key cannot be empty');
160160
if (ttlMs <= 0) throw new RangeError(`ttlMs must be positive, got ${ttlMs}`);
161161

162+
if (key.length > 10000) {
163+
throw new Error('Cache key exceeds maximum allowed length to prevent memory bloat');
164+
}
165+
162166
const maxSize = this.maxSize;
163167
if (maxSize !== undefined && this.store.size >= maxSize && !this.store.has(key)) {
164168
this.sweep();

0 commit comments

Comments
 (0)