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: explicitly check that request.body is a string #1010

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/middleware/node/get-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import AggregateError from "aggregate-error";
type IncomingMessage = any;

export function getPayload(request: IncomingMessage): Promise<string> {
if ("body" in request) {
if (request.body) {
Copy link
Member

Choose a reason for hiding this comment

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

@wolfy1339 I feel like we should probably remove this check entirely, and change our else branch to be else if typeof 'string', as this change means an empty string won't get treated as a string anymore - while in practice I doubt it'll break anything, I think we should ensure we have a clear official logic which I think should be "if object, we assume; if string, we assume" (i.e. regardless of actual value)

what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks Gareth, yes that sounds like it could be a good approach, although I've just briefly tested that this morning and it breaks quite a few unit tests. I can investigate it further this afternoon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After further investigation if (request.body === undefined || typeof request.body === "string") // parsing block gets the unit tests close to passing, but unfortunately I don't think is the right approach.

The existence of a body indicates it has been parsed even if it was a string. Many of the unit tests make this assumption[0]. This is also how node:http treats the body, the body key isn't created unless by a body parser or external logic.

The crux of the issue is @fastify/middie attaches the body key even if it's undefined. I think if (request.body !== undefined) will be a good approach, it will solve the case for Middie while also aligning the handling of body with node:http, and allows for the null and '' case.

[0] such as resolves with the body, if passed via the request within get-payload.test.ts.

Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused by this - just to be sure, when you say "... gets the unit tests close to passing", you are accounting for the nested conditions right? because otherwise yes the tests will fail since you'll be disabling the "is the request.body already a parsed body?" flow.

What I'm getting at is our logic is currently this:

if ('body' in request) {
  if (<something about body>) {
    return ...;
  } else {
    return ...;
  }
}

and with your change, it strikes me that we should be able to do this instead:

if(<something about the body>) {
  return ...;
} else if(typeof request.body === 'string') {
  return ...;
}

From what I'm seeing that should be equivalent because we explicitly type the return of this function as a string and our comment says it should be a string even though we're technically not checking it is a string - if what you're saying is that we actually are sometimes not returning a string due to that lack of check, then that's a whole other concerning thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apologies, I misunderstood with my initial attempt. I've just pushed a change that I think you're meaning. Yes this works well, it fixes the issue with the compatibility with @fastify/middie and all unit tests are passing as expected.

if (
typeof request.body === "object" &&
"rawBody" in request &&
Expand Down
15 changes: 15 additions & 0 deletions test/integration/get-payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,19 @@ describe("getPayload", () => {

expect(await promise).toEqual("foo");
});

it("resolves with a string if the body key of the request is defined but value is undefined", async () => {
const request = new EventEmitter();
// @ts-ignore body is not part of EventEmitter, which we are using
// to mock the request object
request.body = undefined;

const promise = getPayload(request);

// we emit data, to ensure that the body attribute is preferred
request.emit("data", "bar");
request.emit("end");

expect(await promise).toEqual("bar");
});
});
Loading