From 2b326e778c4c76404b75602793f38173527ee1b9 Mon Sep 17 00:00:00 2001 From: jevansnyc Date: Wed, 15 Apr 2026 20:45:20 +0200 Subject: [PATCH 01/67] Add server-side ad templates design spec Co-Authored-By: Claude Sonnet 4.6 --- ...6-04-15-server-side-ad-templates-design.md | 363 ++++++++++++++++++ 1 file changed, 363 insertions(+) create mode 100644 docs/superpowers/specs/2026-04-15-server-side-ad-templates-design.md diff --git a/docs/superpowers/specs/2026-04-15-server-side-ad-templates-design.md b/docs/superpowers/specs/2026-04-15-server-side-ad-templates-design.md new file mode 100644 index 00000000..454f3764 --- /dev/null +++ b/docs/superpowers/specs/2026-04-15-server-side-ad-templates-design.md @@ -0,0 +1,363 @@ +# Server-Side Ad Templates Design + +*April 2026* + +--- + +## 1. Problem Statement + +Today's display ad pipeline on most publisher sites is structurally sequential +and browser-bound: + +1. Page HTML arrives at browser +2. Prebid.js (~300KB) downloads and parses +3. Smart Slots SDK scans the DOM to discover ad placements +4. `addAdUnits()` registers slot definitions +5. Prebid auction fires from the browser (~80–150ms RTT to SSPs) +6. Bids return (~1,000–1,500ms window) +7. GPT `setTargeting()` + `refresh()` fires +8. GAM creative renders + +**Total time to ad visible: ~3,100ms.** + +The browser is the slowest possible place to run an auction. It must first download and parse +multiple SDKs, scan the DOM to discover what ad slots exist, and then fire SSP requests over +a consumer internet connection with high and variable latency. + +Trusted Server sits at the Fastly edge — milliseconds from the user, with data-center-to-data-center +RTT to Prebid Server (~20–30ms vs ~80–150ms from a browser). The server knows, from the request +URL alone, exactly which ad slots are available on any given page. There is no reason to wait for +the browser. + +--- + +## 2. Goal + +Enable Trusted Server to: + +1. Match an incoming page request URL against a set of pre-configured slot templates +2. Immediately fire the full server-side auction (all providers: PBS, APS, future wrappers) in + parallel with the origin HTML fetch — before the browser receives a single byte +3. Inject GPT slot definitions into `` so the client can define slots without any SDK +4. Return pre-collected winning bids to the browser's lightweight `/auction` POST before the + browser would have even finished parsing Prebid.js +5. Eliminate Prebid.js from the client entirely + +**Target time to ad visible: ~1,200ms. Net saving: ~2,000ms.** + +--- + +## 3. Non-Goals + +- Eliminating client-side GPT / Google Ad Manager — GAM remains in the rendering pipeline + for Phase 1. The GAM call (`securepubads.g.doubleclick.net`) moves server-side in a future phase. +- Dynamic slot discovery (reading the DOM) — this design commits to pre-defined, URL-matched + slot templates. Smart Slots' dynamic injection behavior is replaced by server knowledge. +- Changing the `AuctionOrchestrator` internally — the orchestrator already handles parallel + provider fan-out. This design adds a new trigger point, not new auction logic. + +--- + +## 4. Architecture + +### 4.1 New File: `creative-opportunities.toml` + +A new config file at the repo root, alongside `trusted-server.toml`. It holds all slot templates: +page pattern matching rules, ad formats, floor prices, and GAM targeting key-values. Bidder-level +params (placement IDs, account IDs) live in Prebid Server stored requests, keyed by slot ID — not +in this file. + +Loaded at build time via `include_str!()`, parsed into `Vec` at startup. +Ad ops can edit this file independently of server configuration. + +`floor_price` is the publisher-owned hard floor per slot — the source of truth for the minimum +acceptable bid price, enforced at the edge before bids reach the ad server. Any bid below the +floor is discarded at the orchestrator level before it enters `__ts_bids`. SSPs may apply their +own dynamic floors independently within their platforms; this floor is the publisher's baseline +that supersedes all other floor logic by virtue of being enforced earliest in the pipeline. + +**Schema:** + +```toml +[[slot]] +id = "atf_sidebar_ad" +page_patterns = ["/20*/"] +formats = [{ width = 300, height = 250 }] +floor_price = 0.50 + +[slot.targeting] +pos = "atf" +zone = "atfSidebar" + +[[slot]] +id = "below-content-ad" +page_patterns = ["/20*/"] +formats = [{ width = 300, height = 250 }, { width = 728, height = 90 }] +floor_price = 0.25 + +[slot.targeting] +pos = "btf" +zone = "belowContent" + +[[slot]] +id = "ad-homepage-0" +page_patterns = ["/", "/index.html"] +formats = [{ width = 970, height = 250 }, { width = 728, height = 90 }] +floor_price = 1.00 + +[slot.targeting] +pos = "atf" +zone = "homepage" +slot_index = "0" +``` + +**Rust type:** + +```rust +#[derive(Debug, Clone, serde::Deserialize)] +pub struct CreativeOpportunitySlot { + pub id: String, + pub page_patterns: Vec, + pub formats: Vec, + pub floor_price: Option, + pub targeting: HashMap, +} +``` + +### 4.2 URL Pattern Matching + +At request time, TS matches the request path against each slot's `page_patterns`. Patterns are +glob-style strings: + +- `/20*/` — matches all date-prefixed article paths (e.g., `/2024/01/my-article/`) +- `/` — matches the homepage exactly +- `/index.html` — exact match + +Multiple slots can match a single URL. All matching slots are collected and fed into a single +auction as separate impressions. Pattern matching is purely in-memory against the pre-parsed +config — sub-millisecond. + +### 4.3 Auction Trigger + +When slots are matched, TS immediately calls `AuctionOrchestrator::run_auction()` with the +matched slots converted to `AdSlot` objects. This happens at request receipt time — in parallel +with the origin fetch. + +The orchestrator's existing behaviour is unchanged: +- All providers (PBS, APS, any configured wrappers) are dispatched simultaneously +- Per-provider timeout budgets are enforced from the remaining auction deadline +- Floor price filtering, bid unification, and winning bid selection are applied as today +- PBS resolves bidder params from its stored requests by slot ID — no bidder params travel + through TS or the browser + +**On NextJS 14 (buffered mode):** TS must buffer the full origin response before forwarding. +This gives the auction the entire origin response time (~150–400ms typical) to run before +any HTML is forwarded. In practice, bids are often collected before origin even responds. + +**On NextJS 16 (streaming mode):** TS streams HTML chunks to the browser immediately. The +auction runs in parallel. Bid injection into `` must complete before the `` tag +is forwarded. If the auction has not returned by the time `` is encountered, TS waits +up to the remaining auction budget, then flushes with whatever bids have arrived (partial +results) or no targeting if timed out. Content after `` is never held. + +### 4.4 Head Injection + +TS injects two separate ``, not +> raw string interpolation. -Prebid.js is eliminated. The client-side ad bootstrap is replaced by a small inline script -(~20 lines) that reads `__ts_ad_slots` and `__ts_bids` and drives GPT directly: +> **Cache contract:** Any response with `__ts_bids` injected is per-user data and must +> not be cached. TS sets `Cache-Control: private, no-store` on the response before +> forwarding, overriding any conflicting cache headers from the publisher origin. +> `Surrogate-Control` and `Fastly-Surrogate-Control` are also stripped. + +### 4.5 Win Notifications + +Win notification responsibilities are split by where the truth lives: + +**`nurl` (SSP win event) — fired server-side.** When the orchestrator selects a winning +bid, TS fires a fire-and-forget background HTTP request to `nurl` from the edge +(edge→SSP RTT ~20–30ms, no auction-path latency cost). A per-integration switch +(`[integrations.prebid].fire_nurl_at_edge`, default `true`) handles cases where the PBS +deployment already fires win events internally to avoid double-firing. APS win +notification follows its own spec. + +**`burl` (billing event) — fired client-side.** `burl` is embedded per slot in +`__ts_bids` (see §4.4). The `__tsAdInit` script registers a GPT `slotRenderEnded` +listener after defining slots. On render: if `!event.isEmpty` and +`event.slot.getTargeting('hb_adid')[0] === bidData.hb_adid`, the client fires `burl` +via `navigator.sendBeacon`. This confirms both that the ad rendered and that our specific +Prebid bid (not a direct deal or backfill) won the GAM line item match. + +### 4.6 Client Residual + +Prebid.js is eliminated. The client-side ad bootstrap is replaced by a small inline +script (~30 lines) that reads `__ts_ad_slots` and `__ts_bids`, drives GPT directly, and +handles billing notifications: ```javascript -window.__tsAdInit = function() { - var slots = window.__ts_ad_slots || []; - var bids = window.__ts_bids || {}; - googletag.cmd.push(function() { - slots.forEach(function(slot) { - var gptSlot = googletag.defineSlot(slot.id, slot.formats, slot.id) - .addService(googletag.pubads()); +window.__tsAdInit = function () { + var slots = window.__ts_ad_slots || [] + var bids = window.__ts_bids || {} + googletag.cmd.push(function () { + slots.forEach(function (slot) { + var gptSlot = googletag + .defineSlot(slot.gam_unit_path, slot.formats, slot.div_id) + .addService(googletag.pubads()) // Apply static targeting from config - Object.entries(slot.targeting).forEach(function([k, v]) { - gptSlot.setTargeting(k, v); - }); + Object.entries(slot.targeting).forEach(function ([k, v]) { + gptSlot.setTargeting(k, v) + }) // Apply pre-won bid targeting if available - var bidTargeting = bids[slot.id] || {}; - Object.entries(bidTargeting).forEach(function([k, v]) { - gptSlot.setTargeting(k, v); - }); - }); - googletag.pubads().enableSingleRequest(); - googletag.enableServices(); - googletag.pubads().refresh(); - }); -}; + var bidData = bids[slot.id] || {} + ;['hb_pb', 'hb_bidder', 'hb_adid'].forEach(function (key) { + if (bidData[key]) gptSlot.setTargeting(key, bidData[key]) + }) + }) + googletag.pubads().enableSingleRequest() + googletag.enableServices() + // Fire burl on confirmed render + googletag.pubads().addEventListener('slotRenderEnded', function (event) { + var slotId = event.slot.getSlotElementId() + var bidData = bids[slotId] || {} + if ( + !event.isEmpty && + bidData.burl && + event.slot.getTargeting('hb_adid')[0] === bidData.hb_adid + ) { + navigator.sendBeacon(bidData.burl) + } + }) + googletag.pubads().refresh() + }) +} ``` -This script is part of the `tsjs-gpt` integration bundle, injected by TS into every matching -page response alongside the existing GPT integration. +This script is part of the existing `gpt` integration bundle +(`crates/js/lib/src/integrations/gpt/index.ts`), extending the existing GPT shim. +Injected via the `gpt` head injector alongside `window.__ts_ad_slots`. --- @@ -238,21 +440,26 @@ t=0ms GET ts.publisher.com/article arrives at Fastly edge t=1ms URL matched against creative-opportunities.toml Slots matched: [atf_sidebar_ad, below-content-ad, section_ad] + Consent check: TCF consent present → auction proceeds t=2ms AuctionOrchestrator.run_auction() called - PBS + APS dispatched in parallel + PBS + APS dispatched in parallel via send_async() Edge→PBS RTT: ~20–30ms -t=2ms Origin fetch dispatched in parallel +t=2ms Origin fetch dispatched via send_async() in parallel + +t=2ms window.__ts_ad_slots script assembled from config (no auction needed) t=150ms Origin HTML arrives at edge (NextJS 14: buffered) + Auction still running; origin response held at edge -t=502ms Auction timeout fires (500ms budget) - Winning bids collected +t=502ms Auction deadline fires (500ms budget) + Winning bids collected; nurl fired as background requests -t=502ms injection assembled: - - window.__ts_ad_slots (from config, available at t=1ms) - - window.__ts_bids (from auction results) +t=502ms HtmlProcessorConfig constructed with bid results captured + injection assembled: + - window.__ts_ad_slots (from config, ready at t=2ms) + - window.__ts_bids (from auction results; Cache-Control: private, no-store set) t=502ms HTML forwarded to browser with injected @@ -270,7 +477,7 @@ t=822ms GET /gampad/ads t=922ms Creative fetch -t=1222ms Creative sub-resources + paint +t=1222ms Creative sub-resources + paint; burl fired via slotRenderEnded AD VISIBLE ~1200ms ``` @@ -279,18 +486,23 @@ t=1222ms Creative sub-resources + paint ## 6. Performance Summary -| Stage | Client-side today | With TS templates | Saving | -|---|---|---|---| -| Script load chain | ~700ms | ~40ms (tsjs only) | -660ms | -| Script parse/JIT | ~280ms | ~10ms | -270ms | -| Sequential SDK hops | ~200ms | 0 | -200ms | -| Auction window | ~1,500ms | ~500ms | -1,000ms | -| GAM + creative | ~570ms | ~570ms | — | -| **Total** | **~3,250ms** | **~1,200ms** | **~2,000ms** | +| Stage | Client-side today | With TS templates | Saving | +| ------------------- | ----------------- | ----------------- | ------------ | +| Script load chain | ~700ms | ~40ms (tsjs only) | -660ms | +| Script parse/JIT | ~280ms | ~10ms | -270ms | +| Sequential SDK hops | ~200ms | 0 | -200ms | +| Auction window | ~1,500ms | ~500ms | -1,000ms | +| GAM + creative | ~570ms | ~570ms | — | +| TTFB penalty¹ | 0 | up to +350ms | - | +| **Total** | **~3,250ms** | **~1,200ms** | **~2,000ms** | + +¹ Buffered mode only: the origin response is held until the auction resolves. For fast +origins (<150ms) and a 500ms auction deadline, TTFB may increase by up to 350ms. This +tradeoff is net-positive on revenue. The streaming mode (NextJS 16) has no TTFB penalty. -Auction RTT improvement: browser fires SSP requests at 80–150ms RTT; edge fires at 20–30ms. -Auction timeout can drop from 1,000–1,500ms to 500ms while still collecting more complete -results, because edge→PBS latency is ~5–7x lower. +Auction RTT improvement: browser fires SSP requests at 80–150ms RTT; edge fires at +20–30ms. Auction timeout can drop from 1,000–1,500ms to 500ms while still collecting +more complete results, because edge→PBS latency is ~5–7x lower. --- @@ -299,24 +511,42 @@ results, because edge→PBS latency is ~5–7x lower. ### New - `creative-opportunities.toml` — slot template config file -- `crates/trusted-server-core/src/creative_opportunities.rs` — config types, TOML parsing, - URL pattern matching, slot-to-`AdSlot` conversion -- `build.rs` update — `include_str!()` for `creative-opportunities.toml` -- Request handler modification — match slots at request receipt, trigger orchestrator immediately, - hold result for head injection -- `tsjs-gpt` integration update — `__tsAdInit` bootstrap replaces Prebid.js ad unit setup +- `crates/trusted-server-core/src/creative_opportunities.rs` — config types, TOML + parsing, URL glob matching, slot-to-`AdSlot` conversion, price bucketing +- `crates/trusted-server-core/build.rs` — `include_str!()` for + `creative-opportunities.toml`; startup slot-ID validation +- `crates/trusted-server-core/src/price_bucket.rs` — Prebid price granularity tables + (dense default; publisher-configurable); converts raw CPM `f64` to `hb_pb` string ### Modified -- `crates/trusted-server-core/src/integrations/prebid.rs` head injector — emit - `window.__ts_ad_slots` from matched slots -- `crates/trusted-server-core/src/html_processor.rs` — inject `window.__ts_bids` once auction - results are available, before `` -- `trusted-server.toml` — add `creative_opportunities_path` config key pointing to the new file +- **`crates/trusted-server-core/src/publisher.rs`** — primary structural change: + - Convert `handle_publisher_request` from `fn` to `async fn` + - Switch origin fetch from `.send()` to `.send_async()` (returns + `PlatformPendingRequest`) + - Add `orchestrator: &AuctionOrchestrator` parameter + - Match slots, check consent, fire auction and origin fetch concurrently + - Await both and construct `HtmlProcessorConfig` with resolved bid results +- **`crates/trusted-server-adapter-fastly/src/main.rs`** — update `route_request` call + site to `.await` the now-async publisher handler; pass orchestrator reference +- **`crates/trusted-server-core/src/html_processor.rs`** — inject `window.__ts_bids` + before `` via `el.on_end_tag()` on the `` element; set + `Cache-Control: private, no-store` header on injection; HTML-escape bid JSON +- **`crates/trusted-server-core/src/integrations/gpt.rs`** — extend head injector to + emit `window.__ts_ad_slots` from matched slots (not `prebid.rs`); emit `__tsAdInit` + bootstrap script +- **`crates/js/lib/src/integrations/gpt/index.ts`** — add `__tsAdInit` function and + `slotRenderEnded` burl-firing logic to the existing GPT shim +- **`crates/trusted-server-core/src/integrations/prebid.rs`** — add + `fire_nurl_at_edge` config key; add nurl fire-and-forget call in orchestrator result + handling +- **`trusted-server.toml`** — add `[creative_opportunities]` section +- **`crates/trusted-server-core/src/settings.rs`** — add `CreativeOpportunitiesConfig` + to `Settings` ### Unchanged -- `AuctionOrchestrator` — no internal changes; new call site only +- `AuctionOrchestrator` internals — no changes; new call site only - PBS stored request configuration — bidder params remain in PBS, keyed by slot ID - GAM line item configuration — targeting key-values pass through unchanged @@ -324,40 +554,66 @@ results, because edge→PBS latency is ~5–7x lower. ## 8. Edge Cases -**No slots match the URL** — auction is not fired. Head injection emits neither global. GPT -bootstrap detects empty `__ts_ad_slots` and skips initialization. Page loads normally with no -ad stack. +**No slots match the URL** — auction is not fired. Neither global is emitted. The page +loads with no TS ad stack; existing client-side Prebid/GPT flow runs unmodified (for +publishers in dual-mode rollout). + +**Consent absent or denied** — auction is not fired. Neither global is emitted. +`Cache-Control: private, no-store` is still set (to prevent caching the consent-negative +response if personalised ads were previously served). Page loads normally; GAM runs its +own auction without Prebid targeting. + +**Auction times out with partial results** — `__ts_bids` is populated with whatever bids +arrived before the deadline. Slots with no bid are omitted. GPT fires without pre-set +targeting for those slots; GAM falls back to its own auction for them. + +**Auction times out with zero results** — `__ts_bids` is an empty object `{}`. All slots +fire GAM without bid targeting. No revenue impact beyond the timeout scenario itself. -**Auction times out with partial results** — `__ts_bids` is populated with whatever bids arrived -before the deadline. Slots with no bid omitted. GPT fires without pre-set targeting for those slots; -GAM falls back to its own auction. +**Origin is slow (NextJS 14, buffered)** — auction has more time; results more likely to +be complete. TTFB impact is bounded by the origin latency, not additive to it. -**Auction times out with zero results** — `__ts_bids` is an empty object `{}`. All slots fire -GAM without bid targeting. No revenue impact beyond the timeout scenario itself (same as today's -fallback). +**NextJS 16 streaming** — `el.on_end_tag()` on `` gates injection. TS waits up to +the remaining `auction_timeout_ms` budget, then flushes. Content after `` is never +held. If the auction resolves before `` is encountered (common case), injection is +zero-latency. -**Origin is slow (NextJS 14, buffered)** — auction has more time; results more likely to be -complete. No change to streaming behavior. +**`creative-opportunities.toml` missing or malformed** — startup fails with a clear +error. No silent degradation. -**NextJS 16 streaming** — TS must flush `` before `` tag passes through. If auction -not yet complete, TS waits up to `auction_timeout_ms` from the config, then flushes. Content -streaming resumes immediately after `` regardless of bid state. +**Config empty (zero slots)** — treated as "no match" for all URLs; auction never fires. +No error. Useful as a kill-switch: deploying an empty `creative-opportunities.toml` +disables the feature without a code change. -**`creative-opportunities.toml` missing or malformed** — startup fails with a clear error. -No silent degradation. +**Slot ID not found in PBS stored requests** — PBS returns a no-bid for that slot. Slot +is omitted from `__ts_bids`. The remaining slots proceed normally. --- ## 9. Open Questions -1. **URL pattern coverage** — does `/20*/` cover all article paths, or are there +1. **URL pattern coverage** — does `/20**` cover all article paths, or are there non-date-prefixed article URLs? Publisher to confirm. 2. **PBS stored request setup** — slot IDs in `creative-opportunities.toml` must have - corresponding stored requests configured in the publisher's PBS instance before this goes live. -3. **Homepage slot count** — the example shows slots 0 and 1. Are there slots 2–5 following - the same pattern? Slot IDs and count to be confirmed with ad ops. -4. **Auction timeout for server-side trigger** — current `[integrations.prebid].timeout_ms` - is 1,000ms. Recommend reducing to 500ms for server-side triggered auctions given the - lower edge→PBS RTT. Separate config key or override on the new trigger path? -5. **`tsjs-gpt` bootstrap delivery** — the `__tsAdInit` script needs to fire after GPT.js - loads. Confirm injection order with the existing GPT integration head injection. + corresponding stored requests configured in the publisher's PBS instance before this + goes live. +3. **Homepage slot count** — the example shows slots 0 and 1. Are there additional slots + following the same pattern? Slot IDs and count to be confirmed with ad ops. +4. **Auction timeout** — ✅ Resolved: new dedicated key + `[creative_opportunities].auction_timeout_ms` with fallback to `[auction].timeout_ms`. + Per-provider ceilings (`[integrations.prebid].timeout_ms`, + `[integrations.aps].timeout_ms`) remain unchanged; the orchestrator's existing + `min(remaining_budget, provider_timeout)` logic applies. +5. **KV-backed config migration path** — Phase 1 ships with `include_str!()` for + simplicity and cost. When ad ops require live slot edits between deploys, the migration + path is: load from `services.kv_store()` at request time with a compiled-in fallback. + Design tracked as a follow-up before Phase 2. +6. **Phase 2 server-side GAM** — The real latency ceiling is the GAM call + (`securepubads.g.doubleclick.net`). Phase 2 routes the GAM ad request through the edge + (securepubads proxy + creative bundling), eliminating the last browser→Google hop. The + Phase 1 architecture is designed to be shape-compatible with this: `__ts_ad_slots` + gives the edge the full slot inventory it needs to build a server-side GAM request. +7. **`tsjs-gpt` bootstrap delivery** — ✅ Resolved: `__tsAdInit` is part of the existing + `gpt` integration bundle, not a new integration. Injection order: `window.__ts_ad_slots` + → existing GPT shim → `__tsAdInit` — all emitted by the `gpt` head injector in a single + `".to_string() + ), + ad_bids_script: None, + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk(b"T", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!(html.contains("window.__ts_ad_slots"), "should inject ad slots"); + } + + #[test] + fn injects_bids_before_end_of_head() { + let bids_script = ""; + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: None, + ad_bids_script: Some(bids_script.to_string()), + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk(b"T", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!(html.contains("window.__ts_bids"), "should inject bids"); + let bids_pos = html.find("window.__ts_bids").expect("should find bids"); + let end_head_pos = html.find("").expect("should find "); + assert!(bids_pos < end_head_pos, "bids script should appear before "); + } + ``` + + Run: `cargo test -p trusted-server-core html_processor` + Expected: compile error (no `ad_slots_script`/`ad_bids_script` fields, no `empty_for_tests()`) + +- [ ] **Step 2: Add `empty_for_tests()` to `IntegrationRegistry`** + + In `registry.rs`, add: + + ```rust + #[cfg(test)] + impl IntegrationRegistry { + pub fn empty_for_tests() -> Self { + // Minimal registry with no integrations for unit testing html_processor + Self { + inner: Arc::new(RegistryInner { + proxies: Default::default(), + attribute_rewriters: Default::default(), + script_rewriters: Vec::new(), + html_post_processors: Vec::new(), + head_injectors: Vec::new(), + metadata: Default::default(), + }) + } + } + } + ``` + + (Adjust field names to match the actual `RegistryInner` struct.) + +- [ ] **Step 3: Add fields to `HtmlProcessorConfig`** + + ```rust + pub struct HtmlProcessorConfig { + pub origin_host: String, + pub request_host: String, + pub request_scheme: String, + pub integrations: IntegrationRegistry, + /// Pre-computed `` for matched slots. + /// Injected at open, before integration head inserts. `None` when no slots matched. + pub ad_slots_script: Option, + /// Pre-computed `` for winning bids. + /// Injected immediately before via on_end_tag(). `None` when auction not run. + pub ad_bids_script: Option, + } + ``` + + Update `from_settings` to initialize `ad_slots_script: None, ad_bids_script: None`. + +- [ ] **Step 4: Inject `__ts_ad_slots` at head-open AND register `on_end_tag` for `__ts_bids`** + + In `create_html_processor`, within the EXISTING single `element!("head", ...)` handler, make two changes: + 1. Prepend the ad slots script BEFORE the existing integration inserts: + + ```rust + // NEW: inject __ts_ad_slots first + if let Some(ref slots_script) = ad_slots_script { + snippet.push_str(slots_script); + } + // ... existing: for insert in integrations.head_inserts(&ctx) { ... } + ``` + + 2. After `el.prepend(...)`, register the end-tag handler for `__ts_bids`: + ```rust + // Register on_end_tag handler for __ts_bids injection before + if let Some(bids_script) = ad_bids_script.clone() { + el.on_end_tag(move |end_tag| { + end_tag.before(&bids_script, ContentType::Html); + Ok(()) + })?; + } + ``` + + Both changes live inside the same `element!("head", ...)` closure — no second handler needed. + + Capture `ad_slots_script` and `ad_bids_script` into the closure the same way as `injected_tsjs`: + + ```rust + let ad_slots_script = config.ad_slots_script.clone(); + let ad_bids_script = config.ad_bids_script.clone(); + ``` + + > **lol_html `on_end_tag` API note:** `Element::on_end_tag(handler)` is available in lol_html ≥2.0. The handler receives `&mut EndTag` and must return `Result<(), Box>`. Use `ContentType::Html` so the injected `", escaped) + } + + pub(crate) fn build_ad_bids_script( + winning_bids: &std::collections::HashMap, + price_granularity: crate::price_bucket::PriceGranularity, + ) -> String { + let bids_map: serde_json::Map = winning_bids + .iter() + .filter_map(|(slot_id, bid)| { + let cpm = bid.price?; + let entry = serde_json::json!({ + "hb_pb": price_bucket(cpm, price_granularity), + "hb_bidder": bid.bidder, + "hb_adid": bid.ad_id.as_deref().unwrap_or(""), + "burl": bid.burl, + }); + Some((slot_id.clone(), entry)) + }) + .collect(); + let json = serde_json::to_string(&serde_json::Value::Object(bids_map)) + .expect("should serialize bids"); + let escaped = html_escape_for_script(&json); + format!("", escaped) + } + + /// HTML-escape a JSON string for safe inline `" + .to_string(), + // __tsAdInit definition — reads window.__ts_ad_slots / __ts_bids at call time. + concat!( + "" + ).to_string(), + ] + } + } + ``` + +- [ ] **Step 3: Run tests** + + Run: `cargo test -p trusted-server-core integrations::gpt` + Expected: all pass including new test + +- [ ] **Step 4: Commit** + + ```bash + git add crates/trusted-server-core/src/integrations/gpt.rs + git commit -m "Emit __tsAdInit function definition from GPT head injector" + ``` + +--- + +## Task 10: `gpt/index.ts` — TypeScript `__tsAdInit` + +**Files:** + +- Modify: `crates/js/lib/src/integrations/gpt/index.ts` + +The TypeScript version is the authoritative implementation; it must mirror the Rust inline string from Task 9 exactly. + +- [ ] **Step 1: Write a failing test** + + In `crates/js/lib/src/integrations/gpt/index.test.ts`: + + ```typescript + import { describe, it, expect, vi, beforeEach } from 'vitest' + + describe('installTsAdInit', () => { + beforeEach(() => { + delete (window as any).__ts_ad_slots + delete (window as any).__ts_bids + delete (window as any).__tsAdInit + }) + + it('defines googletag slots from __ts_ad_slots and calls refresh', () => { + const mockSlot = { + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + } + const mockPubads = { + enableSingleRequest: vi.fn(), + addEventListener: vi.fn(), + refresh: vi.fn(), + getTargeting: vi.fn().mockReturnValue([]), + } + ;(window as any).googletag = { + cmd: { push: vi.fn((fn: () => void) => fn()) }, + defineSlot: vi.fn().mockReturnValue(mockSlot), + pubads: vi.fn().mockReturnValue(mockPubads), + enableServices: vi.fn(), + } + ;(window as any).__ts_ad_slots = [ + { + id: 'atf', + gam_unit_path: '/123/atf', + div_id: 'atf', + formats: [[300, 250]], + targeting: { pos: 'atf' }, + }, + ] + ;(window as any).__ts_bids = { + atf: { + hb_pb: '1.00', + hb_bidder: 'kargo', + hb_adid: 'abc', + burl: 'https://ssp/bill', + }, + } + + // Must import installTsAdInit from the module + const { installTsAdInit } = require('./index') + installTsAdInit() + ;(window as any).__tsAdInit() + + expect((window as any).googletag.defineSlot).toHaveBeenCalledWith( + '/123/atf', + [[300, 250]], + 'atf' + ) + expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_pb', '1.00') + expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_bidder', 'kargo') + expect(mockPubads.refresh).toHaveBeenCalled() + }) + + it('fires burl via sendBeacon on slotRenderEnded when our bid won', () => { + const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true) + // ... setup and trigger slotRenderEnded event + // Verify: navigator.sendBeacon called with burl + beaconSpy.mockRestore() + }) + }) + ``` + + Run: `cd crates/js/lib && npx vitest run` + Expected: FAIL — `installTsAdInit` not exported + +- [ ] **Step 2: Add `installTsAdInit` to `index.ts`** + + Add to `crates/js/lib/src/integrations/gpt/index.ts` (bottom of file): + + ```typescript + interface TsAdSlot { + id: string + gam_unit_path: string + div_id: string + formats: Array + targeting: Record + } + + interface TsBidData { + hb_pb?: string + hb_bidder?: string + hb_adid?: string + burl?: string + } + + type TsWindow = Window & { + __ts_ad_slots?: TsAdSlot[] + __ts_bids?: Record + __tsAdInit?: () => void + } + + /** + * Install `window.__tsAdInit` — reads `window.__ts_ad_slots` and `window.__ts_bids` + * (injected by the edge into ), defines GPT slots, applies pre-won bid targeting, + * registers a `slotRenderEnded` listener to fire `burl` via `sendBeacon`, then calls + * `refresh()`. + */ + export function installTsAdInit(): void { + const w = window as TsWindow + w.__tsAdInit = function () { + const slots = w.__ts_ad_slots ?? [] + const bids = w.__ts_bids ?? {} + const g = (window as GptWindow).googletag + if (!g) return + g.cmd.push(() => { + slots.forEach((slot) => { + const gptSlot = g.defineSlot?.( + slot.gam_unit_path, + slot.formats, + slot.div_id + ) + if (!gptSlot) return + gptSlot.addService(g.pubads()) + Object.entries(slot.targeting ?? {}).forEach(([k, v]) => + gptSlot.setTargeting(k, v) + ) + const bid = bids[slot.id] ?? {} + ;(['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { + if (bid[key]) gptSlot.setTargeting(key, bid[key]!) + }) + }) + g.pubads().enableSingleRequest() + g.enableServices() + g.pubads().addEventListener?.('slotRenderEnded', (event: any) => { + const slotId: string = event.slot?.getSlotElementId?.() ?? '' + const bid = bids[slotId] ?? {} + if ( + !event.isEmpty && + bid.burl && + event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid + ) { + navigator.sendBeacon(bid.burl) + } + }) + g.pubads().refresh() + }) + } + } + ``` + + Call `installTsAdInit()` from the integration's initialization path so it's set up when the bundle loads. + +- [ ] **Step 3: Run JS tests** + + Run: `cd crates/js/lib && npx vitest run` + Expected: new tests pass + +- [ ] **Step 4: Build JS bundle** + + Run: `cd crates/js/lib && node build-all.mjs` + Expected: clean build + +- [ ] **Step 5: Commit** + + ```bash + git add crates/js/lib/src/integrations/gpt/ + git commit -m "Add __tsAdInit and slotRenderEnded burl firing to GPT integration" + ``` + +--- + +## Task 11: `nurl` fire-and-forget + +**Files:** + +- Modify: `crates/trusted-server-core/src/integrations/prebid.rs` +- Modify: `crates/trusted-server-core/src/publisher.rs` + +- [ ] **Step 1: Write failing test** + + ```rust + #[test] + fn prebid_config_fire_nurl_defaults_to_true() { + let config = PrebidConfig::default(); + assert!(config.fire_nurl_at_edge, "should fire nurl at edge by default"); + } + ``` + + Run: `cargo test -p trusted-server-core integrations::prebid` + Expected: FAIL + +- [ ] **Step 2: Add `fire_nurl_at_edge` to `PrebidConfig`** + + ```rust + #[serde(default = "default_fire_nurl_at_edge")] + pub fire_nurl_at_edge: bool, + ``` + + ```rust + fn default_fire_nurl_at_edge() -> bool { true } + ``` + +- [ ] **Step 3: Fire nurls in publisher.rs after auction** + + After `auction_result` is obtained, add: + + ```rust + if let Some(ref result) = auction_result { + fire_winning_nurls(result, settings); + } + ``` + + Add helper (no `.await` — fire-and-forget): + + ```rust + fn fire_winning_nurls( + result: &crate::auction::orchestrator::OrchestrationResult, + settings: &Settings, + ) { + use crate::backend::BackendConfig; + + let fire_nurl = settings + .integrations + .get_typed::("prebid") + .map(|c| c.fire_nurl_at_edge) + .unwrap_or(true); + + if !fire_nurl { + return; + } + + for bid in result.winning_bids.values() { + let Some(ref nurl) = bid.nurl else { continue }; + let backend_name = match BackendConfig::from_url(nurl, false) { + Ok(name) => name, + Err(e) => { + log::warn!("nurl: cannot create backend for {nurl}: {e:?}"); + continue; + } + }; + match fastly::Request::get(nurl).send_async(&backend_name) { + Ok(_) => log::debug!("nurl: fired for slot {}", bid.slot_id), + Err(e) => log::warn!("nurl: failed for slot {}: {e}", bid.slot_id), + } + } + } + ``` + +- [ ] **Step 4: Run tests** + + Run: `cargo test --workspace` + Expected: all pass + +- [ ] **Step 5: Commit** + + ```bash + git add crates/trusted-server-core/src/integrations/prebid.rs \ + crates/trusted-server-core/src/publisher.rs + git commit -m "Fire winning bid nurl fire-and-forget from edge; add fire_nurl_at_edge config" + ``` + +--- + +## Task 12: End-to-end integration tests + +**Files:** + +- Modify: `crates/trusted-server-core/src/publisher.rs` (test module) + +Tests use `pub(crate)` helpers from Task 8 directly. + +- [ ] **Step 1: Write tests** + + In `publisher.rs` test module: + + ```rust + #[cfg(test)] + mod creative_opportunities_tests { + use super::{build_ad_slots_script, build_ad_bids_script, html_escape_for_script}; + use crate::creative_opportunities::{ + CreativeOpportunitiesConfig, CreativeOpportunitySlot, CreativeOpportunityFormat, + CreativeOpportunitiesFile, match_slots, + }; + use crate::auction::types::{Bid, MediaType}; + use crate::price_bucket::PriceGranularity; + use std::collections::HashMap; + + fn make_config() -> CreativeOpportunitiesConfig { + CreativeOpportunitiesConfig { + gam_network_id: "21765378893".to_string(), + auction_timeout_ms: Some(500), + price_granularity: PriceGranularity::Dense, + } + } + + fn make_slot() -> CreativeOpportunitySlot { + CreativeOpportunitySlot { + id: "atf_sidebar_ad".to_string(), + gam_unit_path: Some("/21765378893/publisher/atf-sidebar".to_string()), + div_id: Some("div-atf-sidebar".to_string()), + page_patterns: vec!["/20**".to_string()], + formats: vec![CreativeOpportunityFormat { + width: 300, height: 250, media_type: MediaType::Banner, + }], + floor_price: Some(0.50), + targeting: [("pos".to_string(), "atf".to_string())].into_iter().collect(), + providers: Default::default(), + } + } + + #[test] + fn ad_slots_script_is_safe_and_parseable() { + let slots = vec![make_slot()]; + let config = make_config(); + let script = build_ad_slots_script(&slots, &config); + assert!(script.contains("window.__ts_ad_slots=JSON.parse"), "should use JSON.parse"); + assert!(script.contains("atf_sidebar_ad"), "should include slot id"); + // Verify no raw < or > that could break HTML parser + let inner = script.trim_start_matches(""); + assert!(!inner.contains('<'), "no unescaped < in script content"); + assert!(!inner.contains('>'), "no unescaped > in script content"); + } + + #[test] + fn ad_bids_script_uses_price_bucket_and_ad_id() { + let mut winning_bids = HashMap::new(); + winning_bids.insert("atf_sidebar_ad".to_string(), Bid { + slot_id: "atf_sidebar_ad".to_string(), + price: Some(2.53), + currency: "USD".to_string(), + creative: None, + adomain: None, + bidder: "kargo".to_string(), + width: 300, height: 250, + nurl: None, + burl: Some("https://ssp.example/billing?id=abc123".to_string()), + ad_id: Some("prebid-uuid-abc123".to_string()), + metadata: HashMap::new(), + }); + let script = build_ad_bids_script(&winning_bids, PriceGranularity::Dense); + assert!(script.contains("\"hb_pb\":\"2.53\""), "should bucket 2.53 as 2.53 (dense)"); + assert!(script.contains("\"hb_bidder\":\"kargo\""), "should include bidder"); + assert!(script.contains("\"hb_adid\":\"prebid-uuid-abc123\""), "should use ad_id not creative markup"); + assert!(script.contains("burl"), "should include burl for billing"); + } + + #[test] + fn html_escape_neutralizes_xss_in_json() { + let malicious = r#"{"zone":""), "should escape "); + assert!(escaped.contains("\\u003c"), "should unicode-escape <"); + assert!(escaped.contains("\\u003e"), "should unicode-escape >"); + } + + #[test] + fn url_matching_end_to_end() { + let file = CreativeOpportunitiesFile { slots: vec![make_slot()] }; + assert_eq!(match_slots(&file.slots, "/2024/01/my-article").len(), 1, "should match article"); + assert_eq!(match_slots(&file.slots, "/about").len(), 0, "should not match /about"); + assert_eq!(match_slots(&file.slots, "/").len(), 0, "should not match root"); + } + } + ``` + +- [ ] **Step 2: Run tests** + + Run: `cargo test -p trusted-server-core creative_opportunities_tests` + Expected: all pass + +- [ ] **Step 3: Run full suite + CI gates** + + ```bash + cargo test --workspace + cargo clippy --workspace --all-targets --all-features -- -D warnings + cargo fmt --all -- --check + cd crates/js/lib && npx vitest run + cd crates/js/lib && npm run format + cd docs && npm run format + ``` + + Expected: all clean + +- [ ] **Step 4: Commit** + + ```bash + git add crates/trusted-server-core/src/publisher.rs + git commit -m "Add integration tests for creative opportunities pipeline (slots, bids, XSS)" + ``` + +--- + +## Manual Verification Checklist + +Run `fastly compute serve` and verify: + +- [ ] **No match:** Request `/about` — no `__ts_ad_slots` or `__ts_bids` in response HTML, no `Cache-Control: private, no-store` +- [ ] **Match:** Request `/2024/01/article` — both globals present in ``, `Cache-Control: private, no-store` set +- [ ] **Empty file kill-switch:** Empty `creative-opportunities.toml` → no globals injected on any URL +- [ ] **Auction timeout:** Set `auction_timeout_ms = 1` → `__ts_bids` injects as `{}`, no slot entries +- [ ] **XSS check:** Add `targeting = { zone = " +``` + +> **Security:** All string values are JSON-serialized via `serde_json` and HTML-escaped +> before insertion into the ``, not -> raw string interpolation. +- If the auction has already completed for ``, response returns immediately + with cached results (cache hit). Typical case for non-trivial origin times. +- If the auction is still in flight, the request blocks until completion or `A_deadline`, + whichever fires first. Long-poll semantics, capped by the auction timeout. +- If `` is unknown (cache miss, expired TTL, or never created), returns + `404`. Client falls back to firing GPT without pre-set targeting. +- If no slot received a bid above floor, returns `{}`. Client fires GPT without targeting. +- Response carries `Cache-Control: private, no-store`. -> **Cache contract:** Any response with `__ts_bids` injected is per-user data and must -> not be cached. TS sets `Cache-Control: private, no-store` on the response before -> forwarding, overriding any conflicting cache headers from the publisher origin. -> `Surrogate-Control` and `Fastly-Surrogate-Control` are also stripped. +**Storage:** auction results cached in-process (per-edge-instance) keyed by request ID +with a 30-second TTL. Sized small (a few KB per entry) and short-lived; no Fastly KV +write on the hot path. + +**Security:** request IDs are 128-bit unguessable UUIDs. Even if a request ID leaks, the +worst-case impact is reading bid metadata that's already destined for that session's +GPT slots — no cross-user data exposure. ### 4.5 Win Notifications @@ -386,119 +455,357 @@ Prebid bid (not a direct deal or backfill) won the GAM line item match. ### 4.6 Client Residual Prebid.js is eliminated. The client-side ad bootstrap is replaced by a small inline -script (~30 lines) that reads `__ts_ad_slots` and `__ts_bids`, drives GPT directly, and -handles billing notifications: +script that reads `__ts_ad_slots`, fetches bids from `/ts-bids`, drives GPT directly, +and handles billing notifications. Slot definition happens immediately; bid targeting +and `refresh()` happen after `/ts-bids` resolves: ```javascript window.__tsAdInit = function () { var slots = window.__ts_ad_slots || [] - var bids = window.__ts_bids || {} + var rid = window.__ts_request_id + + // Kick off bid fetch as early as possible. Fires in parallel with GPT setup. + var bidsPromise = rid + ? fetch('/ts-bids?rid=' + encodeURIComponent(rid), { credentials: 'omit' }) + .then(function (r) { + return r.ok ? r.json() : {} + }) + .catch(function () { + return {} + }) + : Promise.resolve({}) + googletag.cmd.push(function () { - slots.forEach(function (slot) { + // Define slots immediately — no auction wait + var gptSlots = slots.map(function (slot) { var gptSlot = googletag .defineSlot(slot.gam_unit_path, slot.formats, slot.div_id) .addService(googletag.pubads()) - // Apply static targeting from config Object.entries(slot.targeting).forEach(function ([k, v]) { gptSlot.setTargeting(k, v) }) - // Apply pre-won bid targeting if available - var bidData = bids[slot.id] || {} - ;['hb_pb', 'hb_bidder', 'hb_adid'].forEach(function (key) { - if (bidData[key]) gptSlot.setTargeting(key, bidData[key]) - }) + return { id: slot.id, gptSlot: gptSlot } }) + googletag.pubads().enableSingleRequest() googletag.enableServices() - // Fire burl on confirmed render - googletag.pubads().addEventListener('slotRenderEnded', function (event) { - var slotId = event.slot.getSlotElementId() - var bidData = bids[slotId] || {} - if ( - !event.isEmpty && - bidData.burl && - event.slot.getTargeting('hb_adid')[0] === bidData.hb_adid - ) { - navigator.sendBeacon(bidData.burl) - } + + // Apply bid targeting and refresh once /ts-bids resolves. + bidsPromise.then(function (bids) { + gptSlots.forEach(function ({ id, gptSlot }) { + var bidData = bids[id] || {} + ;['hb_pb', 'hb_bidder', 'hb_adid'].forEach(function (key) { + if (bidData[key]) gptSlot.setTargeting(key, bidData[key]) + }) + }) + + // Fire burl on confirmed render + googletag.pubads().addEventListener('slotRenderEnded', function (event) { + var slotId = event.slot.getSlotElementId() + var bidData = bids[slotId] || {} + if ( + !event.isEmpty && + bidData.burl && + event.slot.getTargeting('hb_adid')[0] === bidData.hb_adid + ) { + navigator.sendBeacon(bidData.burl) + } + }) + + googletag.pubads().refresh() }) - googletag.pubads().refresh() }) } ``` +**Why slot definition happens before bid fetch resolves:** GPT slot definition is +synchronous and cheap. Defining slots early lets GPT prepare iframes and start any +internal work that doesn't require ad server response. `refresh()` is the call that +actually triggers the GAM ad request — that's the one we delay until bids arrive. + +**Failure modes:** + +- `/ts-bids` returns 404 (unknown rid, TTL expired) → `bidsPromise` resolves to `{}`, + `refresh()` fires without bid targeting, GAM falls back to its own auction. Same + graceful degradation as no-bid case. +- `/ts-bids` network failure → caught, resolves to `{}`, same fallback. +- Auction times out server-side → `/ts-bids` returns `{}`, same fallback. + This script is part of the existing `gpt` integration bundle (`crates/js/lib/src/integrations/gpt/index.ts`), extending the existing GPT shim. Injected via the `gpt` head injector alongside `window.__ts_ad_slots`. +### 4.7 Caching Behavior + +Page assets and bid results have very different cacheability properties. The +architecture is designed so that everything that can be cached, is. + +**What gets cached where:** + +| Asset | Cached at | Cacheability | +| ------------------------ | -------------------------------- | --------------------------------------------------------- | +| Origin HTML | Fastly edge HTTP cache | Yes, if origin sends `Cache-Control: public, max-age=...` | +| Origin CSS / fonts / JS | Fastly edge + browser | Yes (typically hashed URLs, immutable) | +| `tsjs` bundle | Fastly edge + browser | Yes (already content-hashed via `bundle.rs`, immutable) | +| `__ts_ad_slots` payload | Could be precomputed per pattern | In-memory match is sub-millisecond — not worth caching | +| `__ts_request_id` | **Never** | Per-request UUID, minted at request receipt | +| Bid results (`/ts-bids`) | In-process `bid_cache`, 30s TTL | Per-request, never shared across users | + +**Architecture:** + +1. Fastly's built-in HTTP cache stores the **origin response** keyed by URL. TS + does not implement its own HTML caching layer — it leverages the existing + Fastly cache. +2. On request: TS reads from cache (cache hit, ~5ms) or fetches from origin + (cache miss, ~150ms typical). +3. TS injects `__ts_ad_slots` + `__ts_request_id` at the `` open via the + existing `el.prepend()` head handler. This injection is per-request — origin + HTML in cache is unmodified. +4. TS forces `Transfer-Encoding: chunked` and streams the assembled response + to the browser. +5. The auction runs in parallel regardless of HTML cache state — bids land in + `bid_cache` keyed by `request_id`, served via `/ts-bids` when the client + fetches. + +The `bid_cache` (per-request bid results) and Fastly's HTML cache are +**independent systems**. HTML cache hit/miss does not affect auction firing; +auction firing does not affect HTML caching. + +**`Cache-Control` handling:** + +TS preserves the origin's `Cache-Control` header on the response sent to the +browser, with one override: when `__ts_request_id` is injected (any matched +page), TS sets `Cache-Control: private, no-store` on the **browser-facing** +response to prevent intermediate caches or the browser from caching the +per-user assembled HTML. The Fastly edge cache for the **origin** response is +unaffected — TS reads the cached origin HTML and assembles a fresh per-request +response on every hit. + +`Surrogate-Control` and `Fastly-Surrogate-Control` headers from origin are +preserved (they control Fastly's cache, not the browser's). + +**When caching doesn't apply:** + +- **Logged-in users** — origin typically returns `Cache-Control: private`. Falls + back to cache-miss timing (full origin fetch). +- **Personalized SSR** (per-user content, A/B test variants) — same. +- **Dynamic NextJS routes without ISR** — origin sends `Cache-Control: no-store` + or short max-age. Falls back to cache-miss timing. +- **First request after deploy or cache purge** — cold cache, full origin fetch. +- **Long-tail URLs** — low cache hit rate, treat as cache-miss case. + +For typical news / content publisher sites with anonymous visitors on stable +content pages, expect 70–90%+ edge cache hit rate. The cache-hit timing in §5 +is the realistic common case, not the optimistic best case. + --- ## 5. Request-Time Sequence +Sequence applies to all origins (WordPress, Drupal, Rails, NextJS 14/16, static sites). +TS forces chunked encoding on every response, so origin format is invisible from the +browser's perspective. + +### 5.1 Visual Sequence (full content + creative flow) + +```mermaid +sequenceDiagram + autonumber + participant B as Browser + participant E as TS Edge
(Fastly) + participant C as Fastly HTTP Cache + participant O as Publisher Origin
(WP / NextJS / etc) + participant A as Auction
(PBS + APS) + participant S as SSPs
(Kargo / Index / etc) + participant G as GAM
(securepubads) + + Note over B,G: t=0ms — Navigation start + + B->>E: GET ts.publisher.com/article + + Note over E: t=1ms — URL → slots match
Mint request_id (UUID)
Check consent + + par Auction kicks off server-side + E->>A: POST bid requests
(PBS + APS in parallel) + A->>S: Fan out to all SSPs + S-->>A: Bids return + A-->>E: Aggregated bid responses
(t=502ms) + Note over E: Cache bids in bid_cache
(keyed by request_id, 30s TTL) + E->>S: Fire nurl (fire-and-forget)
for winning bids + and Origin HTML lookup + E->>C: Lookup origin HTML by URL + alt Cache HIT (typical for content pages) + C-->>E: Cached HTML (~5ms) + else Cache MISS (cold / dynamic / logged-in) + C->>O: GET origin HTML + O-->>C: HTML response (~150ms) + C-->>E: HTML response + end + end + + Note over E: Force Transfer-Encoding: chunked
Inject __ts_ad_slots + __ts_request_id
at open
Set Cache-Control: private, no-store + + E-->>B: Stream HTML chunks (no auction wait) + + Note over B: TTFB: ~10ms (hit) / ~155ms (miss)
Browser parses
CSS, fonts, tsjs download
(also from Fastly + browser cache) + + Note over B: flushes immediately
Body parsing begins
🎨 FCP: ~80ms (hit) / ~250ms (miss) + + Note over B: tsjs bundle executes
t=130ms (hit) / t=300ms (miss)
__tsAdInit() defines GPT slots
(no GAM call yet) + + B->>E: GET /ts-bids?rid= + + alt Auction already complete (typical on cache-hit pages) + Note over E: bid_cache hit — return immediately + E-->>B: Bid targeting JSON
(hb_pb, hb_bidder, hb_adid, burl) + else Auction still running + Note over E: Long-poll — block until
auction completes or A_deadline + A-->>E: Bids arrive + E-->>B: Bid targeting JSON
(or {} on timeout) + end + + Note over B: Bids received (~30ms RTT)
setTargeting(hb_*) per slot
Register slotRenderEnded listener
googletag.pubads().refresh() fires + + B->>G: GET /gampad/ads
with hb_* key-values + + Note over G: GAM matches hb_pb against
Prebid line items, selects winner + + G-->>B: Ad markup
(iframe HTML or creative URL) + + Note over B: Creative iframe loads in slot
Fetches sub-resources
(images, scripts, viewability pixels) + + Note over B: 🎯 Creative paints
slotRenderEnded event fires
__tsAdInit checks hb_adid match + + alt Our Prebid bid won the GAM line item match + B->>S: Fire burl (navigator.sendBeacon)
SSP confirms billable impression + else Direct deal / backfill won (hb_adid mismatch or empty) + Note over B: No burl fired — our bid lost
(correct behavior — different creative rendered) + end + + Note over B: window.load fires
(page fully loaded) + + Note over B,G: ✅ AD VISIBLE
Cache hit: ~900ms total
Cache miss: ~1,050ms total
FCP: ~80ms (hit) / ~250ms (miss)

vs client-side today: ~3,250ms ad-visible / FCP ~500ms+ +``` + +### 5.2 Cache-Hit Sequence (typical for content publisher pages) + +This is the common case for anonymous visitors on cacheable content pages. + ``` t=0ms GET ts.publisher.com/article arrives at Fastly edge t=1ms URL matched against creative-opportunities.toml Slots matched: [atf_sidebar_ad, below-content-ad, section_ad] Consent check: TCF consent present → auction proceeds + Request ID minted: 550e8400-e29b-41d4-a716-446655440000 -t=2ms AuctionOrchestrator.run_auction() called +t=2ms AuctionOrchestrator.run_auction() dispatched (parallel) PBS + APS dispatched in parallel via send_async() Edge→PBS RTT: ~20–30ms + Fastly cache lookup dispatched in parallel + __ts_ad_slots + __ts_request_id ".to_string() + r#""# + .to_string() ), - ad_bids_script: None, }; let mut processor = create_html_processor(config); let output = processor .process_chunk(b"T", true) .expect("should process"); let html = std::str::from_utf8(&output).expect("should be utf8"); - assert!(html.contains("window.__ts_ad_slots"), "should inject ad slots"); + assert!(html.contains("window.__ts_ad_slots"), "should inject ad slots at head-open"); + assert!(html.contains("window.__ts_request_id"), "should inject request_id at head-open"); } #[test] - fn injects_bids_before_end_of_head() { - let bids_script = ""; + fn does_not_hold_end_of_head() { + // Verify: no bid data appears before — that hold was rejected by spec §4.3 let config = HtmlProcessorConfig { origin_host: "origin.example.com".to_string(), request_host: "example.com".to_string(), request_scheme: "https".to_string(), integrations: IntegrationRegistry::empty_for_tests(), ad_slots_script: None, - ad_bids_script: Some(bids_script.to_string()), }; let mut processor = create_html_processor(config); let output = processor .process_chunk(b"T", true) .expect("should process"); let html = std::str::from_utf8(&output).expect("should be utf8"); - assert!(html.contains("window.__ts_bids"), "should inject bids"); - let bids_pos = html.find("window.__ts_bids").expect("should find bids"); - let end_head_pos = html.find("").expect("should find "); - assert!(bids_pos < end_head_pos, "bids script should appear before "); + assert!(!html.contains("__ts_bids"), "must not inject bids into head"); } ``` Run: `cargo test -p trusted-server-core html_processor` - Expected: compile error (no `ad_slots_script`/`ad_bids_script` fields, no `empty_for_tests()`) + Expected: compile error (no `ad_slots_script` field, no `empty_for_tests()`) - [ ] **Step 2: Add `empty_for_tests()` to `IntegrationRegistry`** @@ -888,7 +886,6 @@ Adding the two new fields to `HtmlProcessorConfig` and the injection logic is in #[cfg(test)] impl IntegrationRegistry { pub fn empty_for_tests() -> Self { - // Minimal registry with no integrations for unit testing html_processor Self { inner: Arc::new(RegistryInner { proxies: Default::default(), @@ -905,7 +902,9 @@ Adding the two new fields to `HtmlProcessorConfig` and the injection logic is in (Adjust field names to match the actual `RegistryInner` struct.) -- [ ] **Step 3: Add fields to `HtmlProcessorConfig`** +- [ ] **Step 3: Add single field to `HtmlProcessorConfig`** + + Replace any existing `ad_slots_script`/`ad_bids_script` fields with: ```rust pub struct HtmlProcessorConfig { @@ -913,56 +912,47 @@ Adding the two new fields to `HtmlProcessorConfig` and the injection logic is in pub request_host: String, pub request_scheme: String, pub integrations: IntegrationRegistry, - /// Pre-computed `` for matched slots. - /// Injected at open, before integration head inserts. `None` when no slots matched. + /// Pre-computed ``. + /// Injected at `` open, before integration head inserts. `None` when no slots matched. pub ad_slots_script: Option, - /// Pre-computed `` for winning bids. - /// Injected immediately before via on_end_tag(). `None` when auction not run. - pub ad_bids_script: Option, } ``` - Update `from_settings` to initialize `ad_slots_script: None, ad_bids_script: None`. + Update `from_settings` (or wherever `HtmlProcessorConfig` is constructed) to initialize `ad_slots_script: None`. -- [ ] **Step 4: Inject `__ts_ad_slots` at head-open AND register `on_end_tag` for `__ts_bids`** +- [ ] **Step 4: Inject `ad_slots_script` at head-open** - In `create_html_processor`, within the EXISTING single `element!("head", ...)` handler, make two changes: - 1. Prepend the ad slots script BEFORE the existing integration inserts: + In `create_html_processor`, within the EXISTING `element!("head", ...)` handler, build the full snippet string with `ad_slots_script` first (so it appears first in output — lol_html `prepend` inserts before children, with **last-prepend-wins** ordering, so we call `prepend` exactly once with the full combined string): - ```rust - // NEW: inject __ts_ad_slots first - if let Some(ref slots_script) = ad_slots_script { - snippet.push_str(slots_script); - } - // ... existing: for insert in integrations.head_inserts(&ctx) { ... } - ``` + ```rust + let ad_slots_script = config.ad_slots_script.clone(); + // ... existing captures ... - 2. After `el.prepend(...)`, register the end-tag handler for `__ts_bids`: - ```rust - // Register on_end_tag handler for __ts_bids injection before - if let Some(bids_script) = ad_bids_script.clone() { - el.on_end_tag(move |end_tag| { - end_tag.before(&bids_script, ContentType::Html); - Ok(()) - })?; - } - ``` + element!("head", |el| { + let mut snippet = String::new(); - Both changes live inside the same `element!("head", ...)` closure — no second handler needed. + // ad_slots_script first so __ts_ad_slots + __ts_request_id appear before + // integration inserts. DO NOT call prepend multiple times — lol_html stacks + // prepend calls in reverse order, so a single prepend with the full string + // guarantees correct ordering. + if let Some(ref slots_script) = ad_slots_script { + snippet.push_str(slots_script); + } - Capture `ad_slots_script` and `ad_bids_script` into the closure the same way as `injected_tsjs`: + // ... existing: for insert in integrations.head_inserts(&ctx) { snippet.push_str(...) } - ```rust - let ad_slots_script = config.ad_slots_script.clone(); - let ad_bids_script = config.ad_bids_script.clone(); + if !snippet.is_empty() { + el.prepend(&snippet, ContentType::Html); + } + // DO NOT register on_end_tag — flushes immediately per spec §4.3 + Ok(()) + }) ``` - > **lol_html `on_end_tag` API note:** `Element::on_end_tag(handler)` is available in lol_html ≥2.0. The handler receives `&mut EndTag` and must return `Result<(), Box>`. Use `ContentType::Html` so the injected `", escaped) + let slots_json_str = serde_json::to_string(&slots_json) + .expect("should serialize ad slots"); + let escaped_slots = html_escape_for_script(&slots_json_str); + // request_id is a UUID (hex + hyphens only) — safe to embed without escaping. + format!( + r#""# + ) } - pub(crate) fn build_ad_bids_script( + /// Build the `BidMap` stored in `bid_cache` and returned by `/ts-bids`. + /// + /// Keyed by slot ID. Values contain `hb_pb`, `hb_bidder`, `hb_adid`, `burl`. + pub(crate) fn build_bid_map( winning_bids: &std::collections::HashMap, price_granularity: crate::price_bucket::PriceGranularity, - ) -> String { - let bids_map: serde_json::Map = winning_bids + ) -> crate::bid_cache::BidMap { + winning_bids .iter() .filter_map(|(slot_id, bid)| { let cpm = bid.price?; - let entry = serde_json::json!({ - "hb_pb": price_bucket(cpm, price_granularity), - "hb_bidder": bid.bidder, - "hb_adid": bid.ad_id.as_deref().unwrap_or(""), - "burl": bid.burl, - }); - Some((slot_id.clone(), entry)) + let entry: std::collections::HashMap = [ + ("hb_pb".to_string(), serde_json::Value::String(price_bucket(cpm, price_granularity))), + ("hb_bidder".to_string(), serde_json::Value::String(bid.bidder.clone())), + ("hb_adid".to_string(), serde_json::Value::String( + bid.ad_id.as_deref().unwrap_or("").to_string() + )), + ("burl".to_string(), bid.burl.as_deref() + .map(serde_json::Value::from) + .unwrap_or(serde_json::Value::Null)), + ].into_iter().collect(); + Some((slot_id.clone(), entry.into_iter() + .map(|(k, v)| (k, v)) + .collect::>() + .into())) }) - .collect(); - let json = serde_json::to_string(&serde_json::Value::Object(bids_map)) - .expect("should serialize bids"); - let escaped = html_escape_for_script(&json); - format!("", escaped) + .collect() } /// HTML-escape a JSON string for safe inline `" .to_string(), - // __tsAdInit definition — reads window.__ts_ad_slots / __ts_bids at call time. + // __tsAdInit: fetches /ts-bids for bid targeting, then drives GPT. + // window.__ts_ad_slots and window.__ts_request_id are injected at head-open by TS. + // bidsPromise resolves concurrently with page rendering — never blocks FCP. concat!( "" @@ -1394,20 +1825,20 @@ The `HtmlProcessorConfig` fields now exist (Task 7). This task wires the auction ```bash git add crates/trusted-server-core/src/integrations/gpt.rs - git commit -m "Emit __tsAdInit function definition from GPT head injector" + git commit -m "Emit __tsAdInit with /ts-bids fetch pattern from GPT head injector" ``` --- -## Task 10: `gpt/index.ts` — TypeScript `__tsAdInit` +## Task 12: `gpt/index.ts` — TypeScript `__tsAdInit` with `/ts-bids` fetch **Files:** - Modify: `crates/js/lib/src/integrations/gpt/index.ts` -The TypeScript version is the authoritative implementation; it must mirror the Rust inline string from Task 9 exactly. +The TypeScript version mirrors the Rust inline string from Task 11. It uses the `bidsPromise` pattern — fetching `/ts-bids` concurrently with GPT slot definition. -- [ ] **Step 1: Write a failing test** +- [ ] **Step 1: Write failing tests** In `crates/js/lib/src/integrations/gpt/index.test.ts`: @@ -1417,20 +1848,21 @@ The TypeScript version is the authoritative implementation; it must mirror the R describe('installTsAdInit', () => { beforeEach(() => { delete (window as any).__ts_ad_slots - delete (window as any).__ts_bids + delete (window as any).__ts_request_id delete (window as any).__tsAdInit }) - it('defines googletag slots from __ts_ad_slots and calls refresh', () => { + it('fetches /ts-bids with request_id and applies bid targeting before refresh', async () => { const mockSlot = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), + getSlotElementId: vi.fn().mockReturnValue('atf'), + getTargeting: vi.fn().mockReturnValue([]), } const mockPubads = { enableSingleRequest: vi.fn(), addEventListener: vi.fn(), refresh: vi.fn(), - getTargeting: vi.fn().mockReturnValue([]), } ;(window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, @@ -1447,45 +1879,131 @@ The TypeScript version is the authoritative implementation; it must mirror the R targeting: { pos: 'atf' }, }, ] - ;(window as any).__ts_bids = { - atf: { - hb_pb: '1.00', - hb_bidder: 'kargo', - hb_adid: 'abc', - burl: 'https://ssp/bill', - }, - } + ;(window as any).__ts_request_id = 'test-rid-123' + + const fetchSpy = vi.spyOn(global, 'fetch').mockResolvedValue({ + ok: true, + json: async () => ({ + atf: { + hb_pb: '1.00', + hb_bidder: 'kargo', + hb_adid: 'abc', + burl: 'https://ssp/bill', + }, + }), + } as Response) - // Must import installTsAdInit from the module - const { installTsAdInit } = require('./index') + const { installTsAdInit } = await import('./index') installTsAdInit() - ;(window as any).__tsAdInit() + await (window as any).__tsAdInit() - expect((window as any).googletag.defineSlot).toHaveBeenCalledWith( - '/123/atf', - [[300, 250]], - 'atf' + expect(fetchSpy).toHaveBeenCalledWith( + expect.stringContaining('/ts-bids?rid=test-rid-123'), + expect.objectContaining({ credentials: 'omit' }) ) expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_pb', '1.00') expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_bidder', 'kargo') expect(mockPubads.refresh).toHaveBeenCalled() + + fetchSpy.mockRestore() + }) + + it('calls refresh with empty bids when fetch fails', async () => { + const mockPubads = { + enableSingleRequest: vi.fn(), + addEventListener: vi.fn(), + refresh: vi.fn(), + } + ;(window as any).googletag = { + cmd: { push: vi.fn((fn: () => void) => fn()) }, + defineSlot: vi.fn().mockReturnValue({ + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + }), + pubads: vi.fn().mockReturnValue(mockPubads), + enableServices: vi.fn(), + } + ;(window as any).__ts_ad_slots = [] + ;(window as any).__ts_request_id = 'rid-fail' + + vi.spyOn(global, 'fetch').mockRejectedValue(new Error('network error')) + + const { installTsAdInit } = await import('./index') + installTsAdInit() + await (window as any).__tsAdInit() + + expect(mockPubads.refresh).toHaveBeenCalled() }) - it('fires burl via sendBeacon on slotRenderEnded when our bid won', () => { + it('fires burl via sendBeacon on slotRenderEnded when our bid won', async () => { const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true) - // ... setup and trigger slotRenderEnded event - // Verify: navigator.sendBeacon called with burl + let capturedListener: ((e: any) => void) | undefined + + const mockSlot = { + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + getSlotElementId: vi.fn().mockReturnValue('atf'), + getTargeting: vi.fn().mockReturnValue(['abc']), + } + const mockPubads = { + enableSingleRequest: vi.fn(), + refresh: vi.fn(), + addEventListener: vi.fn((event: string, fn: (e: any) => void) => { + if (event === 'slotRenderEnded') capturedListener = fn + }), + } + ;(window as any).googletag = { + cmd: { push: vi.fn((fn: () => void) => fn()) }, + defineSlot: vi.fn().mockReturnValue(mockSlot), + pubads: vi.fn().mockReturnValue(mockPubads), + enableServices: vi.fn(), + } + ;(window as any).__ts_ad_slots = [ + { + id: 'atf', + gam_unit_path: '/123/atf', + div_id: 'atf', + formats: [[300, 250]], + targeting: {}, + }, + ] + ;(window as any).__ts_request_id = 'rid-burl-test' + + vi.spyOn(global, 'fetch').mockResolvedValue({ + ok: true, + json: async () => ({ + atf: { + hb_pb: '1.00', + hb_bidder: 'kargo', + hb_adid: 'abc', + burl: 'https://ssp/bill', + }, + }), + } as Response) + + const { installTsAdInit } = await import('./index') + installTsAdInit() + await (window as any).__tsAdInit() + + // Trigger slotRenderEnded — slot has our winning hb_adid + expect(capturedListener).toBeDefined() + capturedListener!({ + isEmpty: false, + slot: mockSlot, + }) + + expect(beaconSpy).toHaveBeenCalledWith('https://ssp/bill') beaconSpy.mockRestore() }) }) ``` Run: `cd crates/js/lib && npx vitest run` - Expected: FAIL — `installTsAdInit` not exported + Expected: FAIL — `installTsAdInit` not exported or fetches wrong endpoint - [ ] **Step 2: Add `installTsAdInit` to `index.ts`** - Add to `crates/js/lib/src/integrations/gpt/index.ts` (bottom of file): + Add to `crates/js/lib/src/integrations/gpt/index.ts`: ```typescript interface TsAdSlot { @@ -1505,60 +2023,87 @@ The TypeScript version is the authoritative implementation; it must mirror the R type TsWindow = Window & { __ts_ad_slots?: TsAdSlot[] - __ts_bids?: Record + __ts_request_id?: string __tsAdInit?: () => void } /** - * Install `window.__tsAdInit` — reads `window.__ts_ad_slots` and `window.__ts_bids` - * (injected by the edge into ), defines GPT slots, applies pre-won bid targeting, - * registers a `slotRenderEnded` listener to fire `burl` via `sendBeacon`, then calls - * `refresh()`. + * Install `window.__tsAdInit`. + * + * Reads `window.__ts_ad_slots` and `window.__ts_request_id` (both injected by + * the edge at `` open). Fetches bid results from `/ts-bids?rid=` + * concurrently with GPT slot definition. Applies targeting and calls `refresh()` + * after the fetch resolves. Registers `slotRenderEnded` to fire `burl` via + * `sendBeacon` when our specific Prebid bid wins the GAM line item match. */ export function installTsAdInit(): void { const w = window as TsWindow w.__tsAdInit = function () { const slots = w.__ts_ad_slots ?? [] - const bids = w.__ts_bids ?? {} + const rid = w.__ts_request_id + + const bidsPromise: Promise> = rid + ? fetch(`/ts-bids?rid=${encodeURIComponent(rid)}`, { + credentials: 'omit', + }) + .then((r) => (r.ok ? r.json() : {})) + .catch(() => ({})) + : Promise.resolve({}) + const g = (window as GptWindow).googletag if (!g) return + g.cmd.push(() => { - slots.forEach((slot) => { - const gptSlot = g.defineSlot?.( - slot.gam_unit_path, - slot.formats, - slot.div_id - ) - if (!gptSlot) return - gptSlot.addService(g.pubads()) - Object.entries(slot.targeting ?? {}).forEach(([k, v]) => - gptSlot.setTargeting(k, v) - ) - const bid = bids[slot.id] ?? {} - ;(['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { - if (bid[key]) gptSlot.setTargeting(key, bid[key]!) + const gptSlots = slots + .map((slot) => { + const gptSlot = g.defineSlot?.( + slot.gam_unit_path, + slot.formats, + slot.div_id + ) + if (!gptSlot) return null + gptSlot.addService(g.pubads()) + Object.entries(slot.targeting ?? {}).forEach(([k, v]) => + gptSlot.setTargeting(k, v) + ) + return { id: slot.id, gptSlot } }) - }) + .filter(Boolean) as Array<{ + id: string + gptSlot: NonNullable> + }> + g.pubads().enableSingleRequest() g.enableServices() - g.pubads().addEventListener?.('slotRenderEnded', (event: any) => { - const slotId: string = event.slot?.getSlotElementId?.() ?? '' - const bid = bids[slotId] ?? {} - if ( - !event.isEmpty && - bid.burl && - event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid - ) { - navigator.sendBeacon(bid.burl) - } + + bidsPromise.then((bids) => { + gptSlots.forEach(({ id, gptSlot }) => { + const bid = bids[id] ?? {} + ;(['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { + if (bid[key]) gptSlot.setTargeting(key, bid[key]!) + }) + }) + + g.pubads().addEventListener?.('slotRenderEnded', (event: any) => { + const slotId: string = event.slot?.getSlotElementId?.() ?? '' + const bid = bids[slotId] ?? {} + if ( + !event.isEmpty && + bid.burl && + event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid + ) { + navigator.sendBeacon(bid.burl) + } + }) + + g.pubads().refresh() }) - g.pubads().refresh() }) } } ``` - Call `installTsAdInit()` from the integration's initialization path so it's set up when the bundle loads. + Call `installTsAdInit()` from the integration's initialization path. - [ ] **Step 3: Run JS tests** @@ -1574,12 +2119,12 @@ The TypeScript version is the authoritative implementation; it must mirror the R ```bash git add crates/js/lib/src/integrations/gpt/ - git commit -m "Add __tsAdInit and slotRenderEnded burl firing to GPT integration" + git commit -m "Add installTsAdInit with /ts-bids fetch pattern and slotRenderEnded burl firing" ``` --- -## Task 11: `nurl` fire-and-forget +## Task 13: `nurl` fire-and-forget **Files:** @@ -1610,9 +2155,9 @@ The TypeScript version is the authoritative implementation; it must mirror the R fn default_fire_nurl_at_edge() -> bool { true } ``` -- [ ] **Step 3: Fire nurls in publisher.rs after auction** +- [ ] **Step 3: Fire nurls in publisher.rs after bid_cache.put()** - After `auction_result` is obtained, add: + After the `bid_cache.put(...)` call (Task 9 Step 3), add: ```rust if let Some(ref result) = auction_result { @@ -1620,7 +2165,7 @@ The TypeScript version is the authoritative implementation; it must mirror the R } ``` - Add helper (no `.await` — fire-and-forget): + Add helper: ```rust fn fire_winning_nurls( @@ -1671,13 +2216,13 @@ The TypeScript version is the authoritative implementation; it must mirror the R --- -## Task 12: End-to-end integration tests +## Task 14: End-to-end integration tests **Files:** - Modify: `crates/trusted-server-core/src/publisher.rs` (test module) -Tests use `pub(crate)` helpers from Task 8 directly. +Tests use `pub(crate)` helpers from Task 9 directly. - [ ] **Step 1: Write tests** @@ -1686,7 +2231,7 @@ Tests use `pub(crate)` helpers from Task 8 directly. ```rust #[cfg(test)] mod creative_opportunities_tests { - use super::{build_ad_slots_script, build_ad_bids_script, html_escape_for_script}; + use super::{build_head_globals_script, build_bid_map, html_escape_for_script}; use crate::creative_opportunities::{ CreativeOpportunitiesConfig, CreativeOpportunitySlot, CreativeOpportunityFormat, CreativeOpportunitiesFile, match_slots, @@ -1719,20 +2264,32 @@ Tests use `pub(crate)` helpers from Task 8 directly. } #[test] - fn ad_slots_script_is_safe_and_parseable() { + fn head_globals_script_contains_ad_slots_and_request_id() { let slots = vec![make_slot()]; let config = make_config(); - let script = build_ad_slots_script(&slots, &config); - assert!(script.contains("window.__ts_ad_slots=JSON.parse"), "should use JSON.parse"); + let rid = "550e8400-e29b-41d4-a716-446655440000"; + let script = build_head_globals_script(&slots, rid, &config); + assert!(script.contains("window.__ts_ad_slots=JSON.parse"), "should use JSON.parse for slots"); assert!(script.contains("atf_sidebar_ad"), "should include slot id"); - // Verify no raw < or > that could break HTML parser - let inner = script.trim_start_matches(""); + assert!(script.contains(&format!("window.__ts_request_id=\"{rid}\"")), "should include request_id"); + assert!(!script.contains("__ts_bids"), "must NOT contain bids — bids come from /ts-bids"); + } + + #[test] + fn head_globals_script_is_xss_safe() { + let slots = vec![make_slot()]; + let config = make_config(); + let script = build_head_globals_script(&slots, "safe-rid", &config); + // Strip outer "); assert!(!inner.contains('<'), "no unescaped < in script content"); assert!(!inner.contains('>'), "no unescaped > in script content"); } #[test] - fn ad_bids_script_uses_price_bucket_and_ad_id() { + fn bid_map_uses_price_bucket_and_ad_id() { let mut winning_bids = HashMap::new(); winning_bids.insert("atf_sidebar_ad".to_string(), Bid { slot_id: "atf_sidebar_ad".to_string(), @@ -1747,11 +2304,23 @@ Tests use `pub(crate)` helpers from Task 8 directly. ad_id: Some("prebid-uuid-abc123".to_string()), metadata: HashMap::new(), }); - let script = build_ad_bids_script(&winning_bids, PriceGranularity::Dense); - assert!(script.contains("\"hb_pb\":\"2.53\""), "should bucket 2.53 as 2.53 (dense)"); - assert!(script.contains("\"hb_bidder\":\"kargo\""), "should include bidder"); - assert!(script.contains("\"hb_adid\":\"prebid-uuid-abc123\""), "should use ad_id not creative markup"); - assert!(script.contains("burl"), "should include burl for billing"); + let bid_map = build_bid_map(&winning_bids, PriceGranularity::Dense); + let slot_bids = bid_map.get("atf_sidebar_ad").expect("should have slot bids"); + assert_eq!( + slot_bids.get("hb_pb").and_then(|v| v.as_str()), + Some("2.53"), + "should bucket 2.53 as 2.53 (dense)" + ); + assert_eq!( + slot_bids.get("hb_bidder").and_then(|v| v.as_str()), + Some("kargo"), + "should include bidder" + ); + assert_eq!( + slot_bids.get("hb_adid").and_then(|v| v.as_str()), + Some("prebid-uuid-abc123"), + "should use ad_id not creative markup" + ); } #[test] @@ -1795,7 +2364,7 @@ Tests use `pub(crate)` helpers from Task 8 directly. ```bash git add crates/trusted-server-core/src/publisher.rs - git commit -m "Add integration tests for creative opportunities pipeline (slots, bids, XSS)" + git commit -m "Add integration tests for creative opportunities pipeline (head globals, bid map, XSS)" ``` --- @@ -1804,19 +2373,27 @@ Tests use `pub(crate)` helpers from Task 8 directly. Run `fastly compute serve` and verify: -- [ ] **No match:** Request `/about` — no `__ts_ad_slots` or `__ts_bids` in response HTML, no `Cache-Control: private, no-store` -- [ ] **Match:** Request `/2024/01/article` — both globals present in ``, `Cache-Control: private, no-store` set -- [ ] **Empty file kill-switch:** Empty `creative-opportunities.toml` → no globals injected on any URL -- [ ] **Auction timeout:** Set `auction_timeout_ms = 1` → `__ts_bids` injects as `{}`, no slot entries -- [ ] **XSS check:** Add `targeting = { zone = " -``` - -> **Security:** All string values are JSON-serialized via `serde_json` and HTML-escaped -> before insertion into the `, ContentType::Html)`. + +> **Security:** All string values are JSON-serialized via `serde_json` and HTML-escaped +> before insertion into the `"# - .to_string() + r#""#.to_string() ), + ad_bids_state: std::sync::Arc::new(std::sync::RwLock::new(None)), }; let mut processor = create_html_processor(config); let output = processor - .process_chunk(b"T", true) + .process_chunk(b"Tcontent", true) .expect("should process"); let html = std::str::from_utf8(&output).expect("should be utf8"); assert!(html.contains("window.__ts_ad_slots"), "should inject ad slots at head-open"); - assert!(html.contains("window.__ts_request_id"), "should inject request_id at head-open"); + assert!(!html.contains("__ts_request_id"), "must NOT inject request_id — body-injection arch has no request_id"); } #[test] - fn does_not_hold_end_of_head() { - // Verify: no bid data appears before — that hold was rejected by spec §4.3 + fn injects_ts_bids_before_body_close() { + let bids_script = r#""#; + let state = std::sync::Arc::new(std::sync::RwLock::new( + Some(bids_script.to_string()) + )); let config = HtmlProcessorConfig { origin_host: "origin.example.com".to_string(), request_host: "example.com".to_string(), request_scheme: "https".to_string(), integrations: IntegrationRegistry::empty_for_tests(), ad_slots_script: None, + ad_bids_state: state, }; let mut processor = create_html_processor(config); let output = processor - .process_chunk(b"T", true) + .process_chunk(b"content", true) .expect("should process"); let html = std::str::from_utf8(&output).expect("should be utf8"); - assert!(!html.contains("__ts_bids"), "must not inject bids into head"); + assert!(html.contains("window.__ts_bids"), "should inject bids before "); + let bids_pos = html.find("window.__ts_bids").expect("bids should be in output"); + let body_close_pos = html.find("").expect(" should be in output"); + assert!(bids_pos < body_close_pos, "bids must appear before "); + } + + #[test] + fn injects_empty_ts_bids_when_state_is_none() { + let state = std::sync::Arc::new(std::sync::RwLock::new(None)); + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: None, + ad_bids_state: state, + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk(b"content", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!(html.contains("__ts_bids=JSON.parse(\"{}\""), "should inject empty bids on None state"); } ``` Run: `cargo test -p trusted-server-core html_processor` - Expected: compile error (no `ad_slots_script` field, no `empty_for_tests()`) + Expected: compile error (no `ad_bids_state` field yet) - [ ] **Step 2: Add `empty_for_tests()` to `IntegrationRegistry`** @@ -902,9 +930,7 @@ The `hb_pb` value in bid responses is a discretized bucket string from Prebid's (Adjust field names to match the actual `RegistryInner` struct.) -- [ ] **Step 3: Add single field to `HtmlProcessorConfig`** - - Replace any existing `ad_slots_script`/`ad_bids_script` fields with: +- [ ] **Step 3: Update `HtmlProcessorConfig`** ```rust pub struct HtmlProcessorConfig { @@ -912,362 +938,104 @@ The `hb_pb` value in bid responses is a discretized bucket string from Prebid's pub request_host: String, pub request_scheme: String, pub integrations: IntegrationRegistry, - /// Pre-computed ``. - /// Injected at `` open, before integration head inserts. `None` when no slots matched. + /// Pre-computed ``. + /// Injected at `` open. `None` when no slots matched. pub ad_slots_script: Option, + /// Shared auction result script — written by the auction task before HTML processing + /// begins. Handler reads this in `el.on_end_tag()` on the body element. + /// `None` means no auction ran (consent denied, bot UA, no slot match, etc.); + /// inject empty `__ts_bids = {}` as graceful fallback. + pub ad_bids_state: std::sync::Arc>>, } ``` - Update `from_settings` (or wherever `HtmlProcessorConfig` is constructed) to initialize `ad_slots_script: None`. + Update `from_settings` (or wherever `HtmlProcessorConfig` is constructed) to initialize `ad_bids_state: Arc::new(RwLock::new(None))`. - [ ] **Step 4: Inject `ad_slots_script` at head-open** - In `create_html_processor`, within the EXISTING `element!("head", ...)` handler, build the full snippet string with `ad_slots_script` first (so it appears first in output — lol_html `prepend` inserts before children, with **last-prepend-wins** ordering, so we call `prepend` exactly once with the full combined string): + In `create_html_processor`, within the existing `element!("head", ...)` handler: ```rust let ad_slots_script = config.ad_slots_script.clone(); - // ... existing captures ... + // existing captures... element!("head", |el| { let mut snippet = String::new(); - - // ad_slots_script first so __ts_ad_slots + __ts_request_id appear before - // integration inserts. DO NOT call prepend multiple times — lol_html stacks - // prepend calls in reverse order, so a single prepend with the full string - // guarantees correct ordering. if let Some(ref slots_script) = ad_slots_script { snippet.push_str(slots_script); } - - // ... existing: for insert in integrations.head_inserts(&ctx) { snippet.push_str(...) } - + // existing integration head inserts... if !snippet.is_empty() { el.prepend(&snippet, ContentType::Html); } - // DO NOT register on_end_tag — flushes immediately per spec §4.3 + // DO NOT register on_end_tag — flushes immediately Ok(()) }) ``` -- [ ] **Step 5: Run tests** - - Run: `cargo test -p trusted-server-core html_processor` - Expected: all tests pass (including the new ones; no bids injection test must also pass) - -- [ ] **Step 6: Run full suite** - - Run: `cargo test --workspace` - Expected: clean - -- [ ] **Step 7: Commit** - - ```bash - git add crates/trusted-server-core/src/html_processor.rs \ - crates/trusted-server-core/src/integrations/registry.rs - git commit -m "Add ad_slots_script injection to HtmlProcessorConfig at head-open; no hold" - ``` - ---- - -## Task 8: `bid_cache.rs` — In-process auction result cache - -**Files:** - -- Create: `crates/trusted-server-core/src/bid_cache.rs` -- Modify: `crates/trusted-server-core/src/lib.rs` - -The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL. It is shared across concurrent Fastly request handlers via `std::sync::Mutex`. The `/ts-bids` endpoint (Task 10) uses `wait_for()` to block-poll until results arrive or the deadline fires. - -> **WASM note:** `std::time::Instant` and `std::thread::sleep` are both supported in Viceroy and Fastly Compute. The Mutex is uncontested in practice — requests are handled cooperatively with brief lock windows. - -- [ ] **Step 1: Write failing tests** +- [ ] **Step 5: Inject `__ts_bids` before `` via `el.on_end_tag()`** - Create `crates/trusted-server-core/src/bid_cache.rs` with only the tests: + Add a new handler in `create_html_processor`. The shared state is already populated by the time lol_html reaches `` (Task 9 awaits the auction before starting HTML processing): ```rust - #[cfg(test)] - mod tests { - use super::*; - use std::time::{Duration, Instant}; - - fn make_bids() -> BidMap { - let mut m = std::collections::HashMap::new(); - m.insert("atf".to_string(), serde_json::json!({"hb_pb": "1.00"})); - m - } - - #[test] - fn returns_not_found_for_unknown_rid() { - let cache = BidCache::new(Duration::from_secs(30), 100); - let result = cache.try_get("unknown-rid"); - assert!(matches!(result, CacheResult::NotFound), "should return NotFound"); - } - - #[test] - fn returns_pending_before_put() { - let cache = BidCache::new(Duration::from_secs(30), 100); - let deadline = Instant::now() + Duration::from_secs(5); - cache.mark_pending("rid-1", deadline); - let result = cache.try_get("rid-1"); - assert!(matches!(result, CacheResult::Pending), "should be Pending"); - } - - #[test] - fn returns_bids_after_put() { - let cache = BidCache::new(Duration::from_secs(30), 100); - let deadline = Instant::now() + Duration::from_secs(5); - cache.mark_pending("rid-2", deadline); - cache.put("rid-2", make_bids()); - match cache.try_get("rid-2") { - CacheResult::Complete(bids) => { - assert!(bids.contains_key("atf"), "should contain atf bid"); + let ad_bids_state = config.ad_bids_state.clone(); + + element!("body", |el| { + let state = ad_bids_state.clone(); + el.on_end_tag(move |end_tag| { + let script = state.read().expect("should read bid state"); + let bids_script = match &*script { + Some(s) => s.clone(), + None => { + r#""#.to_string() } - other => panic!("expected Complete, got {:?}", other), - } - } - - #[test] - fn returns_not_found_for_expired_entry() { - let cache = BidCache::new(Duration::from_millis(1), 100); - let deadline = Instant::now() + Duration::from_secs(5); - cache.mark_pending("rid-3", deadline); - cache.put("rid-3", make_bids()); - std::thread::sleep(Duration::from_millis(5)); - let result = cache.try_get("rid-3"); - assert!(matches!(result, CacheResult::NotFound), "should expire after TTL"); - } - - #[test] - fn wait_for_returns_bids_immediately_when_complete() { - let cache = BidCache::new(Duration::from_secs(30), 100); - let deadline = Instant::now() + Duration::from_secs(5); - cache.mark_pending("rid-4", deadline); - cache.put("rid-4", make_bids()); - let result = cache.wait_for("rid-4", deadline); - assert!(matches!(result, WaitResult::Bids(_)), "should return bids immediately"); - } - - #[test] - fn wait_for_returns_not_found_for_unknown_rid() { - let cache = BidCache::new(Duration::from_secs(30), 100); - let deadline = Instant::now() + Duration::from_millis(50); - let result = cache.wait_for("never-registered", deadline); - assert!(matches!(result, WaitResult::NotFound), "should return NotFound"); - } - } - ``` - - Run: `cargo test -p trusted-server-core bid_cache` - Expected: compile error (module not exported yet) - -- [ ] **Step 2: Implement bid_cache.rs** - - ```rust - //! In-process auction result cache keyed by request ID. - //! - //! Shared across concurrent Fastly request handlers via a global `Mutex`. - //! Entries expire after a configurable TTL (30 seconds by default). - - use std::collections::HashMap; - use std::sync::Mutex; - use std::time::{Duration, Instant}; - - pub type BidMap = HashMap; - - #[derive(Debug)] - enum EntryState { - Pending { auction_deadline: Instant }, - Complete { bids: BidMap }, - } - - struct CacheEntry { - state: EntryState, - inserted_at: Instant, - } - - struct BidCacheInner { - entries: HashMap, - insertion_order: std::collections::VecDeque, - capacity: usize, - ttl: Duration, - } - - impl BidCacheInner { - fn evict_expired(&mut self) { - let now = Instant::now(); - self.insertion_order.retain(|rid| { - self.entries.get(rid) - .map(|e| now.duration_since(e.inserted_at) < self.ttl) - .unwrap_or(false) - }); - self.entries.retain(|_, e| now.duration_since(e.inserted_at) < self.ttl); - } - - fn evict_oldest_if_full(&mut self) { - while self.entries.len() >= self.capacity { - if let Some(oldest) = self.insertion_order.pop_front() { - self.entries.remove(&oldest); - } else { - break; - } - } - } - } - - /// Outcome of a non-blocking cache lookup. - #[derive(Debug)] - pub enum CacheResult { - /// Auction complete; bids are ready. - Complete(BidMap), - /// Auction registered but not yet complete. - Pending, - /// Request ID never registered, or TTL expired. - NotFound, - } - - /// Outcome of a blocking `wait_for` call. - #[derive(Debug)] - pub enum WaitResult { - /// Auction completed within the deadline. - Bids(BidMap), - /// Deadline passed; bids not available. - Empty, - /// Request ID never registered (caller should return 404). - NotFound, - } - - /// In-process cache for auction results, shared across request handlers. - pub struct BidCache { - inner: Mutex, - } - - impl BidCache { - /// Create a new `BidCache`. - /// - /// # Arguments - /// - `ttl`: how long to keep entries before expiry - /// - `capacity`: max number of concurrent entries (oldest evicted when full) - pub fn new(ttl: Duration, capacity: usize) -> Self { - Self { - inner: Mutex::new(BidCacheInner { - entries: HashMap::new(), - insertion_order: std::collections::VecDeque::new(), - capacity, - ttl, - }), - } - } - - /// Register a request as in-flight. Call at auction start, before `run_auction`. - pub fn mark_pending(&self, request_id: &str, auction_deadline: Instant) { - let mut inner = self.inner.lock().expect("should lock bid_cache"); - inner.evict_expired(); - inner.evict_oldest_if_full(); - inner.entries.insert(request_id.to_string(), CacheEntry { - state: EntryState::Pending { auction_deadline }, - inserted_at: Instant::now(), - }); - inner.insertion_order.push_back(request_id.to_string()); - } - - /// Store completed auction results. Transitions entry from Pending → Complete. - pub fn put(&self, request_id: &str, bids: BidMap) { - let mut inner = self.inner.lock().expect("should lock bid_cache"); - if let Some(entry) = inner.entries.get_mut(request_id) { - entry.state = EntryState::Complete { bids }; - } - } - - /// Non-blocking lookup. Returns current state without sleeping. - pub fn try_get(&self, request_id: &str) -> CacheResult { - let inner = self.inner.lock().expect("should lock bid_cache"); - let now = Instant::now(); - match inner.entries.get(request_id) { - None => CacheResult::NotFound, - Some(entry) if now.duration_since(entry.inserted_at) >= inner.ttl => { - CacheResult::NotFound - } - Some(entry) => match &entry.state { - EntryState::Pending { .. } => CacheResult::Pending, - EntryState::Complete { bids } => CacheResult::Complete(bids.clone()), - }, - } - } - - /// Return the stored auction deadline for a pending entry (the `T₀ + auction_timeout_ms` - /// value minted when the page request arrived). Used by `/ts-bids` to enforce the correct - /// deadline rather than minting a fresh `Instant::now() + timeout`. - /// - /// Returns `None` if the entry is unknown, expired, or already complete. - pub fn get_auction_deadline(&self, request_id: &str) -> Option { - let inner = self.inner.lock().expect("should lock bid_cache"); - let now = Instant::now(); - inner.entries.get(request_id).and_then(|entry| { - if now.duration_since(entry.inserted_at) >= inner.ttl { - return None; - } - match entry.state { - EntryState::Pending { auction_deadline } => Some(auction_deadline), - EntryState::Complete { .. } => None, - } - }) - } - - /// Block until bids are available for `request_id` or `deadline` passes. - /// - /// Polls every 50ms. Returns `NotFound` immediately if `request_id` was never registered. - /// Returns `Empty` if deadline fires before auction completes. - pub fn wait_for(&self, request_id: &str, deadline: Instant) -> WaitResult { - loop { - match self.try_get(request_id) { - CacheResult::Complete(bids) => return WaitResult::Bids(bids), - CacheResult::NotFound => return WaitResult::NotFound, - CacheResult::Pending => { - if Instant::now() >= deadline { - return WaitResult::Empty; - } - std::thread::sleep(Duration::from_millis(50)); - } - } - } - } - } + }; + end_tag.before(&bids_script, ContentType::Html); + Ok(()) + })?; + Ok(()) + }) ``` -- [ ] **Step 3: Export from lib.rs** +- [ ] **Step 6: Run tests** - ```rust - pub mod bid_cache; - ``` + Run: `cargo test -p trusted-server-core html_processor` + Expected: all tests pass -- [ ] **Step 4: Run tests** +- [ ] **Step 7: Run full suite** - Run: `cargo test -p trusted-server-core bid_cache` - Expected: all tests pass + Run: `cargo test --workspace` + Expected: clean -- [ ] **Step 5: Commit** +- [ ] **Step 8: Commit** ```bash - git add crates/trusted-server-core/src/bid_cache.rs \ - crates/trusted-server-core/src/lib.rs - git commit -m "Add BidCache with 30s TTL, pending/complete states, and blocking wait_for" + git add crates/trusted-server-core/src/html_processor.rs \ + crates/trusted-server-core/src/integrations/registry.rs + git commit -m "Inject __ts_ad_slots at head-open and __ts_bids before via shared auction state" ``` --- -## Task 9: `handle_publisher_request` async restructuring +## Task 8: `handle_publisher_request` async restructuring **Files:** - Modify: `crates/trusted-server-core/src/publisher.rs` - Modify: `crates/trusted-server-adapter-fastly/src/main.rs` -> **Key constraint from spec §4.3:** Page rendering is never held for the auction. The auction and origin fetch run concurrently via Fastly's `send_async()` model — origin is dispatched first (non-blocking), then the auction runs its own `send_async` calls, so both overlap on the network. Bid results go to `bid_cache` only — they are NOT injected into the HTML. `Cache-Control: private, no-store` is set whenever slots matched (not just when bids arrived). +> **Key constraint from spec §4.3 and §3:** No `bid_cache`. No `/ts-bids`. No `request_id`. Bids travel inline with the HTML response via body injection. The `Arc>>` is the coordination mechanism within a single request's lifetime — it is written before HTML processing and read by the lol_html `` handler. + +> **Eligibility gating (spec §4.3):** Auctions fire only for real GET requests from non-bot, non-prefetch clients with TCF Purpose 1 consent and at least one matching slot. All other requests proceed with no auction and no `__ts_bids` injection. + +> **Cache-Control (spec §4.7):** Set `Cache-Control: private, max-age=0` (not `no-store`) to preserve BFCache eligibility. Strip `Surrogate-Control` and `Fastly-Surrogate-Control`. - [ ] **Step 1: Update function signature** Change `handle_publisher_request` in `publisher.rs`: + > **Existing context:** The existing `publisher.rs` function body already computes `consent_context`, `ec_id`, `request_info`, `origin_host`, and `backend_name` before the origin fetch. Steps below insert new logic between those existing computations and the origin fetch — they do not replace them. + ```rust pub async fn handle_publisher_request( settings: &Settings, @@ -1275,31 +1043,48 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL services: &RuntimeServices, orchestrator: &crate::auction::orchestrator::AuctionOrchestrator, slots_file: &crate::creative_opportunities::CreativeOpportunitiesFile, - bid_cache: &crate::bid_cache::BidCache, mut req: Request, ) -> Result> ``` - Add imports: + Add imports at top of file: ```rust + use std::sync::{Arc, RwLock}; + use fastly::http::header; use crate::auction::orchestrator::AuctionOrchestrator; use crate::auction::types::{AuctionContext, AuctionRequest, PublisherInfo, UserInfo, SiteInfo}; - use crate::bid_cache::{BidCache, BidMap}; use crate::creative_opportunities::{CreativeOpportunitiesFile, match_slots}; use crate::price_bucket::price_bucket; ``` -- [ ] **Step 2: Mint `request_id`, match URL, check consent** + > **`send_async` return type:** `req.send_async()` returns `fastly::handle::PendingRequestHandle` (re-exported as `fastly::PendingRequest` in recent versions). Confirm the exact type from the `fastly` crate version in `Cargo.toml`; `.wait()` is the blocking resolve method on whichever type is returned. - At the top of the function body, before the origin fetch: +- [ ] **Step 2: Apply auction-eligibility gates** - ```rust - // Mint per-request UUID — included in head injection and /ts-bids lookup key. - let request_id = uuid::Uuid::new_v4().to_string(); + At the top of the function body, before origin fetch: + ```rust let request_path = req.get_path().to_string(); - let matched_slots: Vec<_> = if settings.creative_opportunities.is_some() { + let request_method = req.get_method().clone(); + + // Gate 1: Only GET triggers auctions. HEAD skips everything. + let is_get = request_method == fastly::http::Method::GET; + + // Gate 2: Skip prefetch hints (Sec-Purpose: prefetch or Purpose: prefetch). + let is_prefetch = req.get_header_str("sec-purpose") + .map_or(false, |v| v.contains("prefetch")) + || req.get_header_str("purpose") + .map_or(false, |v| v.contains("prefetch")); + + // Gate 3: Skip well-known crawler UAs (protects SSP QPS budget). + let user_agent = req.get_header_str("user-agent").unwrap_or(""); + let is_bot = ["Googlebot", "Bingbot", "AhrefsBot", "SemrushBot", "DotBot"] + .iter() + .any(|bot| user_agent.contains(bot)); + + // Gate 4: Slot match. + let matched_slots: Vec<_> = if settings.creative_opportunities.is_some() && is_get { match_slots(&slots_file.slots, &request_path) .into_iter() .cloned() @@ -1308,11 +1093,17 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL Vec::new() }; + // Gate 5: TCF Purpose 1 consent. let consent_allows_auction = consent_context .tcf .as_ref() .map_or(false, |tcf| tcf.has_purpose_consent(1)); - let should_run_auction = !matched_slots.is_empty() && consent_allows_auction; + + let should_run_auction = is_get + && !is_prefetch + && !is_bot + && !matched_slots.is_empty() + && consent_allows_auction; let auction_timeout_ms = settings .creative_opportunities @@ -1321,33 +1112,24 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL .unwrap_or(settings.auction.timeout_ms); ``` -- [ ] **Step 3: Register pending in bid_cache, fire origin + auction concurrently** +- [ ] **Step 3: Create shared bid state, fire origin + auction concurrently** ```rust - // Mint T₀ auction deadline. Stored in bid_cache so /ts-bids uses the same deadline, - // not a freshly-minted one when the browser's fetch arrives. - let auction_deadline = std::time::Instant::now() - + std::time::Duration::from_millis(u64::from(auction_timeout_ms)); - - // Register request as in-flight so /ts-bids can long-poll for it. - if should_run_auction { - bid_cache.mark_pending(&request_id, auction_deadline); - } + // Shared state: auction task writes the ready-to-inject script; lol_html + // handler reads it. Both within the same request — no cross-request sharing. + let ad_bids_state: Arc>> = Arc::new(RwLock::new(None)); restrict_accept_encoding(&mut req); req.set_header("host", &origin_host); - // Fire origin request immediately — Fastly's send_async dispatches the HTTP request - // to the network without blocking. The origin fetch is in-flight from this point. - // The auction below also uses send_async internally, so both origin SSP requests - // overlap on the network. This is Fastly's concurrency model — no join! needed. + // Fire origin immediately — both origin and auction SSP calls overlap on the network. let pending_origin = req .send_async(&backend_name) .change_context(TrustedServerError::Proxy { message: "Failed to dispatch async origin request".to_string(), })?; - // Run auction (internal send_async calls overlap with origin fetch on the network). + // Run auction. Internal SSP calls use send_async and overlap with origin fetch. let auction_result = if should_run_auction { let co_config = settings.creative_opportunities.as_ref() .expect("should be present when should_run_auction is true"); @@ -1377,17 +1159,20 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL None }; - // Write auction results to bid_cache — /ts-bids will serve them. + // Write auction result to shared state before HTML processing begins. + // The lol_html handler reads this synchronously — it is always populated here. + // `build_bid_map` returns `serde_json::Map`. if should_run_auction { let co_config = settings.creative_opportunities.as_ref() .expect("should be present"); - // Bind empty map to a local to avoid &Default::default() referencing a temporary. - let empty_bids = std::collections::HashMap::new(); + let empty_bids: std::collections::HashMap = + std::collections::HashMap::new(); let winning_bids = auction_result.as_ref() .map(|r| &r.winning_bids) .unwrap_or(&empty_bids); let bid_map = build_bid_map(winning_bids, co_config.price_granularity); - bid_cache.put(&request_id, bid_map); + let bids_script = build_bids_script(&bid_map); + *ad_bids_state.write().expect("should write bid state") = Some(bids_script); } // Await origin response (may already be buffered since we started it before the auction). @@ -1403,10 +1188,9 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL After acquiring `response`: ```rust - // Build head injection script: __ts_ad_slots + __ts_request_id (never bids). let ad_slots_script = if let Some(co_config) = &settings.creative_opportunities { if !matched_slots.is_empty() { - Some(build_head_globals_script(&matched_slots, &request_id, co_config)) + Some(build_ad_slots_script(&matched_slots, co_config)) } else { None } @@ -1414,33 +1198,91 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL None }; - // When slots matched: prevent browser/CDN caching of the per-user assembled HTML. - // Spec §4.4: set regardless of whether bids arrived — the request_id is now in the page. + // Set cache headers when slots matched. private, max-age=0 (not no-store) preserves + // BFCache eligibility — browser back/forward cache restores the already-rendered ad + // without firing a new GAM call, which is the desired behavior. if ad_slots_script.is_some() { - response.set_header(header::CACHE_CONTROL, "private, no-store"); + response.set_header(header::CACHE_CONTROL, "private, max-age=0"); response.remove_header("surrogate-control"); response.remove_header("fastly-surrogate-control"); } - // Spec §4.3/§4.7: Force chunked encoding on every origin response so that - // reaches the browser immediately as chunks arrive — regardless of whether origin - // sent a buffered response (WordPress, Drupal) or a streaming one (NextJS 16). - // Removing Content-Length is required; sending both headers is invalid HTTP/1.1. + // Force chunked encoding so reaches the browser immediately as chunks arrive. + // Sending both Content-Length and Transfer-Encoding is invalid HTTP/1.1. response.remove_header(header::CONTENT_LENGTH); response.set_header("transfer-encoding", "chunked"); ``` -- [ ] **Step 5: Add `pub(crate)` helper functions** +- [ ] **Step 5: Thread shared state into `OwnedProcessResponseParams`** + + Update `OwnedProcessResponseParams`: + + ```rust + pub struct OwnedProcessResponseParams { + // existing fields... + pub(crate) ad_slots_script: Option, + pub(crate) ad_bids_state: Arc>>, + } + ``` + + Pass both through to `create_html_stream_processor` and into `HtmlProcessorConfig`. + +- [ ] **Step 6: Add `pub(crate)` helper functions** + + > **`BidMap` type:** Use `serde_json::Map` directly — no separate module needed. + + Add helpers in this order (each function is used by the one below it, so define leaf functions first): ```rust + /// HTML-escape a JSON string for safe inline `"#) + } + /// Build the `"# - ) - } - - /// Build the `BidMap` stored in `bid_cache` and returned by `/ts-bids`. - /// - /// Keyed by slot ID. Values contain `hb_pb`, `hb_bidder`, `hb_adid`, `burl`. - pub(crate) fn build_bid_map( - winning_bids: &std::collections::HashMap, - price_granularity: crate::price_bucket::PriceGranularity, - ) -> crate::bid_cache::BidMap { - winning_bids - .iter() - .filter_map(|(slot_id, bid)| { - let cpm = bid.price?; - let entry: std::collections::HashMap = [ - ("hb_pb".to_string(), serde_json::Value::String(price_bucket(cpm, price_granularity))), - ("hb_bidder".to_string(), serde_json::Value::String(bid.bidder.clone())), - ("hb_adid".to_string(), serde_json::Value::String( - bid.ad_id.as_deref().unwrap_or("").to_string() - )), - ("burl".to_string(), bid.burl.as_deref() - .map(serde_json::Value::from) - .unwrap_or(serde_json::Value::Null)), - ].into_iter().collect(); - Some((slot_id.clone(), entry.into_iter() - .map(|(k, v)| (k, v)) - .collect::>() - .into())) - }) - .collect() - } - - /// HTML-escape a JSON string for safe inline `"#) } fn build_auction_request( @@ -1535,39 +1332,25 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL } ``` -- [ ] **Step 6: Thread `ad_slots_script` into `OwnedProcessResponseParams`** - - Update `OwnedProcessResponseParams`: - - ```rust - pub struct OwnedProcessResponseParams { - // existing fields... - pub(crate) ad_slots_script: Option, - } - ``` - - Pass `ad_slots_script` through to `create_html_stream_processor` and into `HtmlProcessorConfig`. + > **Type note:** All helper signatures use `serde_json::Map` directly. Do not create a `BidMap` type alias or `bid_types.rs` module. - [ ] **Step 7: Update `main.rs` call site** In `crates/trusted-server-adapter-fastly/src/main.rs`: ```rust - // At startup — load creative-opportunities.toml and initialize bid_cache. + // At startup (top of main() / request handler setup, before the request dispatch loop). + // include_str! embeds the file at compile time — no runtime file I/O. const CREATIVE_OPPORTUNITIES_TOML: &str = include_str!("../../../creative-opportunities.toml"); - let slots_file: creative_opportunities::CreativeOpportunitiesFile = + let slots_file: trusted_server_core::creative_opportunities::CreativeOpportunitiesFile = toml::from_str(CREATIVE_OPPORTUNITIES_TOML) .expect("should parse creative-opportunities.toml"); - - // BidCache: 30s TTL, capacity 1000 entries (each entry is a few KB). - let bid_cache = crate::bid_cache::BidCache::new( - std::time::Duration::from_secs(30), - 1000, - ); ``` + `slots_file` is a local in the startup/handler scope and passed by reference into `handle_publisher_request` on each request — no `Arc` needed since it's immutable and the handler borrows it. + Update the call to `handle_publisher_request`: ```rust @@ -1575,15 +1358,16 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL settings, integration_registry, &publisher_services, - orchestrator, // existing - &slots_file, // new - &bid_cache, // new + orchestrator, // existing + &slots_file, // new req, ).await { // existing match arms unchanged } ``` + There is **no `/ts-bids` route** to add. The body injection is complete within `handle_publisher_request`. + - [ ] **Step 8: Compile check** Run: `cargo check --workspace` @@ -1599,164 +1383,43 @@ The `BidCache` stores auction results keyed by `request_id` with a 30-second TTL ```bash git add crates/trusted-server-core/src/publisher.rs \ crates/trusted-server-adapter-fastly/src/main.rs - git commit -m "Convert handle_publisher_request to async; auction writes to bid_cache; inject head globals only" + git commit -m "Convert handle_publisher_request to async; body-inject __ts_bids; eligibility gates; max-age=0" ``` --- -## Task 10: `/ts-bids` endpoint - -**Files:** - -- Modify: `crates/trusted-server-adapter-fastly/src/main.rs` - -The `/ts-bids` endpoint is the client's fetch target for bid results. It long-polls until the auction completes or the deadline fires, then returns JSON. Bid results were already stored in `bid_cache` by Task 9. - -- [ ] **Step 1: Write failing test (integration-style)** - - In `main.rs` test module (or a new `tests/ts_bids.rs`): - - ```rust - #[test] - fn ts_bids_response_structure() { - use crate::bid_cache::{BidCache, WaitResult}; - use std::time::{Duration, Instant}; - - let cache = BidCache::new(Duration::from_secs(30), 100); - let rid = "test-rid-abc"; - let deadline = Instant::now() + Duration::from_secs(5); - cache.mark_pending(rid, deadline); - let mut bids = std::collections::HashMap::new(); - bids.insert("atf".to_string(), serde_json::json!({ - "hb_pb": "1.00", "hb_bidder": "kargo", "hb_adid": "abc", "burl": null, - })); - cache.put(rid, bids); - - match cache.wait_for(rid, deadline) { - WaitResult::Bids(b) => { - assert!(b.contains_key("atf"), "should contain atf slot bids"); - } - other => panic!("expected Bids, got {:?}", other), - } - } - ``` - - Run: `cargo test -p trusted-server-adapter-fastly ts_bids` - Expected: compile error (no handler yet, or pass since it's testing bid_cache directly) - -- [ ] **Step 2: Add `/ts-bids` route handler in `main.rs`** - - In the request routing section, before the publisher fallback, add: - - ```rust - if req.get_path() == "/ts-bids" && req.get_method() == fastly::http::Method::GET { - return handle_ts_bids_request(req, &bid_cache, settings); - } - ``` - - Add the handler function: - - ```rust - fn handle_ts_bids_request( - req: fastly::Request, - bid_cache: &crate::bid_cache::BidCache, - settings: &Settings, - ) -> fastly::Response { - // Parse `rid` query param. - let rid = req.get_query_parameter("rid").map(String::from); - let rid = match rid { - Some(r) if !r.is_empty() => r, - _ => { - return fastly::Response::from_status(fastly::http::StatusCode::BAD_REQUEST) - .with_body_text_plain("missing rid parameter"); - } - }; - - // Use the stored T₀ auction deadline from bid_cache — not a freshly-minted - // Instant::now() + timeout, which would extend the window past the original A_deadline. - // Spec §4.4: "/ts-bids blocks until auction completion or A_deadline" where A_deadline - // = T₀ + auction_timeout_ms (minted at page request receipt, stored in bid_cache entry). - let deadline = bid_cache.get_auction_deadline(&rid) - .unwrap_or_else(|| { - // Fallback: rid is unknown or already complete. wait_for returns immediately. - std::time::Instant::now() - }); - - let result = bid_cache.wait_for(&rid, deadline); - - match result { - crate::bid_cache::WaitResult::Bids(bids) => { - let body = serde_json::to_string(&bids) - .unwrap_or_else(|_| "{}".to_string()); - fastly::Response::from_status(fastly::http::StatusCode::OK) - .with_header(fastly::http::header::CONTENT_TYPE, "application/json") - .with_header(fastly::http::header::CACHE_CONTROL, "private, no-store") - .with_body(body) - } - crate::bid_cache::WaitResult::Empty => { - fastly::Response::from_status(fastly::http::StatusCode::OK) - .with_header(fastly::http::header::CONTENT_TYPE, "application/json") - .with_header(fastly::http::header::CACHE_CONTROL, "private, no-store") - .with_body("{}") - } - crate::bid_cache::WaitResult::NotFound => { - fastly::Response::from_status(fastly::http::StatusCode::NOT_FOUND) - .with_header(fastly::http::header::CACHE_CONTROL, "private, no-store") - .with_body_text_plain("unknown request id") - } - } - } - ``` - -- [ ] **Step 3: Compile check** - - Run: `cargo check --workspace` - Expected: clean - -- [ ] **Step 4: Run tests** - - Run: `cargo test --workspace` - Expected: all pass - -- [ ] **Step 5: Commit** - - ```bash - git add crates/trusted-server-adapter-fastly/src/main.rs - git commit -m "Add /ts-bids endpoint with long-poll semantics; serves bid_cache results by request_id" - ``` - ---- - -## Task 11: GPT head injector — emit `__tsAdInit` with `/ts-bids` fetch +## Task 9: GPT head injector — emit `__tsAdInit` with synchronous bid read **Files:** - Modify: `crates/trusted-server-core/src/integrations/gpt.rs` -> **Critical:** The `__tsAdInit` function MUST fetch `/ts-bids?rid=` — it must NOT read from `window.__ts_bids` (which is never set). The `window.__ts_request_id` global (injected at head-open by Task 9) supplies the RID. +> **Critical:** `__tsAdInit` reads `window.__ts_bids` **synchronously** — no fetch, no Promise. `window.__ts_bids` is already on the page (injected before ``) when `__tsAdInit` runs (it executes post-DCL, after `` is received). Both `nurl` and `burl` fire client-side from `slotRenderEnded`; neither is fired server-side. - [ ] **Step 1: Write failing test** ```rust #[test] - fn head_inserts_includes_ts_ad_init_with_ts_bids_fetch() { + fn head_inserts_includes_ts_ad_init_with_synchronous_bids_read() { let config = test_config(); let integration = GptIntegration::new(config); let ctx = make_test_context(); let inserts = integration.head_inserts(&ctx); let combined = inserts.join(""); assert!(combined.contains("__tsAdInit"), "should define __tsAdInit"); - assert!(combined.contains("/ts-bids"), "should fetch from /ts-bids endpoint"); - assert!(combined.contains("__ts_request_id"), "should use __ts_request_id for rid"); - assert!(combined.contains("bidsPromise"), "should use bidsPromise pattern"); + assert!(combined.contains("window.__ts_bids"), "should read window.__ts_bids synchronously"); + assert!(combined.contains("ts_initial"), "should set ts_initial sentinel"); assert!(combined.contains("slotRenderEnded"), "should register slotRenderEnded"); - assert!(combined.contains("sendBeacon"), "should fire burl via sendBeacon"); - assert!(!combined.contains("__ts_bids"), "must NOT read window.__ts_bids — bids come from /ts-bids fetch"); + assert!(combined.contains("sendBeacon"), "should fire nurl and burl via sendBeacon"); + assert!(combined.contains("nurl"), "should fire nurl on confirmed render"); + assert!(!combined.contains("/ts-bids"), "must NOT fetch /ts-bids — bids are inline on the page"); + assert!(!combined.contains("bidsPromise"), "must NOT use bidsPromise — bids are synchronous"); + assert!(!combined.contains("__ts_request_id"), "must NOT reference request_id — no longer used"); } ``` Run: `cargo test -p trusted-server-core integrations::gpt` - Expected: FAIL — `__tsAdInit` not defined / assertion on `/ts-bids` string fails if old version present + Expected: FAIL - [ ] **Step 2: Replace `head_inserts()` in gpt.rs** @@ -1771,42 +1434,39 @@ The `/ts-bids` endpoint is the client's fetch target for bid results. It long-po "" .to_string(), - // __tsAdInit: fetches /ts-bids for bid targeting, then drives GPT. - // window.__ts_ad_slots and window.__ts_request_id are injected at head-open by TS. - // bidsPromise resolves concurrently with page rendering — never blocks FCP. + // __tsAdInit: reads window.__ts_bids synchronously (injected before ). + // No fetch, no Promise. Executes post-DCL when has already arrived. + // Both nurl and burl fire client-side from slotRenderEnded — never server-side. + // Note: window.__tsjs_installGptShim above is an EXISTING function in the + // tsjs-core bundle that stubs googletag.cmd before the real GPT loads. concat!( "" @@ -1825,18 +1485,18 @@ The `/ts-bids` endpoint is the client's fetch target for bid results. It long-po ```bash git add crates/trusted-server-core/src/integrations/gpt.rs - git commit -m "Emit __tsAdInit with /ts-bids fetch pattern from GPT head injector" + git commit -m "Emit __tsAdInit with synchronous window.__ts_bids read; nurl+burl from slotRenderEnded" ``` --- -## Task 12: `gpt/index.ts` — TypeScript `__tsAdInit` with `/ts-bids` fetch +## Task 10: `gpt/index.ts` — TypeScript `__tsAdInit` with slim-Prebid lazy loader **Files:** - Modify: `crates/js/lib/src/integrations/gpt/index.ts` -The TypeScript version mirrors the Rust inline string from Task 11. It uses the `bidsPromise` pattern — fetching `/ts-bids` concurrently with GPT slot definition. +The TypeScript version mirrors the Rust inline string from Task 9 and adds the lazy slim-Prebid loader. Slim-Prebid loads post-`window.load` and handles two things: refresh auctions (via existing GPT refresh triggers) and userID module warm-up to enrich the EC graph for the next request. - [ ] **Step 1: Write failing tests** @@ -1848,16 +1508,16 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the describe('installTsAdInit', () => { beforeEach(() => { delete (window as any).__ts_ad_slots - delete (window as any).__ts_request_id + delete (window as any).__ts_bids delete (window as any).__tsAdInit }) - it('fetches /ts-bids with request_id and applies bid targeting before refresh', async () => { + it('reads window.__ts_bids synchronously and applies bid targeting before refresh', async () => { const mockSlot = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), getSlotElementId: vi.fn().mockReturnValue('atf'), - getTargeting: vi.fn().mockReturnValue([]), + getTargeting: vi.fn().mockReturnValue(['abc']), } const mockPubads = { enableSingleRequest: vi.fn(), @@ -1879,71 +1539,94 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the targeting: { pos: 'atf' }, }, ] - ;(window as any).__ts_request_id = 'test-rid-123' - - const fetchSpy = vi.spyOn(global, 'fetch').mockResolvedValue({ - ok: true, - json: async () => ({ - atf: { - hb_pb: '1.00', - hb_bidder: 'kargo', - hb_adid: 'abc', - burl: 'https://ssp/bill', - }, - }), - } as Response) + ;(window as any).__ts_bids = { + atf: { + hb_pb: '1.00', + hb_bidder: 'kargo', + hb_adid: 'abc', + nurl: 'https://ssp/win', + burl: 'https://ssp/bill', + }, + } + + const fetchSpy = vi.spyOn(global, 'fetch') const { installTsAdInit } = await import('./index') installTsAdInit() - await (window as any).__tsAdInit() + ;(window as any).__tsAdInit() - expect(fetchSpy).toHaveBeenCalledWith( - expect.stringContaining('/ts-bids?rid=test-rid-123'), - expect.objectContaining({ credentials: 'omit' }) - ) + expect(fetchSpy).not.toHaveBeenCalled() expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_pb', '1.00') expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_bidder', 'kargo') + expect(mockSlot.setTargeting).toHaveBeenCalledWith('ts_initial', '1') expect(mockPubads.refresh).toHaveBeenCalled() fetchSpy.mockRestore() }) - it('calls refresh with empty bids when fetch fails', async () => { + it('fires both nurl and burl via sendBeacon on slotRenderEnded when our bid won', async () => { + const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true) + let capturedListener: ((e: any) => void) | undefined + + const mockSlot = { + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + getSlotElementId: vi.fn().mockReturnValue('atf'), + getTargeting: vi.fn().mockReturnValue(['abc']), + } const mockPubads = { enableSingleRequest: vi.fn(), - addEventListener: vi.fn(), refresh: vi.fn(), + addEventListener: vi.fn((event: string, fn: (e: any) => void) => { + if (event === 'slotRenderEnded') capturedListener = fn + }), } ;(window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, - defineSlot: vi.fn().mockReturnValue({ - addService: vi.fn().mockReturnThis(), - setTargeting: vi.fn().mockReturnThis(), - }), + defineSlot: vi.fn().mockReturnValue(mockSlot), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), } - ;(window as any).__ts_ad_slots = [] - ;(window as any).__ts_request_id = 'rid-fail' - - vi.spyOn(global, 'fetch').mockRejectedValue(new Error('network error')) + ;(window as any).__ts_ad_slots = [ + { + id: 'atf', + gam_unit_path: '/123/atf', + div_id: 'atf', + formats: [[300, 250]], + targeting: {}, + }, + ] + ;(window as any).__ts_bids = { + atf: { + hb_pb: '1.00', + hb_bidder: 'kargo', + hb_adid: 'abc', + nurl: 'https://ssp/win', + burl: 'https://ssp/bill', + }, + } const { installTsAdInit } = await import('./index') installTsAdInit() - await (window as any).__tsAdInit() + ;(window as any).__tsAdInit() - expect(mockPubads.refresh).toHaveBeenCalled() + expect(capturedListener).toBeDefined() + capturedListener!({ isEmpty: false, slot: mockSlot }) + + expect(beaconSpy).toHaveBeenCalledWith('https://ssp/win') + expect(beaconSpy).toHaveBeenCalledWith('https://ssp/bill') + beaconSpy.mockRestore() }) - it('fires burl via sendBeacon on slotRenderEnded when our bid won', async () => { + it('does not fire nurl/burl when bid did not win GAM line item', async () => { const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true) let capturedListener: ((e: any) => void) | undefined - const mockSlot = { + const mockSlotNoMatch = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), getSlotElementId: vi.fn().mockReturnValue('atf'), - getTargeting: vi.fn().mockReturnValue(['abc']), + getTargeting: vi.fn().mockReturnValue(['OTHER_BID_ID']), } const mockPubads = { enableSingleRequest: vi.fn(), @@ -1954,7 +1637,7 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the } ;(window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, - defineSlot: vi.fn().mockReturnValue(mockSlot), + defineSlot: vi.fn().mockReturnValue(mockSlotNoMatch), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), } @@ -1967,43 +1650,58 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the targeting: {}, }, ] - ;(window as any).__ts_request_id = 'rid-burl-test' - - vi.spyOn(global, 'fetch').mockResolvedValue({ - ok: true, - json: async () => ({ - atf: { - hb_pb: '1.00', - hb_bidder: 'kargo', - hb_adid: 'abc', - burl: 'https://ssp/bill', - }, - }), - } as Response) + ;(window as any).__ts_bids = { + atf: { + hb_pb: '1.00', + hb_bidder: 'kargo', + hb_adid: 'abc', + nurl: 'https://ssp/win', + burl: 'https://ssp/bill', + }, + } const { installTsAdInit } = await import('./index') installTsAdInit() - await (window as any).__tsAdInit() + ;(window as any).__tsAdInit() + capturedListener!({ isEmpty: false, slot: mockSlotNoMatch }) - // Trigger slotRenderEnded — slot has our winning hb_adid - expect(capturedListener).toBeDefined() - capturedListener!({ - isEmpty: false, - slot: mockSlot, - }) - - expect(beaconSpy).toHaveBeenCalledWith('https://ssp/bill') + expect(beaconSpy).not.toHaveBeenCalled() beaconSpy.mockRestore() }) + + it('calls refresh even when __ts_bids is empty (graceful fallback)', () => { + const mockPubads = { + enableSingleRequest: vi.fn(), + addEventListener: vi.fn(), + refresh: vi.fn(), + } + ;(window as any).googletag = { + cmd: { push: vi.fn((fn: () => void) => fn()) }, + defineSlot: vi.fn().mockReturnValue({ + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + }), + pubads: vi.fn().mockReturnValue(mockPubads), + enableServices: vi.fn(), + } + ;(window as any).__ts_ad_slots = [] + ;(window as any).__ts_bids = {} + + const { installTsAdInit } = require('./index') + installTsAdInit() + ;(window as any).__tsAdInit() + + expect(mockPubads.refresh).toHaveBeenCalled() + }) }) ``` Run: `cd crates/js/lib && npx vitest run` - Expected: FAIL — `installTsAdInit` not exported or fetches wrong endpoint + Expected: FAIL — `installTsAdInit` not defined or assertions fail -- [ ] **Step 2: Add `installTsAdInit` to `index.ts`** +- [ ] **Step 2: Implement `installTsAdInit` in `index.ts`** - Add to `crates/js/lib/src/integrations/gpt/index.ts`: + Replace the old `/ts-bids` fetch implementation with: ```typescript interface TsAdSlot { @@ -2018,38 +1716,30 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the hb_pb?: string hb_bidder?: string hb_adid?: string + nurl?: string burl?: string } type TsWindow = Window & { __ts_ad_slots?: TsAdSlot[] - __ts_request_id?: string + __ts_bids?: Record __tsAdInit?: () => void } /** * Install `window.__tsAdInit`. * - * Reads `window.__ts_ad_slots` and `window.__ts_request_id` (both injected by - * the edge at `` open). Fetches bid results from `/ts-bids?rid=` - * concurrently with GPT slot definition. Applies targeting and calls `refresh()` - * after the fetch resolves. Registers `slotRenderEnded` to fire `burl` via - * `sendBeacon` when our specific Prebid bid wins the GAM line item match. + * Reads `window.__ts_ad_slots` (injected at head-open) and `window.__ts_bids` + * (injected before ) synchronously — no fetch, no Promise. Applies bid + * targeting to GPT slots, sets the `ts_initial` sentinel, registers + * `slotRenderEnded` to fire both nurl and burl via sendBeacon when our + * specific Prebid bid wins the GAM line item match, then calls refresh(). */ export function installTsAdInit(): void { const w = window as TsWindow w.__tsAdInit = function () { const slots = w.__ts_ad_slots ?? [] - const rid = w.__ts_request_id - - const bidsPromise: Promise> = rid - ? fetch(`/ts-bids?rid=${encodeURIComponent(rid)}`, { - credentials: 'omit', - }) - .then((r) => (r.ok ? r.json() : {})) - .catch(() => ({})) - : Promise.resolve({}) - + const bids = w.__ts_bids ?? {} const g = (window as GptWindow).googletag if (!g) return @@ -2066,6 +1756,11 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the Object.entries(slot.targeting ?? {}).forEach(([k, v]) => gptSlot.setTargeting(k, v) ) + const bid = bids[slot.id] ?? {} + ;(['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { + if (bid[key]) gptSlot.setTargeting(key, bid[key]!) + }) + gptSlot.setTargeting('ts_initial', '1') return { id: slot.id, gptSlot } }) .filter(Boolean) as Array<{ @@ -2076,153 +1771,86 @@ The TypeScript version mirrors the Rust inline string from Task 11. It uses the g.pubads().enableSingleRequest() g.enableServices() - bidsPromise.then((bids) => { - gptSlots.forEach(({ id, gptSlot }) => { - const bid = bids[id] ?? {} - ;(['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { - if (bid[key]) gptSlot.setTargeting(key, bid[key]!) - }) - }) - - g.pubads().addEventListener?.('slotRenderEnded', (event: any) => { - const slotId: string = event.slot?.getSlotElementId?.() ?? '' - const bid = bids[slotId] ?? {} - if ( - !event.isEmpty && - bid.burl && - event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid - ) { - navigator.sendBeacon(bid.burl) - } - }) - - g.pubads().refresh() + g.pubads().addEventListener?.('slotRenderEnded', (event: any) => { + const slotId: string = event.slot?.getSlotElementId?.() ?? '' + const bid = bids[slotId] ?? {} + const ourBidWon = + !event.isEmpty && + bid.hb_adid && + event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid + if (ourBidWon) { + if (bid.nurl) navigator.sendBeacon(bid.nurl) + if (bid.burl) navigator.sendBeacon(bid.burl) + } }) + + g.pubads().refresh() }) } } ``` - Call `installTsAdInit()` from the integration's initialization path. +- [ ] **Step 3: Add lazy slim-Prebid loader (post-`window.load`)** -- [ ] **Step 3: Run JS tests** + After `installTsAdInit`, add: - Run: `cd crates/js/lib && npx vitest run` - Expected: new tests pass - -- [ ] **Step 4: Build JS bundle** - - Run: `cd crates/js/lib && node build-all.mjs` - Expected: clean build - -- [ ] **Step 5: Commit** - - ```bash - git add crates/js/lib/src/integrations/gpt/ - git commit -m "Add installTsAdInit with /ts-bids fetch pattern and slotRenderEnded burl firing" - ``` - ---- - -## Task 13: `nurl` fire-and-forget - -**Files:** - -- Modify: `crates/trusted-server-core/src/integrations/prebid.rs` -- Modify: `crates/trusted-server-core/src/publisher.rs` - -- [ ] **Step 1: Write failing test** - - ```rust - #[test] - fn prebid_config_fire_nurl_defaults_to_true() { - let config = PrebidConfig::default(); - assert!(config.fire_nurl_at_edge, "should fire nurl at edge by default"); + ```typescript + /** + * Register the slim-Prebid lazy loader. Fires after window.load — off the + * critical path. slim-Prebid handles refresh auctions and userID module + * warm-up (ID5, sharedID, LiveRamp ATS, Lockr). It skips initial-render slots + * (ts_initial=1) and registers as the GPT refresh handler for scroll/sticky auctions. + * + * Phase 1: no-op unless window.__tsjs_slim_prebid_url is set (it won't be until + * the slim-Prebid bundle build target ships in a later phase). + */ + export function installSlimPrebidLoader(): void { + const url = (window as any).__tsjs_slim_prebid_url as string | undefined + if (!url) return + window.addEventListener('load', () => { + const script = document.createElement('script') + script.src = url + script.defer = true + document.head.appendChild(script) + }) } ``` - Run: `cargo test -p trusted-server-core integrations::prebid` - Expected: FAIL - -- [ ] **Step 2: Add `fire_nurl_at_edge` to `PrebidConfig`** + Call `installTsAdInit()` from the integration's existing initialization path — wherever the module's init function runs at page load (look for the existing `init()` or module-level call that sets up the GPT integration). Add: - ```rust - #[serde(default = "default_fire_nurl_at_edge")] - pub fire_nurl_at_edge: bool, - ``` - - ```rust - fn default_fire_nurl_at_edge() -> bool { true } - ``` - -- [ ] **Step 3: Fire nurls in publisher.rs after bid_cache.put()** - - After the `bid_cache.put(...)` call (Task 9 Step 3), add: - - ```rust - if let Some(ref result) = auction_result { - fire_winning_nurls(result, settings); - } + ```typescript + // In the integration's init / module entry point: + installTsAdInit() ``` - Add helper: - - ```rust - fn fire_winning_nurls( - result: &crate::auction::orchestrator::OrchestrationResult, - settings: &Settings, - ) { - use crate::backend::BackendConfig; - - let fire_nurl = settings - .integrations - .get_typed::("prebid") - .map(|c| c.fire_nurl_at_edge) - .unwrap_or(true); + `window.__tsAdInit()` itself is called by `__tsAdInit` being invoked from the `"); @@ -2289,7 +1914,7 @@ Tests use `pub(crate)` helpers from Task 9 directly. } #[test] - fn bid_map_uses_price_bucket_and_ad_id() { + fn bid_map_includes_nurl_and_burl() { let mut winning_bids = HashMap::new(); winning_bids.insert("atf_sidebar_ad".to_string(), Bid { slot_id: "atf_sidebar_ad".to_string(), @@ -2298,46 +1923,60 @@ Tests use `pub(crate)` helpers from Task 9 directly. creative: None, adomain: None, bidder: "kargo".to_string(), - width: 300, height: 250, + width: 300, + height: 250, + nurl: Some("https://ssp/win".to_string()), + burl: Some("https://ssp/bill".to_string()), + ad_id: Some("abc123".to_string()), + metadata: Default::default(), + }); + let map = build_bid_map(&winning_bids, PriceGranularity::Dense); + let entry = map.get("atf_sidebar_ad").expect("should have bid entry"); + assert_eq!(entry.get("hb_pb").and_then(|v| v.as_str()), Some("2.50")); + assert_eq!(entry.get("hb_bidder").and_then(|v| v.as_str()), Some("kargo")); + assert_eq!(entry.get("hb_adid").and_then(|v| v.as_str()), Some("abc123")); + assert_eq!(entry.get("nurl").and_then(|v| v.as_str()), Some("https://ssp/win")); + assert_eq!(entry.get("burl").and_then(|v| v.as_str()), Some("https://ssp/bill")); + } + + #[test] + fn bid_map_excludes_slot_when_price_is_none() { + let mut winning_bids = HashMap::new(); + winning_bids.insert("no-price-slot".to_string(), Bid { + slot_id: "no-price-slot".to_string(), + price: None, + currency: "USD".to_string(), + creative: None, + adomain: None, + bidder: "kargo".to_string(), + width: 300, + height: 250, nurl: None, - burl: Some("https://ssp.example/billing?id=abc123".to_string()), - ad_id: Some("prebid-uuid-abc123".to_string()), - metadata: HashMap::new(), + burl: None, + ad_id: None, + metadata: Default::default(), }); - let bid_map = build_bid_map(&winning_bids, PriceGranularity::Dense); - let slot_bids = bid_map.get("atf_sidebar_ad").expect("should have slot bids"); - assert_eq!( - slot_bids.get("hb_pb").and_then(|v| v.as_str()), - Some("2.53"), - "should bucket 2.53 as 2.53 (dense)" - ); - assert_eq!( - slot_bids.get("hb_bidder").and_then(|v| v.as_str()), - Some("kargo"), - "should include bidder" - ); - assert_eq!( - slot_bids.get("hb_adid").and_then(|v| v.as_str()), - Some("prebid-uuid-abc123"), - "should use ad_id not creative markup" - ); + let map = build_bid_map(&winning_bids, PriceGranularity::Dense); + assert!(map.is_empty(), "slot with no price should be excluded from bid map"); } #[test] - fn html_escape_neutralizes_xss_in_json() { - let malicious = r#"{"zone":""), "should escape "); - assert!(escaped.contains("\\u003c"), "should unicode-escape <"); - assert!(escaped.contains("\\u003e"), "should unicode-escape >"); + fn bids_script_is_xss_safe() { + let mut map = serde_json::Map::new(); + map.insert("atf".to_string(), serde_json::json!({"hb_pb": "1.00"})); + let script = build_bids_script(&map); + let inner = script + .trim_start_matches(""); + assert!(!inner.contains('<'), "no unescaped < in bids script"); + assert!(!inner.contains('>'), "no unescaped > in bids script"); } #[test] - fn url_matching_end_to_end() { - let file = CreativeOpportunitiesFile { slots: vec![make_slot()] }; - assert_eq!(match_slots(&file.slots, "/2024/01/my-article").len(), 1, "should match article"); - assert_eq!(match_slots(&file.slots, "/about").len(), 0, "should not match /about"); - assert_eq!(match_slots(&file.slots, "/").len(), 0, "should not match root"); + fn html_escape_encodes_special_chars() { + assert_eq!(html_escape_for_script("`. + /// Injected at `` open. `None` when no slots matched. + pub ad_slots_script: Option, + /// Shared auction result — written by auction task before HTML processing begins. + /// Handler reads this in `el.on_end_tag()` on the body element. + /// `None` means no auction ran; inject empty `__ts_bids = {}` as fallback. + pub ad_bids_state: std::sync::Arc>>, } impl HtmlProcessorConfig { @@ -151,6 +158,8 @@ impl HtmlProcessorConfig { request_host: request_host.to_string(), request_scheme: request_scheme.to_string(), integrations: integrations.clone(), + ad_slots_script: None, + ad_bids_state: std::sync::Arc::new(std::sync::RwLock::new(None)), } } } @@ -230,6 +239,8 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso let injected_tsjs = Rc::new(Cell::new(false)); let integration_registry = config.integrations.clone(); let script_rewriters = integration_registry.script_rewriters(); + let ad_slots_script = config.ad_slots_script.clone(); + let ad_bids_state = config.ad_bids_state.clone(); let mut element_content_handlers = vec![ // Inject unified tsjs bundle once at the start of @@ -238,9 +249,14 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso let integrations = integration_registry.clone(); let patterns = patterns.clone(); let document_state = document_state.clone(); + let ad_slots_script = ad_slots_script.clone(); move |el| { if !injected_tsjs.get() { let mut snippet = String::new(); + // Inject ad slots script first so it appears before tsjs bundle. + if let Some(ref slots_script) = ad_slots_script { + snippet.push_str(slots_script); + } let ctx = IntegrationHtmlContext { request_host: &patterns.request_host, request_scheme: &patterns.request_scheme, @@ -265,6 +281,30 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso Ok(()) } }), + // Inject __ts_bids before via end_tag_handlers. + element!("body", { + let state = ad_bids_state.clone(); + move |el| { + let state = state.clone(); + if let Some(handlers) = el.end_tag_handlers() { + let handler: EndTagHandler<'static> = + Box::new(move |end_tag: &mut EndTag<'_>| { + let script_guard = state.read().expect("should read bid state"); + let bids_script = match &*script_guard { + Some(s) => s.clone(), + None => { + r#""# + .to_string() + } + }; + end_tag.before(&bids_script, ContentType::Html); + Ok(()) + }); + handlers.push(handler); + } + Ok(()) + } + }), // Replace URLs in href attributes element!("[href]", { let patterns = patterns.clone(); @@ -540,6 +580,8 @@ mod tests { request_host: "test.example.com".to_string(), request_scheme: "https".to_string(), integrations: IntegrationRegistry::default(), + ad_slots_script: None, + ad_bids_state: std::sync::Arc::new(std::sync::RwLock::new(None)), } } @@ -1185,4 +1227,85 @@ mod tests { "should contain post-processor mutation" ); } + + #[test] + fn injects_ad_slots_at_head_open() { + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: Some( + r#""#.to_string(), + ), + ad_bids_state: std::sync::Arc::new(std::sync::RwLock::new(None)), + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk( + b"Tcontent", + true, + ) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!( + html.contains("window.__ts_ad_slots"), + "should inject ad slots at head-open" + ); + assert!( + !html.contains("__ts_request_id"), + "must NOT inject request_id" + ); + } + + #[test] + fn injects_ts_bids_before_body_close() { + let bids_script = + r#""#; + let state = std::sync::Arc::new(std::sync::RwLock::new(Some(bids_script.to_string()))); + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: None, + ad_bids_state: state, + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk(b"content", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!( + html.contains("window.__ts_bids"), + "should inject bids before " + ); + let bids_pos = html + .find("window.__ts_bids") + .expect("bids should be in output"); + let body_close_pos = html.find("").expect(" should be in output"); + assert!(bids_pos < body_close_pos, "bids must appear before "); + } + + #[test] + fn injects_empty_ts_bids_when_state_is_none() { + let state = std::sync::Arc::new(std::sync::RwLock::new(None)); + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: None, + ad_bids_state: state, + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk(b"content", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!( + html.contains("__ts_bids=JSON.parse(\"{}\")"), + "should inject empty bids on None state" + ); + } } diff --git a/crates/trusted-server-core/src/integrations/registry.rs b/crates/trusted-server-core/src/integrations/registry.rs index 8b55493b..ffad7892 100644 --- a/crates/trusted-server-core/src/integrations/registry.rs +++ b/crates/trusted-server-core/src/integrations/registry.rs @@ -853,6 +853,14 @@ impl IntegrationRegistry { .collect() } + #[cfg(test)] + #[must_use] + pub fn empty_for_tests() -> Self { + Self { + inner: Arc::new(IntegrationRegistryInner::default()), + } + } + #[cfg(test)] #[must_use] pub fn from_rewriters( From 8b9500cf7c5d83d4d7bf97910ddb414651ec704d Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Tue, 5 May 2026 19:47:16 +0530 Subject: [PATCH 16/67] Convert handle_publisher_request to async; body-inject __ts_bids; eligibility gates; max-age=0 - Make handle_publisher_request async; add orchestrator and slots_file params - Dispatch origin request with send_async before running auction in parallel - Gate auction on GET, no prefetch, no bot, matched slots, TCF purpose-1 consent - Run server-side auction and write bucketed bids to ad_bids_state Arc - Compute ad_slots_script after response headers; set Cache-Control: private, max-age=0 - Fix Stream arm to thread actual ad_slots_script and ad_bids_state through - Add build_auction_request, build_bid_map, build_bids_script, build_ad_slots_script helpers - Update route_tests.rs to pass empty slots_file to route_request --- .../src/route_tests.rs | 6 + crates/trusted-server-core/src/publisher.rs | 249 +++++++++++++++++- 2 files changed, 243 insertions(+), 12 deletions(-) diff --git a/crates/trusted-server-adapter-fastly/src/route_tests.rs b/crates/trusted-server-adapter-fastly/src/route_tests.rs index 0fd0113f..06336a9b 100644 --- a/crates/trusted-server-adapter-fastly/src/route_tests.rs +++ b/crates/trusted-server-adapter-fastly/src/route_tests.rs @@ -184,6 +184,8 @@ fn configured_missing_consent_store_only_breaks_consent_routes() { let orchestrator = build_orchestrator(&settings).expect("should build auction orchestrator"); let integration_registry = IntegrationRegistry::new(&settings).expect("should create integration registry"); + let slots_file = + trusted_server_core::creative_opportunities::CreativeOpportunitiesFile::default(); let discovery_req = Request::get("https://test.com/.well-known/trusted-server.json"); let discovery_services = test_runtime_services(&discovery_req); @@ -192,6 +194,7 @@ fn configured_missing_consent_store_only_breaks_consent_routes() { &orchestrator, &integration_registry, &discovery_services, + &slots_file, discovery_req, )) .expect("should route discovery request"); @@ -208,6 +211,7 @@ fn configured_missing_consent_store_only_breaks_consent_routes() { &orchestrator, &integration_registry, &admin_services, + &slots_file, admin_req, )) .expect("should route admin request"); @@ -224,6 +228,7 @@ fn configured_missing_consent_store_only_breaks_consent_routes() { &orchestrator, &integration_registry, &auction_services, + &slots_file, auction_req, )) .expect("should return an error response for auction requests"); @@ -240,6 +245,7 @@ fn configured_missing_consent_store_only_breaks_consent_routes() { &orchestrator, &integration_registry, &publisher_services, + &slots_file, publisher_req, )) .expect("should return an error response for publisher fallback"); diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 5bcef694..4037bdf8 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -12,11 +12,14 @@ //! content-rewriting concern. use std::io::Write; +use std::sync::{Arc, RwLock}; use error_stack::{Report, ResultExt}; use fastly::http::{header, StatusCode}; use fastly::{Body, Request, Response}; +use crate::auction::orchestrator::AuctionOrchestrator; +use crate::auction::types::{AuctionContext, AuctionRequest, Bid, PublisherInfo, SiteInfo, UserInfo}; use crate::backend::BackendConfig; use crate::consent::{allows_ec_creation, build_consent_context, ConsentPipelineInput}; use crate::constants::{COOKIE_TS_EC, HEADER_X_COMPRESS_HINT, HEADER_X_TS_EC}; @@ -26,6 +29,7 @@ use crate::error::TrustedServerError; use crate::http_util::{serve_static_with_etag, RequestInfo}; use crate::integrations::IntegrationRegistry; use crate::platform::RuntimeServices; +use crate::price_bucket::price_bucket; use crate::rsc_flight::RscFlightUrlRewriter; use crate::settings::Settings; use crate::streaming_processor::{Compression, PipelineConfig, StreamProcessor, StreamingPipeline}; @@ -182,6 +186,8 @@ struct ProcessResponseParams<'a> { settings: &'a Settings, content_type: &'a str, integration_registry: &'a IntegrationRegistry, + ad_slots_script: Option<&'a str>, + ad_bids_state: &'a Arc>>, } /// Process response body through the streaming pipeline. @@ -224,6 +230,8 @@ fn process_response_streaming( params.request_scheme, params.settings, params.integration_registry, + params.ad_slots_script.map(str::to_string), + params.ad_bids_state.clone(), )?; StreamingPipeline::new(config, processor).process(body, output)?; } else if is_rsc_flight { @@ -252,18 +260,21 @@ fn create_html_stream_processor( origin_host: &str, request_host: &str, request_scheme: &str, - settings: &Settings, + _settings: &Settings, integration_registry: &IntegrationRegistry, + ad_slots_script: Option, + ad_bids_state: Arc>>, ) -> Result> { use crate::html_processor::{create_html_processor, HtmlProcessorConfig}; - let config = HtmlProcessorConfig::from_settings( - settings, - integration_registry, - origin_host, - request_host, - request_scheme, - ); + let config = HtmlProcessorConfig { + origin_host: origin_host.to_string(), + request_host: request_host.to_string(), + request_scheme: request_scheme.to_string(), + integrations: integration_registry.clone(), + ad_slots_script, + ad_bids_state, + }; Ok(create_html_processor(config)) } @@ -392,6 +403,8 @@ pub struct OwnedProcessResponseParams { pub(crate) request_host: String, pub(crate) request_scheme: String, pub(crate) content_type: String, + pub(crate) ad_slots_script: Option, + pub(crate) ad_bids_state: Arc>>, } /// Stream the publisher response body through the processing pipeline. @@ -420,6 +433,8 @@ pub fn stream_publisher_body( settings, content_type: ¶ms.content_type, integration_registry, + ad_slots_script: params.ad_slots_script.as_deref(), + ad_bids_state: ¶ms.ad_bids_state, }; process_response_streaming(body, output, &borrowed) } @@ -441,10 +456,12 @@ pub fn stream_publisher_body( /// /// Returns a [`TrustedServerError`] if the proxy request fails or the /// origin backend is unreachable. -pub fn handle_publisher_request( +pub async fn handle_publisher_request( settings: &Settings, integration_registry: &IntegrationRegistry, services: &RuntimeServices, + orchestrator: &AuctionOrchestrator, + slots_file: &crate::creative_opportunities::CreativeOpportunitiesFile, mut req: Request, ) -> Result> { log::debug!("Proxying request to publisher_origin"); @@ -520,14 +537,105 @@ pub fn handle_publisher_request( backend_name, settings.publisher.origin_url ); + + let request_path = req.get_path().to_string(); + let is_get = req.get_method() == fastly::http::Method::GET; + + let is_prefetch = req.get_header_str("sec-purpose") + .map_or(false, |v| v.contains("prefetch")) + || req.get_header_str("purpose") + .map_or(false, |v| v.contains("prefetch")); + + let user_agent = req.get_header_str("user-agent").unwrap_or(""); + let is_bot = ["Googlebot", "Bingbot", "AhrefsBot", "SemrushBot", "DotBot"] + .iter() + .any(|bot| user_agent.contains(bot)); + + let matched_slots: Vec<_> = if settings.creative_opportunities.is_some() && is_get { + crate::creative_opportunities::match_slots(&slots_file.slots, &request_path) + .into_iter() + .cloned() + .collect() + } else { + Vec::new() + }; + + let consent_allows_auction = consent_context + .tcf + .as_ref() + .map_or(false, |tcf| tcf.has_purpose_consent(1)); + + let should_run_auction = is_get + && !is_prefetch + && !is_bot + && !matched_slots.is_empty() + && consent_allows_auction; + + let auction_timeout_ms = settings + .creative_opportunities + .as_ref() + .and_then(|co| co.auction_timeout_ms) + .unwrap_or(settings.auction.timeout_ms); + + let ad_bids_state: Arc>> = Arc::new(RwLock::new(None)); + // Only advertise encodings the rewrite pipeline can decode and re-encode. restrict_accept_encoding(&mut req); req.set_header("host", &origin_host); - let mut response = req - .send(&backend_name) + let pending_origin = req + .send_async(&backend_name) .change_context(TrustedServerError::Proxy { - message: "Failed to proxy request to origin".to_string(), + message: "Failed to dispatch async origin request".to_string(), + })?; + + let auction_result = if should_run_auction { + let co_config = settings.creative_opportunities.as_ref() + .expect("should be present when should_run_auction is true"); + let auction_request = build_auction_request( + &matched_slots, + &ec_id, + &consent_context, + &request_info, + co_config, + ); + let placeholder_req = fastly::Request::get("https://placeholder.invalid/"); + let auction_context = AuctionContext { + settings, + request: &placeholder_req, + client_info: services.client_info(), + timeout_ms: auction_timeout_ms, + provider_responses: None, + services, + }; + match orchestrator.run_auction(&auction_request, &auction_context, services).await { + Ok(result) => Some(result), + Err(e) => { + log::warn!("server-side auction failed, proceeding without bids: {e:?}"); + None + } + } + } else { + None + }; + + if should_run_auction { + let co_config = settings.creative_opportunities.as_ref() + .expect("should be present"); + let empty: std::collections::HashMap = + std::collections::HashMap::new(); + let winning_bids = auction_result.as_ref() + .map(|r| &r.winning_bids) + .unwrap_or(&empty); + let bid_map = build_bid_map(winning_bids, co_config.price_granularity); + let bids_script = build_bids_script(&bid_map); + *ad_bids_state.write().expect("should write bid state") = Some(bids_script); + } + + let mut response = pending_origin + .wait() + .change_context(TrustedServerError::Proxy { + message: "Failed to await origin response".to_string(), })?; log::debug!("Response headers:"); @@ -535,6 +643,22 @@ pub fn handle_publisher_request( log::debug!(" {}: {:?}", name, value); } + let ad_slots_script = if let Some(co_config) = &settings.creative_opportunities { + if !matched_slots.is_empty() { + Some(build_ad_slots_script(&matched_slots, co_config)) + } else { + None + } + } else { + None + }; + + if ad_slots_script.is_some() { + response.set_header(header::CACHE_CONTROL, "private, max-age=0"); + response.remove_header("surrogate-control"); + response.remove_header("fastly-surrogate-control"); + } + // Set EC ID / cookie headers BEFORE body processing. // These are body-independent (computed from request cookies + consent). apply_ec_headers( @@ -623,6 +747,8 @@ pub fn handle_publisher_request( request_host: request_host.to_string(), request_scheme: request_scheme.to_string(), content_type, + ad_slots_script: ad_slots_script.clone(), + ad_bids_state: ad_bids_state.clone(), }, }) } @@ -642,6 +768,8 @@ pub fn handle_publisher_request( settings, content_type: &content_type, integration_registry, + ad_slots_script: ad_slots_script.as_deref(), + ad_bids_state: &ad_bids_state, }; let mut output = Vec::new(); process_response_streaming(body, &mut output, ¶ms)?; @@ -654,6 +782,93 @@ pub fn handle_publisher_request( } } +/// Build an [`AuctionRequest`] from matched creative opportunity slots. +pub(crate) fn build_auction_request( + matched_slots: &[crate::creative_opportunities::CreativeOpportunitySlot], + ec_id: &str, + consent_context: &crate::consent::ConsentContext, + request_info: &crate::http_util::RequestInfo, + co_config: &crate::creative_opportunities::CreativeOpportunitiesConfig, +) -> AuctionRequest { + let slots = matched_slots + .iter() + .map(|s| s.to_ad_slot(&co_config.gam_network_id)) + .collect(); + AuctionRequest { + id: format!("ts-{}", ec_id), + slots, + publisher: PublisherInfo { + domain: request_info.host.clone(), + page_url: None, + }, + user: UserInfo { + id: ec_id.to_string(), + fresh_id: ec_id.to_string(), + consent: Some(consent_context.clone()), + }, + device: None, + site: Some(SiteInfo { + domain: request_info.host.clone(), + page: String::new(), + }), + context: std::collections::HashMap::new(), + } +} + +/// Build a price-bucketed bid map from winning bids. +/// +/// Returns a map of slot ID → bucketed CPM string. +pub(crate) fn build_bid_map( + winning_bids: &std::collections::HashMap, + granularity: crate::price_bucket::PriceGranularity, +) -> std::collections::HashMap { + winning_bids + .iter() + .filter_map(|(slot_id, bid)| { + bid.price.map(|cpm| { + let bucket = price_bucket(cpm, granularity); + (slot_id.clone(), bucket) + }) + }) + .collect() +} + +/// Build the `__ts_bids` inline script content from a bucketed bid map. +pub(crate) fn build_bids_script(bid_map: &std::collections::HashMap) -> String { + let entries: Vec = bid_map + .iter() + .map(|(slot_id, bucket)| format!("\"{}\":\"{}\"", slot_id, bucket)) + .collect(); + format!("window.__ts_bids={{{}}};", entries.join(",")) +} + +/// Build the `__ts_ad_slots` inline script content from matched slots. +pub(crate) fn build_ad_slots_script( + matched_slots: &[crate::creative_opportunities::CreativeOpportunitySlot], + co_config: &crate::creative_opportunities::CreativeOpportunitiesConfig, +) -> String { + let entries: Vec = matched_slots + .iter() + .map(|slot| { + let gam_path = slot.resolved_gam_unit_path(&co_config.gam_network_id); + let div_id = slot.resolved_div_id(); + let formats: Vec = slot + .formats + .iter() + .map(|f| format!("[{},{}]", f.width, f.height)) + .collect(); + format!( + "{{\"id\":\"{}\",\"div\":\"{}\",\"path\":\"{}\",\"sizes\":[{}]}}", + slot.id, + div_id, + gam_path, + formats.join(",") + ) + }) + .collect(); + format!("window.__ts_ad_slots=[{}];", entries.join(",")) +} + /// Whether the content type requires processing (URL rewriting, HTML injection). /// /// Text-based and JavaScript/JSON responses are processable; binary types @@ -1366,6 +1581,8 @@ mod tests { request_host: "proxy.example.com".to_string(), request_scheme: "https".to_string(), content_type: "text/css".to_string(), + ad_slots_script: None, + ad_bids_state: Arc::new(RwLock::new(None)), }; let mut output = Vec::new(); @@ -1407,6 +1624,8 @@ mod tests { request_host: "proxy.example.com".to_string(), request_scheme: "https".to_string(), content_type: "text/html; charset=utf-8".to_string(), + ad_slots_script: None, + ad_bids_state: Arc::new(RwLock::new(None)), }; let mut output = Vec::new(); @@ -1439,6 +1658,8 @@ mod tests { request_host: "proxy.example.com".to_string(), request_scheme: "https".to_string(), content_type: "text/html".to_string(), + ad_slots_script: None, + ad_bids_state: Arc::new(RwLock::new(None)), }; let bogus_body = Body::from(b"not gzip".to_vec()); @@ -1538,6 +1759,8 @@ mod tests { request_host: "proxy.example.com".to_string(), request_scheme: "https".to_string(), content_type: "text/html; charset=utf-8".to_string(), + ad_slots_script: None, + ad_bids_state: Arc::new(RwLock::new(None)), }; let mut output = Vec::new(); stream_publisher_body(body, &mut output, ¶ms, &settings, ®istry) @@ -1588,6 +1811,8 @@ mod tests { request_host: "proxy.example.com".to_string(), request_scheme: "https".to_string(), content_type: "text/html".to_string(), + ad_slots_script: None, + ad_bids_state: Arc::new(RwLock::new(None)), }; let mut output = Vec::new(); From 9cdbb36fd7bb62212258a433c2764f07eb8f7e54 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Tue, 5 May 2026 20:05:59 +0530 Subject: [PATCH 17/67] Emit __tsAdInit with synchronous window.__ts_bids read; nurl+burl from slotRenderEnded --- .../src/integrations/gpt.rs | 89 +++++++++++++++++-- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/crates/trusted-server-core/src/integrations/gpt.rs b/crates/trusted-server-core/src/integrations/gpt.rs index 40bcf7f2..796d633e 100644 --- a/crates/trusted-server-core/src/integrations/gpt.rs +++ b/crates/trusted-server-core/src/integrations/gpt.rs @@ -438,13 +438,42 @@ impl IntegrationHeadInjector for GptIntegration { } fn head_inserts(&self, _ctx: &IntegrationHtmlContext<'_>) -> Vec { - // Set the enable flag and best-effort call the activation function - // registered by the GPT shim module. The bundle also auto-installs - // when it sees the pre-set flag, so this works regardless of whether - // the inline bootstrap runs before or after the TSJS bundle. vec![ - "" + "" .to_string(), + concat!( + "" + ).to_string(), ] } } @@ -1020,7 +1049,7 @@ mod tests { let inserts = integration.head_inserts(&ctx); - assert_eq!(inserts.len(), 1, "should emit exactly one head insert"); + assert_eq!(inserts.len(), 2, "should emit exactly two head inserts"); assert_eq!( inserts[0], "", @@ -1028,6 +1057,54 @@ mod tests { ); } + #[test] + fn head_inserts_includes_ts_ad_init_with_synchronous_bids_read() { + let config = test_config(); + let integration = GptIntegration::new(config); + let doc_state = IntegrationDocumentState::default(); + let ctx = IntegrationHtmlContext { + request_host: "edge.example.com", + request_scheme: "https", + origin_host: "example.com", + document_state: &doc_state, + }; + let inserts = integration.head_inserts(&ctx); + let combined = inserts.join(""); + assert!(combined.contains("__tsAdInit"), "should define __tsAdInit"); + assert!( + combined.contains("window.__ts_bids"), + "should read window.__ts_bids synchronously" + ); + assert!( + combined.contains("ts_initial"), + "should set ts_initial sentinel" + ); + assert!( + combined.contains("slotRenderEnded"), + "should register slotRenderEnded" + ); + assert!( + combined.contains("sendBeacon"), + "should fire nurl and burl via sendBeacon" + ); + assert!( + combined.contains("nurl"), + "should fire nurl on confirmed render" + ); + assert!( + !combined.contains("/ts-bids"), + "must NOT fetch /ts-bids — bids are inline on the page" + ); + assert!( + !combined.contains("bidsPromise"), + "must NOT use bidsPromise — bids are synchronous" + ); + assert!( + !combined.contains("__ts_request_id"), + "must NOT reference request_id — no longer used" + ); + } + #[test] fn head_injector_integration_id() { let integration = GptIntegration::new(test_config()); From 6b624e3c9262cdc06b163eec4b7e18d024acdb3e Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 09:32:32 +0530 Subject: [PATCH 18/67] Fix bid map shape and ad slots property names; resolve clippy errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - build_bid_map now returns serde_json::Map with full bid objects (hb_pb, hb_bidder, hb_adid, nurl, burl) instead of a plain CPM string map - build_bids_script / build_ad_slots_script now emit full "# - .to_string() - } + None => r#""# + .to_string(), }; end_tag.before(&bids_script, ContentType::Html); Ok(()) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 4037bdf8..c614e5ed 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -19,7 +19,9 @@ use fastly::http::{header, StatusCode}; use fastly::{Body, Request, Response}; use crate::auction::orchestrator::AuctionOrchestrator; -use crate::auction::types::{AuctionContext, AuctionRequest, Bid, PublisherInfo, SiteInfo, UserInfo}; +use crate::auction::types::{ + AuctionContext, AuctionRequest, Bid, PublisherInfo, SiteInfo, UserInfo, +}; use crate::backend::BackendConfig; use crate::consent::{allows_ec_creation, build_consent_context, ConsentPipelineInput}; use crate::constants::{COOKIE_TS_EC, HEADER_X_COMPRESS_HINT, HEADER_X_TS_EC}; @@ -456,6 +458,12 @@ pub fn stream_publisher_body( /// /// Returns a [`TrustedServerError`] if the proxy request fails or the /// origin backend is unreachable. +/// +/// # Panics +/// +/// Panics if `should_run_auction` is `true` but `settings.creative_opportunities` is `None`. +/// This is a logic invariant: `should_run_auction` is only set when creative opportunities +/// are configured, so this state is unreachable in practice. pub async fn handle_publisher_request( settings: &Settings, integration_registry: &IntegrationRegistry, @@ -541,10 +549,12 @@ pub async fn handle_publisher_request( let request_path = req.get_path().to_string(); let is_get = req.get_method() == fastly::http::Method::GET; - let is_prefetch = req.get_header_str("sec-purpose") - .map_or(false, |v| v.contains("prefetch")) - || req.get_header_str("purpose") - .map_or(false, |v| v.contains("prefetch")); + let is_prefetch = req + .get_header_str("sec-purpose") + .is_some_and(|v| v.contains("prefetch")) + || req + .get_header_str("purpose") + .is_some_and(|v| v.contains("prefetch")); let user_agent = req.get_header_str("user-agent").unwrap_or(""); let is_bot = ["Googlebot", "Bingbot", "AhrefsBot", "SemrushBot", "DotBot"] @@ -563,13 +573,10 @@ pub async fn handle_publisher_request( let consent_allows_auction = consent_context .tcf .as_ref() - .map_or(false, |tcf| tcf.has_purpose_consent(1)); + .is_some_and(|tcf| tcf.has_purpose_consent(1)); - let should_run_auction = is_get - && !is_prefetch - && !is_bot - && !matched_slots.is_empty() - && consent_allows_auction; + let should_run_auction = + is_get && !is_prefetch && !is_bot && !matched_slots.is_empty() && consent_allows_auction; let auction_timeout_ms = settings .creative_opportunities @@ -583,14 +590,16 @@ pub async fn handle_publisher_request( restrict_accept_encoding(&mut req); req.set_header("host", &origin_host); - let pending_origin = req - .send_async(&backend_name) - .change_context(TrustedServerError::Proxy { - message: "Failed to dispatch async origin request".to_string(), - })?; + let pending_origin = + req.send_async(&backend_name) + .change_context(TrustedServerError::Proxy { + message: "Failed to dispatch async origin request".to_string(), + })?; let auction_result = if should_run_auction { - let co_config = settings.creative_opportunities.as_ref() + let co_config = settings + .creative_opportunities + .as_ref() .expect("should be present when should_run_auction is true"); let auction_request = build_auction_request( &matched_slots, @@ -608,7 +617,10 @@ pub async fn handle_publisher_request( provider_responses: None, services, }; - match orchestrator.run_auction(&auction_request, &auction_context, services).await { + match orchestrator + .run_auction(&auction_request, &auction_context, services) + .await + { Ok(result) => Some(result), Err(e) => { log::warn!("server-side auction failed, proceeding without bids: {e:?}"); @@ -620,11 +632,13 @@ pub async fn handle_publisher_request( }; if should_run_auction { - let co_config = settings.creative_opportunities.as_ref() + let co_config = settings + .creative_opportunities + .as_ref() .expect("should be present"); - let empty: std::collections::HashMap = - std::collections::HashMap::new(); - let winning_bids = auction_result.as_ref() + let empty: std::collections::HashMap = std::collections::HashMap::new(); + let winning_bids = auction_result + .as_ref() .map(|r| &r.winning_bids) .unwrap_or(&empty); let bid_map = build_bid_map(winning_bids, co_config.price_granularity); @@ -815,58 +829,103 @@ pub(crate) fn build_auction_request( } } +/// Escape a JSON string so it is safe to embed inside a JS double-quoted string literal. +/// +/// Backslashes are doubled first (so they survive the next pass), then +/// double-quotes are escaped so they do not terminate the JS string. +/// The result is always valid to write as `JSON.parse("…")`. +fn html_escape_for_script(s: &str) -> String { + s.replace('\\', "\\\\").replace('"', "\\\"") +} + /// Build a price-bucketed bid map from winning bids. /// -/// Returns a map of slot ID → bucketed CPM string. +/// Returns a JSON object map of slot ID → bid metadata including the bucketed +/// CPM (`hb_pb`), bidder (`hb_bidder`), and optional ad ID, nurl, and burl. pub(crate) fn build_bid_map( winning_bids: &std::collections::HashMap, granularity: crate::price_bucket::PriceGranularity, -) -> std::collections::HashMap { +) -> serde_json::Map { winning_bids .iter() .filter_map(|(slot_id, bid)| { bid.price.map(|cpm| { let bucket = price_bucket(cpm, granularity); - (slot_id.clone(), bucket) + let mut obj = serde_json::Map::new(); + obj.insert("hb_pb".to_string(), serde_json::Value::String(bucket)); + obj.insert( + "hb_bidder".to_string(), + serde_json::Value::String(bid.bidder.clone()), + ); + if let Some(ref ad_id) = bid.ad_id { + obj.insert( + "hb_adid".to_string(), + serde_json::Value::String(ad_id.clone()), + ); + } + if let Some(ref nurl) = bid.nurl { + obj.insert("nurl".to_string(), serde_json::Value::String(nurl.clone())); + } + if let Some(ref burl) = bid.burl { + obj.insert("burl".to_string(), serde_json::Value::String(burl.clone())); + } + (slot_id.clone(), serde_json::Value::Object(obj)) }) }) .collect() } -/// Build the `__ts_bids` inline script content from a bucketed bid map. -pub(crate) fn build_bids_script(bid_map: &std::collections::HashMap) -> String { - let entries: Vec = bid_map - .iter() - .map(|(slot_id, bucket)| format!("\"{}\":\"{}\"", slot_id, bucket)) - .collect(); - format!("window.__ts_bids={{{}}};", entries.join(",")) +/// Build the `__ts_bids` `` sequences inside the string. +pub(crate) fn build_bids_script(bid_map: &serde_json::Map) -> String { + let json = serde_json::to_string(bid_map).unwrap_or_else(|_| "{}".to_string()); + let escaped = html_escape_for_script(&json); + format!( + "", + escaped + ) } -/// Build the `__ts_ad_slots` inline script content from matched slots. +/// Build the `__ts_ad_slots` `", + escaped + ) } /// Whether the content type requires processing (URL rewriting, HTML injection). From c212ec544138791419b8faed992627101a7a60dc Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 11:47:51 +0530 Subject: [PATCH 19/67] Wire slots_file and orchestrator into adapter; parse creative-opportunities.toml at startup --- Cargo.lock | 8 +++++ .../trusted-server-adapter-fastly/Cargo.toml | 1 + .../trusted-server-adapter-fastly/src/main.rs | 14 ++++++++- crates/trusted-server-core/build.rs | 11 +++---- .../src/creative_opportunities.rs | 30 +++++++++++++------ crates/trusted-server-core/src/lib.rs | 2 +- crates/trusted-server-core/src/settings.rs | 4 ++- 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e06ac75e..65d1d777 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1151,6 +1151,12 @@ dependencies = [ "wasip2", ] +[[package]] +name = "glob" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" + [[package]] name = "group" version = "0.13.0" @@ -2707,6 +2713,7 @@ dependencies = [ "log-fastly", "serde", "serde_json", + "toml 1.0.7+spec-1.1.0", "trusted-server-core", "urlencoding", ] @@ -2731,6 +2738,7 @@ dependencies = [ "fastly", "flate2", "futures", + "glob", "hex", "hmac", "http", diff --git a/crates/trusted-server-adapter-fastly/Cargo.toml b/crates/trusted-server-adapter-fastly/Cargo.toml index e483ea62..a730efcd 100644 --- a/crates/trusted-server-adapter-fastly/Cargo.toml +++ b/crates/trusted-server-adapter-fastly/Cargo.toml @@ -20,6 +20,7 @@ log = { workspace = true } log-fastly = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } +toml = { workspace = true } trusted-server-core = { workspace = true } urlencoding = { workspace = true } diff --git a/crates/trusted-server-adapter-fastly/src/main.rs b/crates/trusted-server-adapter-fastly/src/main.rs index 52c869d7..74414220 100644 --- a/crates/trusted-server-adapter-fastly/src/main.rs +++ b/crates/trusted-server-adapter-fastly/src/main.rs @@ -39,6 +39,8 @@ use crate::error::to_error_response; use crate::logging::init_logger; use crate::platform::{build_runtime_services, open_kv_store, UnavailableKvStore}; +const CREATIVE_OPPORTUNITIES_TOML: &str = include_str!("../../../creative-opportunities.toml"); + /// Entry point for the Fastly Compute program. /// /// Uses an undecorated `main()` with `Request::from_client()` instead of @@ -80,6 +82,10 @@ fn main() { } }; + let slots_file: trusted_server_core::creative_opportunities::CreativeOpportunitiesFile = + toml::from_str(CREATIVE_OPPORTUNITIES_TOML) + .expect("should parse creative-opportunities.toml"); + let integration_registry = match IntegrationRegistry::new(&settings) { Ok(r) => r, Err(e) => { @@ -103,6 +109,7 @@ fn main() { &orchestrator, &integration_registry, &runtime_services, + &slots_file, req, )) { response.send_to_client(); @@ -114,6 +121,7 @@ async fn route_request( orchestrator: &AuctionOrchestrator, integration_registry: &IntegrationRegistry, runtime_services: &RuntimeServices, + slots_file: &trusted_server_core::creative_opportunities::CreativeOpportunitiesFile, mut req: Request, ) -> Option { // Strip client-spoofable forwarded headers at the edge. @@ -221,8 +229,12 @@ async fn route_request( settings, integration_registry, &publisher_services, + orchestrator, + slots_file, req, - ) { + ) + .await + { Ok(PublisherResponse::Stream { mut response, body, diff --git a/crates/trusted-server-core/build.rs b/crates/trusted-server-core/build.rs index 469c1104..b21cb684 100644 --- a/crates/trusted-server-core/build.rs +++ b/crates/trusted-server-core/build.rs @@ -92,14 +92,15 @@ fn main() { let co_path = Path::new(CREATIVE_OPPORTUNITIES_PATH); if co_path.exists() { - let co_content = fs::read_to_string(co_path) - .expect("should read creative-opportunities.toml"); - let co_value: toml::Value = toml::from_str(&co_content) - .expect("creative-opportunities.toml: invalid TOML"); + let co_content = + fs::read_to_string(co_path).expect("should read creative-opportunities.toml"); + let co_value: toml::Value = + toml::from_str(&co_content).expect("creative-opportunities.toml: invalid TOML"); let slot_id_re = regex::Regex::new(r"^[A-Za-z0-9_\-]+$").expect("should compile regex"); if let Some(slots) = co_value.get("slot").and_then(|v| v.as_array()) { for slot in slots { - let id = slot.get("id") + let id = slot + .get("id") .and_then(|v| v.as_str()) .expect("creative-opportunities.toml: slot missing 'id' field"); if !slot_id_re.is_match(id) { diff --git a/crates/trusted-server-core/src/creative_opportunities.rs b/crates/trusted-server-core/src/creative_opportunities.rs index 7bf3856c..f051c340 100644 --- a/crates/trusted-server-core/src/creative_opportunities.rs +++ b/crates/trusted-server-core/src/creative_opportunities.rs @@ -6,9 +6,10 @@ use std::collections::HashMap; -use glob::Pattern; use serde::{Deserialize, Serialize}; +use glob::Pattern; + use crate::auction::types::{AdFormat, AdSlot, MediaType}; use crate::price_bucket::PriceGranularity; @@ -64,8 +65,9 @@ impl CreativeOpportunitySlot { /// Patterns that cannot be compiled even after normalisation are silently skipped. #[must_use] pub fn matches_path(&self, path: &str) -> bool { - self.page_patterns.iter().any(|pattern| { - match Pattern::new(pattern) { + self.page_patterns + .iter() + .any(|pattern| match Pattern::new(pattern) { Ok(p) => p.matches(path), Err(_) => { let normalised = pattern.replace("**", "*"); @@ -73,8 +75,7 @@ impl CreativeOpportunitySlot { .map(|p| p.matches(path)) .unwrap_or(false) } - } - }) + }) } /// Returns the GAM ad unit path for this slot. @@ -227,7 +228,10 @@ mod tests { #[test] fn glob_matches_article_path() { let slot = make_slot("atf", vec!["/20**"]); - assert!(slot.matches_path("/2024/01/my-article/"), "should match article path"); + assert!( + slot.matches_path("/2024/01/my-article/"), + "should match article path" + ); assert!(!slot.matches_path("/"), "should not match root"); } @@ -243,14 +247,20 @@ mod tests { assert!(validate_slot_id("atf_sidebar_ad").is_ok()); assert!(validate_slot_id("below-content-0").is_ok()); assert!(validate_slot_id("").is_err(), "empty id should fail"); - assert!(validate_slot_id("xss"); + assert!(!inner.contains('<'), "no unescaped < in script content"); + assert!(!inner.contains('>'), "no unescaped > in script content"); + } + + #[test] + fn bid_map_includes_nurl_and_burl() { + let mut winning_bids = HashMap::new(); + winning_bids.insert( + "atf_sidebar_ad".to_string(), + make_bid( + "atf_sidebar_ad", + 1.50, + "kargo", + "abc123", + "https://ssp/win", + "https://ssp/bill", + ), + ); + let map = build_bid_map(&winning_bids, PriceGranularity::Dense); + let entry = map.get("atf_sidebar_ad").expect("should have bid entry"); + let obj = entry.as_object().expect("should be object"); + assert_eq!( + obj.get("hb_pb").and_then(|v| v.as_str()), + Some("1.50"), + "should bucket price with dense granularity" + ); + assert_eq!( + obj.get("hb_bidder").and_then(|v| v.as_str()), + Some("kargo"), + "should include bidder" + ); + assert_eq!( + obj.get("hb_adid").and_then(|v| v.as_str()), + Some("abc123"), + "should include ad_id" + ); + assert_eq!( + obj.get("nurl").and_then(|v| v.as_str()), + Some("https://ssp/win"), + "should include nurl" + ); + assert_eq!( + obj.get("burl").and_then(|v| v.as_str()), + Some("https://ssp/bill"), + "should include burl" + ); + } + + #[test] + fn bid_map_excludes_slot_when_price_is_none() { + let mut winning_bids = HashMap::new(); + winning_bids.insert( + "no-price-slot".to_string(), + Bid { + slot_id: "no-price-slot".to_string(), + price: None, + currency: "USD".to_string(), + creative: None, + adomain: None, + bidder: "kargo".to_string(), + width: 300, + height: 250, + nurl: None, + burl: None, + ad_id: None, + metadata: Default::default(), + }, + ); + let map = build_bid_map(&winning_bids, PriceGranularity::Dense); + assert!( + map.is_empty(), + "slot with no price should be excluded from bid map" + ); + } + + #[test] + fn bids_script_is_xss_safe() { + let mut map = serde_json::Map::new(); + map.insert("atf".to_string(), serde_json::json!({"hb_pb": "1.00"})); + let script = build_bids_script(&map); + let inner = script + .trim_start_matches(""); + assert!(!inner.contains('<'), "no unescaped < in bids script"); + assert!(!inner.contains('>'), "no unescaped > in bids script"); + } + + #[test] + fn html_escape_encodes_special_chars() { + assert_eq!( + html_escape_for_script("text\\with\\backslash"), + "text\\\\with\\\\backslash", + "should escape backslashes" + ); + assert_eq!( + html_escape_for_script("string\"with\"quotes"), + "string\\\"with\\\"quotes", + "should escape quotes" + ); + assert_eq!( + html_escape_for_script("simple"), + "simple", + "should not change simple text" + ); + assert_eq!( + html_escape_for_script("both\\\"mixed"), + "both\\\\\\\"mixed", + "should escape both backslashes and quotes" + ); + } + } } From b047add10a3f9138949b4ad19e783fca2e3b9a8d Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 13:24:00 +0530 Subject: [PATCH 23/67] Enable server-side auction with APS provider and adserver_mock mediator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Enable APS and adserver_mock in auction config; set providers and mediator - Increase auction_timeout_ms from 500ms to 3000ms — 500ms was too tight for HTTPS round-trips to mocktioneer, leaving the mediator zero budget - Fix mediation request: send numeric price instead of opaque encoded_price; mocktioneer requires a decoded price field and does not support encoded_price - Expand creative-opportunities slot page_patterns to include /news/** --- .../src/integrations/adserver_mock.rs | 45 +++++++------------ creative-opportunities.toml | 2 +- trusted-server.toml | 12 ++--- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/crates/trusted-server-core/src/integrations/adserver_mock.rs b/crates/trusted-server-core/src/integrations/adserver_mock.rs index 7ed2da59..8ec94a9c 100644 --- a/crates/trusted-server-core/src/integrations/adserver_mock.rs +++ b/crates/trusted-server-core/src/integrations/adserver_mock.rs @@ -133,36 +133,21 @@ impl AdServerMockProvider { .bids .iter() .map(|bid| { - // Check if this is an APS bid with encoded price (inferred from amznbid in metadata) - let encoded_price = bid - .metadata - .get("amznbid") - .and_then(|v| v.as_str()) - .map(String::from); - - if encoded_price.is_some() { - // APS bid - send encoded price for mediation to decode - json!({ - "imp_id": bid.slot_id, - "encoded_price": encoded_price, - "adm": bid.creative, - "w": bid.width, - "h": bid.height, - "crid": format!("{}-creative", bid.bidder), - "adomain": bid.adomain, - }) - } else { - // Regular bid with decoded price - json!({ - "imp_id": bid.slot_id, - "price": bid.price, - "adm": bid.creative, - "w": bid.width, - "h": bid.height, - "crid": format!("{}-creative", bid.bidder), - "adomain": bid.adomain, - }) - } + // Mocktioneer mediator always requires a numeric `price` field. + // APS bids carry price as an opaque encoded string (`amznbid`) + // that cannot be decoded client-side; use `bid.price` when set + // (a real decoded value) or fall back to a mock floor price for + // test/demo purposes. + let price = bid.price.unwrap_or(1.50); + json!({ + "imp_id": bid.slot_id, + "price": price, + "adm": bid.creative, + "w": bid.width, + "h": bid.height, + "crid": format!("{}-creative", bid.bidder), + "adomain": bid.adomain, + }) }) .collect(); diff --git a/creative-opportunities.toml b/creative-opportunities.toml index b44e215b..b79d2381 100644 --- a/creative-opportunities.toml +++ b/creative-opportunities.toml @@ -5,7 +5,7 @@ id = "atf_sidebar_ad" gam_unit_path = "/21765378893/publisher/atf-sidebar" div_id = "div-atf-sidebar" -page_patterns = ["/20**"] +page_patterns = ["/", "/20**", "/news/**"] formats = [{ width = 300, height = 250 }] floor_price = 0.50 diff --git a/trusted-server.toml b/trusted-server.toml index c2ecab33..8036b7ec 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -161,16 +161,16 @@ rewrite_script = true [auction] enabled = true -providers = ["prebid"] -# mediator = "adserver_mock" # will use mediator when set +providers = ["prebid", "aps"] +mediator = "adserver_mock" timeout_ms = 2000 # Context keys the JS client is allowed to forward into auction requests. # Keys not in this list are silently dropped. An empty list blocks all keys. allowed_context_keys = ["permutive_segments"] [integrations.aps] -enabled = false -pub_id = "your-aps-publisher-id" +enabled = true +pub_id = "test-pub" endpoint = "https://origin-mocktioneer.cdintel.com/e/dtb/bid" timeout_ms = 1000 @@ -180,7 +180,7 @@ container_id = "GTM-XXXXXX" # upstream_url = "https://www.googletagmanager.com" [integrations.adserver_mock] -enabled = false +enabled = true endpoint = "https://origin-mocktioneer.cdintel.com/adserver/mediate" timeout_ms = 1000 @@ -192,6 +192,6 @@ permutive_segments = "permutive" [creative_opportunities] gam_network_id = "21765378893" -auction_timeout_ms = 500 +auction_timeout_ms = 3000 price_granularity = "dense" From 6a5df1060471818c8335178ce35ecd88978aa2ac Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 13:35:24 +0530 Subject: [PATCH 24/67] Fix adserver_mock test for numeric price; fix GPT JS formatting --- .../js/lib/src/integrations/gpt/index.test.ts | 150 +++++++++--------- crates/js/lib/src/integrations/gpt/index.ts | 14 +- .../src/integrations/adserver_mock.rs | 19 +-- 3 files changed, 91 insertions(+), 92 deletions(-) diff --git a/crates/js/lib/src/integrations/gpt/index.test.ts b/crates/js/lib/src/integrations/gpt/index.test.ts index 0a699381..7e2783f2 100644 --- a/crates/js/lib/src/integrations/gpt/index.test.ts +++ b/crates/js/lib/src/integrations/gpt/index.test.ts @@ -1,20 +1,20 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest' +import { describe, it, expect, vi, beforeEach } from 'vitest'; describe('installTsAdInit', () => { beforeEach(() => { - vi.resetModules() - delete (window as any).__ts_ad_slots - delete (window as any).__ts_bids - delete (window as any).__tsAdInit + vi.resetModules(); + delete (window as any).__ts_ad_slots; + delete (window as any).__ts_bids; + delete (window as any).__tsAdInit; // jsdom does not implement navigator.sendBeacon; polyfill it for tests if (!('sendBeacon' in navigator)) { Object.defineProperty(navigator, 'sendBeacon', { value: vi.fn().mockReturnValue(true), writable: true, configurable: true, - }) + }); } - }) + }); it('reads window.__ts_bids synchronously and applies bid targeting before refresh', async () => { const mockSlot = { @@ -22,19 +22,19 @@ describe('installTsAdInit', () => { setTargeting: vi.fn().mockReturnThis(), getSlotElementId: vi.fn().mockReturnValue('atf'), getTargeting: vi.fn().mockReturnValue(['abc']), - } + }; const mockPubads = { enableSingleRequest: vi.fn(), addEventListener: vi.fn(), refresh: vi.fn(), - } - ;(window as any).googletag = { + }; + (window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue(mockSlot), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), - } - ;(window as any).__ts_ad_slots = [ + }; + (window as any).__ts_ad_slots = [ { id: 'atf', gam_unit_path: '/123/atf', @@ -42,8 +42,8 @@ describe('installTsAdInit', () => { formats: [[300, 250]], targeting: { pos: 'atf' }, }, - ] - ;(window as any).__ts_bids = { + ]; + (window as any).__ts_bids = { atf: { hb_pb: '1.00', hb_bidder: 'kargo', @@ -51,47 +51,47 @@ describe('installTsAdInit', () => { nurl: 'https://ssp/win', burl: 'https://ssp/bill', }, - } + }; - const fetchSpy = vi.spyOn(global, 'fetch') + const fetchSpy = vi.spyOn(global, 'fetch'); - const { installTsAdInit } = await import('./index') - installTsAdInit() - ;(window as any).__tsAdInit() + const { installTsAdInit } = await import('./index'); + installTsAdInit(); + (window as any).__tsAdInit(); - expect(fetchSpy).not.toHaveBeenCalled() - expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_pb', '1.00') - expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_bidder', 'kargo') - expect(mockSlot.setTargeting).toHaveBeenCalledWith('ts_initial', '1') - expect(mockPubads.refresh).toHaveBeenCalled() + expect(fetchSpy).not.toHaveBeenCalled(); + expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_pb', '1.00'); + expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_bidder', 'kargo'); + expect(mockSlot.setTargeting).toHaveBeenCalledWith('ts_initial', '1'); + expect(mockPubads.refresh).toHaveBeenCalled(); - fetchSpy.mockRestore() - }) + fetchSpy.mockRestore(); + }); it('fires both nurl and burl via sendBeacon on slotRenderEnded when our bid won', async () => { - const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true) - let capturedListener: ((e: any) => void) | undefined + const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); + let capturedListener: ((e: any) => void) | undefined; const mockSlot = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), getSlotElementId: vi.fn().mockReturnValue('atf'), getTargeting: vi.fn().mockReturnValue(['abc']), - } + }; const mockPubads = { enableSingleRequest: vi.fn(), refresh: vi.fn(), addEventListener: vi.fn((event: string, fn: (e: any) => void) => { - if (event === 'slotRenderEnded') capturedListener = fn + if (event === 'slotRenderEnded') capturedListener = fn; }), - } - ;(window as any).googletag = { + }; + (window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue(mockSlot), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), - } - ;(window as any).__ts_ad_slots = [ + }; + (window as any).__ts_ad_slots = [ { id: 'atf', gam_unit_path: '/123/atf', @@ -99,8 +99,8 @@ describe('installTsAdInit', () => { formats: [[300, 250]], targeting: {}, }, - ] - ;(window as any).__ts_bids = { + ]; + (window as any).__ts_bids = { atf: { hb_pb: '1.00', hb_bidder: 'kargo', @@ -108,44 +108,44 @@ describe('installTsAdInit', () => { nurl: 'https://ssp/win', burl: 'https://ssp/bill', }, - } + }; - const { installTsAdInit } = await import('./index') - installTsAdInit() - ;(window as any).__tsAdInit() + const { installTsAdInit } = await import('./index'); + installTsAdInit(); + (window as any).__tsAdInit(); - expect(capturedListener).toBeDefined() - capturedListener!({ isEmpty: false, slot: mockSlot }) + expect(capturedListener).toBeDefined(); + capturedListener!({ isEmpty: false, slot: mockSlot }); - expect(beaconSpy).toHaveBeenCalledWith('https://ssp/win') - expect(beaconSpy).toHaveBeenCalledWith('https://ssp/bill') - beaconSpy.mockRestore() - }) + expect(beaconSpy).toHaveBeenCalledWith('https://ssp/win'); + expect(beaconSpy).toHaveBeenCalledWith('https://ssp/bill'); + beaconSpy.mockRestore(); + }); it('does not fire nurl/burl when bid did not win GAM line item', async () => { - const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true) - let capturedListener: ((e: any) => void) | undefined + const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); + let capturedListener: ((e: any) => void) | undefined; const mockSlotNoMatch = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), getSlotElementId: vi.fn().mockReturnValue('atf'), getTargeting: vi.fn().mockReturnValue(['OTHER_BID_ID']), - } + }; const mockPubads = { enableSingleRequest: vi.fn(), refresh: vi.fn(), addEventListener: vi.fn((event: string, fn: (e: any) => void) => { - if (event === 'slotRenderEnded') capturedListener = fn + if (event === 'slotRenderEnded') capturedListener = fn; }), - } - ;(window as any).googletag = { + }; + (window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue(mockSlotNoMatch), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), - } - ;(window as any).__ts_ad_slots = [ + }; + (window as any).__ts_ad_slots = [ { id: 'atf', gam_unit_path: '/123/atf', @@ -153,8 +153,8 @@ describe('installTsAdInit', () => { formats: [[300, 250]], targeting: {}, }, - ] - ;(window as any).__ts_bids = { + ]; + (window as any).__ts_bids = { atf: { hb_pb: '1.00', hb_bidder: 'kargo', @@ -162,24 +162,24 @@ describe('installTsAdInit', () => { nurl: 'https://ssp/win', burl: 'https://ssp/bill', }, - } + }; - const { installTsAdInit } = await import('./index') - installTsAdInit() - ;(window as any).__tsAdInit() - capturedListener!({ isEmpty: false, slot: mockSlotNoMatch }) + const { installTsAdInit } = await import('./index'); + installTsAdInit(); + (window as any).__tsAdInit(); + capturedListener!({ isEmpty: false, slot: mockSlotNoMatch }); - expect(beaconSpy).not.toHaveBeenCalled() - beaconSpy.mockRestore() - }) + expect(beaconSpy).not.toHaveBeenCalled(); + beaconSpy.mockRestore(); + }); it('calls refresh even when __ts_bids is empty (graceful fallback)', async () => { const mockPubads = { enableSingleRequest: vi.fn(), addEventListener: vi.fn(), refresh: vi.fn(), - } - ;(window as any).googletag = { + }; + (window as any).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue({ addService: vi.fn().mockReturnThis(), @@ -187,14 +187,14 @@ describe('installTsAdInit', () => { }), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), - } - ;(window as any).__ts_ad_slots = [] - ;(window as any).__ts_bids = {} + }; + (window as any).__ts_ad_slots = []; + (window as any).__ts_bids = {}; - const { installTsAdInit } = await import('./index') - installTsAdInit() - ;(window as any).__tsAdInit() + const { installTsAdInit } = await import('./index'); + installTsAdInit(); + (window as any).__tsAdInit(); - expect(mockPubads.refresh).toHaveBeenCalled() - }) -}) + expect(mockPubads.refresh).toHaveBeenCalled(); + }); +}); diff --git a/crates/js/lib/src/integrations/gpt/index.ts b/crates/js/lib/src/integrations/gpt/index.ts index 1494d793..95b6d427 100644 --- a/crates/js/lib/src/integrations/gpt/index.ts +++ b/crates/js/lib/src/integrations/gpt/index.ts @@ -217,7 +217,11 @@ export function installTsAdInit(): void { g.cmd?.push(() => { slots .map((slot) => { - const gptSlot = g.defineSlot?.(slot.gam_unit_path, slot.formats as Array, slot.div_id); + const gptSlot = g.defineSlot?.( + slot.gam_unit_path, + slot.formats as Array, + slot.div_id + ); if (!gptSlot) return null; gptSlot.addService(g.pubads!()); Object.entries(slot.targeting ?? {}).forEach(([k, v]) => gptSlot.setTargeting(k, v)); @@ -280,13 +284,13 @@ export function installSlimPrebidLoader(): void { // regardless of script order, the module also checks for a pre-set enable flag // immediately after registering the function. if (typeof window !== 'undefined') { - const win = window as Record + const win = window as Record; - win.__tsjs_installGptShim = installGptShim + win.__tsjs_installGptShim = installGptShim; if (win.__tsjs_gpt_enabled === true) { - installGptShim() + installGptShim(); } - installTsAdInit() + installTsAdInit(); } diff --git a/crates/trusted-server-core/src/integrations/adserver_mock.rs b/crates/trusted-server-core/src/integrations/adserver_mock.rs index 8ec94a9c..3a42ec2a 100644 --- a/crates/trusted-server-core/src/integrations/adserver_mock.rs +++ b/crates/trusted-server-core/src/integrations/adserver_mock.rs @@ -675,20 +675,15 @@ mod tests { let bid = &bidder_resp["bids"][0]; assert_eq!(bid["imp_id"], "slot-1"); - // Key assertions for APS-style encoded price bids: - // 1. Should NOT have "price" field (or it should be null) - assert!( - bid["price"].is_null(), - "APS bids should not have decoded price, got: {:?}", - bid["price"] - ); - // 2. Should have "encoded_price" field + // APS bids have no decoded price (bid.price == None), so the mock floor + // price (1.50) is used. Mocktioneer requires a numeric price field and + // does not accept an opaque encoded_price string. assert_eq!( - bid["encoded_price"].as_str(), - Some("encoded-price-value"), - "APS bids should have encoded_price from metadata" + bid["price"].as_f64(), + Some(1.50), + "APS bids with no decoded price should fall back to mock floor price 1.50" ); - // 3. adm should be null (not a string) + // adm should be null (not a string) assert!( bid["adm"].is_null(), "Creative-less bids should have null adm, got: {:?}", From e6c18ad5ec4de17713a840d2c44e0d2b532b5946 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 14:13:49 +0530 Subject: [PATCH 25/67] Replace explicit any in GPT integration with typed interfaces Define SlotRenderEndedEvent, SlotRenderEvent, and TestWindow types to eliminate all @typescript-eslint/no-explicit-any violations in gpt/index.ts and gpt/index.test.ts. Extend GptWindow with __tsjs_slim_prebid_url so installSlimPrebidLoader avoids the any cast. --- .../js/lib/src/integrations/gpt/index.test.ts | 61 ++++++++++++------- crates/js/lib/src/integrations/gpt/index.ts | 15 +++-- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/crates/js/lib/src/integrations/gpt/index.test.ts b/crates/js/lib/src/integrations/gpt/index.test.ts index 7e2783f2..e908a201 100644 --- a/crates/js/lib/src/integrations/gpt/index.test.ts +++ b/crates/js/lib/src/integrations/gpt/index.test.ts @@ -1,11 +1,26 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; +interface SlotRenderEvent { + isEmpty: boolean; + slot: { + getSlotElementId(): string; + getTargeting(key: string): string[]; + }; +} + +type TestWindow = Window & { + googletag?: unknown; + __ts_ad_slots?: unknown; + __ts_bids?: unknown; + __tsAdInit?: () => void; +}; + describe('installTsAdInit', () => { beforeEach(() => { vi.resetModules(); - delete (window as any).__ts_ad_slots; - delete (window as any).__ts_bids; - delete (window as any).__tsAdInit; + delete (window as TestWindow).__ts_ad_slots; + delete (window as TestWindow).__ts_bids; + delete (window as TestWindow).__tsAdInit; // jsdom does not implement navigator.sendBeacon; polyfill it for tests if (!('sendBeacon' in navigator)) { Object.defineProperty(navigator, 'sendBeacon', { @@ -28,13 +43,13 @@ describe('installTsAdInit', () => { addEventListener: vi.fn(), refresh: vi.fn(), }; - (window as any).googletag = { + (window as TestWindow).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue(mockSlot), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), }; - (window as any).__ts_ad_slots = [ + (window as TestWindow).__ts_ad_slots = [ { id: 'atf', gam_unit_path: '/123/atf', @@ -43,7 +58,7 @@ describe('installTsAdInit', () => { targeting: { pos: 'atf' }, }, ]; - (window as any).__ts_bids = { + (window as TestWindow).__ts_bids = { atf: { hb_pb: '1.00', hb_bidder: 'kargo', @@ -57,7 +72,7 @@ describe('installTsAdInit', () => { const { installTsAdInit } = await import('./index'); installTsAdInit(); - (window as any).__tsAdInit(); + (window as TestWindow).__tsAdInit!(); expect(fetchSpy).not.toHaveBeenCalled(); expect(mockSlot.setTargeting).toHaveBeenCalledWith('hb_pb', '1.00'); @@ -70,7 +85,7 @@ describe('installTsAdInit', () => { it('fires both nurl and burl via sendBeacon on slotRenderEnded when our bid won', async () => { const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); - let capturedListener: ((e: any) => void) | undefined; + let capturedListener: ((e: SlotRenderEvent) => void) | undefined; const mockSlot = { addService: vi.fn().mockReturnThis(), @@ -81,17 +96,17 @@ describe('installTsAdInit', () => { const mockPubads = { enableSingleRequest: vi.fn(), refresh: vi.fn(), - addEventListener: vi.fn((event: string, fn: (e: any) => void) => { + addEventListener: vi.fn((event: string, fn: (e: SlotRenderEvent) => void) => { if (event === 'slotRenderEnded') capturedListener = fn; }), }; - (window as any).googletag = { + (window as TestWindow).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue(mockSlot), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), }; - (window as any).__ts_ad_slots = [ + (window as TestWindow).__ts_ad_slots = [ { id: 'atf', gam_unit_path: '/123/atf', @@ -100,7 +115,7 @@ describe('installTsAdInit', () => { targeting: {}, }, ]; - (window as any).__ts_bids = { + (window as TestWindow).__ts_bids = { atf: { hb_pb: '1.00', hb_bidder: 'kargo', @@ -112,7 +127,7 @@ describe('installTsAdInit', () => { const { installTsAdInit } = await import('./index'); installTsAdInit(); - (window as any).__tsAdInit(); + (window as TestWindow).__tsAdInit!(); expect(capturedListener).toBeDefined(); capturedListener!({ isEmpty: false, slot: mockSlot }); @@ -124,7 +139,7 @@ describe('installTsAdInit', () => { it('does not fire nurl/burl when bid did not win GAM line item', async () => { const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); - let capturedListener: ((e: any) => void) | undefined; + let capturedListener: ((e: SlotRenderEvent) => void) | undefined; const mockSlotNoMatch = { addService: vi.fn().mockReturnThis(), @@ -135,17 +150,17 @@ describe('installTsAdInit', () => { const mockPubads = { enableSingleRequest: vi.fn(), refresh: vi.fn(), - addEventListener: vi.fn((event: string, fn: (e: any) => void) => { + addEventListener: vi.fn((event: string, fn: (e: SlotRenderEvent) => void) => { if (event === 'slotRenderEnded') capturedListener = fn; }), }; - (window as any).googletag = { + (window as TestWindow).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue(mockSlotNoMatch), pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), }; - (window as any).__ts_ad_slots = [ + (window as TestWindow).__ts_ad_slots = [ { id: 'atf', gam_unit_path: '/123/atf', @@ -154,7 +169,7 @@ describe('installTsAdInit', () => { targeting: {}, }, ]; - (window as any).__ts_bids = { + (window as TestWindow).__ts_bids = { atf: { hb_pb: '1.00', hb_bidder: 'kargo', @@ -166,7 +181,7 @@ describe('installTsAdInit', () => { const { installTsAdInit } = await import('./index'); installTsAdInit(); - (window as any).__tsAdInit(); + (window as TestWindow).__tsAdInit!(); capturedListener!({ isEmpty: false, slot: mockSlotNoMatch }); expect(beaconSpy).not.toHaveBeenCalled(); @@ -179,7 +194,7 @@ describe('installTsAdInit', () => { addEventListener: vi.fn(), refresh: vi.fn(), }; - (window as any).googletag = { + (window as TestWindow).googletag = { cmd: { push: vi.fn((fn: () => void) => fn()) }, defineSlot: vi.fn().mockReturnValue({ addService: vi.fn().mockReturnThis(), @@ -188,12 +203,12 @@ describe('installTsAdInit', () => { pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), }; - (window as any).__ts_ad_slots = []; - (window as any).__ts_bids = {}; + (window as TestWindow).__ts_ad_slots = []; + (window as TestWindow).__ts_bids = {}; const { installTsAdInit } = await import('./index'); installTsAdInit(); - (window as any).__tsAdInit(); + (window as TestWindow).__tsAdInit!(); expect(mockPubads.refresh).toHaveBeenCalled(); }); diff --git a/crates/js/lib/src/integrations/gpt/index.ts b/crates/js/lib/src/integrations/gpt/index.ts index 95b6d427..ffb4a687 100644 --- a/crates/js/lib/src/integrations/gpt/index.ts +++ b/crates/js/lib/src/integrations/gpt/index.ts @@ -32,13 +32,19 @@ interface GoogleTagSlot { getSlotElementId(): string; setTargeting(key: string, value: string | string[]): GoogleTagSlot; addService(service: GoogleTagPubAdsService): GoogleTagSlot; + getTargeting?(key: string): string[]; +} + +interface SlotRenderEndedEvent { + isEmpty: boolean; + slot: GoogleTagSlot; } interface GoogleTagPubAdsService { setTargeting(key: string, value: string | string[]): GoogleTagPubAdsService; getTargeting(key: string): string[]; enableSingleRequest(): void; - addEventListener(event: string, fn: (e: any) => void): void; + addEventListener(event: string, fn: (e: SlotRenderEndedEvent) => void): void; refresh(): void; } @@ -57,6 +63,7 @@ interface GoogleTag { type GptWindow = Window & { googletag?: Partial; + __tsjs_slim_prebid_url?: string; }; // ------------------------------------------------------------------ @@ -237,7 +244,7 @@ export function installTsAdInit(): void { g.pubads!().enableSingleRequest(); g.enableServices?.(); - g.pubads!().addEventListener?.('slotRenderEnded', (event: any) => { + g.pubads!().addEventListener?.('slotRenderEnded', (event: SlotRenderEndedEvent) => { const slotId: string = event.slot?.getSlotElementId?.() ?? ''; const bid = bids[slotId] ?? {}; const ourBidWon = @@ -265,7 +272,7 @@ export function installTsAdInit(): void { * the slim-Prebid bundle build target ships in a later phase). */ export function installSlimPrebidLoader(): void { - const url = (window as any).__tsjs_slim_prebid_url as string | undefined; + const url = (window as GptWindow).__tsjs_slim_prebid_url; if (!url) return; window.addEventListener('load', () => { const script = document.createElement('script'); @@ -284,7 +291,7 @@ export function installSlimPrebidLoader(): void { // regardless of script order, the module also checks for a pre-set enable flag // immediately after registering the function. if (typeof window !== 'undefined') { - const win = window as Record; + const win = window as unknown as Record; win.__tsjs_installGptShim = installGptShim; From 74bbc25b4b52ab1b5ea012894d109c67e606ceb2 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 15:33:39 +0530 Subject: [PATCH 26/67] Update creative-opportunities config to real autoblog.com GAM values Set gam_network_id to 88059007 (autoblog production network). Update atf_sidebar_ad slot to /88059007/autoblog/news with div_id ad-atf_sidebar-0-_r_2_ (desktop ATF sidebar, 300x250); restrict page_patterns to article paths only (/20**, /news/**) since that div does not exist on the homepage. Add homepage_header_ad slot targeting /88059007/autoblog/homepage with ad-header-0-_R_jpalubtak5lb_ for 970x90/728x90/970x250 leaderboard formats. Reduce auction_timeout_ms from 3000 to 500 to cap TTFB at the spec-recommended ceiling. --- creative-opportunities.toml | 21 ++++++++++++++++++--- trusted-server.toml | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/creative-opportunities.toml b/creative-opportunities.toml index b79d2381..0261110a 100644 --- a/creative-opportunities.toml +++ b/creative-opportunities.toml @@ -3,9 +3,9 @@ [[slot]] id = "atf_sidebar_ad" -gam_unit_path = "/21765378893/publisher/atf-sidebar" -div_id = "div-atf-sidebar" -page_patterns = ["/", "/20**", "/news/**"] +gam_unit_path = "/88059007/autoblog/news" +div_id = "ad-atf_sidebar-0-_r_2_" +page_patterns = ["/20**", "/news/**"] formats = [{ width = 300, height = 250 }] floor_price = 0.50 @@ -15,3 +15,18 @@ zone = "atfSidebar" [slot.providers.aps] slot_id = "aps-slot-atf-sidebar" + +[[slot]] +id = "homepage_header_ad" +gam_unit_path = "/88059007/autoblog/homepage" +div_id = "ad-header-0-_R_jpalubtak5lb_" +page_patterns = ["/"] +formats = [{ width = 970, height = 90 }, { width = 728, height = 90 }, { width = 970, height = 250 }] +floor_price = 0.50 + +[slot.targeting] +pos = "atf" +zone = "header" + +[slot.providers.aps] +slot_id = "aps-slot-homepage-header" diff --git a/trusted-server.toml b/trusted-server.toml index 8036b7ec..da00c3ed 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -191,7 +191,7 @@ timeout_ms = 1000 permutive_segments = "permutive" [creative_opportunities] -gam_network_id = "21765378893" -auction_timeout_ms = 3000 +gam_network_id = "88059007" +auction_timeout_ms = 500 price_granularity = "dense" From 51aba8f1b48a5a2c18bf1fb3df5ed76fc66d837c Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 17:49:49 +0530 Subject: [PATCH 27/67] Update auction timeout and APS slot ID bug --- .../src/integrations/aps.rs | 139 ++++++++++++++++-- trusted-server.toml | 2 +- 2 files changed, 128 insertions(+), 13 deletions(-) diff --git a/crates/trusted-server-core/src/integrations/aps.rs b/crates/trusted-server-core/src/integrations/aps.rs index 79eca5a3..ba6c14bb 100644 --- a/crates/trusted-server-core/src/integrations/aps.rs +++ b/crates/trusted-server-core/src/integrations/aps.rs @@ -286,24 +286,46 @@ impl IntegrationConfig for ApsConfig { /// Amazon APS auction provider. pub struct ApsAuctionProvider { config: ApsConfig, + // Maps APS slot ID → creative opportunity slot ID for the in-flight request. + // Written by request_bids before the async send; read by parse_response when the + // response arrives. Safe because Fastly Compute runs each request in an isolated + // single-threaded Wasm instance — the Mutex never contends in practice. + slot_id_map: std::sync::Mutex>, } impl ApsAuctionProvider { /// Create a new APS auction provider. #[must_use] pub fn new(config: ApsConfig) -> Self { - Self { config } + Self { + config, + slot_id_map: std::sync::Mutex::new(HashMap::new()), + } } /// Convert unified `AuctionRequest` to APS TAM bid request format. /// + /// Returns the serialisable `ApsBidRequest` and a map of APS slot ID → + /// creative-opportunity slot ID so the caller can remap bids in the response. /// Populates consent fields (GDPR, US Privacy, GPP) from the /// [`ConsentContext`](crate::consent::ConsentContext) attached to the request. - fn to_aps_request(&self, request: &AuctionRequest) -> ApsBidRequest { + fn to_aps_request(&self, request: &AuctionRequest) -> (ApsBidRequest, HashMap) { + let mut slot_id_map: HashMap = HashMap::new(); let slots: Vec = request .slots .iter() .map(|slot| { + // Use the APS-specific slot ID from [slot.providers.aps] if configured; + // fall back to the creative-opportunity slot ID otherwise. + let aps_slot_id = slot + .bidders + .get("aps") + .and_then(|p| p.get("slotID")) + .and_then(|v| v.as_str()) + .unwrap_or(&slot.id) + .to_string(); + slot_id_map.insert(aps_slot_id.clone(), slot.id.clone()); + // Extract sizes from banner formats let sizes: Vec<[u32; 2]> = slot .formats @@ -313,7 +335,7 @@ impl ApsAuctionProvider { .collect(); ApsSlot { - slot_id: slot.id.clone(), + slot_id: aps_slot_id, sizes, slot_name: Some(slot.id.clone()), } @@ -337,7 +359,7 @@ impl ApsAuctionProvider { }) }); - ApsBidRequest { + let bid_request = ApsBidRequest { pub_id: self.config.pub_id.clone(), slots, page_url: request.publisher.page_url.clone(), @@ -347,7 +369,8 @@ impl ApsAuctionProvider { us_privacy, gpp, gpp_sid, - } + }; + (bid_request, slot_id_map) } /// Parse size string (e.g., "300x250") into width and height. @@ -433,9 +456,19 @@ impl ApsAuctionProvider { aps_response.contextual.slots.len() ); + let slot_map = self + .slot_id_map + .lock() + .expect("should lock APS slot id map"); for slot in aps_response.contextual.slots { match self.parse_aps_slot(&slot) { - Ok(bid) => { + Ok(mut bid) => { + // Remap APS slot ID (e.g. "aps-slot-atf-sidebar") back to the + // creative-opportunity slot ID (e.g. "atf_sidebar_ad") so the + // mediator and bid_map can match by creative slot ID. + if let Some(creative_id) = slot_map.get(&bid.slot_id) { + bid.slot_id = creative_id.clone(); + } let encoded_price = bid .metadata .get("amznbid") @@ -485,8 +518,13 @@ impl AuctionProvider for ApsAuctionProvider { self.config.pub_id ); - // Transform to APS format - let aps_request = self.to_aps_request(request); + // Transform to APS format; store the APS-slot-ID → creative-slot-ID map so + // parse_response can remap bids back to the creative opportunity slot ID. + let (aps_request, slot_id_map) = self.to_aps_request(request); + *self + .slot_id_map + .lock() + .expect("should lock APS slot id map") = slot_id_map; // Serialize to JSON let aps_json = @@ -703,7 +741,7 @@ mod tests { let provider = ApsAuctionProvider::new(config); let auction_request = create_test_auction_request(); - let aps_request = provider.to_aps_request(&auction_request); + let (aps_request, _slot_id_map) = provider.to_aps_request(&auction_request); // Verify basic fields assert_eq!(aps_request.pub_id, "5128"); @@ -729,6 +767,83 @@ mod tests { assert_eq!(slot2.sizes[0], [300, 250]); } + #[test] + fn aps_slot_id_from_bidders_map_used_in_request_and_remapped_in_response() { + use serde_json::json; + + let config = ApsConfig { + enabled: true, + pub_id: "5128".to_string(), + endpoint: default_endpoint(), + timeout_ms: 800, + }; + let provider = ApsAuctionProvider::new(config); + + let mut bidders = HashMap::new(); + bidders.insert( + "aps".to_string(), + json!({ "slotID": "aps-slot-atf-sidebar" }), + ); + let request = AuctionRequest { + id: "test".to_string(), + slots: vec![AdSlot { + id: "atf_sidebar_ad".to_string(), + formats: vec![AdFormat { + media_type: MediaType::Banner, + width: 300, + height: 250, + }], + floor_price: None, + targeting: HashMap::new(), + bidders, + }], + publisher: PublisherInfo { + domain: "example.com".to_string(), + page_url: None, + }, + user: UserInfo { + id: "user-1".to_string(), + fresh_id: "fresh-1".to_string(), + consent: None, + }, + device: None, + site: None, + context: HashMap::new(), + }; + + let (aps_request, slot_id_map) = provider.to_aps_request(&request); + assert_eq!( + aps_request.slots[0].slot_id, "aps-slot-atf-sidebar", + "should send configured APS slot ID to APS" + ); + assert_eq!( + slot_id_map.get("aps-slot-atf-sidebar").map(String::as_str), + Some("atf_sidebar_ad"), + "should build reverse map from APS slot ID to creative slot ID" + ); + + *provider.slot_id_map.lock().expect("should lock") = slot_id_map; + + let aps_response = json!({ + "contextual": { + "slots": [{ + "slotID": "aps-slot-atf-sidebar", + "size": "300x250", + "fif": "1", + "amznbid": "1gtm3q", + "meta": ["slotID"] + }] + } + }); + + let response = provider.parse_aps_response(&aps_response, 100); + assert_eq!(response.bids.len(), 1, "should parse one bid"); + assert_eq!( + response.bids[0].slot_id, "atf_sidebar_ad", + "bid slot_id should be remapped to creative slot ID" + ); + } + #[test] fn test_aps_response_parsing_success() { let config = ApsConfig { @@ -957,7 +1072,7 @@ mod tests { ..Default::default() }); - let aps_request = provider.to_aps_request(&request); + let (aps_request, _slot_id_map) = provider.to_aps_request(&request); // Verify GDPR consent let gdpr = aps_request.gdpr.expect("should have gdpr"); @@ -986,7 +1101,7 @@ mod tests { let provider = ApsAuctionProvider::new(config); let request = create_test_auction_request(); // consent is None - let aps_request = provider.to_aps_request(&request); + let (aps_request, _slot_id_map) = provider.to_aps_request(&request); assert!(aps_request.gdpr.is_none()); assert!(aps_request.us_privacy.is_none()); @@ -1013,7 +1128,7 @@ mod tests { ..Default::default() }); - let aps_request = provider.to_aps_request(&request); + let (aps_request, _slot_id_map) = provider.to_aps_request(&request); let json = serde_json::to_value(&aps_request).expect("should serialize"); // GDPR fields present diff --git a/trusted-server.toml b/trusted-server.toml index da00c3ed..43e090fe 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -192,6 +192,6 @@ permutive_segments = "permutive" [creative_opportunities] gam_network_id = "88059007" -auction_timeout_ms = 500 +auction_timeout_ms = 1500 price_granularity = "dense" From 3d51fe487e68d08621b0c6a5ffa1364406f45ac1 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 18:43:18 +0530 Subject: [PATCH 28/67] Call __tsAdInit after injecting __ts_bids into page The bids script set window.__ts_bids but never invoked the __tsAdInit function, leaving GPT slots undefined and server-side targeting (hb_pb, hb_bidder) never applied. Both the winning-bid path (build_bids_script) and the no-auction fallback (html_processor None branch) now guard-call the function after the assignment. --- crates/trusted-server-core/src/html_processor.rs | 2 +- crates/trusted-server-core/src/publisher.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/trusted-server-core/src/html_processor.rs b/crates/trusted-server-core/src/html_processor.rs index 9ef6edb6..45e06660 100644 --- a/crates/trusted-server-core/src/html_processor.rs +++ b/crates/trusted-server-core/src/html_processor.rs @@ -301,7 +301,7 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso let script_guard = state.read().expect("should read bid state"); let bids_script = match &*script_guard { Some(s) => s.clone(), - None => r#""# + None => r#""# .to_string(), }; end_tag.before(&bids_script, ContentType::Html); diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 73e489dc..193f702c 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -883,7 +883,7 @@ pub(crate) fn build_bids_script(bid_map: &serde_json::Mapwindow.__ts_bids=JSON.parse(\"{}\");", + "", escaped ) } From 4cf6d98c3adae70c1fdec3ca1c97f531136a16ef Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 18:50:33 +0530 Subject: [PATCH 29/67] Fix format error --- crates/trusted-server-core/src/html_processor.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/trusted-server-core/src/html_processor.rs b/crates/trusted-server-core/src/html_processor.rs index 45e06660..a3608d9e 100644 --- a/crates/trusted-server-core/src/html_processor.rs +++ b/crates/trusted-server-core/src/html_processor.rs @@ -296,8 +296,8 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso move |el| { let state = state.clone(); if let Some(handlers) = el.end_tag_handlers() { - let handler: EndTagHandler<'static> = - Box::new(move |end_tag: &mut EndTag<'_>| { + let handler: EndTagHandler<'static> = Box::new( + move |end_tag: &mut EndTag<'_>| { let script_guard = state.read().expect("should read bid state"); let bids_script = match &*script_guard { Some(s) => s.clone(), @@ -306,7 +306,8 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso }; end_tag.before(&bids_script, ContentType::Html); Ok(()) - }); + }, + ); handlers.push(handler); } Ok(()) From e06af4b0fddee2f6e1ecffba436a7af4f333f247 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 19:36:41 +0530 Subject: [PATCH 30/67] Add PBS inline bidder params via creative-opportunities.toml Adds [slot.providers.pbs.bidders] support so PBS bidder params live in creative-opportunities.toml alongside APS params, without needing PBS stored requests configured server-side. PrebidAuctionProvider now sends imp.ext.prebid.storedrequest.id as a fallback for slots with no inline PBS params, and skips non-PBS provider keys (e.g. "aps") that belong to separate auction providers. PrebidImpExt gains an optional storedrequest field; empty bidder maps are omitted during serialisation. Wires mocktioneer and criteo (placeholder IDs) for both autoblog creative-opportunity slots. --- .../src/creative_opportunities.rs | 66 +++++++++- .../src/integrations/prebid.rs | 116 ++++++++++++++++-- crates/trusted-server-core/src/openrtb.rs | 14 ++- creative-opportunities.toml | 8 ++ 4 files changed, 191 insertions(+), 13 deletions(-) diff --git a/crates/trusted-server-core/src/creative_opportunities.rs b/crates/trusted-server-core/src/creative_opportunities.rs index f051c340..a7fd99cb 100644 --- a/crates/trusted-server-core/src/creative_opportunities.rs +++ b/crates/trusted-server-core/src/creative_opportunities.rs @@ -99,7 +99,8 @@ impl CreativeOpportunitySlot { /// Converts this slot into an [`AdSlot`] ready for use in an auction request. /// - /// Provider-specific params (e.g., APS `slotID`) are wired into the `bidders` map. + /// Provider-specific params (e.g., APS `slotID`, PBS bidder params) are wired + /// into the `bidders` map keyed by provider/bidder name. #[must_use] pub fn to_ad_slot(&self, gam_network_id: &str) -> AdSlot { let _ = gam_network_id; @@ -110,6 +111,11 @@ impl CreativeOpportunitySlot { serde_json::json!({ "slotID": aps.slot_id }), ); } + if let Some(ref pbs) = self.providers.pbs { + for (bidder_name, params) in &pbs.bidders { + bidders.insert(bidder_name.clone(), params.clone()); + } + } AdSlot { id: self.id.clone(), formats: self @@ -155,6 +161,8 @@ impl CreativeOpportunityFormat { pub struct SlotProviders { /// Amazon Publisher Services (APS/TAM) slot parameters. pub aps: Option, + /// Prebid Server (PBS) slot parameters. + pub pbs: Option, } /// APS-specific parameters for a slot. @@ -164,6 +172,24 @@ pub struct ApsSlotParams { pub slot_id: String, } +/// PBS-specific parameters for a slot. +/// +/// Bidder params are sent inline to Prebid Server so bidder credentials +/// stay in `creative-opportunities.toml` rather than in PBS stored requests. +#[derive(Debug, Clone, Default, Deserialize)] +pub struct PbsSlotParams { + /// Per-bidder params keyed by bidder name (must match PBS adapter name). + /// + /// Example in TOML: + /// ```toml + /// [slot.providers.pbs.bidders] + /// mocktioneer = { bid = 2.00 } + /// criteo = { networkId = 123456, pubid = "123456" } + /// ``` + #[serde(default)] + pub bidders: HashMap, +} + /// TOML file structure for creative opportunity slot definitions. #[derive(Debug, Clone, Deserialize, Default)] pub struct CreativeOpportunitiesFile { @@ -293,6 +319,44 @@ mod tests { ); } + #[test] + fn to_ad_slot_wires_pbs_bidder_params_into_bidders() { + let mut slot = make_slot("atf_sidebar_ad", vec!["/"]); + slot.providers.pbs = Some(PbsSlotParams { + bidders: [ + ( + "mocktioneer".to_string(), + serde_json::json!({ "bid": 2.00 }), + ), + ( + "criteo".to_string(), + serde_json::json!({ "networkId": 123456, "pubid": "123456" }), + ), + ] + .into_iter() + .collect(), + }); + let ad_slot = slot.to_ad_slot("88059007"); + let mock_params = ad_slot + .bidders + .get("mocktioneer") + .expect("should have mocktioneer bidder"); + assert_eq!( + mock_params.get("bid").and_then(|v| v.as_f64()), + Some(2.0), + "should wire mocktioneer bid param" + ); + let criteo_params = ad_slot + .bidders + .get("criteo") + .expect("should have criteo bidder"); + assert_eq!( + criteo_params.get("networkId").and_then(|v| v.as_i64()), + Some(112141), + "should wire criteo networkId param" + ); + } + #[test] fn to_ad_slot_sets_floor_price_and_formats() { let slot = make_slot("atf", vec!["/"]); diff --git a/crates/trusted-server-core/src/integrations/prebid.rs b/crates/trusted-server-core/src/integrations/prebid.rs index 62e112c7..46b87cc0 100644 --- a/crates/trusted-server-core/src/integrations/prebid.rs +++ b/crates/trusted-server-core/src/integrations/prebid.rs @@ -26,8 +26,8 @@ use crate::integrations::{ }; use crate::openrtb::{ to_openrtb_i32, Banner, ConsentedProvidersSettings, Device, Format, Geo, Imp, ImpExt, - OpenRtbRequest, PrebidExt, PrebidImpExt, Publisher, Regs, RegsExt, RequestExt, Site, ToExt, - TrustedServerExt, User, UserExt, + ImpStoredRequest, OpenRtbRequest, PrebidExt, PrebidImpExt, Publisher, Regs, RegsExt, + RequestExt, Site, ToExt, TrustedServerExt, User, UserExt, }; use crate::platform::RuntimeServices; use crate::request_signing::{RequestSigner, SigningParams, SIGNING_VERSION}; @@ -529,22 +529,27 @@ impl PrebidAuctionProvider { // Build the bidder map for PBS. // The JS adapter sends "trustedServer" as the bidder (our orchestrator // adapter name). Replace it with the real PBS bidders from config. - // Pass through any other bidders with their params as-is. + // Only pass through keys that are known PBS bidders — skip provider-specific + // keys like "aps" which belong to their own separate auction provider. let mut bidder: HashMap = HashMap::new(); for (name, params) in &slot.bidders { if name == TRUSTED_SERVER_BIDDER { bidder.extend(expand_trusted_server_bidders(&self.config.bidders, params)); - } else { + } else if self.config.bidders.iter().any(|b| b == name) { bidder.insert(name.clone(), params.clone()); } } - // Fallback to config bidders if none provided - if bidder.is_empty() { - for b in &self.config.bidders { - bidder.insert(b.clone(), Json::Object(serde_json::Map::new())); - } - } + // When no inline PBS bidder params exist (e.g. creative-opportunity slots + // whose PBS params live in stored requests), tell PBS to resolve bidder + // config from the stored request keyed by this slot ID. + let storedrequest = if bidder.is_empty() { + Some(ImpStoredRequest { + id: slot.id.clone(), + }) + } else { + None + }; // Apply zone-specific bid param overrides when configured. for (name, params) in &mut bidder { @@ -582,7 +587,10 @@ impl PrebidAuctionProvider { secure: Some(true), // require HTTPS creatives tagid: Some(slot.id.clone()), ext: ImpExt { - prebid: PrebidImpExt { bidder }, + prebid: PrebidImpExt { + bidder, + storedrequest, + }, } .to_ext(), ..Default::default() @@ -3044,4 +3052,90 @@ fixed_bottom = {placementId = "_s2sBottom"} assert_eq!(statuses[0]["bidder"], "kargo"); assert_eq!(statuses[1]["status"], "timeout"); } + + // ======================================================================== + // PBS stored request tests + // ======================================================================== + + #[test] + fn to_openrtb_uses_stored_request_when_slot_has_no_pbs_bidder_params() { + // Slot only has "aps" provider — not a PBS bidder + let slot = make_slot( + "atf_sidebar_ad", + HashMap::from([("aps".to_string(), json!({"slotID": "aps-slot-atf-sidebar"}))]), + ); + let request = make_auction_request(vec![slot]); + + let ortb = call_to_openrtb(base_config(), &request); + let ext = ortb.imp[0].ext.as_ref().expect("should have imp ext"); + let prebid = ext.get("prebid").expect("should have prebid in ext"); + + assert!( + prebid.get("bidder").is_none(), + "should not send inline bidder params when using stored request" + ); + assert_eq!( + prebid["storedrequest"]["id"], "atf_sidebar_ad", + "should use slot id as stored request id" + ); + } + + #[test] + fn to_openrtb_uses_stored_request_when_slot_has_empty_bidders() { + let slot = make_slot("homepage_header_ad", HashMap::new()); + let request = make_auction_request(vec![slot]); + + let ortb = call_to_openrtb(base_config(), &request); + let ext = ortb.imp[0].ext.as_ref().expect("should have imp ext"); + let prebid = ext.get("prebid").expect("should have prebid in ext"); + + assert_eq!( + prebid["storedrequest"]["id"], "homepage_header_ad", + "should use slot id as stored request id for slot with no bidder map" + ); + } + + #[test] + fn to_openrtb_uses_inline_bidder_params_not_stored_request_for_trusted_server_slots() { + let mut config = base_config(); + config.bidders = vec!["kargo".to_string()]; + + let slot = make_ts_slot( + "in_content_ad", + &json!({ "kargo": { "placementId": "client_123" } }), + None, + ); + let request = make_auction_request(vec![slot]); + + let ortb = call_to_openrtb(config, &request); + let ext = ortb.imp[0].ext.as_ref().expect("should have imp ext"); + let prebid = ext.get("prebid").expect("should have prebid in ext"); + + assert!( + prebid.get("storedrequest").is_none(), + "should not use stored request when inline bidder params are present" + ); + assert_eq!( + prebid["bidder"]["kargo"]["placementId"], "client_123", + "should use inline bidder params from trustedServer expansion" + ); + } + + #[test] + fn to_openrtb_skips_aps_key_from_slot_bidders_in_pbs_request() { + let slot = make_slot( + "atf_sidebar_ad", + HashMap::from([("aps".to_string(), json!({"slotID": "aps-slot-atf-sidebar"}))]), + ); + let request = make_auction_request(vec![slot]); + + let ortb = call_to_openrtb(base_config(), &request); + let ext = ortb.imp[0].ext.as_ref().expect("should have imp ext"); + let prebid = ext.get("prebid").expect("should have prebid in ext"); + + assert!( + prebid.get("bidder").is_none(), + "should not forward aps key into PBS imp.ext.prebid.bidder" + ); + } } diff --git a/crates/trusted-server-core/src/openrtb.rs b/crates/trusted-server-core/src/openrtb.rs index 3c9be932..eca5e70f 100644 --- a/crates/trusted-server-core/src/openrtb.rs +++ b/crates/trusted-server-core/src/openrtb.rs @@ -162,9 +162,21 @@ pub struct ImpExt { impl ToExt for ImpExt {} -#[derive(Debug, Serialize)] +#[derive(Debug, Default, Serialize)] pub struct PrebidImpExt { + #[serde(skip_serializing_if = "std::collections::HashMap::is_empty")] pub bidder: std::collections::HashMap, + #[serde(skip_serializing_if = "Option::is_none")] + pub storedrequest: Option, +} + +/// PBS imp-level stored request reference. +/// +/// PBS merges the stored imp JSON (keyed by `id`) into the outgoing request, +/// populating bidder params that are not sent inline. +#[derive(Debug, Serialize)] +pub struct ImpStoredRequest { + pub id: String, } #[derive(Debug, Serialize)] diff --git a/creative-opportunities.toml b/creative-opportunities.toml index 0261110a..3cd27f2b 100644 --- a/creative-opportunities.toml +++ b/creative-opportunities.toml @@ -16,6 +16,10 @@ zone = "atfSidebar" [slot.providers.aps] slot_id = "aps-slot-atf-sidebar" +[slot.providers.pbs.bidders] +mocktioneer = { bid = 2.00 } +criteo = { networkId = 123456, pubid = "123456" } + [[slot]] id = "homepage_header_ad" gam_unit_path = "/88059007/autoblog/homepage" @@ -30,3 +34,7 @@ zone = "header" [slot.providers.aps] slot_id = "aps-slot-homepage-header" + +[slot.providers.pbs.bidders] +mocktioneer = { bid = 2.00 } +criteo = { networkId = 123456, pubid = "123456" } From 5cbf05f1f9f908bbd200a2de52cdec119396a34f Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 19:41:03 +0530 Subject: [PATCH 31/67] Fix clippy errors --- crates/trusted-server-core/src/creative_opportunities.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/trusted-server-core/src/creative_opportunities.rs b/crates/trusted-server-core/src/creative_opportunities.rs index a7fd99cb..fa3449fd 100644 --- a/crates/trusted-server-core/src/creative_opportunities.rs +++ b/crates/trusted-server-core/src/creative_opportunities.rs @@ -342,7 +342,7 @@ mod tests { .get("mocktioneer") .expect("should have mocktioneer bidder"); assert_eq!( - mock_params.get("bid").and_then(|v| v.as_f64()), + mock_params.get("bid").and_then(serde_json::Value::as_f64), Some(2.0), "should wire mocktioneer bid param" ); @@ -351,7 +351,7 @@ mod tests { .get("criteo") .expect("should have criteo bidder"); assert_eq!( - criteo_params.get("networkId").and_then(|v| v.as_i64()), + criteo_params.get("networkId").and_then(serde_json::Value::as_i64), Some(112141), "should wire criteo networkId param" ); From 60011f08b25f8e062366e15c863623767476acd6 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 19:46:07 +0530 Subject: [PATCH 32/67] Fix test assertion --- crates/trusted-server-core/src/creative_opportunities.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/trusted-server-core/src/creative_opportunities.rs b/crates/trusted-server-core/src/creative_opportunities.rs index fa3449fd..7a4a10df 100644 --- a/crates/trusted-server-core/src/creative_opportunities.rs +++ b/crates/trusted-server-core/src/creative_opportunities.rs @@ -351,8 +351,10 @@ mod tests { .get("criteo") .expect("should have criteo bidder"); assert_eq!( - criteo_params.get("networkId").and_then(serde_json::Value::as_i64), - Some(112141), + criteo_params + .get("networkId") + .and_then(serde_json::Value::as_i64), + Some(123456), "should wire criteo networkId param" ); } From cf5091fabfaa76e08aeb34c5905943ae54dd38de Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 20:27:56 +0530 Subject: [PATCH 33/67] Fix double __ts_bids injection --- .../trusted-server-core/src/html_processor.rs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/trusted-server-core/src/html_processor.rs b/crates/trusted-server-core/src/html_processor.rs index a3608d9e..86a8abe7 100644 --- a/crates/trusted-server-core/src/html_processor.rs +++ b/crates/trusted-server-core/src/html_processor.rs @@ -20,6 +20,7 @@ use std::cell::Cell; use std::io; use std::rc::Rc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use lol_html::{ @@ -246,6 +247,7 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso }); let injected_tsjs = Rc::new(Cell::new(false)); + let injected_bids = Arc::new(AtomicBool::new(false)); let integration_registry = config.integrations.clone(); let script_rewriters = integration_registry.script_rewriters(); let ad_slots_script = config.ad_slots_script.clone(); @@ -291,13 +293,20 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso } }), // Inject __ts_bids before via end_tag_handlers. + // Guard with AtomicBool so the script is only injected once even if + // the origin HTML contains multiple elements (e.g. template fragments). element!("body", { let state = ad_bids_state.clone(); + let injected_bids = injected_bids.clone(); move |el| { let state = state.clone(); + let injected_bids = injected_bids.clone(); if let Some(handlers) = el.end_tag_handlers() { let handler: EndTagHandler<'static> = Box::new( move |end_tag: &mut EndTag<'_>| { + if injected_bids.swap(true, Ordering::SeqCst) { + return Ok(()); + } let script_guard = state.read().expect("should read bid state"); let bids_script = match &*script_guard { Some(s) => s.clone(), @@ -1295,6 +1304,32 @@ mod tests { assert!(bids_pos < body_close_pos, "bids must appear before "); } + #[test] + fn injects_ts_bids_only_once_with_multiple_body_elements() { + let bids_script = + r#""#; + let state = std::sync::Arc::new(std::sync::RwLock::new(Some(bids_script.to_string()))); + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: None, + ad_bids_state: state, + }; + let mut processor = create_html_processor(config); + // Malformed HTML with two elements (common in CMS template pages) + let output = processor + .process_chunk(b"content", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert_eq!( + html.matches("window.__ts_bids").count(), + 1, + "should inject __ts_bids exactly once even with multiple elements" + ); + } + #[test] fn injects_empty_ts_bids_when_state_is_none() { let state = std::sync::Arc::new(std::sync::RwLock::new(None)); From eccfd4538547ddb71b2761669fa7e053d88b4cb0 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 6 May 2026 21:10:45 +0530 Subject: [PATCH 34/67] Fix max-age cookie issue -> no-store --- crates/trusted-server-core/src/publisher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 193f702c..c7744ed2 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -668,7 +668,7 @@ pub async fn handle_publisher_request( }; if ad_slots_script.is_some() { - response.set_header(header::CACHE_CONTROL, "private, max-age=0"); + response.set_header(header::CACHE_CONTROL, "private, no-store"); response.remove_header("surrogate-control"); response.remove_header("fastly-surrogate-control"); } From 5bb12d08257da12d3bfa43c85394ee4f4b6198e0 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 7 May 2026 13:02:30 +0530 Subject: [PATCH 35/67] Add /__ts/page-bids endpoint for pushState/replaceState --- .../js/lib/src/integrations/gpt/index.test.ts | 16 +- crates/js/lib/src/integrations/gpt/index.ts | 159 ++++++++++++++---- .../trusted-server-adapter-fastly/src/main.rs | 14 +- crates/trusted-server-core/src/publisher.rs | 150 ++++++++++++++++- 4 files changed, 301 insertions(+), 38 deletions(-) diff --git a/crates/js/lib/src/integrations/gpt/index.test.ts b/crates/js/lib/src/integrations/gpt/index.test.ts index e908a201..4d501ae3 100644 --- a/crates/js/lib/src/integrations/gpt/index.test.ts +++ b/crates/js/lib/src/integrations/gpt/index.test.ts @@ -13,6 +13,9 @@ type TestWindow = Window & { __ts_ad_slots?: unknown; __ts_bids?: unknown; __tsAdInit?: () => void; + __tsPrevGptSlots?: unknown; + __tsServicesEnabled?: boolean; + __tsSpaHookInstalled?: boolean; }; describe('installTsAdInit', () => { @@ -21,6 +24,9 @@ describe('installTsAdInit', () => { delete (window as TestWindow).__ts_ad_slots; delete (window as TestWindow).__ts_bids; delete (window as TestWindow).__tsAdInit; + delete (window as TestWindow).__tsPrevGptSlots; + delete (window as TestWindow).__tsSpaHookInstalled; + (window as TestWindow).__tsServicesEnabled = false; // jsdom does not implement navigator.sendBeacon; polyfill it for tests if (!('sendBeacon' in navigator)) { Object.defineProperty(navigator, 'sendBeacon', { @@ -203,7 +209,15 @@ describe('installTsAdInit', () => { pubads: vi.fn().mockReturnValue(mockPubads), enableServices: vi.fn(), }; - (window as TestWindow).__ts_ad_slots = []; + (window as TestWindow).__ts_ad_slots = [ + { + id: 'atf', + gam_unit_path: '/123/atf', + div_id: 'atf', + formats: [[300, 250]], + targeting: {}, + }, + ]; (window as TestWindow).__ts_bids = {}; const { installTsAdInit } = await import('./index'); diff --git a/crates/js/lib/src/integrations/gpt/index.ts b/crates/js/lib/src/integrations/gpt/index.ts index ffb4a687..06bc7143 100644 --- a/crates/js/lib/src/integrations/gpt/index.ts +++ b/crates/js/lib/src/integrations/gpt/index.ts @@ -45,7 +45,7 @@ interface GoogleTagPubAdsService { getTargeting(key: string): string[]; enableSingleRequest(): void; addEventListener(event: string, fn: (e: SlotRenderEndedEvent) => void): void; - refresh(): void; + refresh(slots?: GoogleTagSlot[]): void; } interface GoogleTag { @@ -56,6 +56,7 @@ interface GoogleTag { size: Array, elementId: string ): GoogleTagSlot | null; + destroySlots(slots?: GoogleTagSlot[]): boolean; enableServices(): void; display(elementId: string): void; _loaded_?: boolean; @@ -202,6 +203,8 @@ type TsWindow = Window & { __ts_ad_slots?: TsAdSlot[]; __ts_bids?: Record; __tsAdInit?: () => void; + __tsPrevGptSlots?: GoogleTagSlot[]; + __tsServicesEnabled?: boolean; }; /** @@ -212,6 +215,9 @@ type TsWindow = Window & { * targeting to GPT slots, sets the `ts_initial` sentinel, registers * `slotRenderEnded` to fire both nurl and burl via sendBeacon when our * specific Prebid bid wins the GAM line item match, then calls refresh(). + * + * Idempotent: destroys previously created TS-managed slots before redefining them, + * so it is safe to call again after SPA navigation updates `__ts_ad_slots`/`__ts_bids`. */ export function installTsAdInit(): void { const w = window as TsWindow; @@ -222,46 +228,128 @@ export function installTsAdInit(): void { if (!g) return; g.cmd?.push(() => { - slots - .map((slot) => { - const gptSlot = g.defineSlot?.( - slot.gam_unit_path, - slot.formats as Array, - slot.div_id - ); - if (!gptSlot) return null; - gptSlot.addService(g.pubads!()); - Object.entries(slot.targeting ?? {}).forEach(([k, v]) => gptSlot.setTargeting(k, v)); - const bid = bids[slot.id] ?? {}; - (['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { - if (bid[key]) gptSlot.setTargeting(key, bid[key]!); - }); - gptSlot.setTargeting('ts_initial', '1'); - return { id: slot.id, gptSlot }; - }) - .filter(Boolean); - - g.pubads!().enableSingleRequest(); - g.enableServices?.(); - - g.pubads!().addEventListener?.('slotRenderEnded', (event: SlotRenderEndedEvent) => { - const slotId: string = event.slot?.getSlotElementId?.() ?? ''; - const bid = bids[slotId] ?? {}; - const ourBidWon = - !event.isEmpty && - bid.hb_adid && - event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid; - if (ourBidWon) { - if (bid.nurl) navigator.sendBeacon(bid.nurl); - if (bid.burl) navigator.sendBeacon(bid.burl); - } + // Destroy previously defined TS slots before redefining for the new page. + if (w.__tsPrevGptSlots && w.__tsPrevGptSlots.length > 0) { + g.destroySlots?.(w.__tsPrevGptSlots); + w.__tsPrevGptSlots = []; + } + + const newSlots: GoogleTagSlot[] = []; + + slots.forEach((slot) => { + const gptSlot = g.defineSlot?.( + slot.gam_unit_path, + slot.formats as Array, + slot.div_id + ); + if (!gptSlot) return; + gptSlot.addService(g.pubads!()); + Object.entries(slot.targeting ?? {}).forEach(([k, v]) => gptSlot.setTargeting(k, v)); + const bid = bids[slot.id] ?? {}; + (['hb_pb', 'hb_bidder', 'hb_adid'] as const).forEach((key) => { + if (bid[key]) gptSlot.setTargeting(key, bid[key]!); + }); + gptSlot.setTargeting('ts_initial', '1'); + newSlots.push(gptSlot); }); - g.pubads!().refresh(); + w.__tsPrevGptSlots = newSlots; + + // enableSingleRequest and enableServices must only be called once per page load. + if (!w.__tsServicesEnabled) { + g.pubads!().enableSingleRequest(); + g.enableServices?.(); + w.__tsServicesEnabled = true; + + g.pubads!().addEventListener?.('slotRenderEnded', (event: SlotRenderEndedEvent) => { + const slotId: string = event.slot?.getSlotElementId?.() ?? ''; + const bid = (w.__ts_bids ?? {})[slotId] ?? {}; + const ourBidWon = + !event.isEmpty && + bid.hb_adid && + event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid; + if (ourBidWon) { + if (bid.nurl) navigator.sendBeacon(bid.nurl); + if (bid.burl) navigator.sendBeacon(bid.burl); + } + }); + } + + if (newSlots.length > 0) { + g.pubads!().refresh(newSlots); + } }); }; } +interface PageBidsResponse { + slots: TsAdSlot[]; + bids: Record; +} + +/** + * Install SPA navigation hook. + * + * Patches `history.pushState` and `history.replaceState`, and listens to + * `popstate`, so that after each client-side route change the trusted server + * fetches fresh slots + bids from `/__ts/page-bids?path=`, updates + * `window.__ts_ad_slots` / `window.__ts_bids`, and calls `window.__tsAdInit()`. + * + * Idempotent: guarded by `window.__tsSpaHookInstalled` so multiple calls are safe. + */ +export function installSpaAuctionHook(): void { + if (typeof window === 'undefined') return; + const win = window as TsWindow & { __tsSpaHookInstalled?: boolean }; + if (win.__tsSpaHookInstalled) return; + win.__tsSpaHookInstalled = true; + + let inflight: AbortController | null = null; + + async function onNavigate(path: string): Promise { + inflight?.abort(); + const controller = new AbortController(); + inflight = controller; + + try { + const res = await fetch(`/__ts/page-bids?path=${encodeURIComponent(path)}`, { + credentials: 'include', + signal: controller.signal, + }); + if (!res.ok) return; + const data = (await res.json()) as PageBidsResponse; + win.__ts_ad_slots = data.slots; + win.__ts_bids = data.bids; + win.__tsAdInit?.(); + } catch (err) { + if (err instanceof DOMException && err.name === 'AbortError') return; + log.warn('SPA auction hook: fetch failed', err); + } + } + + function patchHistoryMethod(method: 'pushState' | 'replaceState'): void { + const original = history[method].bind(history); + history[method] = function ( + state: unknown, + unused: string, + url?: string | URL | null + ): void { + const prevPath = location.pathname; + original(state, unused, url); + const newPath = url ? new URL(String(url), location.href).pathname : location.pathname; + if (newPath !== prevPath) { + void onNavigate(newPath); + } + }; + } + + patchHistoryMethod('pushState'); + patchHistoryMethod('replaceState'); + + window.addEventListener('popstate', () => { + void onNavigate(location.pathname); + }); +} + /** * Register the slim-Prebid lazy loader. Fires after window.load — off the * critical path. slim-Prebid handles refresh auctions and userID module @@ -300,4 +388,5 @@ if (typeof window !== 'undefined') { } installTsAdInit(); + installSpaAuctionHook(); } diff --git a/crates/trusted-server-adapter-fastly/src/main.rs b/crates/trusted-server-adapter-fastly/src/main.rs index 74414220..55af1468 100644 --- a/crates/trusted-server-adapter-fastly/src/main.rs +++ b/crates/trusted-server-adapter-fastly/src/main.rs @@ -19,7 +19,8 @@ use trusted_server_core::proxy::{ handle_first_party_proxy_sign, }; use trusted_server_core::publisher::{ - handle_publisher_request, handle_tsjs_dynamic, stream_publisher_body, PublisherResponse, + handle_page_bids, handle_publisher_request, handle_tsjs_dynamic, stream_publisher_body, + PublisherResponse, }; use trusted_server_core::request_signing::{ handle_deactivate_key, handle_rotate_key, handle_trusted_server_discovery, @@ -194,6 +195,17 @@ async fn route_request( } } + // SPA/CSR navigation endpoint — returns slots + bids JSON for the given path + (Method::GET, "/__ts/page-bids") => { + match runtime_services_for_consent_route(settings, runtime_services) { + Ok(publisher_services) => { + handle_page_bids(settings, orchestrator, &publisher_services, slots_file, req) + .await + } + Err(e) => Err(e), + } + } + // tsjs endpoints (Method::GET, "/first-party/proxy") => { handle_first_party_proxy(settings, runtime_services, req).await diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index c7744ed2..ec4f4a22 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -668,7 +668,7 @@ pub async fn handle_publisher_request( }; if ad_slots_script.is_some() { - response.set_header(header::CACHE_CONTROL, "private, no-store"); + response.set_header(header::CACHE_CONTROL, "private, max-age=0"); response.remove_header("surrogate-control"); response.remove_header("fastly-surrogate-control"); } @@ -990,6 +990,154 @@ fn apply_ec_headers( } } +/// Handle `GET /__ts/page-bids?path=` — server-side auction for SPA navigation. +/// +/// Matches creative opportunity slots for the given path, runs a server-side +/// auction (APS + PBS), and returns the slot definitions and winning bids as JSON. +/// Called by the client-side SPA navigation hook after `pushState` / `popstate`. +/// +/// # Errors +/// +/// Returns [`TrustedServerError`] if cookie parsing or EC ID generation fails. +pub async fn handle_page_bids( + settings: &Settings, + orchestrator: &AuctionOrchestrator, + services: &RuntimeServices, + slots_file: &crate::creative_opportunities::CreativeOpportunitiesFile, + req: Request, +) -> Result> { + let Some(co_config) = &settings.creative_opportunities else { + return Ok(Response::from_status(StatusCode::NOT_FOUND) + .with_body_text_plain("Creative opportunities not configured")); + }; + + let path_param = req + .get_url() + .query_pairs() + .find(|(k, _)| k == "path") + .map(|(_, v)| v.into_owned()) + .unwrap_or_else(|| "/".to_string()); + + let matched_slots: Vec<_> = + crate::creative_opportunities::match_slots(&slots_file.slots, &path_param) + .into_iter() + .cloned() + .collect(); + + let request_info = crate::http_util::RequestInfo::from_request(&req, &services.client_info); + let cookie_jar = handle_request_cookies(&req)?; + let ec_id = get_or_generate_ec_id(settings, services, &req)?; + let geo = services + .geo() + .lookup(services.client_info.client_ip) + .unwrap_or_else(|e| { + log::warn!("geo lookup failed: {e}"); + None + }); + let consent_context = build_consent_context(&ConsentPipelineInput { + jar: cookie_jar.as_ref(), + req: &req, + config: &settings.consent, + geo: geo.as_ref(), + ec_id: Some(ec_id.as_str()), + kv_store: settings + .consent + .consent_store + .as_deref() + .map(|_| services.kv_store()), + }); + + let consent_allows_auction = consent_context + .tcf + .as_ref() + .is_some_and(|tcf| tcf.has_purpose_consent(1)); + + let winning_bids = if !matched_slots.is_empty() && consent_allows_auction { + let mut auction_request = build_auction_request( + &matched_slots, + &ec_id, + &consent_context, + &request_info, + co_config, + ); + let page_url = format!( + "{}://{}{}", + request_info.scheme, request_info.host, path_param + ); + auction_request.publisher.page_url = Some(page_url.clone()); + if let Some(ref mut site) = auction_request.site { + site.page = page_url; + } + let timeout_ms = co_config + .auction_timeout_ms + .unwrap_or(settings.auction.timeout_ms); + let placeholder_req = fastly::Request::get("https://placeholder.invalid/"); + let auction_context = AuctionContext { + settings, + request: &placeholder_req, + client_info: services.client_info(), + timeout_ms, + provider_responses: None, + services, + }; + match orchestrator + .run_auction(&auction_request, &auction_context, services) + .await + { + Ok(result) => result.winning_bids, + Err(e) => { + log::warn!("page-bids auction failed: {e:?}"); + std::collections::HashMap::new() + } + } + } else { + std::collections::HashMap::new() + }; + + let bid_map = build_bid_map(&winning_bids, co_config.price_granularity); + + let slots_json: Vec = matched_slots + .iter() + .map(|slot| { + let gam_path = slot.resolved_gam_unit_path(&co_config.gam_network_id); + let div_id = slot.resolved_div_id(); + let formats: Vec = slot + .formats + .iter() + .map(|f| serde_json::json!([f.width, f.height])) + .collect(); + let targeting: serde_json::Map = slot + .targeting + .iter() + .map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone()))) + .collect(); + serde_json::json!({ + "id": slot.id, + "gam_unit_path": gam_path, + "div_id": div_id, + "formats": formats, + "targeting": targeting, + }) + }) + .collect(); + + let body = serde_json::json!({ + "slots": slots_json, + "bids": bid_map, + }); + + let json_str = serde_json::to_string(&body).change_context(TrustedServerError::Proxy { + message: "Failed to serialize page-bids response".to_string(), + })?; + + let mut response = Response::from_status(StatusCode::OK); + response.set_header(header::CONTENT_TYPE, "application/json"); + response.set_header(header::CACHE_CONTROL, "private, no-store"); + response.set_body(json_str); + + Ok(response) +} + #[cfg(test)] mod tests { use super::*; From 982fa3edbf8ed881797c6dff4aacd51d2878d68b Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 7 May 2026 13:04:19 +0530 Subject: [PATCH 36/67] Fix format ts --- crates/js/lib/src/integrations/gpt/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/js/lib/src/integrations/gpt/index.ts b/crates/js/lib/src/integrations/gpt/index.ts index 06bc7143..bf9fc99d 100644 --- a/crates/js/lib/src/integrations/gpt/index.ts +++ b/crates/js/lib/src/integrations/gpt/index.ts @@ -328,11 +328,7 @@ export function installSpaAuctionHook(): void { function patchHistoryMethod(method: 'pushState' | 'replaceState'): void { const original = history[method].bind(history); - history[method] = function ( - state: unknown, - unused: string, - url?: string | URL | null - ): void { + history[method] = function (state: unknown, unused: string, url?: string | URL | null): void { const prevPath = location.pathname; original(state, unused, url); const newPath = url ? new URL(String(url), location.href).pathname : location.pathname; From 77d3c4a2e92f7d098c901322637463657bdb01ee Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 7 May 2026 15:46:49 +0530 Subject: [PATCH 37/67] =?UTF-8?q?=5F=5FtsDivToSlotId=20now=20replaced=20pe?= =?UTF-8?q?r=20navigation=20(not=20merged)=20=E2=80=94=20stale=20div=5Fid?= =?UTF-8?q?=20entries=20from=20destroyed=20slots=20no=20longer=20persist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/lib/src/integrations/gpt/index.test.ts | 138 ++++++++++++++++-- crates/js/lib/src/integrations/gpt/index.ts | 17 ++- 2 files changed, 138 insertions(+), 17 deletions(-) diff --git a/crates/js/lib/src/integrations/gpt/index.test.ts b/crates/js/lib/src/integrations/gpt/index.test.ts index 4d501ae3..87455591 100644 --- a/crates/js/lib/src/integrations/gpt/index.test.ts +++ b/crates/js/lib/src/integrations/gpt/index.test.ts @@ -16,6 +16,7 @@ type TestWindow = Window & { __tsPrevGptSlots?: unknown; __tsServicesEnabled?: boolean; __tsSpaHookInstalled?: boolean; + __tsDivToSlotId?: Record; }; describe('installTsAdInit', () => { @@ -26,6 +27,7 @@ describe('installTsAdInit', () => { delete (window as TestWindow).__tsAdInit; delete (window as TestWindow).__tsPrevGptSlots; delete (window as TestWindow).__tsSpaHookInstalled; + delete (window as TestWindow).__tsDivToSlotId; (window as TestWindow).__tsServicesEnabled = false; // jsdom does not implement navigator.sendBeacon; polyfill it for tests if (!('sendBeacon' in navigator)) { @@ -41,7 +43,7 @@ describe('installTsAdInit', () => { const mockSlot = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), - getSlotElementId: vi.fn().mockReturnValue('atf'), + getSlotElementId: vi.fn().mockReturnValue('div-atf-sidebar'), getTargeting: vi.fn().mockReturnValue(['abc']), }; const mockPubads = { @@ -57,15 +59,15 @@ describe('installTsAdInit', () => { }; (window as TestWindow).__ts_ad_slots = [ { - id: 'atf', + id: 'atf_sidebar_ad', gam_unit_path: '/123/atf', - div_id: 'atf', + div_id: 'div-atf-sidebar', formats: [[300, 250]], targeting: { pos: 'atf' }, }, ]; (window as TestWindow).__ts_bids = { - atf: { + atf_sidebar_ad: { hb_pb: '1.00', hb_bidder: 'kargo', hb_adid: 'abc', @@ -96,7 +98,7 @@ describe('installTsAdInit', () => { const mockSlot = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), - getSlotElementId: vi.fn().mockReturnValue('atf'), + getSlotElementId: vi.fn().mockReturnValue('div-atf-sidebar'), getTargeting: vi.fn().mockReturnValue(['abc']), }; const mockPubads = { @@ -114,15 +116,15 @@ describe('installTsAdInit', () => { }; (window as TestWindow).__ts_ad_slots = [ { - id: 'atf', + id: 'atf_sidebar_ad', gam_unit_path: '/123/atf', - div_id: 'atf', + div_id: 'div-atf-sidebar', formats: [[300, 250]], targeting: {}, }, ]; (window as TestWindow).__ts_bids = { - atf: { + atf_sidebar_ad: { hb_pb: '1.00', hb_bidder: 'kargo', hb_adid: 'abc', @@ -143,6 +145,64 @@ describe('installTsAdInit', () => { beaconSpy.mockRestore(); }); + it('fires beacons for APS bid (no hb_adid) when ad renders in our slot', async () => { + const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); + let capturedListener: ((e: SlotRenderEvent) => void) | undefined; + + const mockSlot = { + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + getSlotElementId: vi.fn().mockReturnValue('div-atf-sidebar'), + getTargeting: vi.fn().mockReturnValue([]), + }; + const mockPubads = { + enableSingleRequest: vi.fn(), + refresh: vi.fn(), + addEventListener: vi.fn((event: string, fn: (e: SlotRenderEvent) => void) => { + if (event === 'slotRenderEnded') capturedListener = fn; + }), + }; + (window as TestWindow).googletag = { + cmd: { push: vi.fn((fn: () => void) => fn()) }, + defineSlot: vi.fn().mockReturnValue(mockSlot), + pubads: vi.fn().mockReturnValue(mockPubads), + enableServices: vi.fn(), + }; + (window as TestWindow).__ts_ad_slots = [ + { + id: 'atf_sidebar_ad', + gam_unit_path: '/123/atf', + div_id: 'div-atf-sidebar', + formats: [[300, 250]], + targeting: {}, + }, + ]; + (window as TestWindow).__ts_bids = { + atf_sidebar_ad: { + hb_pb: '1.50', + hb_bidder: 'aps', + nurl: 'https://aps/win', + burl: 'https://aps/bill', + }, + }; + + const { installTsAdInit } = await import('./index'); + installTsAdInit(); + (window as TestWindow).__tsAdInit!(); + + expect(capturedListener).toBeDefined(); + capturedListener!({ isEmpty: false, slot: mockSlot }); + + expect(beaconSpy).toHaveBeenCalledWith('https://aps/win'); + expect(beaconSpy).toHaveBeenCalledWith('https://aps/bill'); + + beaconSpy.mockClear(); + capturedListener!({ isEmpty: true, slot: mockSlot }); + expect(beaconSpy).not.toHaveBeenCalled(); + + beaconSpy.mockRestore(); + }); + it('does not fire nurl/burl when bid did not win GAM line item', async () => { const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); let capturedListener: ((e: SlotRenderEvent) => void) | undefined; @@ -150,7 +210,7 @@ describe('installTsAdInit', () => { const mockSlotNoMatch = { addService: vi.fn().mockReturnThis(), setTargeting: vi.fn().mockReturnThis(), - getSlotElementId: vi.fn().mockReturnValue('atf'), + getSlotElementId: vi.fn().mockReturnValue('div-atf-sidebar'), getTargeting: vi.fn().mockReturnValue(['OTHER_BID_ID']), }; const mockPubads = { @@ -168,15 +228,15 @@ describe('installTsAdInit', () => { }; (window as TestWindow).__ts_ad_slots = [ { - id: 'atf', + id: 'atf_sidebar_ad', gam_unit_path: '/123/atf', - div_id: 'atf', + div_id: 'div-atf-sidebar', formats: [[300, 250]], targeting: {}, }, ]; (window as TestWindow).__ts_bids = { - atf: { + atf_sidebar_ad: { hb_pb: '1.00', hb_bidder: 'kargo', hb_adid: 'abc', @@ -194,6 +254,56 @@ describe('installTsAdInit', () => { beaconSpy.mockRestore(); }); + it('does not fire beacons for slotRenderEnded on slots not owned by TS', async () => { + const beaconSpy = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true); + let capturedListener: ((e: SlotRenderEvent) => void) | undefined; + + const mockSlot = { + addService: vi.fn().mockReturnThis(), + setTargeting: vi.fn().mockReturnThis(), + getSlotElementId: vi.fn().mockReturnValue('div-atf-sidebar'), + getTargeting: vi.fn().mockReturnValue(['abc']), + }; + const arenaSlot = { + getSlotElementId: () => 'arena-owned-div', + getTargeting: () => [], + }; + const mockPubads = { + enableSingleRequest: vi.fn(), + refresh: vi.fn(), + addEventListener: vi.fn((event: string, fn: (e: SlotRenderEvent) => void) => { + if (event === 'slotRenderEnded') capturedListener = fn; + }), + }; + (window as TestWindow).googletag = { + cmd: { push: vi.fn((fn: () => void) => fn()) }, + defineSlot: vi.fn().mockReturnValue(mockSlot), + pubads: vi.fn().mockReturnValue(mockPubads), + enableServices: vi.fn(), + }; + (window as TestWindow).__ts_ad_slots = [ + { + id: 'atf_sidebar_ad', + gam_unit_path: '/123/atf', + div_id: 'div-atf-sidebar', + formats: [[300, 250]], + targeting: {}, + }, + ]; + (window as TestWindow).__ts_bids = { + atf_sidebar_ad: { hb_pb: '1.00', hb_bidder: 'kargo', hb_adid: 'abc' }, + }; + + const { installTsAdInit } = await import('./index'); + installTsAdInit(); + (window as TestWindow).__tsAdInit!(); + + capturedListener!({ isEmpty: false, slot: arenaSlot }); + + expect(beaconSpy).not.toHaveBeenCalled(); + beaconSpy.mockRestore(); + }); + it('calls refresh even when __ts_bids is empty (graceful fallback)', async () => { const mockPubads = { enableSingleRequest: vi.fn(), @@ -211,9 +321,9 @@ describe('installTsAdInit', () => { }; (window as TestWindow).__ts_ad_slots = [ { - id: 'atf', + id: 'atf_sidebar_ad', gam_unit_path: '/123/atf', - div_id: 'atf', + div_id: 'div-atf-sidebar', formats: [[300, 250]], targeting: {}, }, diff --git a/crates/js/lib/src/integrations/gpt/index.ts b/crates/js/lib/src/integrations/gpt/index.ts index bf9fc99d..fee79c1b 100644 --- a/crates/js/lib/src/integrations/gpt/index.ts +++ b/crates/js/lib/src/integrations/gpt/index.ts @@ -205,6 +205,7 @@ type TsWindow = Window & { __tsAdInit?: () => void; __tsPrevGptSlots?: GoogleTagSlot[]; __tsServicesEnabled?: boolean; + __tsDivToSlotId?: Record; }; /** @@ -235,6 +236,7 @@ export function installTsAdInit(): void { } const newSlots: GoogleTagSlot[] = []; + const divToSlotId: Record = {}; slots.forEach((slot) => { const gptSlot = g.defineSlot?.( @@ -250,10 +252,13 @@ export function installTsAdInit(): void { if (bid[key]) gptSlot.setTargeting(key, bid[key]!); }); gptSlot.setTargeting('ts_initial', '1'); + divToSlotId[slot.div_id] = slot.id; newSlots.push(gptSlot); }); w.__tsPrevGptSlots = newSlots; + // Replace (not merge) so destroyed slots from previous navigation don't linger. + w.__tsDivToSlotId = divToSlotId; // enableSingleRequest and enableServices must only be called once per page load. if (!w.__tsServicesEnabled) { @@ -262,12 +267,18 @@ export function installTsAdInit(): void { w.__tsServicesEnabled = true; g.pubads!().addEventListener?.('slotRenderEnded', (event: SlotRenderEndedEvent) => { - const slotId: string = event.slot?.getSlotElementId?.() ?? ''; + const divId: string = event.slot?.getSlotElementId?.() ?? ''; + const slotId = (w.__tsDivToSlotId ?? {})[divId]; + if (!slotId) return; const bid = (w.__ts_bids ?? {})[slotId] ?? {}; + // Prebid: compare hb_adid targeting to verify the specific creative won. + // APS: no hb_adid equivalent — fires if bidder exists and slot is non-empty. + // Known limitation: APS path may over-fire if a non-APS line item wins. const ourBidWon = !event.isEmpty && - bid.hb_adid && - event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid; + (bid.hb_adid + ? event.slot?.getTargeting?.('hb_adid')?.[0] === bid.hb_adid + : !!bid.hb_bidder); if (ourBidWon) { if (bid.nurl) navigator.sendBeacon(bid.nurl); if (bid.burl) navigator.sendBeacon(bid.burl); From 38c8bf17701dea1a76ba1344620f726a0a18711b Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 7 May 2026 17:20:49 +0530 Subject: [PATCH 38/67] Update timeout for mocktioneer --- trusted-server.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/trusted-server.toml b/trusted-server.toml index 43e090fe..d17e8647 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -172,7 +172,7 @@ allowed_context_keys = ["permutive_segments"] enabled = true pub_id = "test-pub" endpoint = "https://origin-mocktioneer.cdintel.com/e/dtb/bid" -timeout_ms = 1000 +timeout_ms = 400 [integrations.google_tag_manager] enabled = false @@ -182,7 +182,7 @@ container_id = "GTM-XXXXXX" [integrations.adserver_mock] enabled = true endpoint = "https://origin-mocktioneer.cdintel.com/adserver/mediate" -timeout_ms = 1000 +timeout_ms = 400 # Map auction-request context keys to mediation URL query parameters. # Each key is a context key from the JS client; the value becomes the @@ -192,6 +192,6 @@ permutive_segments = "permutive" [creative_opportunities] gam_network_id = "88059007" -auction_timeout_ms = 1500 +auction_timeout_ms = 500 price_granularity = "dense" From e32bfa556e99d1fb225876c286e2fb7164317c9f Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 7 May 2026 17:28:59 +0530 Subject: [PATCH 39/67] Revert with updated tiomeout --- trusted-server.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/trusted-server.toml b/trusted-server.toml index d17e8647..43e090fe 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -172,7 +172,7 @@ allowed_context_keys = ["permutive_segments"] enabled = true pub_id = "test-pub" endpoint = "https://origin-mocktioneer.cdintel.com/e/dtb/bid" -timeout_ms = 400 +timeout_ms = 1000 [integrations.google_tag_manager] enabled = false @@ -182,7 +182,7 @@ container_id = "GTM-XXXXXX" [integrations.adserver_mock] enabled = true endpoint = "https://origin-mocktioneer.cdintel.com/adserver/mediate" -timeout_ms = 400 +timeout_ms = 1000 # Map auction-request context keys to mediation URL query parameters. # Each key is a context key from the JS client; the value becomes the @@ -192,6 +192,6 @@ permutive_segments = "permutive" [creative_opportunities] gam_network_id = "88059007" -auction_timeout_ms = 500 +auction_timeout_ms = 1500 price_granularity = "dense" From b1e74c986ec44f78053d4ef2dbadfc34697bb824 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Sat, 9 May 2026 14:45:34 +0530 Subject: [PATCH 40/67] Wip: Align with the spec --- .../trusted-server-adapter-fastly/src/main.rs | 18 +- .../src/auction/orchestrator.rs | 338 ++++++++++++++++++ .../src/creative_opportunities.rs | 16 +- .../trusted-server-core/src/html_processor.rs | 47 ++- crates/trusted-server-core/src/publisher.rs | 329 +++++++++++++++-- trusted-server.toml | 6 + 6 files changed, 710 insertions(+), 44 deletions(-) diff --git a/crates/trusted-server-adapter-fastly/src/main.rs b/crates/trusted-server-adapter-fastly/src/main.rs index 55af1468..895299f5 100644 --- a/crates/trusted-server-adapter-fastly/src/main.rs +++ b/crates/trusted-server-adapter-fastly/src/main.rs @@ -19,7 +19,7 @@ use trusted_server_core::proxy::{ handle_first_party_proxy_sign, }; use trusted_server_core::publisher::{ - handle_page_bids, handle_publisher_request, handle_tsjs_dynamic, stream_publisher_body, + handle_page_bids, handle_publisher_request, handle_tsjs_dynamic, stream_publisher_body_async, PublisherResponse, }; use trusted_server_core::request_signing::{ @@ -250,18 +250,26 @@ async fn route_request( Ok(PublisherResponse::Stream { mut response, body, - params, + mut params, }) => { // Streaming path: finalize headers, then stream body to client. + // TTFB happens at stream_to_client() — SSP bids are already + // in-flight in Fastly's native layer (dispatched before origin wait). finalize_response(settings, geo_info.as_ref(), &mut response); let mut streaming_body = response.stream_to_client(); - if let Err(e) = stream_publisher_body( + // stream_publisher_body_async falls back to the sync path + // when no auction was dispatched (dispatched_auction is None). + let stream_result = stream_publisher_body_async( body, &mut streaming_body, - ¶ms, + &mut *params, settings, integration_registry, - ) { + orchestrator, + &publisher_services, + ) + .await; + if let Err(e) = stream_result { // Headers already committed. Log and abort — client // sees a truncated response. Standard proxy behavior. log::error!("Streaming processing failed: {e:?}"); diff --git a/crates/trusted-server-core/src/auction/orchestrator.rs b/crates/trusted-server-core/src/auction/orchestrator.rs index 0a52b07c..953ed6a0 100644 --- a/crates/trusted-server-core/src/auction/orchestrator.rs +++ b/crates/trusted-server-core/src/auction/orchestrator.rs @@ -13,6 +13,23 @@ use super::config::AuctionConfig; use super::provider::AuctionProvider; use super::types::{AuctionContext, AuctionRequest, AuctionResponse, Bid, BidStatus}; +/// In-flight auction requests dispatched to SSP backends. +/// +/// Created by [`AuctionOrchestrator::dispatch_auction`] and consumed by +/// [`AuctionOrchestrator::collect_dispatched_auction`]. Carrying this handle +/// across `pending_origin.wait()` lets origin response and SSP HTTP requests +/// race in Fastly's native layer, enabling TTFB ≈ origin latency rather than +/// TTFB ≈ auction timeout. +pub struct DispatchedAuction { + pending_requests: Vec, + backend_to_provider: HashMap)>, + auction_start: Instant, + timeout_ms: u32, + floor_prices: HashMap, + /// Carried so the mediator call in collect can pass it as the auction request. + request: AuctionRequest, +} + /// Compute the remaining time budget from a deadline. /// /// Returns the number of milliseconds left before `timeout_ms` is exceeded, @@ -584,6 +601,327 @@ impl AuctionOrchestrator { }) } + /// Dispatch SSP bid requests without blocking WASM. + /// + /// Calls each enabled provider's [`AuctionProvider::request_bids`] (which + /// internally calls Fastly's `send_async`), then returns immediately with a + /// [`DispatchedAuction`] token. The Fastly host begins the SSP round-trips + /// while WASM continues to `pending_origin.wait()`. + /// + /// Returns `None` when no providers are configured or all providers are + /// disabled / over budget. The caller should fall back to the synchronous + /// `run_auction` path. + #[must_use] + pub fn dispatch_auction( + &self, + request: &AuctionRequest, + context: &AuctionContext<'_>, + ) -> Option { + let provider_names = self.config.provider_names(); + if provider_names.is_empty() { + return None; + } + + let auction_start = Instant::now(); + let mut backend_to_provider: HashMap)> = + HashMap::new(); + let mut pending_requests: Vec = Vec::new(); + + for provider_name in provider_names { + let provider = match self.providers.get(provider_name) { + Some(p) => p, + None => { + log::warn!("Provider '{}' not registered, skipping", provider_name); + continue; + } + }; + + if !provider.is_enabled() { + log::debug!("Provider '{}' is disabled, skipping", provider.provider_name()); + continue; + } + + let remaining_ms = remaining_budget_ms(auction_start, context.timeout_ms); + let effective_timeout = remaining_ms.min(provider.timeout_ms()); + + if effective_timeout == 0 { + log::warn!( + "Auction timeout ({}ms) exhausted before launching '{}' — skipping", + context.timeout_ms, + provider.provider_name() + ); + continue; + } + + let backend_name = match provider.backend_name(effective_timeout) { + Some(name) => name, + None => { + log::warn!("Provider '{}' has no backend_name, skipping", provider.provider_name()); + continue; + } + }; + + let provider_context = AuctionContext { + settings: context.settings, + request: context.request, + client_info: context.client_info, + timeout_ms: effective_timeout, + provider_responses: context.provider_responses, + services: context.services, + }; + + let start_time = Instant::now(); + match provider.request_bids(request, &provider_context) { + Ok(pending) => { + log::info!( + "Dispatching bid request to '{}' (backend: {}, budget: {}ms)", + provider.provider_name(), + backend_name, + effective_timeout + ); + backend_to_provider.insert( + backend_name.clone(), + (provider.provider_name().to_string(), start_time, Arc::clone(provider)), + ); + pending_requests + .push(PlatformPendingRequest::new(pending).with_backend_name(backend_name)); + } + Err(e) => { + log::warn!( + "Provider '{}' failed to dispatch request: {:?}", + provider.provider_name(), + e + ); + } + } + } + + if pending_requests.is_empty() { + return None; + } + + log::info!( + "Dispatched {} SSP requests (timeout: {}ms); Fastly host will race them against origin", + pending_requests.len(), + context.timeout_ms + ); + + Some(DispatchedAuction { + pending_requests, + backend_to_provider, + auction_start, + timeout_ms: context.timeout_ms, + floor_prices: self.floor_prices_by_slot(request), + request: request.clone(), + }) + } + + /// Collect bid responses from a previously-dispatched auction. + /// + /// Runs the select-loop phase (equivalent to Phase 2 of + /// `run_providers_parallel`) and, if the orchestrator has a mediator + /// configured, forwards collected bids to it. The overall auction deadline + /// is enforced from `dispatched.auction_start`. + /// + /// On any error or partial failure the method returns the best available + /// result rather than propagating — the caller should still inject the + /// winning bids even if some providers timed out. + pub async fn collect_dispatched_auction( + &self, + dispatched: DispatchedAuction, + services: &RuntimeServices, + context: &AuctionContext<'_>, + ) -> OrchestrationResult { + let DispatchedAuction { + pending_requests, + mut backend_to_provider, + auction_start, + timeout_ms, + floor_prices, + request, + } = dispatched; + + let deadline = Duration::from_millis(u64::from(timeout_ms)); + + log::info!( + "Collecting {} in-flight SSP responses (timeout: {}ms remaining: {}ms)", + pending_requests.len(), + timeout_ms, + remaining_budget_ms(auction_start, timeout_ms), + ); + + let mut responses: Vec = Vec::new(); + let mut remaining = pending_requests; + + while !remaining.is_empty() { + let select_result = match services + .http_client() + .select(remaining) + .await + .change_context(TrustedServerError::Auction { + message: "HTTP select failed".to_string(), + }) { + Ok(r) => r, + Err(e) => { + log::warn!("select() failed during auction collection: {:?}", e); + break; + } + }; + remaining = select_result.remaining; + + match select_result.ready { + Ok(platform_response) => { + let backend_name = platform_response.backend_name.clone().unwrap_or_default(); + if let Some((provider_name, start_time, provider)) = + backend_to_provider.remove(&backend_name) + { + let response_time_ms = start_time.elapsed().as_millis() as u64; + match platform_response_to_fastly(platform_response) { + Ok(response) => match provider.parse_response(response, response_time_ms) { + Ok(auction_response) => { + log::info!( + "Provider '{}' returned {} bids ({}ms)", + auction_response.provider, + auction_response.bids.len(), + auction_response.response_time_ms + ); + responses.push(auction_response); + } + Err(e) => { + log::warn!("Provider '{}' parse failed: {:?}", provider_name, e); + responses.push(AuctionResponse::error(&provider_name, response_time_ms)); + } + }, + Err(e) => { + log::warn!("Provider '{}' unsupported body: {:?}", provider_name, e); + responses.push(AuctionResponse::error(&provider_name, response_time_ms)); + } + } + } else { + log::warn!("Received response from unknown backend '{}', ignoring", backend_name); + } + } + Err(e) => { + log::warn!("A provider request failed during collection: {:?}", e); + } + } + + if auction_start.elapsed() >= deadline && !remaining.is_empty() { + log::warn!( + "Auction timeout ({}ms) reached, dropping {} remaining request(s)", + timeout_ms, + remaining.len() + ); + break; + } + } + + let (mediator_response, winning_bids) = if let Some(mediator_name) = &self.config.mediator { + match self.providers.get(mediator_name.as_str()) { + Some(mediator) => { + let remaining_ms = remaining_budget_ms(auction_start, timeout_ms); + if remaining_ms == 0 { + log::warn!("Auction timeout exhausted during bidding — skipping mediator"); + let winning = self.select_winning_bids(&responses, &floor_prices); + return OrchestrationResult { + provider_responses: responses, + mediator_response: None, + winning_bids: winning, + total_time_ms: auction_start.elapsed().as_millis() as u64, + metadata: HashMap::new(), + }; + } + let placeholder = fastly::Request::get("https://placeholder.invalid/"); + let mediator_context = AuctionContext { + settings: context.settings, + request: &placeholder, + client_info: context.client_info, + timeout_ms: remaining_ms, + provider_responses: Some(&responses), + services: context.services, + }; + match mediator.request_bids(&request, &mediator_context) { + Ok(pending) => { + let platform_resp = services + .http_client() + .wait(PlatformPendingRequest::new(pending)) + .await; + match platform_resp.change_context(TrustedServerError::Auction { + message: format!("Mediator {} request failed", mediator.provider_name()), + }) { + Ok(platform_resp) => { + match platform_response_to_fastly(platform_resp).change_context( + TrustedServerError::Auction { + message: format!("Mediator {} unsupported body", mediator.provider_name()), + }, + ) { + Ok(response) => { + let response_time_ms = + remaining_ms as u64 - remaining_budget_ms(auction_start, timeout_ms) as u64; + match mediator.parse_response(response, response_time_ms) { + Ok(mediator_resp) => { + let winning = mediator_resp + .bids + .iter() + .filter_map(|bid| { + if bid.price.is_none() { + log::warn!( + "Mediator '{}' returned bid for slot '{}' without decoded price - skipping", + mediator.provider_name(), + bid.slot_id + ); + None + } else { + Some((bid.slot_id.clone(), bid.clone())) + } + }) + .collect(); + let winning = self.apply_floor_prices(winning, &floor_prices); + (Some(mediator_resp), winning) + } + Err(e) => { + log::warn!("Mediator '{}' parse failed: {:?}", mediator.provider_name(), e); + let winning = self.select_winning_bids(&responses, &floor_prices); + (None, winning) + } + } + } + Err(e) => { + log::warn!("Mediator body error: {:?}", e); + (None, self.select_winning_bids(&responses, &floor_prices)) + } + } + } + Err(e) => { + log::warn!("Mediator request failed: {:?}", e); + (None, self.select_winning_bids(&responses, &floor_prices)) + } + } + } + Err(e) => { + log::warn!("Mediator '{}' failed to dispatch: {:?}", mediator.provider_name(), e); + (None, self.select_winning_bids(&responses, &floor_prices)) + } + } + } + None => { + log::warn!("Mediator '{}' not registered", mediator_name); + (None, self.select_winning_bids(&responses, &floor_prices)) + } + } + } else { + (None, self.select_winning_bids(&responses, &floor_prices)) + }; + + OrchestrationResult { + provider_responses: responses, + mediator_response, + winning_bids, + total_time_ms: auction_start.elapsed().as_millis() as u64, + metadata: HashMap::new(), + } + } + /// Check if orchestrator is enabled. #[must_use] pub fn is_enabled(&self) -> bool { diff --git a/crates/trusted-server-core/src/creative_opportunities.rs b/crates/trusted-server-core/src/creative_opportunities.rs index 7a4a10df..12957d4b 100644 --- a/crates/trusted-server-core/src/creative_opportunities.rs +++ b/crates/trusted-server-core/src/creative_opportunities.rs @@ -18,7 +18,21 @@ use crate::price_bucket::PriceGranularity; pub struct CreativeOpportunitiesConfig { /// GAM network ID used to build default unit paths. pub gam_network_id: String, - /// Auction timeout in milliseconds. + /// Maximum time in milliseconds to wait for the server-side auction before + /// closing the response body. + /// + /// The auction runs concurrently with HTML body streaming. Body content + /// above `` has already been delivered and painted before the hold + /// begins, so **FCP is not affected**. What this timeout bounds is the slip + /// on `DOMContentLoaded` and `window.load`: third-party scripts that hook + /// those events fire later by at most this duration. + /// + /// The worst case is a cache-hit page where the origin drains in <50 ms + /// but the auction takes the full timeout — the browser sits idle waiting + /// for ``. 500 ms is the recommended default and the hard upper + /// bound on DCL slip the publisher is willing to accept. + /// + /// When absent, falls back to `[auction].timeout_ms` from global config. #[serde(default)] pub auction_timeout_ms: Option, /// Price granularity for header-bidding price bucketing. diff --git a/crates/trusted-server-core/src/html_processor.rs b/crates/trusted-server-core/src/html_processor.rs index 86a8abe7..26978cef 100644 --- a/crates/trusted-server-core/src/html_processor.rs +++ b/crates/trusted-server-core/src/html_processor.rs @@ -292,13 +292,21 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso Ok(()) } }), - // Inject __ts_bids before via end_tag_handlers. + // Inject __ts_bids before via end_tag_handlers — only when + // slots matched this URL. When no slots matched, skip injection entirely + // so the publisher's existing client-side Prebid/GPT flow is unmodified + // (dual-mode rollout: calling __tsAdInit with empty slots would invoke + // enableSingleRequest/enableServices and conflict with the publisher's GPT init). // Guard with AtomicBool so the script is only injected once even if // the origin HTML contains multiple elements (e.g. template fragments). element!("body", { let state = ad_bids_state.clone(); let injected_bids = injected_bids.clone(); + let has_slots = ad_slots_script.is_some(); move |el| { + if !has_slots { + return Ok(()); + } let state = state.clone(); let injected_bids = injected_bids.clone(); if let Some(handlers) = el.end_tag_handlers() { @@ -1285,7 +1293,7 @@ mod tests { request_host: "example.com".to_string(), request_scheme: "https".to_string(), integrations: IntegrationRegistry::empty_for_tests(), - ad_slots_script: None, + ad_slots_script: Some("".to_string()), ad_bids_state: state, }; let mut processor = create_html_processor(config); @@ -1314,7 +1322,7 @@ mod tests { request_host: "example.com".to_string(), request_scheme: "https".to_string(), integrations: IntegrationRegistry::empty_for_tests(), - ad_slots_script: None, + ad_slots_script: Some("".to_string()), ad_bids_state: state, }; let mut processor = create_html_processor(config); @@ -1331,14 +1339,16 @@ mod tests { } #[test] - fn injects_empty_ts_bids_when_state_is_none() { + fn injects_empty_ts_bids_when_slots_matched_but_auction_returned_nothing() { + // Slots matched (ad_slots_script is Some) but auction task never wrote a result + // (state is None) — e.g. auction timed out with zero bids. Fallback to {}. let state = std::sync::Arc::new(std::sync::RwLock::new(None)); let config = HtmlProcessorConfig { origin_host: "origin.example.com".to_string(), request_host: "example.com".to_string(), request_scheme: "https".to_string(), integrations: IntegrationRegistry::empty_for_tests(), - ad_slots_script: None, + ad_slots_script: Some("".to_string()), ad_bids_state: state, }; let mut processor = create_html_processor(config); @@ -1348,7 +1358,32 @@ mod tests { let html = std::str::from_utf8(&output).expect("should be utf8"); assert!( html.contains("__ts_bids=JSON.parse(\"{}\")"), - "should inject empty bids on None state" + "should inject empty bids fallback when auction produced nothing" + ); + } + + #[test] + fn does_not_inject_ts_bids_when_no_slots_matched() { + // No slots matched this URL — ad_slots_script is None. __ts_bids must be + // omitted entirely so the publisher's existing client-side GPT flow is + // unmodified (spec §8: "Existing client-side Prebid/GPT flow runs unmodified"). + let state = std::sync::Arc::new(std::sync::RwLock::new(None)); + let config = HtmlProcessorConfig { + origin_host: "origin.example.com".to_string(), + request_host: "example.com".to_string(), + request_scheme: "https".to_string(), + integrations: IntegrationRegistry::empty_for_tests(), + ad_slots_script: None, + ad_bids_state: state, + }; + let mut processor = create_html_processor(config); + let output = processor + .process_chunk(b"content", true) + .expect("should process"); + let html = std::str::from_utf8(&output).expect("should be utf8"); + assert!( + !html.contains("__ts_bids"), + "should NOT inject __ts_bids when no slots matched" ); } } diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index ec4f4a22..4a39c962 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -18,7 +18,7 @@ use error_stack::{Report, ResultExt}; use fastly::http::{header, StatusCode}; use fastly::{Body, Request, Response}; -use crate::auction::orchestrator::AuctionOrchestrator; +use crate::auction::orchestrator::{AuctionOrchestrator, DispatchedAuction}; use crate::auction::types::{ AuctionContext, AuctionRequest, Bid, PublisherInfo, SiteInfo, UserInfo, }; @@ -31,7 +31,7 @@ use crate::error::TrustedServerError; use crate::http_util::{serve_static_with_etag, RequestInfo}; use crate::integrations::IntegrationRegistry; use crate::platform::RuntimeServices; -use crate::price_bucket::price_bucket; +use crate::price_bucket::{price_bucket, PriceGranularity}; use crate::rsc_flight::RscFlightUrlRewriter; use crate::settings::Settings; use crate::streaming_processor::{Compression, PipelineConfig, StreamProcessor, StreamingPipeline}; @@ -301,8 +301,9 @@ pub enum PublisherResponse { response: Response, /// Origin body to be piped through the streaming pipeline. body: Body, - /// Parameters for `process_response_streaming`. - params: OwnedProcessResponseParams, + /// Parameters for `process_response_streaming`. Boxed to keep this + /// variant's on-stack size comparable to the other variants. + params: Box, }, /// Non-processable 2xx response (images, fonts, video). The adapter must /// reattach the body via `response.set_body(body)` before returning. @@ -407,6 +408,12 @@ pub struct OwnedProcessResponseParams { pub(crate) content_type: String, pub(crate) ad_slots_script: Option, pub(crate) ad_bids_state: Arc>>, + /// In-flight SSP bids dispatched before `pending_origin.wait()`. + /// The streaming phase collects these and writes bids to `ad_bids_state` + /// before processing the last body chunk, so `` injection sees live bids. + pub(crate) dispatched_auction: Option, + /// Price granularity used to bucket bids when building `__ts_bids`. + pub(crate) price_granularity: PriceGranularity, } /// Stream the publisher response body through the processing pipeline. @@ -441,6 +448,261 @@ pub fn stream_publisher_body( process_response_streaming(body, output, &borrowed) } +/// Stream publisher body with a "last-chunk hold" for live bid injection. +/// +/// Drives the origin body through the HTML pipeline one chunk at a time, using a +/// one-behind buffer so the last raw origin chunk is held back. When the origin +/// body is exhausted (`read` returns `Ok(0)`): +/// +/// 1. [`collect_dispatched_auction`](AuctionOrchestrator::collect_dispatched_auction) +/// is awaited with the remaining deadline. +/// 2. Winning bids are written to `ad_bids_state`. +/// 3. The held last chunk is fed through the pipeline — `lol_html` fires its +/// `` handler with bids now in state. +/// +/// For non-HTML content types the auction is collected before any body bytes +/// are written (no `` to inject). If `params.dispatched_auction` is +/// `None` the function falls back to the synchronous +/// [`stream_publisher_body`] path. +/// +/// # Errors +/// +/// Returns an error if processing fails mid-stream. Headers are already +/// committed at that point; the caller logs and drops the `StreamingBody`. +pub async fn stream_publisher_body_async( + body: Body, + output: &mut W, + params: &mut OwnedProcessResponseParams, + settings: &Settings, + integration_registry: &IntegrationRegistry, + orchestrator: &AuctionOrchestrator, + services: &RuntimeServices, +) -> Result<(), Report> { + let Some(dispatched) = params.dispatched_auction.take() else { + // No auction — use the existing sync pipeline unchanged. + return stream_publisher_body(body, output, params, settings, integration_registry); + }; + + let is_html = params.content_type.contains("text/html"); + + if !is_html { + // Non-HTML: collect auction first, then stream. There is no + // to hold, so delaying the entire body until collection is acceptable. + let placeholder = Request::get("https://placeholder.invalid/"); + let result = orchestrator + .collect_dispatched_auction(dispatched, services, &make_collect_context(settings, services, &placeholder)) + .await; + write_bids_to_state(&result.winning_bids, params.price_granularity, ¶ms.ad_bids_state); + return stream_publisher_body(body, output, params, settings, integration_registry); + } + + // HTML: build the processor once and drive it chunk by chunk. + // One-behind buffer: stream chunk N-1 immediately; hold chunk N until origin + // EOF, then await auction and process chunk N (which contains ). + let mut processor = create_html_stream_processor( + ¶ms.origin_host, + ¶ms.request_host, + ¶ms.request_scheme, + settings, + integration_registry, + params.ad_slots_script.as_deref().map(str::to_string), + params.ad_bids_state.clone(), + )?; + + let compression = Compression::from_content_encoding(¶ms.content_encoding); + stream_html_with_auction_hold( + body, + output, + &mut processor, + compression, + AuctionCollectCtx { + dispatched, + price_granularity: params.price_granularity, + ad_bids_state: ¶ms.ad_bids_state, + orchestrator, + services, + settings, + }, + ) + .await +} + +/// Build a minimal [`AuctionContext`] for the mediator call in collection. +/// +/// The `request` field is a short-lived placeholder (providers use it only for +/// header extraction; the placeholder is functionally equivalent to the original +/// since `req` was already consumed by `send_async` before dispatch). +fn make_collect_context<'a>( + settings: &'a Settings, + services: &'a RuntimeServices, + placeholder: &'a Request, +) -> AuctionContext<'a> { + AuctionContext { + settings, + request: placeholder, + client_info: services.client_info(), + timeout_ms: 0, + provider_responses: None, + services, + } +} + +/// Write winning bids from an auction result into the shared `ad_bids_state` lock. +pub(crate) fn write_bids_to_state( + winning_bids: &std::collections::HashMap, + price_granularity: PriceGranularity, + ad_bids_state: &Arc>>, +) { + let bid_map = build_bid_map(winning_bids, price_granularity); + let bids_script = build_bids_script(&bid_map); + *ad_bids_state.write().expect("should write bid state") = Some(bids_script); +} + +/// Bundles the auction-collection dependencies passed through the streaming helpers. +struct AuctionCollectCtx<'a> { + dispatched: DispatchedAuction, + price_granularity: PriceGranularity, + ad_bids_state: &'a Arc>>, + orchestrator: &'a AuctionOrchestrator, + services: &'a RuntimeServices, + settings: &'a Settings, +} + +/// Run the one-behind chunk loop for HTML bodies, collecting the auction before +/// the last chunk so `lol_html`'s `` handler sees live bids. +async fn stream_html_with_auction_hold( + body: Body, + output: &mut W, + processor: &mut P, + compression: Compression, + ctx: AuctionCollectCtx<'_>, +) -> Result<(), Report> { + use brotli::enc::writer::CompressorWriter; + use brotli::enc::BrotliEncoderParams; + use brotli::Decompressor; + use flate2::read::{GzDecoder, ZlibDecoder}; + use flate2::write::{GzEncoder, ZlibEncoder}; + + match compression { + Compression::None => one_behind_loop(body, output, processor, ctx).await, + Compression::Gzip => { + let decoder = GzDecoder::new(body); + let mut encoder = GzEncoder::new(&mut *output, flate2::Compression::default()); + one_behind_loop(decoder, &mut encoder, processor, ctx).await?; + encoder.finish().change_context(TrustedServerError::Proxy { + message: "Failed to finalize gzip encoder".to_string(), + })?; + Ok(()) + } + Compression::Deflate => { + let decoder = ZlibDecoder::new(body); + let mut encoder = ZlibEncoder::new(&mut *output, flate2::Compression::default()); + one_behind_loop(decoder, &mut encoder, processor, ctx).await?; + encoder.finish().change_context(TrustedServerError::Proxy { + message: "Failed to finalize deflate encoder".to_string(), + })?; + Ok(()) + } + Compression::Brotli => { + let decoder = Decompressor::new(body, 4096); + let params = BrotliEncoderParams { + quality: 4, + lgwin: 22, + ..Default::default() + }; + let mut encoder = CompressorWriter::with_params(&mut *output, 4096, ¶ms); + one_behind_loop(decoder, &mut encoder, processor, ctx).await?; + let _ = encoder.into_inner(); + Ok(()) + } + } +} + +/// Core one-behind chunk loop. +/// +/// Reads from `reader`, writing processed output to `writer` for every chunk +/// except the current one (which is held pending). On EOF, the auction is +/// collected, bids written, and the held chunk processed last. +async fn one_behind_loop( + mut reader: R, + writer: &mut W, + processor: &mut P, + ctx: AuctionCollectCtx<'_>, +) -> Result<(), Report> { + let AuctionCollectCtx { dispatched, price_granularity, ad_bids_state, orchestrator, services, settings } = ctx; + const CHUNK_SIZE: usize = 8192; + let mut buffer = vec![0u8; CHUNK_SIZE]; + let mut pending: Vec = Vec::new(); + + loop { + match reader.read(&mut buffer) { + Ok(0) => { + // Origin exhausted — pending holds the last chunk. + // Collect the auction before feeding it to lol_html so that + // the handler sees populated ad_bids_state. + let placeholder = Request::get("https://placeholder.invalid/"); + let collect_ctx = make_collect_context(settings, services, &placeholder); + let result = orchestrator + .collect_dispatched_auction(dispatched, services, &collect_ctx) + .await; + write_bids_to_state(&result.winning_bids, price_granularity, ad_bids_state); + + // Process the held last chunk (not is_last — finalization is separate). + if !pending.is_empty() { + let out = processor.process_chunk(&pending, false).change_context( + TrustedServerError::Proxy { + message: "Failed to process last chunk".to_string(), + }, + )?; + if !out.is_empty() { + writer.write_all(&out).change_context(TrustedServerError::Proxy { + message: "Failed to write last chunk".to_string(), + })?; + } + } + // Signal EOF to lol_html (fires end() which flushes remaining state). + let final_out = processor.process_chunk(&[], true).change_context( + TrustedServerError::Proxy { + message: "Failed to finalize processor".to_string(), + }, + )?; + if !final_out.is_empty() { + writer.write_all(&final_out).change_context(TrustedServerError::Proxy { + message: "Failed to write finalized output".to_string(), + })?; + } + break; + } + Ok(n) => { + // Stream the previously held chunk (it is not the last). + if !pending.is_empty() { + let out = processor.process_chunk(&pending, false).change_context( + TrustedServerError::Proxy { + message: "Failed to process chunk".to_string(), + }, + )?; + if !out.is_empty() { + writer.write_all(&out).change_context(TrustedServerError::Proxy { + message: "Failed to write chunk".to_string(), + })?; + } + } + pending = buffer[..n].to_vec(); + } + Err(e) => { + return Err(Report::new(TrustedServerError::Proxy { + message: format!("Failed to read origin body: {e}"), + })); + } + } + } + + writer.flush().change_context(TrustedServerError::Proxy { + message: "Failed to flush output".to_string(), + })?; + Ok(()) +} + /// Proxies requests to the publisher's origin server. /// /// Returns a [`PublisherResponse`] indicating how the response should be sent: @@ -590,13 +852,22 @@ pub async fn handle_publisher_request( restrict_accept_encoding(&mut req); req.set_header("host", &origin_host); + // Dispatch origin request first. let pending_origin = req.send_async(&backend_name) .change_context(TrustedServerError::Proxy { message: "Failed to dispatch async origin request".to_string(), })?; - let auction_result = if should_run_auction { + // Dispatch SSP bid requests BEFORE awaiting origin — all HTTP is now in-flight + // in Fastly's native layer. WASM yields only for origin (fast, cache-hit path), + // so TTFB ≈ origin latency instead of TTFB ≈ auction timeout. + let price_granularity = settings + .creative_opportunities + .as_ref() + .map(|co| co.price_granularity) + .unwrap_or_default(); + let dispatched_auction = if should_run_auction { let co_config = settings .creative_opportunities .as_ref() @@ -617,35 +888,12 @@ pub async fn handle_publisher_request( provider_responses: None, services, }; - match orchestrator - .run_auction(&auction_request, &auction_context, services) - .await - { - Ok(result) => Some(result), - Err(e) => { - log::warn!("server-side auction failed, proceeding without bids: {e:?}"); - None - } - } + orchestrator.dispatch_auction(&auction_request, &auction_context) } else { None }; - if should_run_auction { - let co_config = settings - .creative_opportunities - .as_ref() - .expect("should be present"); - let empty: std::collections::HashMap = std::collections::HashMap::new(); - let winning_bids = auction_result - .as_ref() - .map(|r| &r.winning_bids) - .unwrap_or(&empty); - let bid_map = build_bid_map(winning_bids, co_config.price_granularity); - let bids_script = build_bids_script(&bid_map); - *ad_bids_state.write().expect("should write bid state") = Some(bids_script); - } - + // Now yield for origin — SSP requests are already racing in Fastly's native layer. let mut response = pending_origin .wait() .change_context(TrustedServerError::Proxy { @@ -754,7 +1002,7 @@ pub async fn handle_publisher_request( Ok(PublisherResponse::Stream { response, body, - params: OwnedProcessResponseParams { + params: Box::new(OwnedProcessResponseParams { content_encoding, origin_host, origin_url: settings.publisher.origin_url.clone(), @@ -763,7 +1011,9 @@ pub async fn handle_publisher_request( content_type, ad_slots_script: ad_slots_script.clone(), ad_bids_state: ad_bids_state.clone(), - }, + dispatched_auction, + price_granularity, + }), }) } ResponseRoute::BufferedProcessed => { @@ -1790,6 +2040,9 @@ mod tests { content_type: "text/css".to_string(), ad_slots_script: None, ad_bids_state: Arc::new(RwLock::new(None)), + dispatched_auction: None, + price_granularity: crate::price_bucket::PriceGranularity::default(), + }; let mut output = Vec::new(); @@ -1833,6 +2086,9 @@ mod tests { content_type: "text/html; charset=utf-8".to_string(), ad_slots_script: None, ad_bids_state: Arc::new(RwLock::new(None)), + dispatched_auction: None, + price_granularity: crate::price_bucket::PriceGranularity::default(), + }; let mut output = Vec::new(); @@ -1867,6 +2123,9 @@ mod tests { content_type: "text/html".to_string(), ad_slots_script: None, ad_bids_state: Arc::new(RwLock::new(None)), + dispatched_auction: None, + price_granularity: crate::price_bucket::PriceGranularity::default(), + }; let bogus_body = Body::from(b"not gzip".to_vec()); @@ -1968,6 +2227,9 @@ mod tests { content_type: "text/html; charset=utf-8".to_string(), ad_slots_script: None, ad_bids_state: Arc::new(RwLock::new(None)), + dispatched_auction: None, + price_granularity: crate::price_bucket::PriceGranularity::default(), + }; let mut output = Vec::new(); stream_publisher_body(body, &mut output, ¶ms, &settings, ®istry) @@ -2020,6 +2282,9 @@ mod tests { content_type: "text/html".to_string(), ad_slots_script: None, ad_bids_state: Arc::new(RwLock::new(None)), + dispatched_auction: None, + price_granularity: crate::price_bucket::PriceGranularity::default(), + }; let mut output = Vec::new(); diff --git a/trusted-server.toml b/trusted-server.toml index 43e090fe..b1b5a0b0 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -192,6 +192,12 @@ permutive_segments = "permutive" [creative_opportunities] gam_network_id = "88059007" +# FCP is not affected by this value — body content above has already +# streamed and painted before the hold begins. What this caps is the slip on +# DOMContentLoaded and window.load. Worst case: a cache-hit page where origin +# drains in <50 ms but the auction runs to the limit. 500 ms is the recommended +# default; raise only if your SSPs need more headroom and your analytics confirm +# the DCL slip is acceptable. auction_timeout_ms = 1500 price_granularity = "dense" From a2d08e78ea7c4bc3fd73c4259d3cb4b66f37a153 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Sun, 10 May 2026 16:13:47 +0530 Subject: [PATCH 41/67] Fix clippy explicit-auto-deref in stream_publisher_body_async call --- crates/trusted-server-adapter-fastly/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/trusted-server-adapter-fastly/src/main.rs b/crates/trusted-server-adapter-fastly/src/main.rs index 895299f5..94f09519 100644 --- a/crates/trusted-server-adapter-fastly/src/main.rs +++ b/crates/trusted-server-adapter-fastly/src/main.rs @@ -262,7 +262,7 @@ async fn route_request( let stream_result = stream_publisher_body_async( body, &mut streaming_body, - &mut *params, + &mut params, settings, integration_registry, orchestrator, From b03af6b1f94b4bb34b8a59db8b28b69a747287ca Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Sun, 10 May 2026 16:34:20 +0530 Subject: [PATCH 42/67] =?UTF-8?q?Fix=20Cache-Control=20headers=20applied?= =?UTF-8?q?=20only=20when=20slots=20matched=20=E2=80=94=20apply=20to=20all?= =?UTF-8?q?=20HTML=20responses=20per=20spec=20=C2=A74.7=20+=20=C2=A78?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/trusted-server-core/src/publisher.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 4a39c962..dc93af76 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -915,7 +915,13 @@ pub async fn handle_publisher_request( None }; - if ad_slots_script.is_some() { + // §4.7: assembled HTML responses must never be shared-cached — per-user bid data + // travels inline. Apply regardless of slot match or auction outcome (§8). + let origin_content_type = response + .get_header(header::CONTENT_TYPE) + .and_then(|h| h.to_str().ok()) + .unwrap_or_default(); + if origin_content_type.contains("text/html") { response.set_header(header::CACHE_CONTROL, "private, max-age=0"); response.remove_header("surrogate-control"); response.remove_header("fastly-surrogate-control"); From 349dcdcb2689d039085e1267f0011f8e94f00b2a Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Sun, 10 May 2026 16:45:54 +0530 Subject: [PATCH 43/67] cargo fmt --- .../src/auction/orchestrator.rs | 111 +++++++++++++----- crates/trusted-server-core/src/publisher.rs | 50 +++++--- 2 files changed, 114 insertions(+), 47 deletions(-) diff --git a/crates/trusted-server-core/src/auction/orchestrator.rs b/crates/trusted-server-core/src/auction/orchestrator.rs index 953ed6a0..82003105 100644 --- a/crates/trusted-server-core/src/auction/orchestrator.rs +++ b/crates/trusted-server-core/src/auction/orchestrator.rs @@ -637,7 +637,10 @@ impl AuctionOrchestrator { }; if !provider.is_enabled() { - log::debug!("Provider '{}' is disabled, skipping", provider.provider_name()); + log::debug!( + "Provider '{}' is disabled, skipping", + provider.provider_name() + ); continue; } @@ -656,7 +659,10 @@ impl AuctionOrchestrator { let backend_name = match provider.backend_name(effective_timeout) { Some(name) => name, None => { - log::warn!("Provider '{}' has no backend_name, skipping", provider.provider_name()); + log::warn!( + "Provider '{}' has no backend_name, skipping", + provider.provider_name() + ); continue; } }; @@ -681,7 +687,11 @@ impl AuctionOrchestrator { ); backend_to_provider.insert( backend_name.clone(), - (provider.provider_name().to_string(), start_time, Arc::clone(provider)), + ( + provider.provider_name().to_string(), + start_time, + Arc::clone(provider), + ), ); pending_requests .push(PlatformPendingRequest::new(pending).with_backend_name(backend_name)); @@ -777,28 +787,45 @@ impl AuctionOrchestrator { { let response_time_ms = start_time.elapsed().as_millis() as u64; match platform_response_to_fastly(platform_response) { - Ok(response) => match provider.parse_response(response, response_time_ms) { - Ok(auction_response) => { - log::info!( - "Provider '{}' returned {} bids ({}ms)", - auction_response.provider, - auction_response.bids.len(), - auction_response.response_time_ms - ); - responses.push(auction_response); - } - Err(e) => { - log::warn!("Provider '{}' parse failed: {:?}", provider_name, e); - responses.push(AuctionResponse::error(&provider_name, response_time_ms)); + Ok(response) => { + match provider.parse_response(response, response_time_ms) { + Ok(auction_response) => { + log::info!( + "Provider '{}' returned {} bids ({}ms)", + auction_response.provider, + auction_response.bids.len(), + auction_response.response_time_ms + ); + responses.push(auction_response); + } + Err(e) => { + log::warn!( + "Provider '{}' parse failed: {:?}", + provider_name, + e + ); + responses.push(AuctionResponse::error( + &provider_name, + response_time_ms, + )); + } } - }, + } Err(e) => { - log::warn!("Provider '{}' unsupported body: {:?}", provider_name, e); - responses.push(AuctionResponse::error(&provider_name, response_time_ms)); + log::warn!( + "Provider '{}' unsupported body: {:?}", + provider_name, + e + ); + responses + .push(AuctionResponse::error(&provider_name, response_time_ms)); } } } else { - log::warn!("Received response from unknown backend '{}', ignoring", backend_name); + log::warn!( + "Received response from unknown backend '{}', ignoring", + backend_name + ); } } Err(e) => { @@ -847,18 +874,27 @@ impl AuctionOrchestrator { .wait(PlatformPendingRequest::new(pending)) .await; match platform_resp.change_context(TrustedServerError::Auction { - message: format!("Mediator {} request failed", mediator.provider_name()), + message: format!( + "Mediator {} request failed", + mediator.provider_name() + ), }) { Ok(platform_resp) => { match platform_response_to_fastly(platform_resp).change_context( TrustedServerError::Auction { - message: format!("Mediator {} unsupported body", mediator.provider_name()), + message: format!( + "Mediator {} unsupported body", + mediator.provider_name() + ), }, ) { Ok(response) => { - let response_time_ms = - remaining_ms as u64 - remaining_budget_ms(auction_start, timeout_ms) as u64; - match mediator.parse_response(response, response_time_ms) { + let response_time_ms = remaining_ms as u64 + - remaining_budget_ms(auction_start, timeout_ms) + as u64; + match mediator + .parse_response(response, response_time_ms) + { Ok(mediator_resp) => { let winning = mediator_resp .bids @@ -876,19 +912,30 @@ impl AuctionOrchestrator { } }) .collect(); - let winning = self.apply_floor_prices(winning, &floor_prices); + let winning = self + .apply_floor_prices(winning, &floor_prices); (Some(mediator_resp), winning) } Err(e) => { - log::warn!("Mediator '{}' parse failed: {:?}", mediator.provider_name(), e); - let winning = self.select_winning_bids(&responses, &floor_prices); + log::warn!( + "Mediator '{}' parse failed: {:?}", + mediator.provider_name(), + e + ); + let winning = self.select_winning_bids( + &responses, + &floor_prices, + ); (None, winning) } } } Err(e) => { log::warn!("Mediator body error: {:?}", e); - (None, self.select_winning_bids(&responses, &floor_prices)) + ( + None, + self.select_winning_bids(&responses, &floor_prices), + ) } } } @@ -899,7 +946,11 @@ impl AuctionOrchestrator { } } Err(e) => { - log::warn!("Mediator '{}' failed to dispatch: {:?}", mediator.provider_name(), e); + log::warn!( + "Mediator '{}' failed to dispatch: {:?}", + mediator.provider_name(), + e + ); (None, self.select_winning_bids(&responses, &floor_prices)) } } diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index dc93af76..3a10e85f 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -490,9 +490,17 @@ pub async fn stream_publisher_body_async( // to hold, so delaying the entire body until collection is acceptable. let placeholder = Request::get("https://placeholder.invalid/"); let result = orchestrator - .collect_dispatched_auction(dispatched, services, &make_collect_context(settings, services, &placeholder)) + .collect_dispatched_auction( + dispatched, + services, + &make_collect_context(settings, services, &placeholder), + ) .await; - write_bids_to_state(&result.winning_bids, params.price_granularity, ¶ms.ad_bids_state); + write_bids_to_state( + &result.winning_bids, + params.price_granularity, + ¶ms.ad_bids_state, + ); return stream_publisher_body(body, output, params, settings, integration_registry); } @@ -629,7 +637,14 @@ async fn one_behind_loop( processor: &mut P, ctx: AuctionCollectCtx<'_>, ) -> Result<(), Report> { - let AuctionCollectCtx { dispatched, price_granularity, ad_bids_state, orchestrator, services, settings } = ctx; + let AuctionCollectCtx { + dispatched, + price_granularity, + ad_bids_state, + orchestrator, + services, + settings, + } = ctx; const CHUNK_SIZE: usize = 8192; let mut buffer = vec![0u8; CHUNK_SIZE]; let mut pending: Vec = Vec::new(); @@ -655,9 +670,11 @@ async fn one_behind_loop( }, )?; if !out.is_empty() { - writer.write_all(&out).change_context(TrustedServerError::Proxy { - message: "Failed to write last chunk".to_string(), - })?; + writer + .write_all(&out) + .change_context(TrustedServerError::Proxy { + message: "Failed to write last chunk".to_string(), + })?; } } // Signal EOF to lol_html (fires end() which flushes remaining state). @@ -667,9 +684,11 @@ async fn one_behind_loop( }, )?; if !final_out.is_empty() { - writer.write_all(&final_out).change_context(TrustedServerError::Proxy { - message: "Failed to write finalized output".to_string(), - })?; + writer + .write_all(&final_out) + .change_context(TrustedServerError::Proxy { + message: "Failed to write finalized output".to_string(), + })?; } break; } @@ -682,9 +701,11 @@ async fn one_behind_loop( }, )?; if !out.is_empty() { - writer.write_all(&out).change_context(TrustedServerError::Proxy { - message: "Failed to write chunk".to_string(), - })?; + writer + .write_all(&out) + .change_context(TrustedServerError::Proxy { + message: "Failed to write chunk".to_string(), + })?; } } pending = buffer[..n].to_vec(); @@ -2048,7 +2069,6 @@ mod tests { ad_bids_state: Arc::new(RwLock::new(None)), dispatched_auction: None, price_granularity: crate::price_bucket::PriceGranularity::default(), - }; let mut output = Vec::new(); @@ -2094,7 +2114,6 @@ mod tests { ad_bids_state: Arc::new(RwLock::new(None)), dispatched_auction: None, price_granularity: crate::price_bucket::PriceGranularity::default(), - }; let mut output = Vec::new(); @@ -2131,7 +2150,6 @@ mod tests { ad_bids_state: Arc::new(RwLock::new(None)), dispatched_auction: None, price_granularity: crate::price_bucket::PriceGranularity::default(), - }; let bogus_body = Body::from(b"not gzip".to_vec()); @@ -2235,7 +2253,6 @@ mod tests { ad_bids_state: Arc::new(RwLock::new(None)), dispatched_auction: None, price_granularity: crate::price_bucket::PriceGranularity::default(), - }; let mut output = Vec::new(); stream_publisher_body(body, &mut output, ¶ms, &settings, ®istry) @@ -2290,7 +2307,6 @@ mod tests { ad_bids_state: Arc::new(RwLock::new(None)), dispatched_auction: None, price_granularity: crate::price_bucket::PriceGranularity::default(), - }; let mut output = Vec::new(); From 78885f9c1f53007be16c1c18e480487c6e6a9fc3 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Sun, 10 May 2026 19:54:52 +0530 Subject: [PATCH 44/67] =?UTF-8?q?Fix=20auction=20consent=20gate=20blocking?= =?UTF-8?q?=20non-GDPR=20regions=20=E2=80=94=20only=20require=20TCF=20Purp?= =?UTF-8?q?ose=201=20when=20gdpr=5Fapplies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/trusted-server-core/src/publisher.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 3a10e85f..81637ae5 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -853,10 +853,13 @@ pub async fn handle_publisher_request( Vec::new() }; - let consent_allows_auction = consent_context - .tcf - .as_ref() - .is_some_and(|tcf| tcf.has_purpose_consent(1)); + // Non-GDPR regions (US, etc.) have no TCF string — auction is freely allowed. + // GDPR regions require TCF Purpose 1 (storage/access) before firing. + let consent_allows_auction = !consent_context.gdpr_applies + || consent_context + .tcf + .as_ref() + .is_some_and(|tcf| tcf.has_purpose_consent(1)); let should_run_auction = is_get && !is_prefetch && !is_bot && !matched_slots.is_empty() && consent_allows_auction; @@ -1324,10 +1327,11 @@ pub async fn handle_page_bids( .map(|_| services.kv_store()), }); - let consent_allows_auction = consent_context - .tcf - .as_ref() - .is_some_and(|tcf| tcf.has_purpose_consent(1)); + let consent_allows_auction = !consent_context.gdpr_applies + || consent_context + .tcf + .as_ref() + .is_some_and(|tcf| tcf.has_purpose_consent(1)); let winning_bids = if !matched_slots.is_empty() && consent_allows_auction { let mut auction_request = build_auction_request( From 3783e68104258e71b8325820ac1c34026b928fc0 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Sun, 10 May 2026 20:52:05 +0530 Subject: [PATCH 45/67] =?UTF-8?q?Fix=20SSP=20requests=20using=20placeholde?= =?UTF-8?q?r=20headers=20=E2=80=94=20pass=20real=20request=20to=20dispatch?= =?UTF-8?q?=5Fauction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dispatch_auction was building AuctionContext with a placeholder Request (GET https://placeholder.invalid/) that carried no headers. Prebid's request_bids copies User-Agent, x-forwarded-for, Referer, Accept-Language, and cookies from context.request before sending to Prebid Server, so SSPs received stripped requests and returned empty bids. Fix: dispatch SSP requests before req.send_async(), using the original request directly as AuctionContext.request. DispatchedAuction holds no lifetime reference to Request, so the borrow ends at return and req can be modified (restrict_accept_encoding, Host header) and sent to origin immediately after. --- crates/trusted-server-core/src/publisher.rs | 36 +++++++++++---------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 81637ae5..f5caa571 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -872,25 +872,16 @@ pub async fn handle_publisher_request( let ad_bids_state: Arc>> = Arc::new(RwLock::new(None)); - // Only advertise encodings the rewrite pipeline can decode and re-encode. - restrict_accept_encoding(&mut req); - req.set_header("host", &origin_host); - - // Dispatch origin request first. - let pending_origin = - req.send_async(&backend_name) - .change_context(TrustedServerError::Proxy { - message: "Failed to dispatch async origin request".to_string(), - })?; - - // Dispatch SSP bid requests BEFORE awaiting origin — all HTTP is now in-flight - // in Fastly's native layer. WASM yields only for origin (fast, cache-hit path), - // so TTFB ≈ origin latency instead of TTFB ≈ auction timeout. let price_granularity = settings .creative_opportunities .as_ref() .map(|co| co.price_granularity) .unwrap_or_default(); + + // Dispatch SSP bid requests while req still has the original client headers + // (User-Agent, x-forwarded-for, cookies, etc.). The borrow ends when + // dispatch_auction returns — DispatchedAuction holds no lifetime — so req + // can be mutated and sent to origin immediately after. let dispatched_auction = if should_run_auction { let co_config = settings .creative_opportunities @@ -903,10 +894,9 @@ pub async fn handle_publisher_request( &request_info, co_config, ); - let placeholder_req = fastly::Request::get("https://placeholder.invalid/"); let auction_context = AuctionContext { settings, - request: &placeholder_req, + request: &req, client_info: services.client_info(), timeout_ms: auction_timeout_ms, provider_responses: None, @@ -917,7 +907,19 @@ pub async fn handle_publisher_request( None }; - // Now yield for origin — SSP requests are already racing in Fastly's native layer. + // Only advertise encodings the rewrite pipeline can decode and re-encode. + restrict_accept_encoding(&mut req); + req.set_header("host", &origin_host); + + // Dispatch origin — SSP requests are already racing in Fastly's native layer. + // TTFB ≈ origin latency instead of TTFB ≈ auction timeout. + let pending_origin = + req.send_async(&backend_name) + .change_context(TrustedServerError::Proxy { + message: "Failed to dispatch async origin request".to_string(), + })?; + + // Now yield for origin. let mut response = pending_origin .wait() .change_context(TrustedServerError::Proxy { From 0c9465206f4e6b3ad865277dae63378090831a79 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 12:36:25 +0530 Subject: [PATCH 46/67] Fix async auction collect abandoning SSP bids when origin is slow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In collect_dispatched_auction, the select loop checked `auction_start.elapsed() >= deadline` after each SSP response and broke early if the 1500ms budget had elapsed. When origin TTFB + body download exceeded the auction budget, the check fired after collecting the first SSP response, abandoning the second SSP's already-buffered response. This left responses with only one (possibly errored) SSP, causing remaining_ms == 0 which skipped the mediator, and select_winning_bids on the partial set returned zero bids. The deadline break is wrong in this context: SSP HTTP connections are already bounded by the backend first_byte_timeout set at dispatch time (1000ms per provider). By the time collect is called at origin EOF, all SSPs have either responded or been errored by Fastly's host. The select() calls drain instantly — no WASM-level deadline enforcement is needed or safe. Also add info-level log statements at dispatch, collect, and write_bids_to_state to make the auction pipeline observable without requiring a dashboard. --- .../src/auction/orchestrator.rs | 10 --------- crates/trusted-server-core/src/publisher.rs | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/trusted-server-core/src/auction/orchestrator.rs b/crates/trusted-server-core/src/auction/orchestrator.rs index 82003105..867bdf3a 100644 --- a/crates/trusted-server-core/src/auction/orchestrator.rs +++ b/crates/trusted-server-core/src/auction/orchestrator.rs @@ -751,8 +751,6 @@ impl AuctionOrchestrator { request, } = dispatched; - let deadline = Duration::from_millis(u64::from(timeout_ms)); - log::info!( "Collecting {} in-flight SSP responses (timeout: {}ms remaining: {}ms)", pending_requests.len(), @@ -833,14 +831,6 @@ impl AuctionOrchestrator { } } - if auction_start.elapsed() >= deadline && !remaining.is_empty() { - log::warn!( - "Auction timeout ({}ms) reached, dropping {} remaining request(s)", - timeout_ms, - remaining.len() - ); - break; - } } let (mediator_response, winning_bids) = if let Some(mediator_name) = &self.config.mediator { diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index f5caa571..5284e20f 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -561,6 +561,15 @@ pub(crate) fn write_bids_to_state( price_granularity: PriceGranularity, ad_bids_state: &Arc>>, ) { + log::info!( + "write_bids_to_state: {} winning bid(s): [{}]", + winning_bids.len(), + winning_bids + .keys() + .cloned() + .collect::>() + .join(", ") + ); let bid_map = build_bid_map(winning_bids, price_granularity); let bids_script = build_bids_script(&bid_map); *ad_bids_state.write().expect("should write bid state") = Some(bids_script); @@ -655,11 +664,16 @@ async fn one_behind_loop( // Origin exhausted — pending holds the last chunk. // Collect the auction before feeding it to lol_html so that // the handler sees populated ad_bids_state. + log::info!("one_behind_loop: EOF — collecting dispatched auction"); let placeholder = Request::get("https://placeholder.invalid/"); let collect_ctx = make_collect_context(settings, services, &placeholder); let result = orchestrator .collect_dispatched_auction(dispatched, services, &collect_ctx) .await; + log::info!( + "one_behind_loop: collect complete — {} winning bid(s)", + result.winning_bids.len() + ); write_bids_to_state(&result.winning_bids, price_granularity, ad_bids_state); // Process the held last chunk (not is_last — finalization is separate). @@ -906,6 +920,14 @@ pub async fn handle_publisher_request( } else { None }; + log::info!( + "dispatch_auction: {}", + if dispatched_auction.is_some() { + "Some — auction running async" + } else { + "None — falling back to sync or skipped" + } + ); // Only advertise encodings the rewrite pipeline can decode and re-encode. restrict_accept_encoding(&mut req); From f172f4477c60ae0fc0e0d68b0d9f969652dc092f Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 13:15:23 +0530 Subject: [PATCH 47/67] Cargo fmt --- crates/trusted-server-core/src/auction/orchestrator.rs | 1 - crates/trusted-server-core/src/publisher.rs | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/trusted-server-core/src/auction/orchestrator.rs b/crates/trusted-server-core/src/auction/orchestrator.rs index 867bdf3a..e9d8fa19 100644 --- a/crates/trusted-server-core/src/auction/orchestrator.rs +++ b/crates/trusted-server-core/src/auction/orchestrator.rs @@ -830,7 +830,6 @@ impl AuctionOrchestrator { log::warn!("A provider request failed during collection: {:?}", e); } } - } let (mediator_response, winning_bids) = if let Some(mediator_name) = &self.config.mediator { diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 5284e20f..3a9eb089 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -564,11 +564,7 @@ pub(crate) fn write_bids_to_state( log::info!( "write_bids_to_state: {} winning bid(s): [{}]", winning_bids.len(), - winning_bids - .keys() - .cloned() - .collect::>() - .join(", ") + winning_bids.keys().cloned().collect::>().join(", ") ); let bid_map = build_bid_map(winning_bids, price_granularity); let bids_script = build_bids_script(&bid_map); From 14cd493ee907fbf2cce5030062bfed4ccac41043 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 13:32:28 +0530 Subject: [PATCH 48/67] Fix mediator always skipped when origin body exceeds SSP auction budget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In collect_dispatched_auction, the mediator was skipped when remaining_budget_ms(auction_start, timeout_ms) == 0. In the async-dispatch path, auction_start is set before pending_origin.wait(), so elapsed time includes the full origin TTFB and body download. For heavy SSR pages (autoblog), this exceeds the 1500ms SSP budget, making remaining_ms == 0 at every collection and causing the mediator to be permanently skipped. The mediator (adserver_mock) is the primary bid source — SSPs alone return no bids. Skipping it means window.__ts_bids == {} on every full page load, while handle_page_bids (which uses the sequential run_auction path) works correctly because it measures remaining time from after SSP collection. Fix: give the mediator its own configured timeout (mediator.timeout_ms()) instead of the exhausted SSP budget. This mirrors how run_parallel_mediation works: the mediator's deadline is independent of SSP round-trip time. Side effect: mediator backend name is now stable (always t1000 for adserver_mock) rather than varying per request with remaining_ms. --- .../src/auction/orchestrator.rs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/trusted-server-core/src/auction/orchestrator.rs b/crates/trusted-server-core/src/auction/orchestrator.rs index e9d8fa19..892ff9eb 100644 --- a/crates/trusted-server-core/src/auction/orchestrator.rs +++ b/crates/trusted-server-core/src/auction/orchestrator.rs @@ -835,24 +835,25 @@ impl AuctionOrchestrator { let (mediator_response, winning_bids) = if let Some(mediator_name) = &self.config.mediator { match self.providers.get(mediator_name.as_str()) { Some(mediator) => { - let remaining_ms = remaining_budget_ms(auction_start, timeout_ms); - if remaining_ms == 0 { - log::warn!("Auction timeout exhausted during bidding — skipping mediator"); - let winning = self.select_winning_bids(&responses, &floor_prices); - return OrchestrationResult { - provider_responses: responses, - mediator_response: None, - winning_bids: winning, - total_time_ms: auction_start.elapsed().as_millis() as u64, - metadata: HashMap::new(), - }; - } + // Use the mediator's own configured timeout, not the remaining SSP + // budget. In the async-dispatch path, SSPs race against origin, so + // auction_start.elapsed() can exceed the SSP budget by the time the + // origin body finishes streaming. Skipping the mediator in that case + // would discard all bids — the mediator is the primary bid source. + let mediator_timeout = mediator.timeout_ms(); + let mediator_start = Instant::now(); + log::info!( + "Running mediator '{}' with {}ms budget (SSP budget remaining: {}ms)", + mediator.provider_name(), + mediator_timeout, + remaining_budget_ms(auction_start, timeout_ms), + ); let placeholder = fastly::Request::get("https://placeholder.invalid/"); let mediator_context = AuctionContext { settings: context.settings, request: &placeholder, client_info: context.client_info, - timeout_ms: remaining_ms, + timeout_ms: mediator_timeout, provider_responses: Some(&responses), services: context.services, }; @@ -878,9 +879,8 @@ impl AuctionOrchestrator { }, ) { Ok(response) => { - let response_time_ms = remaining_ms as u64 - - remaining_budget_ms(auction_start, timeout_ms) - as u64; + let response_time_ms = + mediator_start.elapsed().as_millis() as u64; match mediator .parse_response(response, response_time_ms) { From 6210ebbf1560558813c7d30865b032a5a7962649 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 14:04:28 +0530 Subject: [PATCH 49/67] Cargo fmt --- crates/trusted-server-core/src/publisher.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 6fd9b0d6..30abc532 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -1332,7 +1332,8 @@ pub async fn handle_page_bids( .collect(); let http_req = compat::from_fastly_headers_ref(&req); - let request_info = crate::http_util::RequestInfo::from_request(&http_req, &services.client_info); + let request_info = + crate::http_util::RequestInfo::from_request(&http_req, &services.client_info); let cookie_jar = handle_request_cookies(&http_req)?; let ec_id = get_or_generate_ec_id_from_http_request(settings, services, &http_req)?; let geo = services From 3cdf9952f54fcfe4c4c7cc87f8064aa1af8aa721 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 14:44:58 +0530 Subject: [PATCH 50/67] Adding debug info for auction --- crates/trusted-server-core/src/publisher.rs | 44 +++++++++++++++------ crates/trusted-server-core/src/settings.rs | 6 +++ trusted-server.toml | 6 ++- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 30abc532..ba594d80 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -677,6 +677,29 @@ async fn one_behind_loop( ); write_bids_to_state(&result.winning_bids, price_granularity, ad_bids_state); + if settings.debug.auction_html_comment { + let ssp_count = result.provider_responses.len(); + let mediator_info = match &result.mediator_response { + Some(r) => format!("ok({}_bids)", r.bids.len()), + None => "none".to_string(), + }; + let debug_comment = format!( + "", + result.winning_bids.len() + ); + let mut state = ad_bids_state + .write() + .expect("should write bid state for debug"); + match &mut *state { + Some(script) => { + *script = format!("{debug_comment}\n{script}"); + } + None => { + *state = Some(debug_comment); + } + } + } + // Process the held last chunk (not is_last — finalization is separate). if !pending.is_empty() { let out = processor.process_chunk(&pending, false).change_context( @@ -909,6 +932,7 @@ pub async fn handle_publisher_request( &ec_id, &consent_context, &request_info, + &request_path, co_config, ); let auction_context = AuctionContext { @@ -1109,18 +1133,23 @@ pub(crate) fn build_auction_request( ec_id: &str, consent_context: &crate::consent::ConsentContext, request_info: &crate::http_util::RequestInfo, + request_path: &str, co_config: &crate::creative_opportunities::CreativeOpportunitiesConfig, ) -> AuctionRequest { let slots = matched_slots .iter() .map(|s| s.to_ad_slot(&co_config.gam_network_id)) .collect(); + let page_url = format!( + "{}://{}{}", + request_info.scheme, request_info.host, request_path + ); AuctionRequest { id: format!("ts-{}", ec_id), slots, publisher: PublisherInfo { domain: request_info.host.clone(), - page_url: None, + page_url: Some(page_url.clone()), }, user: UserInfo { id: ec_id.to_string(), @@ -1130,7 +1159,7 @@ pub(crate) fn build_auction_request( device: None, site: Some(SiteInfo { domain: request_info.host.clone(), - page: String::new(), + page: page_url, }), context: std::collections::HashMap::new(), } @@ -1363,21 +1392,14 @@ pub async fn handle_page_bids( .is_some_and(|tcf| tcf.has_purpose_consent(1)); let winning_bids = if !matched_slots.is_empty() && consent_allows_auction { - let mut auction_request = build_auction_request( + let auction_request = build_auction_request( &matched_slots, &ec_id, &consent_context, &request_info, + &path_param, co_config, ); - let page_url = format!( - "{}://{}{}", - request_info.scheme, request_info.host, path_param - ); - auction_request.publisher.page_url = Some(page_url.clone()); - if let Some(ref mut site) = auction_request.site { - site.page = page_url; - } let timeout_ms = co_config .auction_timeout_ms .unwrap_or(settings.auction.timeout_ms); diff --git a/crates/trusted-server-core/src/settings.rs b/crates/trusted-server-core/src/settings.rs index e09c50e2..386f0d54 100644 --- a/crates/trusted-server-core/src/settings.rs +++ b/crates/trusted-server-core/src/settings.rs @@ -410,6 +410,12 @@ pub struct DebugConfig { /// Fastly-observed TLS details that browser JS cannot normally read. #[serde(default)] pub ja4_endpoint_enabled: bool, + + /// Inject a `` HTML comment before `` showing + /// auction pipeline stats (SSP count, mediator status, winning bid count). + /// Never enable in production — visible in page source. + #[serde(default)] + pub auction_html_comment: bool, } #[derive(Debug, Default, Clone, Deserialize, Serialize, Validate)] diff --git a/trusted-server.toml b/trusted-server.toml index 60876389..a71abfdd 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -208,7 +208,11 @@ endpoint = "https://origin-mocktioneer.cdintel.com/adserver/mediate" timeout_ms = 1000 # Debug configuration (all flags default to false — do not enable in production) -# [debug] +# TODO: remove [debug] block before merging to main +[debug] +# Inject before . +# Visible in page source. Disable after investigation. +auction_html_comment = true # Enable the JA4/TLS fingerprint debug endpoint at GET /_ts/debug/ja4. # Returns a plain-text response with the following fields (Fastly-observed values): # ja4 — JA4 TLS client fingerprint From e35c593f3b26f64ee148ed39a698b19c23b594db Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 15:30:15 +0530 Subject: [PATCH 51/67] Fix auction bids missing on Next.js buffered HTML path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `classify_response_route` returns `BufferedProcessed` when HTML has post-processors registered (e.g. the Next.js integration registers one via `with_html_post_processor`). Unlike the `Stream` path, which drives `one_behind_loop` to collect the dispatched auction at origin EOF, the `BufferedProcessed` branch previously discarded `dispatched_auction` entirely — so `ad_bids_state` stayed `None` and lol_html injected the fallback `window.__ts_bids = {}` instead of real bids. Fix: collect the in-flight dispatched auction in the `BufferedProcessed` branch before calling `process_response_streaming`, using the same `collect_dispatched_auction` + `write_bids_to_state` pattern that the stream path uses. The `debug.auction_html_comment` injection is mirrored here as well so the comment appears in both code paths when enabled. --- crates/trusted-server-core/src/publisher.rs | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index ba594d80..0178d56d 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -1103,6 +1103,49 @@ pub async fn handle_publisher_request( content_type, content_encoding, request_host, origin_host ); + // Collect any in-flight auction before processing buffered HTML. + // BufferedProcessed is taken when HTML has post-processors (e.g. Next.js rewriters). + // Unlike the Stream path, the body is fully buffered first — collect auction + // now so bids are available when the handler fires. + if let Some(dispatched) = dispatched_auction { + let placeholder = fastly::Request::get("https://placeholder.invalid/"); + let result = orchestrator + .collect_dispatched_auction( + dispatched, + services, + &make_collect_context(settings, services, &placeholder), + ) + .await; + log::info!( + "BufferedProcessed: auction collected — {} winning bid(s)", + result.winning_bids.len() + ); + write_bids_to_state(&result.winning_bids, price_granularity, &ad_bids_state); + + if settings.debug.auction_html_comment { + let ssp_count = result.provider_responses.len(); + let mediator_info = match &result.mediator_response { + Some(r) => format!("ok({}_bids)", r.bids.len()), + None => "none".to_string(), + }; + let debug_comment = format!( + "", + result.winning_bids.len() + ); + let mut state = ad_bids_state + .write() + .expect("should write bid state for debug"); + match &mut *state { + Some(script) => { + *script = format!("{debug_comment}\n{script}"); + } + None => { + *state = Some(debug_comment); + } + } + } + } + let body = response.take_body(); let params = ProcessResponseParams { content_encoding: &content_encoding, From 3dac7760f6361d022e831137b37314084d3687b9 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 15:57:54 +0530 Subject: [PATCH 52/67] Add path label and auction time to debug HTML comment --- crates/trusted-server-core/src/publisher.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 0178d56d..21399ef7 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -684,8 +684,9 @@ async fn one_behind_loop( None => "none".to_string(), }; let debug_comment = format!( - "", - result.winning_bids.len() + "", + result.winning_bids.len(), + result.total_time_ms, ); let mut state = ad_bids_state .write() @@ -1129,8 +1130,9 @@ pub async fn handle_publisher_request( None => "none".to_string(), }; let debug_comment = format!( - "", - result.winning_bids.len() + "", + result.winning_bids.len(), + result.total_time_ms, ); let mut state = ad_bids_state .write() From 65c0ad3090427a84e3655d81c01ff13c7079472f Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Mon, 11 May 2026 19:53:15 +0530 Subject: [PATCH 53/67] Fix XSS in script injection and cap mediator at A_deadline html_escape_for_script now unicode-escapes <, >, & and U+2028/2029 in addition to \ and ". These characters allow a crafted bid value to break out of the ` injection breaking out of the script context +/// - U+2028, U+2029 — line/paragraph separators that are valid JSON but terminate +/// a JS string literal in some parsers +/// +/// All substitutions use `\uXXXX` form, which is valid inside both JSON strings +/// and JS string literals. The result is always safe to write as `JSON.parse("…")`. fn html_escape_for_script(s: &str) -> String { - s.replace('\\', "\\\\").replace('"', "\\\"") + s.replace('\\', "\\\\") + .replace('"', "\\\"") + .replace('<', "\\u003C") + .replace('>', "\\u003E") + .replace('&', "\\u0026") + .replace('\u{2028}', "\\u2028") + .replace('\u{2029}', "\\u2029") } /// Build a price-bucketed bid map from winning bids. @@ -2633,6 +2645,26 @@ mod tests { "both\\\\\\\"mixed", "should escape both backslashes and quotes" ); + assert_eq!( + html_escape_for_script(""), + "\\u003Cscript\\u003Ealert(1)\\u003C/script\\u003E", + "should unicode-escape angle brackets to prevent script injection" + ); + assert_eq!( + html_escape_for_script("a&b"), + "a\\u0026b", + "should unicode-escape ampersand" + ); + assert_eq!( + html_escape_for_script("line\u{2028}sep"), + "line\\u2028sep", + "should unicode-escape U+2028 line separator" + ); + assert_eq!( + html_escape_for_script("para\u{2029}sep"), + "para\\u2029sep", + "should unicode-escape U+2029 paragraph separator" + ); } } } From 0f67a8d9d24368513a7ba68a9fa8e7cf3df20f9f Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 13 May 2026 14:36:58 +0530 Subject: [PATCH 54/67] Added footer slot id --- creative-opportunities.toml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/creative-opportunities.toml b/creative-opportunities.toml index 3cd27f2b..95ea849a 100644 --- a/creative-opportunities.toml +++ b/creative-opportunities.toml @@ -38,3 +38,22 @@ slot_id = "aps-slot-homepage-header" [slot.providers.pbs.bidders] mocktioneer = { bid = 2.00 } criteo = { networkId = 123456, pubid = "123456" } + +[[slot]] +id = "homepage_footer_ad" +gam_unit_path = "/88059007/autoblog/homepage" +div_id = "ad-fixed_bottom-0-_R_klubtak5lb_" +page_patterns = ["/"] +formats = [{ width = 728, height = 90 }] +floor_price = 0.50 + +[slot.targeting] +pos = "btf" +zone = "fixedBottom" + +[slot.providers.aps] +slot_id = "aps-slot-homepage-footer" + +[slot.providers.pbs.bidders] +mocktioneer = { bid = 1.50 } +criteo = { networkId = 123456, pubid = "123456" } From a7e87512c3bfe7b1fe0296a45e624edfb2498f0a Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 14 May 2026 15:37:11 +0530 Subject: [PATCH 55/67] Fix page-bids auction context, protect Cache-Control from operator override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pass the real incoming request to AuctionContext in handle_page_bids instead of a placeholder — SSPs now receive browser UA, referer, and cookies on SPA navigation bids. Guard Cache-Control in finalize_response so operator response_headers cannot overwrite the private/no-store directives set for per-user HTML and page-bids responses. Disable auction_html_comment debug flag in trusted-server.toml. --- crates/trusted-server-adapter-fastly/src/main.rs | 10 ++++++++++ crates/trusted-server-core/src/publisher.rs | 3 +-- trusted-server.toml | 3 +-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/trusted-server-adapter-fastly/src/main.rs b/crates/trusted-server-adapter-fastly/src/main.rs index fc66fcfd..24c447d3 100644 --- a/crates/trusted-server-adapter-fastly/src/main.rs +++ b/crates/trusted-server-adapter-fastly/src/main.rs @@ -404,6 +404,16 @@ fn finalize_response(settings: &Settings, geo_info: Option<&GeoInfo>, response: } for (key, value) in &settings.response_headers { + // Never overwrite a privacy-critical Cache-Control header (private, no-store, etc.) + // that was set for per-user responses (HTML or page-bids). + if **key == header::CACHE_CONTROL + && response + .get_header(header::CACHE_CONTROL) + .and_then(|v| v.to_str().ok()) + .is_some_and(|v| v.contains("private")) + { + continue; + } response.set_header(key, value); } } diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 30955f91..5ab8d1ae 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -1460,10 +1460,9 @@ pub async fn handle_page_bids( let timeout_ms = co_config .auction_timeout_ms .unwrap_or(settings.auction.timeout_ms); - let placeholder_req = fastly::Request::get("https://placeholder.invalid/"); let auction_context = AuctionContext { settings, - request: &placeholder_req, + request: &req, client_info: services.client_info(), timeout_ms, provider_responses: None, diff --git a/trusted-server.toml b/trusted-server.toml index a71abfdd..899c8c89 100644 --- a/trusted-server.toml +++ b/trusted-server.toml @@ -208,11 +208,10 @@ endpoint = "https://origin-mocktioneer.cdintel.com/adserver/mediate" timeout_ms = 1000 # Debug configuration (all flags default to false — do not enable in production) -# TODO: remove [debug] block before merging to main [debug] # Inject before . # Visible in page source. Disable after investigation. -auction_html_comment = true +# auction_html_comment = true # Enable the JA4/TLS fingerprint debug endpoint at GET /_ts/debug/ja4. # Returns a plain-text response with the following fields (Fastly-observed values): # ja4 — JA4 TLS client fingerprint From 401136378c6026aa445366b8c0225f132e90ab5a Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 14 May 2026 16:14:24 +0530 Subject: [PATCH 56/67] Restore nurl/burl/ad_id through adserver_mock mediation path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mock mediator endpoint does not echo nurl/burl/ad_id back in its response. Build a bid index in request_bids keyed by (provider, slot_id, bidder) — where bidder is recovered from the echoed crid field — and restore the fields in parse_mediation_response from the original SSP bids. Fixes the spec requirement: both nurl and burl must travel in __ts_bids for client-side sendBeacon firing on slotRenderEnded (§4.5). --- .../src/integrations/adserver_mock.rs | 81 +++++++++++++++---- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/crates/trusted-server-core/src/integrations/adserver_mock.rs b/crates/trusted-server-core/src/integrations/adserver_mock.rs index 3a42ec2a..c8d0ca7b 100644 --- a/crates/trusted-server-core/src/integrations/adserver_mock.rs +++ b/crates/trusted-server-core/src/integrations/adserver_mock.rs @@ -10,7 +10,7 @@ use fastly::Request; use serde::{Deserialize, Serialize}; use serde_json::{json, Value as Json}; use std::collections::{BTreeMap, HashMap}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::time::Duration; use validator::Validate; @@ -88,16 +88,28 @@ impl IntegrationConfig for AdServerMockConfig { // Provider // ============================================================================ +/// Lookup index built from original SSP bids during `request_bids`, consumed +/// during `parse_response` to restore `nurl`/`burl`/`ad_id` that the mock +/// mediator endpoint does not echo back. +/// +/// Keyed by `(provider_name, slot_id, bidder_name)`. +type BidIndex = HashMap<(String, String, String), Bid>; + /// Mock ad server mediator provider. pub struct AdServerMockProvider { config: AdServerMockConfig, + /// Bridges SSP bid metadata (nurl/burl/ad_id) from request_bids to parse_response. + bid_index: Mutex>, } impl AdServerMockProvider { /// Create a new mock ad server provider. #[must_use] pub fn new(config: AdServerMockConfig) -> Self { - Self { config } + Self { + config, + bid_index: Mutex::new(None), + } } /// Build the mediation endpoint URL, appending context values as query @@ -212,8 +224,17 @@ impl AdServerMockProvider { /// Parse `OpenRTB` response from mediation endpoint. /// Mediation returns decoded prices for all bids (including APS bids that were encoded). - fn parse_mediation_response(&self, json: &Json, response_time_ms: u64) -> AuctionResponse { - // Parse OpenRTB response + /// + /// `bid_index` is the SSP-bid lookup built in `request_bids`. The mock mediator + /// does not echo `nurl`/`burl`/`ad_id` back, so they are restored from the index + /// using `(seat, impid, bidder)` where bidder is recovered from the echoed `crid` + /// field (`"{bidder}-creative"` format set during request construction). + fn parse_mediation_response( + &self, + json: &Json, + response_time_ms: u64, + bid_index: &BidIndex, + ) -> AuctionResponse { let empty_array = vec![]; let seatbid = json["seatbid"].as_array().unwrap_or(&empty_array); @@ -225,10 +246,18 @@ impl AdServerMockProvider { let bids = seat["bid"].as_array().unwrap_or(&empty_bids); for bid in bids { - // Mediation layer returns decoded prices for all bids + let slot_id = bid["impid"].as_str().unwrap_or("").to_string(); + + // Recover bidder name from crid ("{bidder}-creative") to look up the + // original SSP bid and restore nurl/burl/ad_id the mediator drops. + let crid = bid["crid"].as_str().unwrap_or(""); + let bidder = crid.strip_suffix("-creative").unwrap_or(""); + let key = (seat_name.to_string(), slot_id.clone(), bidder.to_string()); + let original = bid_index.get(&key); + all_bids.push(Bid { - slot_id: bid["impid"].as_str().unwrap_or("").to_string(), - price: bid["price"].as_f64(), // Now properly decoded by mediation + slot_id, + price: bid["price"].as_f64(), currency: "USD".to_string(), creative: bid["adm"].as_str().map(String::from), width: bid["w"].as_u64().unwrap_or(0) as u32, @@ -239,9 +268,9 @@ impl AdServerMockProvider { .filter_map(|v| v.as_str().map(String::from)) .collect() }), - nurl: None, - burl: None, - ad_id: None, + nurl: original.and_then(|b| b.nurl.clone()), + burl: original.and_then(|b| b.burl.clone()), + ad_id: original.and_then(|b| b.ad_id.clone()), metadata: HashMap::new(), }); } @@ -274,6 +303,19 @@ impl AuctionProvider for AdServerMockProvider { bidder_responses.len() ); + // Build bid index so parse_response can restore nurl/burl/ad_id from + // the original SSP bids (the mock mediator does not echo these fields). + let mut index = BidIndex::new(); + for response in bidder_responses { + for bid in &response.bids { + index.insert( + (response.provider.clone(), bid.slot_id.clone(), bid.bidder.clone()), + bid.clone(), + ); + } + } + *self.bid_index.lock().expect("should lock bid index") = Some(index); + // Build mediation request let mediation_req = self .build_mediation_request(request, bidder_responses) @@ -349,7 +391,15 @@ impl AuctionProvider for AdServerMockProvider { log::trace!("AdServer Mock response: {:?}", response_json); - let auction_response = self.parse_mediation_response(&response_json, response_time_ms); + let bid_index = self + .bid_index + .lock() + .expect("should lock bid index") + .take() + .unwrap_or_default(); + + let auction_response = + self.parse_mediation_response(&response_json, response_time_ms, &bid_index); log::info!( "AdServer Mock returned {} bids in {}ms", @@ -571,7 +621,8 @@ mod tests { "cur": "USD" }); - let auction_response = provider.parse_mediation_response(&mediation_response, 200); + let auction_response = + provider.parse_mediation_response(&mediation_response, 200, &BidIndex::new()); assert_eq!(auction_response.provider, "adserver_mock"); assert_eq!(auction_response.status, BidStatus::Success); @@ -597,7 +648,8 @@ mod tests { "cur": "USD" }); - let auction_response = provider.parse_mediation_response(&mediation_response, 100); + let auction_response = + provider.parse_mediation_response(&mediation_response, 100, &BidIndex::new()); assert_eq!(auction_response.status, BidStatus::NoBid); assert_eq!(auction_response.bids.len(), 0); @@ -791,7 +843,8 @@ mod tests { "cur": "USD" }); - let auction_response = provider.parse_mediation_response(&mediation_response, 200); + let auction_response = + provider.parse_mediation_response(&mediation_response, 200, &BidIndex::new()); assert_eq!(auction_response.status, BidStatus::Success); assert_eq!(auction_response.bids.len(), 2); From 8516caa8e5cd5369755f5edad665311f98fca2dd Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 14 May 2026 16:17:40 +0530 Subject: [PATCH 57/67] Populate device.user_agent in auction request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit APS reads user agent from request.device — without it, real APS bids arrive with wrong or missing device targeting. Pass the incoming UA from both the page-load and page-bids auction paths. --- crates/trusted-server-core/src/publisher.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 5ab8d1ae..18fc62c8 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -20,7 +20,7 @@ use fastly::{Body, Request, Response}; use crate::auction::orchestrator::{AuctionOrchestrator, DispatchedAuction}; use crate::auction::types::{ - AuctionContext, AuctionRequest, Bid, PublisherInfo, SiteInfo, UserInfo, + AuctionContext, AuctionRequest, Bid, DeviceInfo, PublisherInfo, SiteInfo, UserInfo, }; use crate::backend::BackendConfig; use crate::compat; @@ -935,6 +935,7 @@ pub async fn handle_publisher_request( &request_info, &request_path, co_config, + req.get_header_str("user-agent"), ); let auction_context = AuctionContext { settings, @@ -1180,6 +1181,7 @@ pub(crate) fn build_auction_request( request_info: &crate::http_util::RequestInfo, request_path: &str, co_config: &crate::creative_opportunities::CreativeOpportunitiesConfig, + user_agent: Option<&str>, ) -> AuctionRequest { let slots = matched_slots .iter() @@ -1201,7 +1203,11 @@ pub(crate) fn build_auction_request( fresh_id: ec_id.to_string(), consent: Some(consent_context.clone()), }, - device: None, + device: user_agent.filter(|ua| !ua.is_empty()).map(|ua| DeviceInfo { + user_agent: Some(ua.to_string()), + ip: None, + geo: None, + }), site: Some(SiteInfo { domain: request_info.host.clone(), page: page_url, @@ -1456,6 +1462,7 @@ pub async fn handle_page_bids( &request_info, &path_param, co_config, + req.get_header_str("user-agent"), ); let timeout_ms = co_config .auction_timeout_ms From 9e0ec5ba566dd925e01dc967b265142b37e782fb Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 14 May 2026 16:31:42 +0530 Subject: [PATCH 58/67] Fix __tsAdInit fallback: look up bids by slot id not div id slotRenderEnded gives a div element id via getSlotElementId(), but __ts_bids is keyed by slot id. Build a divToSlotId map during slot setup (matching the TS implementation) and use it in the event handler. Without this, nurl/burl beacons and hb_adid match checks silently fail in the server-rendered fallback whenever div_id != id. --- crates/trusted-server-core/src/integrations/gpt.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/trusted-server-core/src/integrations/gpt.rs b/crates/trusted-server-core/src/integrations/gpt.rs index 796d633e..690ba048 100644 --- a/crates/trusted-server-core/src/integrations/gpt.rs +++ b/crates/trusted-server-core/src/integrations/gpt.rs @@ -447,22 +447,24 @@ impl IntegrationHeadInjector for GptIntegration { "window.__tsAdInit=function(){", "var slots=window.__ts_ad_slots||[];", "var bids=window.__ts_bids||{};", + "var divToSlotId={};", "googletag.cmd.push(function(){", - "var gptSlots=slots.map(function(slot){", + "slots.map(function(slot){", "var s=googletag.defineSlot(slot.gam_unit_path,slot.formats,slot.div_id);", - "if(!s)return null;", + "if(!s)return;", "s.addService(googletag.pubads());", "Object.entries(slot.targeting||{}).forEach(function(e){s.setTargeting(e[0],e[1]);});", "var b=bids[slot.id]||{};", "[\"hb_pb\",\"hb_bidder\",\"hb_adid\"].forEach(function(k){if(b[k])s.setTargeting(k,b[k]);});", "s.setTargeting(\"ts_initial\",\"1\");", - "return{id:slot.id,gptSlot:s};", - "}).filter(Boolean);", + "divToSlotId[slot.div_id]=slot.id;", + "});", "googletag.pubads().enableSingleRequest();", "googletag.enableServices();", "googletag.pubads().addEventListener(\"slotRenderEnded\",function(ev){", - "var id=ev.slot.getSlotElementId();", - "var b=bids[id]||{};", + "var divId=ev.slot.getSlotElementId();", + "var slotId=divToSlotId[divId]||divId;", + "var b=bids[slotId]||{};", "var ourBidWon=!ev.isEmpty&&b.hb_adid&&ev.slot.getTargeting(\"hb_adid\")[0]===b.hb_adid;", "if(ourBidWon){", "if(b.nurl)navigator.sendBeacon(b.nurl);", From d27a329919e48ea35afc66ad91115f898a3fd10c Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 14 May 2026 16:32:12 +0530 Subject: [PATCH 59/67] Format lint using cargo fmt --- .../trusted-server-core/src/integrations/adserver_mock.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/trusted-server-core/src/integrations/adserver_mock.rs b/crates/trusted-server-core/src/integrations/adserver_mock.rs index c8d0ca7b..a8f7cadf 100644 --- a/crates/trusted-server-core/src/integrations/adserver_mock.rs +++ b/crates/trusted-server-core/src/integrations/adserver_mock.rs @@ -309,7 +309,11 @@ impl AuctionProvider for AdServerMockProvider { for response in bidder_responses { for bid in &response.bids { index.insert( - (response.provider.clone(), bid.slot_id.clone(), bid.bidder.clone()), + ( + response.provider.clone(), + bid.slot_id.clone(), + bid.bidder.clone(), + ), bid.clone(), ); } From 790c1232f0efa050eff1ecc5c84a7cd9307a78ea Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Thu, 14 May 2026 16:44:22 +0530 Subject: [PATCH 60/67] Fix clippy doc-markdown lint in adserver_mock --- crates/trusted-server-core/src/integrations/adserver_mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/trusted-server-core/src/integrations/adserver_mock.rs b/crates/trusted-server-core/src/integrations/adserver_mock.rs index a8f7cadf..4330ea66 100644 --- a/crates/trusted-server-core/src/integrations/adserver_mock.rs +++ b/crates/trusted-server-core/src/integrations/adserver_mock.rs @@ -98,7 +98,7 @@ type BidIndex = HashMap<(String, String, String), Bid>; /// Mock ad server mediator provider. pub struct AdServerMockProvider { config: AdServerMockConfig, - /// Bridges SSP bid metadata (nurl/burl/ad_id) from request_bids to parse_response. + /// Bridges SSP bid metadata (`nurl`/`burl`/`ad_id`) from `request_bids` to `parse_response`. bid_index: Mutex>, } From 299f6ba95704dcf0b35a56f2f28d74a7075c6a55 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Fri, 15 May 2026 15:59:24 +0530 Subject: [PATCH 61/67] Remove inline PBS bidder params from creative-opportunities.toml PBS bidder credentials (mocktioneer, criteo placeholder params) were being sent directly to PBS on every auction request. Per the design spec, PBS bidder params belong in PBS stored requests keyed by slot ID, not in the edge config file. Removes PbsSlotParams struct, SlotProviders.pbs field, the to_ad_slot wiring block, and the corresponding test. Slots without inline bidder params trigger the existing storedrequest fallback path in the Prebid provider. Closes #697 --- .../src/creative_opportunities.rs | 65 ------------------- creative-opportunities.toml | 12 ---- 2 files changed, 77 deletions(-) diff --git a/crates/trusted-server-core/src/creative_opportunities.rs b/crates/trusted-server-core/src/creative_opportunities.rs index 12957d4b..25add829 100644 --- a/crates/trusted-server-core/src/creative_opportunities.rs +++ b/crates/trusted-server-core/src/creative_opportunities.rs @@ -125,11 +125,6 @@ impl CreativeOpportunitySlot { serde_json::json!({ "slotID": aps.slot_id }), ); } - if let Some(ref pbs) = self.providers.pbs { - for (bidder_name, params) in &pbs.bidders { - bidders.insert(bidder_name.clone(), params.clone()); - } - } AdSlot { id: self.id.clone(), formats: self @@ -175,8 +170,6 @@ impl CreativeOpportunityFormat { pub struct SlotProviders { /// Amazon Publisher Services (APS/TAM) slot parameters. pub aps: Option, - /// Prebid Server (PBS) slot parameters. - pub pbs: Option, } /// APS-specific parameters for a slot. @@ -186,24 +179,6 @@ pub struct ApsSlotParams { pub slot_id: String, } -/// PBS-specific parameters for a slot. -/// -/// Bidder params are sent inline to Prebid Server so bidder credentials -/// stay in `creative-opportunities.toml` rather than in PBS stored requests. -#[derive(Debug, Clone, Default, Deserialize)] -pub struct PbsSlotParams { - /// Per-bidder params keyed by bidder name (must match PBS adapter name). - /// - /// Example in TOML: - /// ```toml - /// [slot.providers.pbs.bidders] - /// mocktioneer = { bid = 2.00 } - /// criteo = { networkId = 123456, pubid = "123456" } - /// ``` - #[serde(default)] - pub bidders: HashMap, -} - /// TOML file structure for creative opportunity slot definitions. #[derive(Debug, Clone, Deserialize, Default)] pub struct CreativeOpportunitiesFile { @@ -333,46 +308,6 @@ mod tests { ); } - #[test] - fn to_ad_slot_wires_pbs_bidder_params_into_bidders() { - let mut slot = make_slot("atf_sidebar_ad", vec!["/"]); - slot.providers.pbs = Some(PbsSlotParams { - bidders: [ - ( - "mocktioneer".to_string(), - serde_json::json!({ "bid": 2.00 }), - ), - ( - "criteo".to_string(), - serde_json::json!({ "networkId": 123456, "pubid": "123456" }), - ), - ] - .into_iter() - .collect(), - }); - let ad_slot = slot.to_ad_slot("88059007"); - let mock_params = ad_slot - .bidders - .get("mocktioneer") - .expect("should have mocktioneer bidder"); - assert_eq!( - mock_params.get("bid").and_then(serde_json::Value::as_f64), - Some(2.0), - "should wire mocktioneer bid param" - ); - let criteo_params = ad_slot - .bidders - .get("criteo") - .expect("should have criteo bidder"); - assert_eq!( - criteo_params - .get("networkId") - .and_then(serde_json::Value::as_i64), - Some(123456), - "should wire criteo networkId param" - ); - } - #[test] fn to_ad_slot_sets_floor_price_and_formats() { let slot = make_slot("atf", vec!["/"]); diff --git a/creative-opportunities.toml b/creative-opportunities.toml index 95ea849a..b6ed8900 100644 --- a/creative-opportunities.toml +++ b/creative-opportunities.toml @@ -16,10 +16,6 @@ zone = "atfSidebar" [slot.providers.aps] slot_id = "aps-slot-atf-sidebar" -[slot.providers.pbs.bidders] -mocktioneer = { bid = 2.00 } -criteo = { networkId = 123456, pubid = "123456" } - [[slot]] id = "homepage_header_ad" gam_unit_path = "/88059007/autoblog/homepage" @@ -35,10 +31,6 @@ zone = "header" [slot.providers.aps] slot_id = "aps-slot-homepage-header" -[slot.providers.pbs.bidders] -mocktioneer = { bid = 2.00 } -criteo = { networkId = 123456, pubid = "123456" } - [[slot]] id = "homepage_footer_ad" gam_unit_path = "/88059007/autoblog/homepage" @@ -53,7 +45,3 @@ zone = "fixedBottom" [slot.providers.aps] slot_id = "aps-slot-homepage-footer" - -[slot.providers.pbs.bidders] -mocktioneer = { bid = 1.50 } -criteo = { networkId = 123456, pubid = "123456" } From 03d39f29bc2d643c25b9d85ccbae9fc304ee5d5c Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Fri, 15 May 2026 16:16:51 +0530 Subject: [PATCH 62/67] Clarify and test APS floor price enforcement in mediation path - Rewrite misleading comment in apply_floor_prices: price=None bids pass through in the parallel-only path because decoding is deferred; in the mediation path the mediator decodes prices before this function runs - Add test: decoded APS bid below slot floor is dropped - Add test: decoded APS bid at or above slot floor is kept Closes #698 --- .../src/auction/orchestrator.rs | 83 ++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/crates/trusted-server-core/src/auction/orchestrator.rs b/crates/trusted-server-core/src/auction/orchestrator.rs index 17fe405d..58f46b14 100644 --- a/crates/trusted-server-core/src/auction/orchestrator.rs +++ b/crates/trusted-server-core/src/auction/orchestrator.rs @@ -542,7 +542,11 @@ impl AuctionOrchestrator { let starting_count = winning_bids.len(); winning_bids.retain(|slot_id, bid| match floor_prices.get(slot_id) { Some(floor) => { - // Bids without price (e.g., APS) pass through - floor checked in mediation + // price=None means the SSP returned an encoded price (e.g. APS amznbid). + // In the parallel-only path this bid cannot yet be floor-checked; it passes + // through and will be decoded (and re-checked) by the mediation layer. + // In the mediation path, mediation decodes prices before calling this + // function, so any bid still carrying price=None is dropped upstream. match bid.price { Some(price) if price >= *floor => true, Some(_) => { @@ -554,7 +558,7 @@ impl AuctionOrchestrator { } None => { log::debug!( - "Passing bid with encoded price for slot '{}' - floor check deferred to mediation", + "Passing encoded-price bid for slot '{}' - price not yet decoded", slot_id ); true @@ -1305,4 +1309,79 @@ mod tests { "Price should still be None (not decoded yet)" ); } + + #[test] + fn test_apply_floor_prices_drops_decoded_aps_bid_below_floor() { + // After mediation decodes an APS bid, apply_floor_prices must enforce the + // slot floor on the resulting price=Some(x) value. This test simulates the + // state of a bid after mediator decoding: price is Some, amznbid is gone. + let orchestrator = AuctionOrchestrator::new(AuctionConfig::default()); + let mut floor_prices = HashMap::new(); + floor_prices.insert("atf".to_string(), 0.50); + + let mut winning_bids = HashMap::new(); + winning_bids.insert( + "atf".to_string(), + Bid { + slot_id: "atf".to_string(), + price: Some(0.30), // decoded APS price — below $0.50 floor + currency: "USD".to_string(), + creative: Some("
APS Ad
".to_string()), + adomain: None, + bidder: "aps".to_string(), + width: 300, + height: 250, + nurl: None, + burl: None, + ad_id: None, + metadata: HashMap::new(), + }, + ); + + let filtered = orchestrator.apply_floor_prices(winning_bids, &floor_prices); + + assert!( + filtered.is_empty(), + "Decoded APS bid below slot floor should be dropped" + ); + } + + #[test] + fn test_apply_floor_prices_keeps_decoded_aps_bid_at_or_above_floor() { + let orchestrator = AuctionOrchestrator::new(AuctionConfig::default()); + let mut floor_prices = HashMap::new(); + floor_prices.insert("atf".to_string(), 0.50); + + let mut winning_bids = HashMap::new(); + winning_bids.insert( + "atf".to_string(), + Bid { + slot_id: "atf".to_string(), + price: Some(0.75), // decoded APS price — above floor + currency: "USD".to_string(), + creative: Some("
APS Ad
".to_string()), + adomain: None, + bidder: "aps".to_string(), + width: 300, + height: 250, + nurl: None, + burl: None, + ad_id: None, + metadata: HashMap::new(), + }, + ); + + let filtered = orchestrator.apply_floor_prices(winning_bids, &floor_prices); + + assert_eq!( + filtered.len(), + 1, + "Decoded APS bid at or above floor should be kept" + ); + assert_eq!( + filtered.get("atf").expect("atf should be present").price, + Some(0.75), + "Price should be preserved" + ); + } } From f09eb34ffac5b3230a7a1c4af5804890e9b4954b Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Fri, 15 May 2026 16:24:28 +0530 Subject: [PATCH 63/67] Document and test /auction API contract for non-Prebid.js callers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Expand handle_auction doc: inline-params vs stored-request paths, config passthrough and allowed_context_keys, response headers - Document AdRequest, AdUnit, BidConfig with the stored-request contract: absent/empty bids → empty bidders map → PBS stored-request fallback - Add tests for convert_tsjs_to_auction_request: - No bids → empty bidders map (stored-request path) - Inline bids → bidders map populated - Allowed config key passes through; disallowed key dropped - Invalid 3-element banner size returns error Closes #699 --- .../src/auction/endpoints.rs | 41 +++- .../src/auction/formats.rs | 195 +++++++++++++++++- 2 files changed, 230 insertions(+), 6 deletions(-) diff --git a/crates/trusted-server-core/src/auction/endpoints.rs b/crates/trusted-server-core/src/auction/endpoints.rs index 0430f08b..5a9ac6f1 100644 --- a/crates/trusted-server-core/src/auction/endpoints.rs +++ b/crates/trusted-server-core/src/auction/endpoints.rs @@ -16,11 +16,44 @@ use super::formats::{convert_to_openrtb_response, convert_tsjs_to_auction_reques use super::types::AuctionContext; use super::AuctionOrchestrator; -/// Handle auction request from /auction endpoint. +/// Handle auction request from `POST /auction`. /// -/// This is the main entry point for running header bidding auctions. -/// It orchestrates bids from multiple providers (Prebid, APS, GAM, etc.) and returns -/// the winning bids in `OpenRTB` format with creative HTML inline in the `adm` field. +/// Accepts a JSON body matching [`AdRequest`][`super::formats::AdRequest`]. +/// The minimum valid request is: +/// +/// ```json +/// { +/// "adUnits": [{ +/// "code": "atf_sidebar_ad", +/// "mediaTypes": { "banner": { "sizes": [[300, 250]] } } +/// }] +/// } +/// ``` +/// +/// ## Bidder params: inline vs. stored-request +/// +/// Each ad unit's `bids` array is **optional**. When absent or empty the PBS +/// integration falls back to a stored-request keyed by the unit's `code` +/// field (`imp.ext.prebid.storedrequest = { id: "" }`). A PBS stored +/// request must therefore exist for every slot code that omits inline params. +/// +/// When `bids` is supplied, each entry's `bidder`/`params` pair is forwarded +/// directly as `imp.ext.prebid.bidder.`. +/// +/// ## Context passthrough (`config`) +/// +/// The optional `config` object is filtered through +/// [`auction.allowed_context_keys`][`crate::settings::AuctionConfig::allowed_context_keys`]. +/// Only keys listed there reach the auction providers (e.g. `"permutive_segments"`). +/// All other keys are silently dropped. Values must be either strings or arrays of +/// strings. +/// +/// ## Response +/// +/// Returns an `OpenRTB 2.x` response. Creative HTML is inlined in each bid's +/// `adm` field after sanitisation and first-party URL rewriting. Response +/// headers include `X-TS-EC` (the caller's Edge Cookie ID) and +/// `X-TS-EC-Fresh` (a freshly generated ID for cookie renewal). /// /// # Errors /// diff --git a/crates/trusted-server-core/src/auction/formats.rs b/crates/trusted-server-core/src/auction/formats.rs index 5237921a..53c6474a 100644 --- a/crates/trusted-server-core/src/auction/formats.rs +++ b/crates/trusted-server-core/src/auction/formats.rs @@ -28,7 +28,11 @@ use super::types::{ PublisherInfo, SiteInfo, UserInfo, }; -/// Request body format for auction endpoints (tsjs/Prebid.js format). +/// Request body for `POST /auction` (tsjs / Prebid.js wire format). +/// +/// `adUnits` lists the placements to bid on. `config` carries optional +/// context values (e.g. audience segments) filtered through +/// [`auction.allowed_context_keys`][`crate::settings::AuctionConfig::allowed_context_keys`]. #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct AdRequest { @@ -36,6 +40,15 @@ pub struct AdRequest { pub config: Option, } +/// A single ad placement in an [`AdRequest`]. +/// +/// `code` identifies the slot (e.g. `"atf_sidebar_ad"`) and becomes the +/// impression ID in the outgoing `OpenRTB` request. +/// +/// `bids` is optional. When absent or empty the PBS provider falls back to +/// a stored-request keyed by `code` (`imp.ext.prebid.storedrequest.id`). +/// When present, each entry's params are forwarded inline to PBS as +/// `imp.ext.prebid.bidder.`. #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct AdUnit { @@ -44,7 +57,11 @@ pub struct AdUnit { pub bids: Option>, } -/// Bidder configuration from the request. +/// Inline bidder params for one SSP within an [`AdUnit`]. +/// +/// `params` is passed verbatim to the corresponding PBS bidder adapter. +/// When the `bids` array is absent, the slot falls back to PBS stored +/// requests — see [`AdUnit`] for details. #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct BidConfig { @@ -318,3 +335,177 @@ pub fn convert_to_openrtb_response( .with_header(HEADER_X_TS_EC_FRESH, &auction_request.user.fresh_id) .with_body(body_bytes)) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::consent::ConsentContext; + use crate::platform::test_support::noop_services; + use crate::test_support::tests::crate_test_settings_str; + use fastly::http::Method; + use fastly::Request; + + fn make_settings() -> Settings { + Settings::from_toml(&crate_test_settings_str()).expect("should parse test settings") + } + + fn make_req() -> Request { + Request::new(Method::POST, "https://test-publisher.com/auction") + } + + fn call_convert(body: &AdRequest) -> AuctionRequest { + let settings = make_settings(); + let services = noop_services(); + let req = make_req(); + convert_tsjs_to_auction_request( + body, + &settings, + &services, + &req, + ConsentContext::default(), + "test-ec-id", + None, + ) + .expect("should convert without error") + } + + #[test] + fn no_bids_produces_empty_bidders_map() { + // An ad unit with no `bids` array must produce an empty bidders map. + // An empty bidders map triggers the PBS stored-request fallback: + // the PBS provider sets imp.ext.prebid.storedrequest = { id: "" }. + let body = AdRequest { + ad_units: vec![AdUnit { + code: "atf_sidebar_ad".to_string(), + media_types: Some(MediaTypes { + banner: Some(BannerUnit { + sizes: vec![vec![300, 250]], + }), + }), + bids: None, + }], + config: None, + }; + + let auction_request = call_convert(&body); + + assert_eq!(auction_request.slots.len(), 1, "should have one slot"); + let slot = &auction_request.slots[0]; + assert_eq!(slot.id, "atf_sidebar_ad", "slot id should match unit code"); + assert!( + slot.bidders.is_empty(), + "absent bids array should yield empty bidders map (PBS stored-request path)" + ); + } + + #[test] + fn inline_bids_populate_bidders_map() { + // When bids are supplied, each bidder+params pair should appear in the + // slot's bidders map so PBS receives inline params. + let body = AdRequest { + ad_units: vec![AdUnit { + code: "homepage_header_ad".to_string(), + media_types: Some(MediaTypes { + banner: Some(BannerUnit { + sizes: vec![vec![970, 90]], + }), + }), + bids: Some(vec![BidConfig { + bidder: "kargo".to_string(), + params: serde_json::json!({ "placementId": "client_123" }), + }]), + }], + config: None, + }; + + let auction_request = call_convert(&body); + + let slot = &auction_request.slots[0]; + assert!( + slot.bidders.contains_key("kargo"), + "kargo bidder should be present in slot bidders map" + ); + assert_eq!( + slot.bidders["kargo"]["placementId"], "client_123", + "bidder params should be forwarded verbatim" + ); + } + + #[test] + fn config_allowed_key_passes_through() { + // Keys in auction.allowed_context_keys must reach the auction context. + // The test settings do not set allowed_context_keys so the default + // (empty) applies — verify a key is NOT present rather than IS. + // To test the allow-list, inject a key via a custom settings string. + let settings_str = format!( + "{}\n[auction]\nallowed_context_keys = [\"permutive_segments\"]\n", + crate_test_settings_str() + ); + let settings = Settings::from_toml(&settings_str).expect("should parse"); + let services = noop_services(); + let req = make_req(); + + let body = AdRequest { + ad_units: vec![], + config: Some(serde_json::json!({ + "permutive_segments": ["seg1", "seg2"], + "disallowed_key": "should be dropped", + })), + }; + + let auction_request = convert_tsjs_to_auction_request( + &body, + &settings, + &services, + &req, + ConsentContext::default(), + "test-ec-id", + None, + ) + .expect("should convert"); + + assert!( + auction_request.context.contains_key("permutive_segments"), + "allowed key should be in auction context" + ); + assert!( + !auction_request.context.contains_key("disallowed_key"), + "unlisted key should be dropped" + ); + } + + #[test] + fn invalid_banner_size_returns_error() { + // Banner sizes must be [width, height] pairs; a 3-element size is invalid. + let body = AdRequest { + ad_units: vec![AdUnit { + code: "bad_slot".to_string(), + media_types: Some(MediaTypes { + banner: Some(BannerUnit { + sizes: vec![vec![300, 250, 99]], // invalid — 3 elements + }), + }), + bids: None, + }], + config: None, + }; + + let settings = make_settings(); + let services = noop_services(); + let req = make_req(); + let result = convert_tsjs_to_auction_request( + &body, + &settings, + &services, + &req, + ConsentContext::default(), + "test-ec-id", + None, + ); + + assert!( + result.is_err(), + "3-element banner size should return an error" + ); + } +} From a03c70a8cbbf6065eacb34c5a1ee1ad53c556025 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Fri, 15 May 2026 17:07:35 +0530 Subject: [PATCH 64/67] Verify and document graceful degradation when no slots match URL - Add debug log at no-match gate in handle_publisher_request and handle_page_bids so operators can confirm the feature is inactive on non-article URLs without reading source code - Add test: empty slots file (kill-switch) returns slots:[] bids:{} - Add test: URL not matching any slot pattern returns slots:[] bids:{} Closes #700 --- crates/trusted-server-core/src/publisher.rs | 117 ++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index 18fc62c8..a4773a62 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -905,6 +905,13 @@ pub async fn handle_publisher_request( let should_run_auction = is_get && !is_prefetch && !is_bot && !matched_slots.is_empty() && consent_allows_auction; + if matched_slots.is_empty() && settings.creative_opportunities.is_some() { + log::debug!( + "No creative opportunity slots matched path '{}' — skipping auction and injection", + request_path + ); + } + let auction_timeout_ms = settings .creative_opportunities .as_ref() @@ -1454,6 +1461,13 @@ pub async fn handle_page_bids( .as_ref() .is_some_and(|tcf| tcf.has_purpose_consent(1)); + if matched_slots.is_empty() { + log::debug!( + "No creative opportunity slots matched path '{}' — skipping auction", + path_param + ); + } + let winning_bids = if !matched_slots.is_empty() && consent_allows_auction { let auction_request = build_auction_request( &matched_slots, @@ -2673,4 +2687,107 @@ mod tests { ); } } + + mod page_bids_no_match_tests { + use super::super::*; + use crate::auction::AuctionOrchestrator; + use crate::creative_opportunities::{ + CreativeOpportunitiesFile, CreativeOpportunityFormat, CreativeOpportunitySlot, + }; + use crate::platform::test_support::noop_services; + use crate::test_support::tests::crate_test_settings_str; + use fastly::http::Method; + use fastly::Request; + + fn settings_with_co() -> Settings { + let toml = format!( + "{}\n[creative_opportunities]\ngam_network_id = \"12345\"\n", + crate_test_settings_str() + ); + Settings::from_toml(&toml).expect("should parse settings with creative_opportunities") + } + + fn file_with_article_slot() -> CreativeOpportunitiesFile { + CreativeOpportunitiesFile { + slots: vec![CreativeOpportunitySlot { + id: "atf".to_string(), + gam_unit_path: None, + div_id: None, + page_patterns: vec!["/20**".to_string()], + formats: vec![CreativeOpportunityFormat { + width: 300, + height: 250, + media_type: crate::auction::types::MediaType::Banner, + }], + floor_price: Some(0.50), + targeting: Default::default(), + providers: Default::default(), + }], + } + } + + fn make_page_bids_request(path: &str) -> Request { + Request::new( + Method::GET, + format!("https://test-publisher.com/_ts/page-bids?path={path}"), + ) + } + + #[tokio::test] + async fn empty_slots_file_returns_empty_slots_and_bids() { + // Spec §8 kill-switch: creative-opportunities.toml with zero slots disables + // all server-side auction activity and injection. + let settings = settings_with_co(); + let orchestrator = AuctionOrchestrator::new(settings.auction.clone()); + let services = noop_services(); + let slots_file = CreativeOpportunitiesFile { slots: vec![] }; + let req = make_page_bids_request("/2024/01/my-article/"); + + let response = handle_page_bids(&settings, &orchestrator, &services, &slots_file, req) + .await + .expect("should return ok response"); + + let body: serde_json::Value = + serde_json::from_slice(&response.into_body_bytes()).expect("should be json"); + + assert_eq!( + body["slots"].as_array().expect("slots should be array").len(), + 0, + "empty slots file should produce zero injected slots" + ); + assert_eq!( + body["bids"].as_object().expect("bids should be object").len(), + 0, + "empty slots file should produce zero bids" + ); + } + + #[tokio::test] + async fn url_not_matching_any_pattern_returns_empty_response() { + // Slots exist but request path does not match — no auction, no injection. + let settings = settings_with_co(); + let orchestrator = AuctionOrchestrator::new(settings.auction.clone()); + let services = noop_services(); + let slots_file = file_with_article_slot(); // slot matches /20** only + let req = make_page_bids_request("/about"); // does not match + + let response = handle_page_bids(&settings, &orchestrator, &services, &slots_file, req) + .await + .expect("should return ok response"); + + let body: serde_json::Value = + serde_json::from_slice(&response.into_body_bytes()).expect("should be json"); + + assert_eq!( + body["slots"].as_array().expect("slots should be array").len(), + 0, + "non-matching URL should produce zero injected slots" + ); + assert_eq!( + body["bids"].as_object().expect("bids should be object").len(), + 0, + "non-matching URL should produce zero bids" + ); + } + } } From 421833399efc17692a869e7355d5f105e6b99944 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Fri, 15 May 2026 17:10:45 +0530 Subject: [PATCH 65/67] Format publisher.rs with cargo fmt --- crates/trusted-server-core/src/publisher.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/trusted-server-core/src/publisher.rs b/crates/trusted-server-core/src/publisher.rs index a4773a62..235be817 100644 --- a/crates/trusted-server-core/src/publisher.rs +++ b/crates/trusted-server-core/src/publisher.rs @@ -2751,12 +2751,18 @@ mod tests { serde_json::from_slice(&response.into_body_bytes()).expect("should be json"); assert_eq!( - body["slots"].as_array().expect("slots should be array").len(), + body["slots"] + .as_array() + .expect("slots should be array") + .len(), 0, "empty slots file should produce zero injected slots" ); assert_eq!( - body["bids"].as_object().expect("bids should be object").len(), + body["bids"] + .as_object() + .expect("bids should be object") + .len(), 0, "empty slots file should produce zero bids" ); @@ -2779,12 +2785,18 @@ mod tests { serde_json::from_slice(&response.into_body_bytes()).expect("should be json"); assert_eq!( - body["slots"].as_array().expect("slots should be array").len(), + body["slots"] + .as_array() + .expect("slots should be array") + .len(), 0, "non-matching URL should produce zero injected slots" ); assert_eq!( - body["bids"].as_object().expect("bids should be object").len(), + body["bids"] + .as_object() + .expect("bids should be object") + .len(), 0, "non-matching URL should produce zero bids" ); From 0762999f37b977ffe7d61705cac5bb266ce32140 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Fri, 15 May 2026 17:13:35 +0530 Subject: [PATCH 66/67] Document scroll/refresh handoff contract between TS and slim-Prebid - Clarify in handle_auction doc that /auction is for initial render and programmatic callers; scroll/refresh/SPA navigation is slim-Prebid's domain in Phase 1 - Note Phase 2 slot-template-aware refresh API as deferred future work - Add head_inserts doc clarifying __tsAdInit handles initial render only; slotRenderEnded fires win beacons but does not trigger refresh auctions Closes #702 --- .../trusted-server-core/src/auction/endpoints.rs | 14 ++++++++++++++ crates/trusted-server-core/src/integrations/gpt.rs | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/crates/trusted-server-core/src/auction/endpoints.rs b/crates/trusted-server-core/src/auction/endpoints.rs index 5a9ac6f1..5d5bb292 100644 --- a/crates/trusted-server-core/src/auction/endpoints.rs +++ b/crates/trusted-server-core/src/auction/endpoints.rs @@ -55,6 +55,20 @@ use super::AuctionOrchestrator; /// headers include `X-TS-EC` (the caller's Edge Cookie ID) and /// `X-TS-EC-Fresh` (a freshly generated ID for cookie renewal). /// +/// ## Scroll, refresh, and SPA navigation +/// +/// This endpoint is intended for **initial page render** and **programmatic +/// callers** (e.g. slim-Prebid, native apps, server-to-server integrations). +/// It is **not** the intended path for scroll or GPT refresh events. +/// +/// In Phase 1, slim-Prebid owns scroll and refresh: it runs post-`window.load`, +/// listens for GPT refresh events, and runs client-side auctions independently +/// of this endpoint. SPAs that use pushState routing do not trigger TS page-level +/// auctions — slim-Prebid handles those cases too. +/// +/// A slot-template-aware refresh API (`POST /auction/refresh`) is deferred to a +/// future phase and not designed here. +/// /// # Errors /// /// Returns an error if: diff --git a/crates/trusted-server-core/src/integrations/gpt.rs b/crates/trusted-server-core/src/integrations/gpt.rs index 690ba048..85ea800e 100644 --- a/crates/trusted-server-core/src/integrations/gpt.rs +++ b/crates/trusted-server-core/src/integrations/gpt.rs @@ -437,6 +437,19 @@ impl IntegrationHeadInjector for GptIntegration { GPT_INTEGRATION_ID } + /// Injects the `__tsAdInit` bootstrap script into ``. + /// + /// ## Scroll / refresh handoff contract (Phase 1) + /// + /// `__tsAdInit` handles **initial render only**: it wires server-side bid + /// targeting into GPT slots and fires win beacons (`nurl`/`burl`) via + /// `slotRenderEnded`. It does **not** trigger refresh auctions or handle + /// GPT slot refresh events. + /// + /// Post-`window.load`, slim-Prebid takes over: it listens for GPT refresh + /// events, runs client-side auctions, and sets targeting for subsequent + /// impressions. SPA pushState navigation is also slim-Prebid's domain. + /// The `POST /auction` endpoint is not involved in scroll or refresh flows. fn head_inserts(&self, _ctx: &IntegrationHtmlContext<'_>) -> Vec { vec![ "