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

FormData body throws if request is redirected #3803

Closed
DTrombett opened this issue Nov 5, 2024 · 10 comments · Fixed by #3815
Closed

FormData body throws if request is redirected #3803

DTrombett opened this issue Nov 5, 2024 · 10 comments · Fixed by #3815
Labels
bug Something isn't working

Comments

@DTrombett
Copy link
Contributor

Bug Description

When sending a FormData as body with request or stream, the request fails if redirected with the following TypeError: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Array

Reproducible By

In the following example a POST request is made to http://github.com, which automatically redirects to https://github.com. The issue is reproducible with any other url that triggers a redirect.

import { FormData, request } from "undici";

const body = new FormData();

body.append("hello", "world");
await request("http://github.com", {
	body,
	method: "POST",
	maxRedirections: 1,
});

Expected Behavior

Not sure about the specs but the body should be either redirected or discarded based on the redirect type, without any errors.

Logs & Screenshots

node:buffer:776
    throw new ERR_INVALID_ARG_TYPE(
          ^

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Array
    at Function.byteLength (node:buffer:776:11)
    at AsyncWriter.write (D:\test\node_modules\undici\lib\dispatcher\client-h1.js:1259:24)
    at writeIterable (D:\test\node_modules\undici\lib\dispatcher\client-h1.js:1219:19) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Environment

Windows 11 Home, Node v22.11.0

Additional context

Tested with various redirect urls and the issue is always reproducible

@DTrombett DTrombett added the bug Something isn't working label Nov 5, 2024
@KhafraDev
Copy link
Member

What version of undici are you using?

@DTrombett
Copy link
Contributor Author

I'm pretty sure it's the latest because I had just upgraded but I'll let you know asap

@KhafraDev
Copy link
Member

KhafraDev commented Nov 6, 2024

This is a bug in v6 that is patched in v7 (7.0.0-alpha.3). The fix will have to be backported.

@DTrombett
Copy link
Contributor Author

I see, I'll try updating to alpha, thanks!

@mcollina
Copy link
Member

mcollina commented Nov 7, 2024

@KhafraDev do you know which PR?

@DTrombett
Copy link
Contributor Author

It seems that using the alpha the redirect is not followed at all? Not sure if I'm doing something wrong but I'll try to send an example

@DTrombett
Copy link
Contributor Author

DTrombett commented Nov 7, 2024

import { request } from "undici";

const data = await request("http://github.com", { maxRedirections: 8 });

console.log(data.statusCode, data.headers.location);

This logs 301 https://github.com/ while it should log 200 undefined

Unless I missed a breaking change in the 7.0

Edit: Manually using the RedirectHandler works

@DTrombett
Copy link
Contributor Author

So, as I said above, maxRedirections doesn't redirect the request in the latest alpha version so it's impossible to test the same code, but using a RedirectHandler I can confirm that the issue is still present

@KhafraDev
Copy link
Member

maxRedirections is ignored unless you are using the redirect interceptor.

@DTrombett
Copy link
Contributor Author

The patch seems not to work with h2:

import { Agent, FormData, interceptors, request } from "undici";

const body = new FormData();

body.append("hello", "world");
await request("http://github.com", {
	dispatcher: new Agent({ allowH2: true }).compose(
		interceptors.redirect({ maxRedirections: 1 }),
	),
	body,
	method: "POST",
});
      throw new ERR_INVALID_ARG_TYPE(
            ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Array
    at _write (node:internal/streams/writable:482:13)
    at Writable.write (node:internal/streams/writable:510:10)
    at writeIterable (D:\-\node_modules\undici\lib\dispatcher\client-h2.js:740:28) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants