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

Reject invalid HTTP methods and resources #1019

Merged

Conversation

andy-maier
Copy link
Contributor

@andy-maier andy-maier commented Mar 30, 2024

This change addresses issue #1018 (currently, any HTTP method is handled by returning success and metrics data, which causes network scanners to report issues).

Note, this needs careful review w.r.t.:

  • Does Prometheus issue any HTTP requests that are now rejected.
  • Does HEAD need to be supported.
  • Does the /metrics resource for which metrics are returned, need to be configurable by the users of this package.
  • Does the strict handling need to be enabled/disabled by the users of this package.
  • Are unit tests needed.

Note, the pinning of asgiref==3.6.0 removes a test error in the py3.8 tox environment (same fix that already existed for the pypy3.8 tox environment). I don't know why the error on py3.8 comes up in the first place. I guess someone more experienced than me needs to look at that.

For details, see the commit message.

Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

I double checked and Prometheus only issues GET requests and there is no way to override that, so I think blocking non-GET methods is reasonable.

Would you be willing to move the test fix into a separate PR?

# Serve empty response for browsers
status = '200 OK'
headers = [('', '')]
output = b''
elif environ['PATH_INFO'].strip('/') not in ('', 'metrics'):
Copy link
Member

@csmarchbanks csmarchbanks Apr 8, 2024

Choose a reason for hiding this comment

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

This is the breaking change I am most worried about, some users might be using non-standard paths to get their metrics as that all works right now and we don't require /metrics. What would you think of omitting this check?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with this. Even though /metrics is the convention, it's not enforced. e.g. for the client_golang, one can specify anything when they register a handler https://github.com/prometheus/client_golang/blob/e133e490296d2ff915bfc23cdec87c235ad36ef3/examples/simple/main.go#L43

Copy link
Contributor Author

@andy-maier andy-maier May 31, 2024

Choose a reason for hiding this comment

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

I can move the test fix into a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was also unsure about the path checking and I can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have now updated the PR to remove the path check for GET. For consistency, I have also removed the path check for OPTIONS.

@kakkoyun @csmarchbanks Could you please review again, and merge if you agree?

@andy-maier
Copy link
Contributor Author

@csmarchbanks I moved the test fix into its own PR #1053 .

This change addresses the issue that currently, any HTTP method is handled
by returning success and metrics data, which causes network scanners to
report issues.

Details:

* This change rejects any HTTP methods and resources other than the following:

    OPTIONS (any) - returns 200 and an 'Allow' header indicating allowed methods
    GET (any) - returns 200 and metrics
    GET /favicon.ico - returns 200 and no body (this is no change)

  Other HTTP methods than these are rejected with 405 "Method Not Allowed"
  and an 'Allow' header indicating the allowed HTTP methods.

  Any returned HTTP errors are also displayed in the response body after a
  hash sign and with a brief hint,
  e.g. "# HTTP 405 Method Not Allowed: XXX; use OPTIONS or GET".

Signed-off-by: Andreas Maier <[email protected]>
Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the updates!

@csmarchbanks csmarchbanks merged commit 7c45f84 into prometheus:master Aug 2, 2024
11 checks passed
@andy-maier andy-maier deleted the andy/strict-http-methods branch September 28, 2024 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants