incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject git commit: [#5455] Fallback to REMOTE_ADDR if X_FORWARDED_FOR not present
Date Thu, 13 Dec 2012 15:50:07 GMT
Updated Branches:
  refs/heads/tv/5455 424999c33 -> f154fa05c


[#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/f154fa05
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/f154fa05
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/f154fa05

Branch: refs/heads/tv/5455
Commit: f154fa05c52fa61bac88b95e87303b43c658d07a
Parents: 424999c
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Thu Dec 13 15:49:52 2012 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Thu Dec 13 15:49:52 2012 +0000

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


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f154fa05/Allura/allura/lib/custom_middleware.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/custom_middleware.py b/Allura/allura/lib/custom_middleware.py
index e6a1d68..5159ac0 100644
--- a/Allura/allura/lib/custom_middleware.py
+++ b/Allura/allura/lib/custom_middleware.py
@@ -153,6 +153,7 @@ class AlluraTimerMiddleware(TimerMiddleware):
         import jinja2
         import markdown
         import ming
+        import psycopg2
         import pymongo
         import socket
         import urllib2
@@ -172,6 +173,7 @@ class AlluraTimerMiddleware(TimerMiddleware):
             Timer('mongo', pymongo.cursor.Cursor, 'count', 'distinct',
                 'explain', 'hint', 'limit', 'next', 'rewind', 'skip',
                 'sort', 'where'),
+            Timer('postgres', psycopg2._psycopg.cursor, 'execute'),
             Timer('jinja', jinja2.Template, 'render', 'stream', 'generate'),
             # urlopen and socket io may or may not overlap partially
             Timer('urlopen', urllib2, 'urlopen'),

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f154fa05/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/f154fa05/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