DataContent rejects RFC 2397 data URIs that give only a charset (data:;charset=utf-8,...)

Abierto Apto para principiantes
#7,960 1 comentario 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@caigen ya está trabajando en esto.

Desde el 20/11/2026.

  • #7968 de @caigen — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp
Área
api, backend

Línea de trabajo

Lee src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataUriParser.cs alrededor de la llamada a MediaTypeHeaderValue.TryParse y, después, inspecciona DataContent.cs y sus pruebas. Añade cobertura para los tres casos de reproducción, incluidos los metadatos que solo contienen parámetros, con y sin ;base64, y un tipo de medio explícito. El trabajo está terminado cuando todos los casos se analizan como los bytes hello y producen los tipos de medio esperados.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

area-ai untriaged
Description

RFC 2397 allows the text/plain part of the media type to be omitted while the charset is still given:

As a shorthand, "text/plain" can be omitted but the charset parameter supplied.

So data:;charset=utf-8,hello is a valid data URI.

DataContent throws on it. #7247 fixed the case where the whole media type is omitted (data:;base64,...), but when the metadata holds only parameters (with or without ;base64), DataUriParser.Parse hands ;charset=utf-8 to MediaTypeHeaderValue.TryParse, which fails: https://github.com/dotnet/extensions/blob/8cf4ff6658e9b36eca01ac6045ea9835f3b3b751/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataUriParser.cs#L68-L77

Passing mediaType explicitly does not help, because the URI is parsed before the supplied media type is looked at: https://github.com/dotnet/extensions/blob/8cf4ff6658e9b36eca01ac6045ea9835f3b3b751/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs#L93-L96

Reproduction Steps
using Microsoft.Extensions.AI;

var a = new DataContent("data:;charset=utf-8,hello");
var b = new DataContent("data:;charset=utf-8;base64,aGVsbG8=");
var c = new DataContent("data:;charset=utf-8,hello", "text/plain");
Expected behavior

All three parse to the bytes of hello. The first two get media type text/plain;charset=utf-8, the same as data:text/plain;charset=utf-8,hello; the third keeps the supplied text/plain.

Actual behavior

Each one throws:

System.UriFormatException: Invalid data URI format: the media type is not a valid.
Regression?

No, this shape never parsed as far as I can tell. #7247 covered only the empty media type.

Known Workarounds

Insert text/plain after data: before constructing the DataContent.

Configuration

main at 8cf4ff6658e9b36eca01ac6045ea9835f3b3b751, .NET 10.0.103 SDK, Windows 11 x64. Not configuration specific; the parsing is plain string handling.

Other information

Related: #7246 / #7247. Happy to send a PR with tests if this is wanted.

Lenguaje dominante
C#
Estrellas
3.3k
Forks
925
Merge medio
2 d 7 h
PR fusionados (30 d)
28

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dotnet/extensions

Todos los issues de dotnet/extensions

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.