Skip to content

Commit

Permalink
Fallback to 0.0.0.0 if REMOTE_ADDR is an empty string
Browse files Browse the repository at this point in the history
It complements 5ebc785 which fixes the
issue when REMOTE_ADDR evaluates to None. It appears that on some
environment it can also be an empty string. With this change, both cases
are supported.

See also dj-stripe#640
  • Loading branch information
Julien Enselme authored and therefromhere committed May 20, 2019
1 parent 5438071 commit 7d49351
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ History
This is a bugfix-only version:

- Don't save event objects if the webhook processing fails (#832).
- Fixed IntegrityError when ``REMOTE_ADDR`` is an empty string.

2.0.1 (2019-04-29)
------------------
Expand Down
2 changes: 1 addition & 1 deletion djstripe/models/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def from_request(cls, request):
body = "(error decoding body)"

ip = request.META.get("REMOTE_ADDR")
if ip is None:
if not ip:
warnings.warn(
"Could not determine remote IP (missing REMOTE_ADDR). "
"This is likely an issue with your wsgi/server setup."
Expand Down
18 changes: 17 additions & 1 deletion tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_webhook_no_signature(self):
self.assertEqual(resp.status_code, 400)
self.assertEqual(WebhookEventTrigger.objects.count(), 0)

def test_webhook_no_remote_addr(self):
def test_webhook_remote_addr_is_none(self):
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Expand All @@ -185,6 +185,22 @@ def test_webhook_no_remote_addr(self):
event_trigger = WebhookEventTrigger.objects.first()
self.assertEqual(event_trigger.remote_ip, "0.0.0.0")

def test_webhook_remote_addr_is_empty_string(self):
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Client().post(
reverse("djstripe:webhook"),
"{}",
content_type="application/json",
HTTP_STRIPE_SIGNATURE="PLACEHOLDER",
REMOTE_ADDR="",
)

self.assertEqual(WebhookEventTrigger.objects.count(), 1)
event_trigger = WebhookEventTrigger.objects.first()
self.assertEqual(event_trigger.remote_ip, "0.0.0.0")

@patch(
"djstripe.models.WebhookEventTrigger.validate", return_value=True, autospec=True
)
Expand Down

0 comments on commit 7d49351

Please sign in to comment.