allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [1/2] git commit: [#5455] Fallback to REMOTE_ADDR if X_FORWARDED_FOR not present
Date Thu, 13 Dec 2012 16:10:53 GMT
Updated Branches:
  refs/heads/master 78888e7d3 -> c09d674fb


[#5455] Fallback to REMOTE_ADDR if X_FORWARDED_FOR not present


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/c09d674f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/c09d674f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/c09d674f

Branch: refs/heads/master
Commit: c09d674fbc7b872c5fc1dc251727605a67d40aa9
Parents: 424999c
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Thu Dec 13 15:49:52 2012 +0000
Committer: Dave Brondsema <dbrondsema@geek.net>
Committed: Thu Dec 13 15:53:52 2012 +0000

----------------------------------------------------------------------
 Allura/allura/lib/spam/akismetservice.py |    7 +++--
 Allura/allura/tests/unit/test_spam.py    |   31 +++++++++++++++++-------
 2 files changed, 26 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c09d674f/Allura/allura/lib/spam/akismetservice.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/akismetservice.py b/Allura/allura/lib/spam/akismetservice.py
index 344a7ad..9378914 100644
--- a/Allura/allura/lib/spam/akismetservice.py
+++ b/Allura/allura/lib/spam/akismetservice.py
@@ -19,9 +19,10 @@ class Akismet(akismet.Akismet):
         if user:
             kw['comment_author'] = user.display_name or user.username
             kw['comment_author_email'] = user.email_addresses[0] if user.email_addresses
else ''
-        kw['user_ip'] = request.environ['HTTP_X_REMOTE_ADDR']
-        kw['user_agent'] = request.environ['HTTP_USER_AGENT']
-        kw['referrer'] = request.environ['HTTP_REFERER']
+        user_ip = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
+        kw['user_ip'] = user_ip.split(',')[0].strip()
+        kw['user_agent'] = request.headers.get('USER_AGENT')
+        kw['referrer'] = request.headers.get('REFERER')
         res = self.comment_check(text, data=kw, build_data=False)
         log.info("spam=%s (akismet): %s" % (str(res), log_msg))
         return res

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c09d674f/Allura/allura/tests/unit/test_spam.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/test_spam.py b/Allura/allura/tests/unit/test_spam.py
index ea87b43..d09c907 100644
--- a/Allura/allura/tests/unit/test_spam.py
+++ b/Allura/allura/tests/unit/test_spam.py
@@ -15,10 +15,11 @@ class TestAkismet(unittest.TestCase):
         self.fake_artifact = mock.Mock(**{'url.return_value': 'artifact url'})
         self.fake_user = mock.Mock(display_name='Some User',
                 email_addresses=['user@domain'])
-        self.fake_environ = dict(
-            HTTP_X_REMOTE_ADDR='some ip',
-            HTTP_USER_AGENT='some browser',
-            HTTP_REFERER='some url')
+        self.fake_headers = dict(
+            REMOTE_ADDR='fallback ip',
+            X_FORWARDED_FOR='some ip',
+            USER_AGENT='some browser',
+            REFERER='some url')
         self.content = 'spam text'
         self.expected_data = dict(
             comment_content=self.content,
@@ -30,7 +31,7 @@ class TestAkismet(unittest.TestCase):
     @mock.patch('allura.lib.spam.akismetservice.c')
     @mock.patch('allura.lib.spam.akismetservice.request')
     def test_check(self, request, c):
-        request.environ = self.fake_environ
+        request.headers = self.fake_headers
         c.user = None
         self.akismet.check(self.content)
         self.akismet.comment_check.assert_called_once_with(self.content,
@@ -39,7 +40,7 @@ class TestAkismet(unittest.TestCase):
     @mock.patch('allura.lib.spam.akismetservice.c')
     @mock.patch('allura.lib.spam.akismetservice.request')
     def test_check_with_explicit_content_type(self, request, c):
-        request.environ = self.fake_environ
+        request.headers = self.fake_headers
         c.user = None
         self.akismet.check(self.content, content_type='some content type')
         self.expected_data['comment_type'] = 'some content type'
@@ -49,7 +50,7 @@ class TestAkismet(unittest.TestCase):
     @mock.patch('allura.lib.spam.akismetservice.c')
     @mock.patch('allura.lib.spam.akismetservice.request')
     def test_check_with_artifact(self, request, c):
-        request.environ = self.fake_environ
+        request.headers = self.fake_headers
         c.user = None
         self.akismet.check(self.content, artifact=self.fake_artifact)
         expected_data = self.expected_data
@@ -60,7 +61,7 @@ class TestAkismet(unittest.TestCase):
     @mock.patch('allura.lib.spam.akismetservice.c')
     @mock.patch('allura.lib.spam.akismetservice.request')
     def test_check_with_user(self, request, c):
-        request.environ = self.fake_environ
+        request.headers = self.fake_headers
         c.user = None
         self.akismet.check(self.content, user=self.fake_user)
         expected_data = self.expected_data
@@ -72,7 +73,7 @@ class TestAkismet(unittest.TestCase):
     @mock.patch('allura.lib.spam.akismetservice.c')
     @mock.patch('allura.lib.spam.akismetservice.request')
     def test_check_with_implicit_user(self, request, c):
-        request.environ = self.fake_environ
+        request.headers = self.fake_headers
         c.user = self.fake_user
         self.akismet.check(self.content)
         expected_data = self.expected_data
@@ -80,3 +81,15 @@ class TestAkismet(unittest.TestCase):
                 comment_author_email='user@domain')
         self.akismet.comment_check.assert_called_once_with(self.content,
                 data=expected_data, build_data=False)
+
+    @mock.patch('allura.lib.spam.akismetservice.c')
+    @mock.patch('allura.lib.spam.akismetservice.request')
+    def test_check_with_fallback_ip(self, request, c):
+        self.expected_data['user_ip'] = 'fallback ip'
+        self.fake_headers.pop('X_FORWARDED_FOR')
+        request.headers = self.fake_headers
+        request.remote_addr = self.fake_headers['REMOTE_ADDR']
+        c.user = None
+        self.akismet.check(self.content)
+        self.akismet.comment_check.assert_called_once_with(self.content,
+                data=self.expected_data, build_data=False)


Mime
View raw message