tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

check-topics-request-header-in-img.py (1504B)


      1 def main(request, response):
      2    """
      3    This file is intended to be requested twice to verify that the correct headers
      4    are included for images.
      5    1. Make an initial request for an img. The `sec-browsing-topics` header will
      6       be stored for step 2. The request will be redirected to an image.
      7    2. Make a request with the query parameter set. The stashed header from the
      8       first step will be returned in the response content.
      9 
     10    Parameters:
     11    `token` should be a unique UUID request parameter for the duration of this
     12    request. It will get stored in the server stash and will be used later in
     13    a query request.
     14    `query` should be a request parameter indicating the request would like
     15    to know the last `sec-browsing-topics` header with that token.
     16    """
     17 
     18    token = request.GET.first(b"token", None)
     19    is_query = request.GET.first(b"query", None) is not None
     20    topics_header = request.headers.get(b"sec-browsing-topics", b"NO_TOPICS_HEADER")
     21 
     22    queried_topics_header = b"NO_PREVIOUS_REQUEST"
     23    with request.server.stash.lock:
     24        value = request.server.stash.take(token)
     25        if value is not None:
     26            queried_topics_header = value
     27        if not is_query:
     28            request.server.stash.put(token, topics_header)
     29 
     30    if is_query:
     31        return (200, [(b"Access-Control-Allow-Origin", b"*")], queried_topics_header)
     32 
     33    headers = [(b"Location", "pixel.png"),
     34            (b"Access-Control-Allow-Origin", b"*")]
     35    return 301, headers, b""