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

fix(fetch): properly redirect non-ascii location header url #2971

Merged
merged 8 commits into from
Mar 21, 2024

Conversation

Xvezda
Copy link
Contributor

@Xvezda Xvezda commented Mar 18, 2024

This relates to...

Rationale

The fetch function behaves differently from major web browsers(chrome, safari, firefox etc), and even CURL (with -L option).

Here is a minimal reproducible URL I made: https://non-ascii-location-header.sys.workers.dev/redirect

Worker code
export default {
	async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
		try {
			const url = new URL(request.url);
			if (url.pathname === '/redirect') {
				return new Response('', {
					status: 302,
					headers: {
						Location: '/안녕',
					},
				});
			}
			return new Response(`pathname ${url.pathname}`);
		} catch (e) {
			return new Response(`error: ${(e as Error).message}`, { status: 500 });
		}
	},
};

By accessing the address at https://non-ascii-location-header.sys.workers.dev/redirect directly
or https://non-ascii-location-header.sys.workers.dev/ first and then making a fetch request to the /redirect path using the developer tools leads to reaching https://non-ascii-location-header.sys.workers.dev/%EC%95%88%EB%85%95
which shows pathname /%EC%95%88%EB%85%95 as a result.

However, using undici fetch function does not behave in the same way.

> r = await fetch('https://non-ascii-location-header.sys.workers.dev/redirect')
... await r.text();
pathname /%C3%AC%C2%95%C2%88%C3%AB%C2%85%C2%95

This issue can also be observed at chrome web store: https://chromewebstore.google.com/detail/gbgmenmdglilmbmemagekpeaodajbeei

Welcome to Node.js v21.6.2.
Type ".help" for more information.
> await fetch('https://chromewebstore.google.com/detail/gbgmenmdglilmbmemagekpeaodajbeei')
Uncaught TypeError: fetch failed
    at node:internal/deps/undici/undici:12443:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async REPL1:1:33 {
  cause: Error: redirect count exceeded
      at makeNetworkError (node:internal/deps/undici/undici:5675:35)
      at httpRedirectFetch (node:internal/deps/undici/undici:10696:32)
      at httpFetch (node:internal/deps/undici/undici:10668:28)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async node:internal/deps/undici/undici:10440:20
      at async mainFetch (node:internal/deps/undici/undici:10430:20)
      at async httpFetch (node:internal/deps/undici/undici:10668:22)
      at async node:internal/deps/undici/undici:10440:20
      at async mainFetch (node:internal/deps/undici/undici:10430:20)
      at async httpFetch (node:internal/deps/undici/undici:10668:22)
}

I do not have an in-depth understanding of the fetch standard, so if this PR request violates the standard in any way, please feel free to close it.

Thank you.

Changes

Features

Bug Fixes

Breaking Changes and Deprecations

Status

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is correct spec-wise

@KhafraDev
Copy link
Member

@anonrig do you know why this is needed?

@anonrig
Copy link
Member

anonrig commented Mar 18, 2024

cc @lemire

@anonrig anonrig self-requested a review March 18, 2024 21:48
@anonrig
Copy link
Member

anonrig commented Mar 18, 2024

Please don't merge it until I have some time to review this tomorrow.

@KhafraDev
Copy link
Member

KhafraDev commented Mar 18, 2024

> base = `http://127.0.0.1:51619/redirect`
'http://127.0.0.1:51619/redirect'
> location = `/ì\x95\x88ë\x85\x95`
'/ì\x95\x88ë\x85\x95'
> new URL(location, base).toString()
'http://127.0.0.1:51619/%C3%AC%C2%95%C2%88%C3%AB%C2%85%C2%95'

I am confused

Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KhafraDev This is intended, and the output is correct. Look at the example below:

> let u = new URL('http://127.0.0.1:51619/redirect')
undefined
> u.pathname = '/ì\x95\x88ë\x85\x95'
'/ì\x95\x88ë\x85\x95'
> u
URL {
  href: 'http://127.0.0.1:51619/%C3%AC%C2%95%C2%88%C3%AB%C2%85%C2%95',
  origin: 'http://127.0.0.1:51619',
  protocol: 'http:',
  username: '',
  password: '',
  host: '127.0.0.1:51619',
  hostname: '127.0.0.1',
  port: '51619',
  pathname: '/%C3%AC%C2%95%C2%88%C3%AB%C2%85%C2%95',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

URL specification path state (https://url.spec.whatwg.org/#path-state) says that:

UTF-8 percent-encode c using the path percent-encode set and append the result to buffer.

@anonrig
Copy link
Member

anonrig commented Mar 18, 2024

You can replicate the same behavior on Safari as well.

@@ -44,7 +44,8 @@ function responseLocationURL (response, requestFragment) {
// 3. If location is a header value, then set location to the result of
// parsing location with response’s URL.
if (location !== null && isValidHeaderValue(location)) {
location = new URL(location, responseURL(response))
// Make sure location is properly encoded.
location = new URL(Buffer.from(location, 'ascii').toString('utf8'), responseURL(response))
Copy link
Member

@lemire lemire Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems strange to me: Buffer.from(location, 'ascii').toString('utf8'). I suggest at least some comments to explain what the intention is.

Copy link
Member

@lemire lemire Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-03-18 at 7 06 00 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "parsing location with response URL" mean precisely? I interpret it is a pathname setter...

The spec links "parsing" to https://url.spec.whatwg.org/#concept-url-parser

Copy link
Contributor Author

@Xvezda Xvezda Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lemire
This issue arises when the location header's address is not encoded as with encodeURI, but is responded in binary form.
For example, while the result of encodeURIComponent('안녕') is %EC%95%88%EB%85%95, in binary form, this appears as \xEC\x95\x88\xEB\x85\x95, same as the outcome of Buffer.from('안녕', 'utf8').toString('binary').
What I am attempting in this code is to normalize characters that exist outside the ASCII range into UTF-8 form and URL encode them, to ensure behavior consistent with major web browsers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KhafraDev Yes. Parsing with the base will work the same in ada, test here : ada-url/ada#611

@KhafraDev
Copy link
Member

This is intended, and the output is correct. Look at the example below:

This seems like quite a bad issue if it's causing pages not to load. How does Safari handle loading the google webstore in this case (example)?

@Xvezda
Copy link
Contributor Author

Xvezda commented Mar 19, 2024

I've added comments to make the code clearer and revised it to be more concise. Please take a look.

*/
function normalizeBinaryStringToUtf8 (value) {
return Buffer.from(value, 'binary').toString('utf8')
}
Copy link
Member

@lemire lemire Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  let b = Buffer.from(value, 'binary');
  if(Buffer.isAscii(b)) { return value; } // skip new string alloc.
  return b.toString('utf8'); // must alloc a new string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call Buffer.from(). Buffer.isAscii(b) is a static function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anonrig We get at the core of the issue here. The value is a string not a buffer. At some point it was wrongly interpreted as latin1 (maybe in http) and so we must now convert it to a buffer and then back to a string.

I think that this function should receive a Buffer and then the Buffer could be interpreted as UTF-8 and we'd be done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is far beyond the scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The irony is that we seem to try to purposefully (in http) prevent people from sending UTF-8 content as UTF-8... and then we go back and say "no, it is really UTF-8".

Much technical debt.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

@anonrig PTAL

@mcollina mcollina merged commit 1b62571 into nodejs:main Mar 21, 2024
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants