HttpHeaders.withAdditionalHeaders ignores case, so rawRequest silently drops headers like idempotency-key

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

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

Chưa có ai nhận issue này.

Đá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ệ
java
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Bắt đầu với HttpHeaders.withAdditionalHeaders() và cách tái hiện trong issue, sau đó lần theo cách rawRequest() áp dụng các header bổ sung. Bổ sung kiểm thử cho trường hợp thay thế một header hiện có bằng tên khác nhau về chữ hoa chữ thường và xác nhận giá trị được cung cấp vẫn được giữ lại; issue không nêu tên tệp kiểm thử, vì vậy hãy tìm các kiểm thử hiện có cho HttpHeaders hoặc raw request. Hoàn tất khi các bước tái hiện luôn trả về giá trị mới được cung cấp.

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

Mô tả

bug
Describe the bug

HttpHeaders stores headers in a case-insensitive map. But withAdditionalHeaders() first copies them into a plain java.util.HashMap, which is case-sensitive:
Map<String, List<String>> newHeaderMap = new HashMap<>(this.map()); newHeaderMap.putAll(headerMap); return HttpHeaders.of(newHeaderMap);

If you add idempotency-key and the request already has Idempotency-Key, the HashMap ends up holding both as separate keys. HttpHeaders.of() then folds them back into one case-insensitive entry, and whichever key the HashMap happens to iterate last wins. So whether your header survives depends on hash order, not on what you asked for.

This is reachable through the public API. LiveStripeResponseGetter.rawRequest() applies every entry from RawRequestOptions.getAdditionalHeaders() through StripeRequest.withAdditionalHeader(). HTTP header names are case-insensitive (RFC 9110 §5.1), so lowercase names are valid and common, for example when they're forwarded from frameworks or proxies.

For Idempotency-Key, the dropped value is replaced by an auto-generated UUID. A retried POST then isn't idempotent and can create duplicate charges or payments.

To Reproduce
  1. Start a MockWebServer and build a client: StripeClient.builder().setApiKey("sk_test_123").setApiBase(server.url("").toString()).build()
  2. Send a raw request with a lowercase additional header:
    Map<String, String> h = new HashMap<>();
    h.put("idempotency-key", "my-key-123");
    client.rawRequest(RequestMethod.POST, "/v1/customers", "",
        RawRequestOptions.builder().setAdditionalHeaders(h).build());
    
  3. Look at the recorded request's Idempotency-Key header. It is a random UUID, not my-key-123.

Observed results:

Header passed | Value actually sent -- | -- Idempotency-Key | ✅ user value idempotency-key | ❌ random UUID IDEMPOTENCY-KEY | ❌ random UUID stripe-account | ✅ user value accept-charset | ✅ user value

You can also reproduce it directly on the class:

HttpHeaders.of(Map.of("Idempotency-Key", List.of("old")))
    .withAdditionalHeader("idempotency-key", "new")
    .firstValue("Idempotency-Key");   // returns "old", expected "new"
Expected behavior

Header names should be compared case-insensitively, as the class's own map already does. A header added through withAdditionalHeader(s) should always replace an existing header with the same name in any casing. A user-supplied idempotency-key should always be sent.

Code snippets
Proposed fix in src/main/java/com/stripe/net/HttpHeaders.java:

`
public HttpHeaders withAdditionalHeaders(Map<String, List<String>> headerMap) {
  requireNonNull(headerMap);
  CaseInsensitiveMap<List<String>> newHeaderMap = CaseInsensitiveMap.of(this.headerMap);
  newHeaderMap.putAll(headerMap);
  return new HttpHeaders(newHeaderMap);
}
`
OS

macOS

Java version

Java 17 (Corretto 17.0.12)

stripe-java version

v34.2.0 (master @ a9bf6e40ed9)

API version

2026-11-18.endive

Additional context

The same code path also affects HttpURLConnectionClient, which merges the user-agent headers through withAdditionalHeaders. The existing RawRequestTest.testAdditionalHeaders* tests only use a new header name (foo), so they never cover overriding a default header in a different case. I'm happy to open a PR with the fix plus a regression test.

Ngôn ngữ chính
Java
Star
1k
Fork
412
Merge trung bình
4 ngày 12 giờ
Pull request đã merge (30 ngày)
16

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

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 stripe/stripe-java

Tất cả issue của stripe/stripe-java

Issue tương tự

Thêm issue về Java

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.