bidding-logic.sub.py (3368B)
1 from pathlib import Path 2 3 from fledge.tentative.resources import fledge_http_server_util 4 5 # General bidding logic script. Depending on query parameters, it can 6 # simulate a variety of network errors, and its generateBid(), reportWin(), 7 # and reportAdditionalBidWin() functions can have arbitrary Javascript code 8 # injected in them. generateBid() will by default return a bid of 9 for the 9 # first ad. 10 def main(request, response): 11 if fledge_http_server_util.handle_cors_headers_fail_if_preflight(request, response): 12 return 13 14 error = request.GET.first(b"error", None) 15 16 if error == b"close-connection": 17 # Close connection without writing anything, to simulate a network 18 # error. The write call is needed to avoid writing the default headers. 19 response.writer.write("") 20 response.close_connection = True 21 return 22 23 if error == b"http-error": 24 response.status = (404, b"OK") 25 else: 26 response.status = (200, b"OK") 27 28 if error == b"wrong-content-type": 29 response.headers.set(b"Content-Type", b"application/json") 30 elif error != b"no-content-type": 31 response.headers.set(b"Content-Type", b"application/javascript") 32 33 if error == b"bad-allow-fledge": 34 response.headers.set(b"Ad-Auction-Allowed", b"sometimes") 35 elif error == b"fledge-not-allowed": 36 response.headers.set(b"Ad-Auction-Allowed", b"false") 37 elif error != b"no-allow-fledge": 38 response.headers.set(b"Ad-Auction-Allowed", b"true") 39 40 if error == b"no-body": 41 return b'' 42 43 body = (Path(__file__).parent.resolve() / 'worklet-helpers.js').read_text().encode("ASCII") 44 if error != b"no-generateBid": 45 # Use bid query param if present. Otherwise, use a bid of 9. 46 bid = (request.GET.first(b"bid", None) or b"9").decode("ASCII") 47 48 bidCurrency = "" 49 bidCurrencyParam = request.GET.first(b"bidCurrency", None) 50 if bidCurrencyParam != None: 51 bidCurrency = "bidCurrency: '" + bidCurrencyParam.decode("ASCII") + "'," 52 53 allowComponentAuction = "" 54 allowComponentAuctionParam = request.GET.first(b"allowComponentAuction", None) 55 if allowComponentAuctionParam != None: 56 allowComponentAuction = f"allowComponentAuction: {allowComponentAuctionParam.decode('ASCII')}," 57 58 body += f""" 59 function generateBid(interestGroup, auctionSignals, perBuyerSignals, 60 trustedBiddingSignals, browserSignals, 61 directFromSellerSignals, 62 crossOriginTrustedBiddingSignals) {{ 63 {{{{GET[generateBid]}}}}; 64 return {{ 65 bid: {bid}, 66 {bidCurrency} 67 {allowComponentAuction} 68 render: interestGroup.ads[0].renderURL 69 }}; 70 }}""".encode() 71 if error != b"no-reportWin": 72 body += b""" 73 function reportWin(auctionSignals, perBuyerSignals, sellerSignals, 74 browserSignals, directFromSellerSignals) { 75 {{GET[reportWin]}}; 76 }""" 77 if error != b"no-reportAdditionalBidWin": 78 body += b""" 79 function reportAdditionalBidWin(auctionSignals, perBuyerSignals, 80 sellerSignals, browserSignals, 81 directFromSellerSignals) { 82 {{GET[reportAdditionalBidWin]}}; 83 }""" 84 return body