Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Add request feature detection - #79

Merged
mattrobenolt merged 5 commits into
getsentry:masterfrom
seegno-forks:feature/request-feature-detection
Jun 23, 2014
Merged

mattrobenolt merged 5 commits into
getsentry:masterfrom
seegno-forks:feature/request-feature-detection

Conversation

@ruimarinho

Copy link
Copy Markdown
Contributor
  • Add several tests for each request property available on the HTTP
    interface.
  • Add support for request abstractions such as Koa and Express.
  • Improve error handling for malformed or incomplete request objects.
  • Add support for X-Forwarded-Port for more robus HTTPS detection.

* Add several tests for each request property available on the HTTP
  interface.
* Add support for request abstractions such as Koa and Express.
* Improve error handling for malformed or incomplete request objects.
* Add support for `X-Forwarded-Port` for more robus HTTPS detection.
Comment thread lib/parsers.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

X-Forwarded-Proto doesn't keep getting appended to like X-Forwarded-For, so there's no need to split apart.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, that is not the case. X-Forwarded-Proto also gets multiple protocols added. I personally am aware about support for this in koa and express.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's dumb. That 100% doesn't make sense why anything would do that.

What proxy out there appends these together? There's only one valid value, and that's https. There's no value whatsoever in setting it to http or appending a chain of them.

So what out there is appending these? I know of none.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And I'd argue that if something were doing that, they're doing it wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I know this is irrelevant at this point, but I'm curious about your statement. Other searches point to the same logic - e.g., one of the most popular php frameworks also does it. Is it precaution, lack of standard or just incorrect? I'm not aware of any proxy doing this, but I'd like to know from your experience though.

@mattrobenolt

Copy link
Copy Markdown
Contributor

Can you comment on where all the information is coming from? Especially when they're values coming from a framework and why we have to do the various fallbacks? I'll never remember and someone will ask me in the future. :)

@ruimarinho

Copy link
Copy Markdown
Contributor Author

@mattrobenolt updated with all the suggestions you asked for. I think the only left for merging this PR is deciding whether the x-forwarded-stuff should be kept or not! Let me know how you'd like to proceed.

Comment thread package.json Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we really need to include this dependency? It's very trivial to check if something is an object.

Why can't we just do: if (''+req.cookies === '[object Object]') ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No. ''+(new function(){}).toString() === ''+({}) which is the reason why I've used the lodash function. Anyway, I've removed this and decided to only rely on the cookie parser instead.

Rui Marinho added 3 commits June 4, 2014 00:38
This should be moved to the middleware layer instead, delegating
`X-Forwarded-For` to `req.ip` and `X-Forwarded-Proto` to `req.secure`.
Instead of looking at `req.cookies`, the parser will only detect cookies
set in headers because ultimately that is where middleware originally
grabs the information from. This results in a much simplified logic.
koa uses `request.header` as an alias to `req.headers`, while express
inherits `req.headers` from IncomingMessage.
@ruimarinho

Copy link
Copy Markdown
Contributor Author

@mattrobenolt do you think there is anything left to do before merging this?

@mattrobenolt

Copy link
Copy Markdown
Contributor

Any idea why this is erroring on 0.8? I'm tempted to just pull this and try to fix it on master. :/

@ruimarinho

Copy link
Copy Markdown
Contributor Author

nock no longer supports node < 0.10 due to the use of setImmediate (see issue). They specifically mention that the use of process.nextTick is not desired, so a polyfill cannot be used to re-introduce this functionality. This is one of the reasons why tests are failing. Do you want to drop support for node < 0.10 or lock nock to an older version?

@ruimarinho

Copy link
Copy Markdown
Contributor Author

Confirmed tests are passing in node 0.8 with [email protected]. My opinion is that support for node 0.8 should be dropped with a note on the README that things are likely working (as tests show), but that no further development effort will be done towards maintaining it.

@mattrobenolt

Copy link
Copy Markdown
Contributor

I think I'd rather just pin nock. Thanks for looking into this, really. :) I'll merge this and fix in master.

mattrobenolt added a commit that referenced this pull request Jun 23, 2014
@mattrobenolt
mattrobenolt merged commit 5d50f47 into getsentry:master Jun 23, 2014
@ruimarinho

Copy link
Copy Markdown
Contributor Author

Thank you for your time in reviewing this PR! Could you please tag a new release when you have a moment to spare? Cheers!

@ruimarinho
ruimarinho deleted the feature/request-feature-detection branch June 23, 2014 15:01
@mattrobenolt

Copy link
Copy Markdown
Contributor

Yep, I'm working on a couple things on master, but it'll be released today.

@ruimarinho

Copy link
Copy Markdown
Contributor Author

@mattrobenolt are those changes complete? A version bump would be greatly appreciated :) sorry for pushing this issue again.

@mattrobenolt

Copy link
Copy Markdown
Contributor

I just pushed 0.7.0 to npm. :)

@ruimarinho

Copy link
Copy Markdown
Contributor Author

Thank you!

@wbyoung

wbyoung commented Aug 27, 2017

Copy link
Copy Markdown

It looks like this reverted the changes made in #78. What's the desired way to set the user when running behind a proxy that sets X-Forwarded-For now? To write your own middleware like:

/* @flow */

import Raven from 'raven';
import type {
  $Request as Request,
  $Response as Response,
  NextFunction,
  Middleware,
} from 'express';

export default function sentryContextMiddleware(): Middleware {
  return (req: Request, res: Response, next: NextFunction) => {
    const remoteIp =
      (req.headers['x-forwarded-for'] || '').split(',')[0] ||
      (req.connection && (req.connection: any).remoteAddress);

    const user: { [string]: mixed } = {
      ip_address: remoteIp, // eslint-disable-line camelcase
    };

    Raven.mergeContext({ user });

    next();
  };
}

Should that (or is that) documented somewhere?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants