decision-logic.sub.py (2823B)
1 from pathlib import Path 2 3 # General decision logic script. Depending on query parameters, it can 4 # simulate a variety of network errors, and its scoreAd() and 5 # reportResult() functions can have arbitrary Javascript code injected 6 # in them. scoreAd() will by default return a desirability score of 7 # twice the bid for each ad, as long as the ad URL ends with the uuid. 8 def main(request, response): 9 error = request.GET.first(b"error", None) 10 11 if error == b"close-connection": 12 # Close connection without writing anything, to simulate a network 13 # error. The write call is needed to avoid writing the default headers. 14 response.writer.write("") 15 response.close_connection = True 16 return 17 18 if error == b"http-error": 19 response.status = (404, b"OK") 20 else: 21 response.status = (200, b"OK") 22 23 if error == b"wrong-content-type": 24 response.headers.set(b"Content-Type", b"application/json") 25 elif error != b"no-content-type": 26 response.headers.set(b"Content-Type", b"application/javascript") 27 28 if error == b"bad-allow-fledge": 29 response.headers.set(b"Ad-Auction-Allowed", b"sometimes") 30 elif error == b"fledge-not-allowed": 31 response.headers.set(b"Ad-Auction-Allowed", b"false") 32 elif error != b"no-allow-fledge": 33 response.headers.set(b"Ad-Auction-Allowed", b"true") 34 35 if error == b"no-body": 36 return b'' 37 38 permitCrossOriginTrustedSignals = request.GET.get( 39 b"permit-cross-origin-trusted-signals", None) 40 if permitCrossOriginTrustedSignals != None: 41 response.headers.set(b"Ad-Auction-Allow-Trusted-Scoring-Signals-From", 42 permitCrossOriginTrustedSignals) 43 44 body = (Path(__file__).parent.resolve() / 'worklet-helpers.js').read_text().encode("ASCII") 45 if error != b"no-scoreAd": 46 body += b""" 47 function scoreAd(adMetadata, bid, auctionConfig, trustedScoringSignals, 48 browserSignals, directFromSellerSignals, 49 crossOriginTrustedScoringSignals) { 50 // Don't bid on interest group with the wrong uuid. This is to prevent 51 // left over interest groups from other tests from affecting auction 52 // results. 53 if (!browserSignals.renderURL.endsWith('uuid={{GET[uuid]}}') && 54 !browserSignals.renderURL.includes('uuid={{GET[uuid]}}&')) { 55 return 0; 56 } 57 58 {{GET[scoreAd]}}; 59 return {desirability: 2 * bid, allowComponentAuction: true}; 60 }""" 61 if error != b"no-reportResult": 62 body += b""" 63 function reportResult(auctionConfig, browserSignals, directFromSellerSignals) { 64 {{GET[reportResult]}}; 65 }""" 66 return body