Skip to content

Commit

Permalink
Cortex: Add workaround for XSRF token requirement
Browse files Browse the repository at this point in the history
Our usage of a retry-configured session has been observed to confuse
Cortex by learning CSRF token and even session cookies from responses
and submitting them back to the server. In keeping with the default
behaviour of the cortex4py module we configure a cookie policy in our
session that prevents any cookies from being learned from and returned
to the server.
  • Loading branch information
Jack28 authored and michaelweiser committed Jan 13, 2021
1 parent fcd1c52 commit fafa69b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions peekaboo/toolbox/cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
""" Interface to Cortex. """

import datetime
import http.cookiejar
import logging
import os
import threading
Expand Down Expand Up @@ -347,6 +348,17 @@ def is_retry(self, method, status_code, has_retry_after=False):
return super().is_retry(method, status_code, has_retry_after)


class NocookiesPolicy(http.cookiejar.DefaultCookiePolicy):
""" A cookie policy that denies to accept any cookies. """

# CookiePolicy as a base class is not enough. CookieJar makes assumptions
# about the expansive interface of DefaultCookiePolicy.

def set_ok(self, cookie, request):
""" No cookie will be accepted ever. """
return False


class Cortex:
""" Interfaces with a Cortex installation via its REST API. """
def __init__(self, job_queue, url="http://localhost:9001", api_token="",
Expand Down Expand Up @@ -414,6 +426,13 @@ def __init__(self, job_queue, url="http://localhost:9001", api_token="",
self.session = requests.sessions.Session()
self.session.mount('http://', retry_adapter)
self.session.mount('https://', retry_adapter)
# attach a cookie policy that refuses to learn any cookies from
# responses. This is because Cortex sometimes hands out CSRF tokens and
# even session cookies in response to our bearer-token-authenticated
# API requests which we don't need but have the potential to confuse
# Cortex on subsequent requests.
self.session.cookies = requests.cookies.RequestsCookieJar(
NocookiesPolicy())

self.api = None
self.tracker = None
Expand Down

0 comments on commit fafa69b

Please sign in to comment.