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

Log HTTP referer and origin #1429

Merged
merged 3 commits into from May 13, 2024
Merged

Log HTTP referer and origin #1429

merged 3 commits into from May 13, 2024

Conversation

rzats
Copy link
Contributor

@rzats rzats commented May 9, 2024

Related: #1386.

Summary:

Logs the HTTP Referer (or referrer) header as req_referrer and Origin header as req_origin, helping us track where requests come from. The easiest way to test this is via Postman:

Screenshot 2024-05-09 at 14 53 50

This generates the following log string:

{"method": "GET", "url": "http://localhost:10080/epidata/signal_dashboard_status/", "form_args": {}, "req_length": null, "remote_addr": "172.18.0.1", "real_remote_addr": "172.18.0.1", "user_agent": "PostmanRuntime/7.30.1", "api_key": null, "user_id": "None", "req_referrer": "https://test.com/test", "event": "Received API request", "logger": "server_api", "level": "info", "pid": 8, "timestamp": "2024-05-09T11:28:19.206454Z"}
{ ... "req_referrer": "https://test.com/test" ... }

An automated version of this test has also been included. If no referrer is present, the HTTP Origin is used instead.

As mentioned in #1386, we might also need to modify the referrer policy across our webapps in order to properly set the relevant fields. The default referrer policy is:

Send the origin, path, and querystring when performing a same-origin request. For cross-origin requests send the origin (only) when the protocol security level stays same (HTTPS→HTTPS). Don't send the [Referer](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer) header to less secure destinations (HTTPS→HTTP).

Since the API endpoint is HTTPS, for some webapps these headers are likely to be already set out of the box, but further testing will still be necessary.

Prerequisites:

  • Unless it is a documentation hotfix it should be merged against the dev branch
  • Branch is up-to-date with the branch to be merged with, i.e. dev
  • Build is successful
  • Code is cleaned up and formatted

@rzats rzats requested review from melange396 and dshemetov May 9, 2024 11:57
dshemetov
dshemetov previously approved these changes May 9, 2024
Copy link
Collaborator

@melange396 melange396 left a comment

Choose a reason for hiding this comment

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

Please also add to the log_info_with_request() method!

tests/server/test_validate.py Outdated Show resolved Hide resolved
Comment on lines 129 to 130
req_referrer=request.referrer,
req_origin=request.environ.get('HTTP_ORIGIN', '')
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • Can you move this to right underneath user_agent=... (since they come from the same place (request headers) and because they both identify client context)?

  • Why not just use request.origin? Presumably so you can default to the empty string, but why prefer that over None?

  • We dont need to keep both values; if both are provided, refer[r]er should be a superstring of origin.

Suggested change
req_referrer=request.referrer,
req_origin=request.environ.get('HTTP_ORIGIN', '')
refer_origin=request.referrer or request.origin,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

Copy link
Collaborator

@melange396 melange396 left a comment

Choose a reason for hiding this comment

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

just a couple quick changes and we should be good :)

src/server/_common.py Show resolved Hide resolved
src/server/_common.py Outdated Show resolved Hide resolved
tests/server/test_validate.py Outdated Show resolved Hide resolved
dshemetov
dshemetov previously approved these changes May 11, 2024
Copy link

sonarcloud bot commented May 13, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@melange396 melange396 merged commit 58175ce into dev May 13, 2024
7 checks passed
@melange396 melange396 deleted the rzatserkovnyi/referer-origin branch May 13, 2024 14:10
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.

None yet

3 participants