Skip to content

EventSource sets credentials to "omit" instead of "include" when withCredentials is true #5378

@AliMahmoudDev

Description

@AliMahmoudDev

Bug Description

When creating an EventSource with withCredentials: true, the underlying fetch request sets credentials to "omit" instead of "include". This means cookies and auth headers are not sent with the EventSource request, even though the user explicitly requested credentials.

Reproduction

import { EventSource } from "undici"

const es = new EventSource("http://localhost:3000/events", {
  withCredentials: true
})

Expected Behavior

Per the WHATWG EventSource spec, when withCredentials is true, the request credentials mode should be "include".

Actual Behavior

In lib/web/eventsource/eventsource.js lines 167-169:

credentials: corsAttributeState === "anonymous"
  ? "same-origin"
  : "omit",

When withCredentials is true, corsAttributeState is set to USE_CREDENTIALS (not "anonymous"), so the ternary evaluates to "omit". It should evaluate to "include".

The fix is straightforward: change "omit" to "include":

- credentials: corsAttributeState === "anonymous"
-   ? "same-origin"
-   : "omit",
+ credentials: corsAttributeState === "anonymous"
+   ? "same-origin"
+   : "include",

Environment

  • undici 8.3.0 (current main)
  • Node.js v22.x

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions