tor-browser

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

fledge-decision-logic.py (2004B)


      1 # These functions are used by FLEDGE to determine the logic for the ad seller.
      2 # For our testing purposes, we only need the minimal amount of boilerplate
      3 # code in place to allow them to be invoked properly and move the FLEDGE
      4 # process along. The tests do not deal with reporting results, so we leave
      5 # `reportResult` empty. See `generateURNFromFledge` in "utils.js" to see how
      6 # this file is used.
      7 
      8 from wptserve.utils import isomorphic_decode
      9 
     10 def main(request, response):
     11  # Set up response headers.
     12  headers = [
     13    ('Content-Type', 'Application/Javascript'),
     14    ('Ad-Auction-Allowed', 'true')
     15  ]
     16 
     17  # Parse URL params.
     18  requested_size = request.GET.first(b"requested-size", None)
     19 
     20  # Use URL params to modify Javascript.
     21  requested_size_check = ''
     22  if requested_size is not None:
     23    # request.GET stores URL keys and values in iso-8859-1 binary encoding. We
     24    # have to decode the values back to a string to parse width/height. Don't
     25    # bother sanitizing the size, because it is sanitized before auction logic
     26    # runs already.
     27    width, height = isomorphic_decode(requested_size).split('-')
     28 
     29    requested_size_check = (
     30      f'''
     31        if (!(auctionConfig.requestedSize.width === '{width}') &&
     32             (auctionConfig.requestedSize.height === '{height}')) {{
     33          throw new Error('requestedSize missing/incorrect in auctionConfig');
     34        }}
     35      '''
     36    )
     37 
     38  # Generate Javascript.
     39  # Note: Python fstrings use double-brackets ( {{, }} ) to insert bracket
     40  # literals instead of substitution sequences.
     41  score_ad = (
     42    f'''function scoreAd(
     43      adMetadata,
     44      bid,
     45      auctionConfig,
     46      trustedScoringSignals,
     47      browserSignals) {{
     48        {requested_size_check}
     49        return 2*bid;
     50      }}
     51    '''
     52  )
     53 
     54  report_result = (
     55    f'''function reportResult(
     56      auctionConfig,
     57      browserSignals) {{
     58        {requested_size_check}
     59        return;
     60      }}
     61    '''
     62  )
     63 
     64  content = f'{score_ad}\n{report_result}'
     65 
     66  return (headers, content)