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 edge cases with unknown IP address #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

yuekui
Copy link
Contributor

@yuekui yuekui commented Jul 25, 2024

For some reason, HTTP_X_FORWARDED_FOR might be set as "unknown, 128.1.1.9", we should be able to parse and use the closest valid value.

Summary by Sourcery

This pull request addresses an edge case in IP address parsing where the HTTP_X_FORWARDED_FOR header might contain 'unknown'. The code now correctly parses and uses the closest valid IP address. Additionally, a test has been added to ensure this functionality works as expected.

  • Bug Fixes:
    • Fixed edge case where HTTP_X_FORWARDED_FOR header contains 'unknown' by parsing and using the closest valid IP address.
  • Tests:
    • Added test to verify correct IP address is logged when HTTP_X_FORWARDED_FOR contains 'unknown'.

Copy link
Contributor

sourcery-ai bot commented Jul 25, 2024

Reviewer's Guide by Sourcery

This pull request addresses the issue where HTTP_X_FORWARDED_FOR might contain 'unknown' followed by a valid IP address. The _get_ip_address method in rest_framework_tracking/base_mixins.py was updated to parse and use the closest valid IP address. Additionally, a new test case was added to tests/test_mixins.py to ensure the new logic works as expected.

File-Level Changes

Files Changes
rest_framework_tracking/base_mixins.py
tests/test_mixins.py
Enhanced IP address extraction logic to handle edge cases with 'unknown' values and added corresponding test cases.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @yuekui - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 4 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines +114 to +115
raw_possibles = request.META.get("HTTP_X_FORWARDED_FOR", "").split(",")
raw_possibles += request.META.get("REMOTE_ADDR", "").split(",")
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Combining lists directly can be inefficient.

Using the += operator to combine lists can be less efficient than using extend(), especially for large lists. Consider using raw_possibles.extend(request.META.get("REMOTE_ADDR", "").split(",")) for better performance.

Suggested change
raw_possibles = request.META.get("HTTP_X_FORWARDED_FOR", "").split(",")
raw_possibles += request.META.get("REMOTE_ADDR", "").split(",")
raw_possibles = request.META.get("HTTP_X_FORWARDED_FOR", "").split(",")
raw_possibles.extend(request.META.get("REMOTE_ADDR", "").split(","))

for addr in possibles:
try:
return str(ipaddress.ip_address(addr))
except ValueError:
pass

return ipaddr
return raw_possibles[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Returning the first element of raw_possibles may not be reliable.

If raw_possibles is empty, this line will raise an IndexError. Consider adding a check to ensure raw_possibles is not empty before attempting to access its first element.

rest_framework_tracking/base_mixins.py Outdated Show resolved Hide resolved
rest_framework_tracking/base_mixins.py Outdated Show resolved Hide resolved
tests/test_mixins.py Outdated Show resolved Hide resolved
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.

1 participant