Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native fetch client certificate support #48977

Closed
ottob opened this issue Jul 31, 2023 · 9 comments
Closed

Native fetch client certificate support #48977

ottob opened this issue Jul 31, 2023 · 9 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@ottob
Copy link

ottob commented Jul 31, 2023

What is the problem this feature will solve?

I could not find a way to make a TLS request with a client certificate (mutual tls) with the new native fetch client.

With node-fetch I could use an https.Agent:

 const agent = new https.Agent({
   cert,
   key,
   keepAlive: true,
   timeout: 3 * 1000,
})

But the agent option is not available in the api:
Object literal may only specify known properties, and 'agent' does not exist in type 'RequestInit'

Is there a way of doing that that I am missing?

What is the feature you are proposing to solve the problem?

I read that the new client is based on undici and I found their docs for this here:
https://undici.nodejs.org/#/docs/best-practices/client-certificate

Could the undici Client be exposed from node?

What alternatives have you considered?

No response

@ottob ottob added the feature request Issues that request new features to be added to Node.js. label Jul 31, 2023
@alexwhitman
Copy link

alexwhitman commented Jul 31, 2023

You can do it but you have to install undici.

const { Agent } = require('undici');

fetch(url, {
    dispatcher: new Agent({
        connect: {
            cert: cert,
            key: key,
            ca: ca
        }
    })
});

Could the undici Client be exposed from node?

That would useful to not have to install undici separately if it's bundled in the core anyway.

@ottob
Copy link
Author

ottob commented Jul 31, 2023

Thanks for the workaround.

That would useful to not have to install undici separately if it's bundled in the core anyway.

Yes, that would be very nice. The point of using the native version is to not have to depend on external libraries.

@ottob
Copy link
Author

ottob commented Jul 31, 2023

Will that workaround work? The type definition for fetch in native does not expose dispatcher on RequestInit.

@alexwhitman
Copy link

It works, I've got code that uses it against a service requiring mTLS.

@ottob
Copy link
Author

ottob commented Jul 31, 2023

It works, I've got code that uses it against a service requiring mTLS.

Strange. Im using "undici": "5.22.1" and cert and key are not available in the Agent options:

Object literal may only specify known properties, and 'cert' does not exist in type 'Options'

Also, are you sure you are using fetch from core and not importing it from undici?

import { fetch, Agent } from 'undici'

@alexwhitman
Copy link

My mistake, I typed the example out wrong rather than copying/pasting. The cert, key and ca parameters should be within a connect object. I've updated the earlier example.

@ottob
Copy link
Author

ottob commented Jul 31, 2023

I've updated the earlier example.

Thanks, I got it to work now. dispatcher is not defined in the RequestInit type for core fetch. But if I cast it with as any it works.

So if the undici Agent was exposed from core this would work out of the box. Lets hope this will happen.

@ottob
Copy link
Author

ottob commented Jul 31, 2023

And there already seems to be an issue for that: #47592

@kchojhu
Copy link

kchojhu commented May 28, 2024

Thanks for this post and it worked. Please include this soon to next minor versions of Next.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

3 participants