Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit 179af8c

Browse files
Merge pull request #188 from matthewbelisle-wf/RED-5108
RED-5108 - Check taskqueue ip
2 parents dd483d8 + add5ae0 commit 179af8c

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

Diff for: furious/__init__.py

+18
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,21 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
17+
import logging
18+
19+
import webapp2
20+
21+
22+
def csrf_check(request):
23+
"""
24+
Throws an HTTP 403 error if a CSRF attack is detected, same logic as the deferred module.
25+
26+
https://cloud.google.com/appengine/docs/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred
27+
"""
28+
in_prod = (
29+
not request.environ.get("SERVER_SOFTWARE").startswith("Devel"))
30+
if in_prod and request.environ.get("REMOTE_ADDR") != "0.1.0.2":
31+
logging.error("Detected an attempted CSRF attack from {}. This request did "
32+
"not originate from Task Queue.".format(request.environ.get("REMOTE_ADDR")))
33+
webapp2.abort(403)

Diff for: furious/_pkg_meta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version_info = (1, 6, 3)
1+
version_info = (1, 6, 4)
22
version = '.'.join(map(str, version_info))

Diff for: furious/config.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ def get_completion_cleanup_delay():
7171
return config.get('cleanupdelay')
7272

7373

74+
def get_csrf_check():
75+
"""Get the CSRF check function, which takes one arg (webapp2.Reqeust)
76+
and returns None, throwing an exception if a CSRF attack is detected.
77+
"""
78+
from furious.job_utils import path_to_reference
79+
80+
return path_to_reference(get_config().get('csrf_check'))
81+
82+
7483
def _get_configured_module(option_name, known_modules=None):
7584
"""Get the module specified by the value of option_name. The value of the
7685
configuration option will be used to load the module by name from the known
@@ -155,7 +164,8 @@ def default_config():
155164
'cleanupqueue': 'default',
156165
'cleanupdelay': 7600,
157166
'defaultqueue': 'default',
158-
'task_system': 'appengine_taskqueue'}
167+
'task_system': 'appengine_taskqueue',
168+
'csrf_check': 'furious.csrf_check'}
159169

160170

161171
def _load_yaml_config(path=None):

Diff for: furious/handlers/webapp.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from furious.handlers import process_async_task
1919
from furious.errors import AbortAndRestart
20-
20+
from furious.config import get_csrf_check
2121

2222
class AsyncJobHandler(webapp2.RequestHandler):
2323
"""Handles requests for the webapp framework."""
@@ -29,6 +29,9 @@ def post(self):
2929

3030
def _handle_task(self):
3131
"""Pass request info to the async framework."""
32+
# Check for CSRF
33+
get_csrf_check()(self.request)
34+
3235
headers = self.request.headers
3336

3437
message = None

0 commit comments

Comments
 (0)