Add defensive check for data URL prefix in summarizeDataUrl - #1264
Add defensive check for data URL prefix in summarizeDataUrl#1264pavankumar-vh wants to merge 1 commit into
Conversation
The summarizeDataUrl function was called from summarizeLargeValue which checks if a string starts with 'data:' before calling it. However, if summarizeDataUrl were ever called from a different context or if the prefix check were removed, the function would incorrectly try to parse non-data-URL strings. Added a defensive check at the start of summarizeDataUrl that returns the value unchanged if it doesn't start with 'data:'. This makes the function safer to use independently and prevents incorrect parsing of malformed inputs. Also added comprehensive test coverage demonstrating: 1. Valid data URLs are properly summarized 2. Non-data-URL strings pass through unchanged 3. Edge case: data URL-like strings without comma are handled gracefully All 3 tests pass.
|
Thanks for the tests - they're clear and correctly demonstrate current behavior of That said, the actual code change in If you'd found (or anticipated) a real second call site, or if the function were exported for external use, this would be worth it. As-is, it's speculative hardening rather than a bug fix - the PR description itself says 'if summarizeDataUrl were ever called from a different context,' which is a hypothetical, not an observed bug. Two options that would make this land better:
Appreciate the test-first thinking; just want to see the code change justified by an actual reachable path or an exported surface. |
Overview
Add a defensive check for the data URL prefix in
summarizeDataUrlfunction incommon/src/util/cache-debug.ts.Bug Description
The
summarizeDataUrlfunction was called fromsummarizeLargeValuewhich checks if a string starts withdata:before calling it. However, ifsummarizeDataUrlwere ever called from a different context or if the prefix check were removed, the function would incorrectly try to parse non-data-URL strings, potentially producing misleading summaries.Fix
Added a defensive check at the start of
summarizeDataUrlthat returns the value unchanged if it doesn't start withdata:. This makes the function safer to use independently and prevents incorrect parsing of malformed inputs.Testing
Added comprehensive test coverage demonstrating:
All 3 tests pass.
Files Changed
common/src/util/cache-debug.ts- Added defensive prefix checkcommon/src/util/__tests__/cache-debug.test.ts- New test file with 3 test casesScope
This change only touches
common/which is an approved contribution area per the Contributing Guide.