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

If request method is HEAD don't allow writing body bytes. #47480

Closed
ronag opened this issue Apr 8, 2023 · 11 comments
Closed

If request method is HEAD don't allow writing body bytes. #47480

ronag opened this issue Apr 8, 2023 · 11 comments
Labels
feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem.

Comments

@ronag
Copy link
Member

ronag commented Apr 8, 2023

I've encountered this bug a few times, and it's always confusing and tricky to debug. It would be easier if node did not allow writing bytes to the response body when the request method is HEAD, i.e. throw.

@ronag ronag added the feature request Issues that request new features to be added to Node.js. label Apr 8, 2023
@github-project-automation github-project-automation bot moved this to Pending Triage in Node.js feature requests Apr 8, 2023
@ronag ronag added good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem. labels Apr 8, 2023
@sumitshinde-84
Copy link

// Example Express.js route handler
app.head('/example', (req, res) => {
// Perform some actions
// ...

// If the request method is HEAD, only send headers, not body
if (req.method === 'HEAD') {
// Send response headers
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Custom-Header', 'Custom Value');
res.end();
} else {
// If the request method is not HEAD, send the complete response including body
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Custom-Header', 'Custom Value');
res.end('Response body');
}
});

@sumitshinde-84
Copy link

may be this solution will work , i would like to work on this issue

@HinataKah0
Copy link
Contributor

I think the issue is asking to add the check inside Node JS API (not inside the request handler).

The API used to write to the response body is this.
The code can be found in lib/_http_outgoing.js.
Maybe we can explore from that, CMIIW.

@bnoordhuis
Copy link
Member

It would be easier if node did not allow writing bytes to the response body

What does this mean? Ignore the data, raise an exception, something else?

It should already be ignoring it (and does, AFAICT.)

@faraz16iqbal
Copy link

faraz16iqbal commented Apr 9, 2023

Can I work on this issue?

@ronag
Copy link
Member Author

ronag commented Apr 11, 2023

What does this mean? Ignore the data, raise an exception, something else?

Raise an exception.

@bnoordhuis
Copy link
Member

That's a change that breaks longstanding behavior. I remember when Ryan introduced it, must've been around 2010, 2011.

Should be very carefully tested with citgm and probably not added to v20.x (because LTS.)

@Trott
Copy link
Member

Trott commented Apr 20, 2023

may be this solution will work , i would like to work on this issue

Can I work on this issue?

Hi, everyone. If you want to work on it, go for it. But no one gets to claim this issue or have it assigned to them or anything like that. You don't need to ask for permission to work on it. This is to avoid cookie-licking.

@mertcanaltin
Copy link
Member

Greetings, I tried to make a solution, do you think it is a good way, I would be very happy if you have comments ❤️

@gerrard00
Copy link
Contributor

gerrard00 commented Apr 26, 2023

I know there's a PR in progress, but I wanted to give it a crack. If nothing else I am now more familiar with the codebase.

My change would also effect responses with the status 204 or 304.

#47732

@marco-ippolito
Copy link
Member

I think this was solved with #47732 if not reopen

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. good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

No branches or pull requests

9 participants