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

未关闭 适合新手
#2,341 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java

调研方向

从 HttpHeaders.withAdditionalHeaders() 和 issue 中的复现步骤开始,然后追踪 rawRequest() 如何应用额外的标头。添加测试,覆盖用大小写不同的名称替换现有标头,并确认提供的值得以保留;issue 未指出测试文件,因此请查找现有的 HttpHeaders 或原始请求测试。复现步骤始终返回新提供的值时,即为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Java
星标
1k
派生
412
平均合并
4 天 12 小时
30 天内合并 PR
16

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

stripe/stripe-java 的其他 Issue

查看 stripe/stripe-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。