Skip to content

Add defensive check for data URL prefix in summarizeDataUrl - #1264

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/cache-debug-data-url-test
Open

Add defensive check for data URL prefix in summarizeDataUrl#1264
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/cache-debug-data-url-test

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Add a defensive check for the data URL prefix in summarizeDataUrl function in common/src/util/cache-debug.ts.

Bug Description

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, potentially producing misleading summaries.

Fix

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.

Testing

Added comprehensive test coverage demonstrating:

  1. Valid data URLs are properly summarized (image/png with base64 payload)
  2. Non-data-URL strings (https://example.com/image.png) pass through unchanged
  3. Edge case: data URL-like strings without comma are handled gracefully

All 3 tests pass.

Files Changed

  • common/src/util/cache-debug.ts - Added defensive prefix check
  • common/src/util/__tests__/cache-debug.test.ts - New test file with 3 test cases

Scope

This change only touches common/ which is an approved contribution area per the Contributing Guide.

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.
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 this pull request may close these issues.

1 participant