Conversation
aram356
left a comment
There was a problem hiding this comment.
Thanks for making changes. Few more things introduced with recent changes that needs addressing.
cdd3763 to
8eed73d
Compare
8eed73d to
f24ada2
Compare
There was a problem hiding this comment.
Summary
👍 Well-designed feature that forwards Permutive audience segments from the client through the auction pipeline to the ad server as URL query parameters. The architecture is clean — a generic context provider registry on the JS side and a config-driven allowed_context_keys allowlist on the Rust side prevent tight coupling between integrations and the auction system. Good security posture with server-side allowlisting.
Findings
🔧 High (P1)
unwrap()in tests:context.rs:233,239,245— three deserialization tests use.unwrap()instead of.expect("should ..."). (inline comment)
🤔 Medium (P2)
ContextValue::Number(f64)precision:context.rs:24— works for current string-based segments, butf64loses precision for integers above 2^53. Low risk now but worth noting.HashSetrebuilt per request:formats.rs:144—allowed_context_keysis converted fromVectoHashSeton every auction request. Negligible with a small list, but could be stored as aHashSetinAuctionConfig. (inline comment)- Comment names Permutive but code is generic:
adserver_mock.rs:286— thecontext_query_paramsmechanism is integration-agnostic, but the comment says "Permutive segments". (inline comment) - Bare
catchswallows all errors:segments.ts:58— catches everything including unexpected errors, logs a misleading message. (inline comment)
⛏ Low (P3)
- Test names use
test_prefix:context.rs,adserver_mock.rs,settings.rs— consistent with existing codebase style. - No end-to-end test for context provider registration:
segments.test.tscoversgetPermutiveSegments()but theregisterContextProvidercall inindex.tsisn't tested e2e (registry itself is tested incontext.test.ts).
CI Status
- cargo fmt: PASS
- cargo test: PASS
- vitest: PASS
- CodeQL: PASS
|
|
||
| #[test] | ||
| fn test_context_value_deserialize_array() { | ||
| let v: ContextValue = serde_json::from_str(r#"["a","b"]"#).unwrap(); |
There was a problem hiding this comment.
🔧 unwrap() in tests: These three deserialization tests (lines 233, 239, 245) use .unwrap() instead of .expect("should ...") per project conventions.
Fix:
let v: ContextValue = serde_json::from_str(r#"["a","b"]"#).expect("should deserialize string list");
// ...
let v: ContextValue = serde_json::from_str(r#""hello""#).expect("should deserialize text");
// ...
let v: ContextValue = serde_json::from_str("42").expect("should deserialize number");| // arbitrary data by a malicious client payload. | ||
| let allowed: HashSet<&str> = settings | ||
| .auction | ||
| .allowed_context_keys |
There was a problem hiding this comment.
🤔 HashSet rebuilt per request: allowed_context_keys is converted from Vec<String> to HashSet<&str> on every auction request. With a small allowlist this is negligible, but could be avoided by storing the allowlist as a HashSet<String> in AuctionConfig at deserialization time.
|
|
||
| log::debug!("AdServer Mock: mediation request: {:?}", mediation_req); | ||
|
|
||
| // Build endpoint URL with optional Permutive segments query string |
There was a problem hiding this comment.
🤔 Comment names Permutive but code is generic: The context_query_params mechanism is integration-agnostic, but this comment says "Permutive segments query string". Future integrations using the same mechanism would make this misleading.
Fix:
// Build endpoint URL with context-driven query parameters| } | ||
| } | ||
| } | ||
| } catch { |
There was a problem hiding this comment.
🤔 Bare catch swallows all errors: This catches everything (JSON parse failures, property access errors, out-of-memory) and logs "failed to read from localStorage" which isn't accurate for all error types.
Fix: Consider logging the error object for debuggability:
} catch (err) {
log.debug('getPermutiveSegments: failed to read segments', err);
}
Passes Permutive audience segments from the browser through to the ad server mediation endpoint, enabling segment-targeted ad decisioning.
Changes
Closes #132
How to Manually Test
Load the page with the tsjs tag. In the DevTools Console, filter for context — you should see a log like:
requestAds: payload { units: N, contextKeys: ["permutive_segments"] }
Verify segments are sent to the server
Open DevTools Network tab and filter for /auction.
Trigger an ad request.
Inspect the /auction POST request body — the config object should contain: