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

Aperta Adatta ai principianti
#2,341 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java

Direzione di ricerca

Inizia con HttpHeaders.withAdditionalHeaders() e la riproduzione nell’issue, poi segui il modo in cui rawRequest() applica le intestazioni aggiuntive. Aggiungi test per sostituire un’intestazione esistente con un nome che differisce per maiuscole e minuscole e conferma che il valore fornito venga mantenuto; l’issue non indica un file di test, quindi individua i test esistenti di HttpHeaders o delle richieste raw. Il lavoro è completato quando le riproduzioni restituiscono in modo coerente il nuovo valore fornito.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Java
Stelle
1k
Fork
412
Merge medio
4g 12h
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di stripe/stripe-java

Tutte le issue di stripe/stripe-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.