ai icon indicating copy to clipboard operation
ai copied to clipboard

isFullMediaType incorrectly returns true for media types without top-level type or wildcard type (/png, */png)

Open nikolas-sapa opened this issue 1 month ago • 3 comments

Description

isFullMediaType(mediaType) in @ai-sdk/provider-utils is defined to verify whether a media type has a concrete type/subtype format (e.g. image/png) rather than a top-level shorthand (image) or wildcard (image/*).

However, its implementation only checks mediaType.indexOf('/') and whether the subtype is not '*':

export function isFullMediaType(mediaType: string): boolean {
  const slashIndex = mediaType.indexOf('/');
  if (slashIndex === -1) {
    return false;
  }
  const subtype = mediaType.substring(slashIndex + 1);
  return subtype.length > 0 && subtype !== '*';
}

Because it does not validate that the top-level type (the portion before the slash) is non-empty and non-wildcard:

  • isFullMediaType('/png') returns true (top-level type is empty string "").
  • isFullMediaType('*/png') returns true (top-level type is wildcard '*').
  • isFullMediaType('*/*') returns false (only because subtype is '*').

In contrast, getTopLevelMediaType('/png') returns "". This causes downstream consumers (e.g. resolveFullMediaType) to treat malformed or wildcard strings as valid full media types instead of falling back to sniffing or erroring cleanly.

Reproduction

import { isFullMediaType } from '@ai-sdk/provider-utils';

console.log(isFullMediaType('/png')); // true (Expected: false)
console.log(isFullMediaType('*/png')); // true (Expected: false)

Proposed Fix

Verify that slashIndex > 0 and that the top-level segment is not '*':

export function isFullMediaType(mediaType: string): boolean {
  const slashIndex = mediaType.indexOf('/');
  if (slashIndex <= 0) {
    return false;
  }
  const type = mediaType.substring(0, slashIndex);
  if (type === '*') {
    return false;
  }
  const subtype = mediaType.substring(slashIndex + 1);
  return subtype.length > 0 && subtype !== '*';
}

nikolas-sapa avatar Oct 25 '26 12:10 nikolas-sapa

Identify issue type

Identified as Bug
Confidence: High

Rationale

The issue identifies a specific validation defect in existing SDK behavior, demonstrates incorrect output, and proposes a corrective implementation.

Evidence

  • isFullMediaType('/png') and isFullMediaType('*/png') return true despite lacking a concrete top-level media type.
  • A concrete reproduction and expected false results are provided.
  • The incorrect validation can cause downstream consumers to accept malformed or wildcard media types as valid full media types.

ai-sdk-factory[bot] avatar Oct 25 '26 12:10 ai-sdk-factory[bot]

Identify bug discovery

Discovery source: discovery:audit (medium confidence).

The report appears to originate from inspecting the SDK’s media-type validation logic and testing edge cases against that implementation. It provides no evidence of discovery through a production application.

  • The author quotes and analyzes the implementation of isFullMediaType.
  • The report identifies the precise missing checks for an empty or wildcard top-level type.
  • It compares behavior with another internal utility, getTopLevelMediaType, and traces the consequence to resolveFullMediaType.
  • The proposed fix directly modifies the inspected validation logic.
  • No real application or production incident is described.

ai-sdk-factory[bot] avatar Oct 25 '26 12:10 ai-sdk-factory[bot]

If you find bugs through an agent audit, please share the prompts you used instead. Please see our updated guidelines at https://github.com/vercel/ai/issues/17243. If you have any questions, comment on #24118.

gr2m avatar Oct 27 '26 18:10 gr2m