node icon indicating copy to clipboard operation
node copied to clipboard

feat(esm): expose `User-Agent` header

Open Fyko opened this issue 3 years ago • 17 comments

Closes #43851

Fyko avatar Jul 15 '22 18:07 Fyko

Review requested:

  • [ ] @nodejs/modules

nodejs-github-bot avatar Jul 15 '22 18:07 nodejs-github-bot

I'm extremely wary of this being used as a security related issue. I would be more comfortable if this can be disabled or custom configured.

On Fri, Jul 15, 2022, 1:31 PM Node.js GitHub Bot @.***> wrote:

Review requested:

— Reply to this email directly, view it on GitHub https://github.com/nodejs/node/pull/43852#issuecomment-1185802138, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABZJI4X6SL2KPYWLL3DEUTVUGVB5ANCNFSM53WM77PQ . You are receiving this because you are on a team that was mentioned.Message ID: @.***>

bmeck avatar Jul 15 '22 18:07 bmeck

Perhaps this default with an environment variable to customize makes sense. Definitely seems a useful feature.

guybedford avatar Jul 15 '22 19:07 guybedford

hey, @Fyko would you mind adding tests for this feature?

ErickWendel avatar Aug 08 '22 18:08 ErickWendel

hey, @Fyko would you mind adding tests for this feature?

I would love to! Except... I'm not quite sure how. Could you point me in the right direction? @ErickWendel

Edit: did some searching, this looks relevant https://github.com/nodejs/node/commit/c0f4289c65

Fyko avatar Aug 08 '22 18:08 Fyko

hey, @Fyko would you mind adding tests for this feature?

I would love to! Except... I'm not quite sure how. Could you point me in the right direction? @ErickWendel

Edit: did some searching, this looks relevant c0f4289c65

Sure. I think it'd be easier if you go to this file https://github.com/nodejs/node/blob/3fcf2fe78e0b2af6f67dff767bb440a5265f661a/test/parallel/test-fetch.mjs#L32 and add an assertion for the User-Agent

assert.strictEqual(response.headers["User-Agent"], `Node.js/${process.version}`);

WDYT?

ErickWendel avatar Aug 08 '22 18:08 ErickWendel

Not quite sure if that would work considering the UA logic was added to fetch_module.js.

I found the file https://github.com/nodejs/node/blob/main/test/es-module/test-http-imports.mjs and considered adding:

if (_req.getHeaders()['user-agent'] !== `Node.js/${process.version}`) {
	res.writeHead(400);
	res.end('bad user-agent');
	return;
}

right after the createServer call https://github.com/nodejs/node/blob/472edc775d683aed2d9ed39ca7cf381f3e7e3ce2/test/es-module/test-http-imports.mjs#L68

Fyko avatar Aug 08 '22 19:08 Fyko

I suggested going to the tests-fetch file as it ensures what are the default values returned from the fetch response.

As your change will affect fetch.get adding by default the User Agent flag I think this might be the best place to check it out.

The file you mentioned has another goal. There you'd test the behavior when importing a module from an URL such as import module from "HTTP://domain/mymodule"

Not quite sure if that would work considering the UA logic was added to fetch_module.js.

Even though the logic was added to fetch_module, the test-fetch actually uses it (but notice that fetch is a global object that's why it's not being imported there)

WDYT?

ErickWendel avatar Aug 08 '22 19:08 ErickWendel

It seems the test you made is returning undefined. Was it working on your local environment? Lmk if you need something 🤩

ErickWendel avatar Aug 09 '22 14:08 ErickWendel

Hey @Fyko do you need any help on this?

ErickWendel avatar Aug 23 '22 18:08 ErickWendel

I'll get around to this tomorrow. Sorry for the delay!

Fyko avatar Aug 25 '22 17:08 Fyko

@jasnell @guybedford @bmeck

this pr has my attention again, and im adamant about getting this done. if not a default User-Agent, what would you suggest? env var? what would you name it? NODE_MODULE_FETCH_UA=node/16.x.x? i feel it would be better to be a toggle.

Fyko avatar Sep 15 '22 21:09 Fyko

if not a default User-Agent

I think we should set it by default, and users could override it on a per-fetch basis:

fetch(url, { headers: { 'User-Agent': 'whatever' } })

If we need a global override, that could happen later.

GeoffreyBooth avatar Sep 15 '22 22:09 GeoffreyBooth

i think you're misunderstanding. the purpose of my PR is to implement the User-Agent header only when fetching remote modules -- URL imports

Fyko avatar Sep 15 '22 22:09 Fyko

i think you’re misunderstanding. the purpose of my PR is to implement the User-Agent header only when fetching remote modules – URL imports

You mean --experimental-network-imports? I think it should apply to all fetch calls, both as a result of network imports and user code. I don’t think Node’s calls need a way to be customized.

GeoffreyBooth avatar Sep 15 '22 22:09 GeoffreyBooth

Got it, just making sure we're on the same page. I'm not exactly sure what to do here -- I've got conflicting opinions from different members. I'm going to fix my tests so CI passes and we'll see what happens! 🤗

Fyko avatar Sep 15 '22 22:09 Fyko

Got it, just making sure we’re on the same page. I’m not exactly sure what to do here – I’ve got conflicting opinions from different members. I’m going to fix my tests so CI passes and we’ll see what happens! 🤗

https://github.com/nodejs/node/issues/43851 doesn’t mention this applying to only --experimental-network-imports, so I think maybe that issue could be used to debate what we want the intended behavior should be, and this PR discussion could be restricted to technical discussion of the implementation of whatever design we settle on. We shouldn’t land something until we have a decision regarding what we want. If we don’t reach consensus in discussion on that thread it can go to the TSC for a vote.

Also, please always rebase on main rather than merging main into your branch.

GeoffreyBooth avatar Sep 15 '22 23:09 GeoffreyBooth

Any updates on this?

renhiyama avatar Jan 07 '24 12:01 renhiyama