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

Đang mở Phù hợp với người mới
#7,960 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@caigen đang làm issue này rồi.

Từ ngày 20/11/2026.

  • #7968 của @caigen — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
api, backend

Hướng nghiên cứu

Đọc src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataUriParser.cs quanh lệnh gọi MediaTypeHeaderValue.TryParse, sau đó kiểm tra DataContent.cs và các bài kiểm thử của nó. Bổ sung kiểm thử cho ba trường hợp tái hiện, bao gồm siêu dữ liệu chỉ có tham số, có và không có ;base64, cùng một kiểu phương tiện được chỉ định tường minh. Hoàn tất khi tất cả các trường hợp được phân tích thành các byte hello và tạo ra kiểu phương tiện như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
3.3k
Fork
925
Merge trung bình
2 ngày 7 giờ
Pull request đã merge (30 ngày)
28

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/extensions

Tất cả issue của dotnet/extensions

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.