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] ctx.request.origin ignores proxy flag (X-Forwarded-Proto) #1746

Open
3 tasks done
soknifedev opened this issue Feb 3, 2023 · 2 comments
Open
3 tasks done

[fix] ctx.request.origin ignores proxy flag (X-Forwarded-Proto) #1746

soknifedev opened this issue Feb 3, 2023 · 2 comments
Labels

Comments

@soknifedev
Copy link

soknifedev commented Feb 3, 2023

Describe the bug

When using the proxy flag set to true, ctx.request.origin ignores it, returning http:// instead of https:// (taken from X-Forwarded-Proto, available in ctx.protocol)

Node.js version: v16.14.0

OS version: Debian 10

Description: With koa's proxy flag to true, ctx.request.origin returns wrong protocol because it doesn't match with the X-Forwarded-Proto header (available in ctx.protocol).

Actual behavior [app.proxy=true]

http://mywebsite.com

Expected behavior [app.proxy=true]

https://mywebsite.com

Code to reproduce

const Koa = require('koa');
const app = new Koa({ proxy: true });
app.use(ctx => {
  ctx.body = 'Hello Koa, origin:' + ctx.request.origin + '; protocol=' + ctx.protocol;
});

app.listen(3000);

I didn't submit a pull request since this is a very simple bug and it requires a reverse proxy setup (like nginx) to reproduce

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@soknifedev soknifedev added the bug label Feb 3, 2023
@siakc
Copy link

siakc commented Dec 22, 2023

Could not reproduce. Code looked OK. Behavior looked OK. When proxy option is on, any X-Forwarded-Proto value is shown as the protocol and ctx.protocol is consistent with ctx.request.origin.

@soknifedev
Copy link
Author

Could not reproduce. Code looked OK. Behavior looked OK. When proxy option is on, any X-Forwarded-Proto value is shown as the protocol and ctx.protocol is consistent with ctx.request.origin.

Sorry for late reply. I'll re-verify the consistency of ctx.protocol again.

I did it before using a nginx reverse proxy + cloudflare, that may be causing the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants