Skip to content

Headers.append: xxx is an invalid header value #2219

Open
@brunoscopelliti

Description

@brunoscopelliti

Checklist

Description

I've reproduced the problem also with nextjs-auth0 sample app.

Add a new endpoint, that uses auth0.updateSession to update session.

export const PATCH = async function route(request) {
  try {
    const session = await auth0.getSession();

    const response = NextResponse.json({
      data: session.user,
      status: 'OK'
    });

    await auth0.updateSession(request, response, { ...session, user: { ...session.user, updated: true } });

    return response;
  } catch (error) {
    return NextResponse.json({ error: error.message }, { status: error.status || 500 });
  }
};

When I call this endpoint I get this error:

'Headers.append: "append(name, value) {
        webidl.brandCheck(this, _Headers);
        webidl.argumentLengthCheck(arguments, 2, "Headers.append");
        const prefix = "Headers.append";
        name = webidl.converters.ByteString(name, prefix, "name");
        value = webidl.converters.ByteString(value, prefix, "value");
        return appendHeader(this, name, value);
      }" is an invalid header value.'

The error is triggered in this function:

createRequestCookies(req) {
    const headers = new Headers();
    for (const key in req.headers) {
        if (Array.isArray(req.headers[key])) {
            for (const value of req.headers[key]) {
                headers.append(key, value);
            }
        }
        else {
            headers.append(key, req.headers[key] ?? "");
        }
    }
    return new _cookies_js__WEBPACK_IMPORTED_MODULE_4__.RequestCookies(headers);
}

I suspect the error stems from the fact we are using for…in loop, that loops over the properties of the headers object (append, delete, get, …), instead of for…of, that loops over the list of actual headers (['accept', '*/*'], ['accept-encoding', 'gzip, deflate, br, zstd'], …).

Could someone please confirm this?

Reproduction

See description.

Additional context

No response

nextjs-auth0 version

4.8.0

Next.js version

15.2.4

Node.js version

22.12

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions