Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ jobs:
- name: Run tests
run: cargo test-fastly

- name: Verify identity conformance selection and Fastly lifecycle
run: |
node --test scripts/test-identity-conformance.test.mjs
node scripts/test-identity-conformance.mjs fastly

- name: Run Fastly absolute deadline slow-network tests
run: node crates/trusted-server-adapter-fastly/tests/deadline-origin.mjs "$(command -v viceroy)"

Expand Down Expand Up @@ -165,6 +170,9 @@ jobs:
- name: Run Axum adapter tests
run: cargo test-axum

- name: Verify native identity conformance rejection controls
run: cargo test -p trusted-server-core --lib identity_conformance

- name: Verify finalization harness process cleanup
run: cargo test --manifest-path crates/trusted-server-integration-tests/Cargo.toml --target x86_64-unknown-linux-gnu --features html-eof-runtime --test html_eof_runtime

Expand Down
277 changes: 277 additions & 0 deletions crates/trusted-server-adapter-fastly/src/identity_conformance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
//! EC conformance through the Fastly application and its production finalizer.

use crate::sandbox::RetainedApp;
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use edgezero_adapter_fastly::delivery::FastlyResponseCommitState;
use edgezero_adapter_fastly::delivery::testing::response_delivery;
use edgezero_adapter_fastly::request::FastlyService;
use edgezero_core::http::StatusCode;
use edgezero_core::response_egress_framing::ResponseEgressFramingMode;
use futures::executor::block_on;
use std::cell::RefCell;
use trusted_server_core::ec::kv::KvIdentityGraph;
use trusted_server_core::test_support::identity_conformance::{
Adapter, Consent, Geo, Navigation, Observation, RecordingStore, Scenario,
};
use trusted_server_core::test_support::scripted_client::{
ScriptedHttpClient, ScriptedReply, TestClock,
};

use edgezero_core::outbound::HttpClient;
use std::time::{SystemTime, UNIX_EPOCH};

use super::{
ClientRequestValues, FinalizeInputs, assemble_app, handle_delivery, with_finalization,
};
use crate::platform::RequestGeo;
use crate::sandbox::SandboxCounters;
use crate::tinybird::TelemetryQueue;
use fastly::Request as FastlyRequest;
use trusted_server_core::ec::device::DeviceSignals;
use trusted_server_core::geo::GeoInfo;
use trusted_server_core::platform::ClientInfo;
use trusted_server_core::settings::Settings;

const DOCUMENT: &str = "<html><head></head><body>Example article</body></html>";
const ORIGIN: &str = "https://origin.example.com/article";
const USER_AGENT: &str =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/146.0.0.0 Safari/537.36";

thread_local! {
static GRAPH: RefCell<Option<KvIdentityGraph>> = const { RefCell::new(None) };
}

pub(crate) fn fixture_graph(settings: &Settings) -> Option<KvIdentityGraph> {
if settings.ec.ec_store.as_deref() == Some("identity-conformance") {
GRAPH.with_borrow(Clone::clone)
} else {
None
}
}

struct GraphGuard(Option<KvIdentityGraph>);

impl GraphGuard {
fn install(store: &RecordingStore) -> Self {
Self(GRAPH.replace(Some(KvIdentityGraph::new(store.clone()))))
}
}

impl Drop for GraphGuard {
fn drop(&mut self) {
GRAPH.replace(self.0.take());
}
}

fn settings() -> Settings {
Settings::from_toml(
r#"
[[handlers]]
path = "^/_ts/admin"
username = "example-admin"
password = "example-password"
[publisher]
domain = "publisher.example.com"
cookie_domain = ".publisher.example.com"
origin_url = "https://origin.example.com"
proxy_secret = "example-proxy-secret"
[ec]
passphrase = "example-secret-at-least-32-bytes-long"
ec_store = "identity-conformance"
[request_signing]
enabled = false
config_store_id = "example-config"
secret_store_id = "example-secrets"
"#,
)
.expect("should parse conformance settings")
}

fn consent_cookie(granted: bool) -> String {
let mut core = [0_u8; 32];
// TCF v2, followed by current Created and LastUpdated decisecond fields.
core[0] = 2 << 2;
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("should read fixture time")
.as_secs()
* 10;
for offset in [6, 42] {
for bit in 0..36 {
if now & (1 << (35 - bit)) != 0 {
core[(offset + bit) / 8] |= 1 << (7 - (offset + bit) % 8);
}
}
}
// Purpose 1 is the first purpose-consent bit, at offset 152.
core[19] = if granted { 0x80 } else { 0 };
format!("euconsent-v2={}", URL_SAFE_NO_PAD.encode(core))
}

struct FastlyAdapter {
settings: Settings,
retained: RetainedApp,
origin: ScriptedHttpClient,
store: RecordingStore,
}

impl FastlyAdapter {
fn new(store: RecordingStore) -> Self {
let settings = settings();
let clock = TestClock::new();
let origin = ScriptedHttpClient::new(&clock);
let mut retained = assemble_app(Ok(settings.clone()), &"")
.expect("should assemble the conformance application");
retained.app.set_monotonic_clock(clock.monotonic_clock());
Self {
settings,
retained,
origin,
store,
}
}
}

impl Adapter for FastlyAdapter {
fn navigate(&mut self, navigation: &Navigation) -> Observation {
let _guard = GraphGuard::install(&self.store);
self.origin.respond_to(
edgezero_core::http::Method::GET,
ORIGIN,
ScriptedReply::status(StatusCode::OK)
.header("content-type", "text/html")
.body(DOCUMENT),
);
let geo = (navigation.geo == Geo::Gdpr).then(|| GeoInfo {
country: "DE".to_owned(),
city: String::new(),
continent: "EU".to_owned(),
latitude: 0.0,
longitude: 0.0,
metro_code: 0,
region: None,
asn: None,
});
let counters = SandboxCounters {
ordinal: 1,
builds: 1,
request_id: "example-conformance-request".to_owned(),
};
let inputs = FinalizeInputs {
settings: &self.settings,
geo: geo.as_ref(),
counters: &counters,
};
let mut cookies = Vec::new();
match navigation.consent {
Consent::Granted => cookies.push(consent_cookie(true)),
Consent::Denied => cookies.push(consent_cookie(false)),
Consent::Missing => {}
}
if let Some(identity) = &navigation.ec_cookie {
cookies.push(format!("ts-ec={identity}"));
}
let mut request = FastlyRequest::get("https://publisher.example.com/article")
.with_header("sec-fetch-dest", navigation.destination)
.with_header(
"accept",
if navigation.destination == "document" {
"text/html"
} else {
"*/*"
},
)
.with_header("user-agent", USER_AGENT)
.with_header("cookie", cookies.join("; "));
// Production's native read and `prepare` run unchanged. The fixture
// replaces only the values Viceroy cannot supply: a routable client IP,
// its geo result and, for browser scenarios, JA4-backed device signals.
let mut values = ClientRequestValues::read(&mut request, &self.settings, false);
let ip = Some("192.0.2.7".parse().expect("should parse fixture IP"));
values.client_info = ClientInfo {
client_ip: ip,
..ClientInfo::default()
};
values.request_geo = if navigation.geo == Geo::Failed {
RequestGeo::failing(ip)
} else {
RequestGeo::new(ip, geo.clone())
};
if navigation.browser {
values.device_signals = Some(DeviceSignals::derive(
USER_AGENT,
Some("t13d1516h2_example_example"),
None,
));
}
let queue = TelemetryQueue::default();
let mut observation = None;
let origin = self.origin.clone();
let delivery = with_finalization(&inputs, |finalize| {
FastlyService::new(&self.retained.app).run_request_with_hooks_for_test(
request,
|_request, extensions| {
values.prepare(extensions, &self.settings, &queue);
extensions.insert(HttpClient::with_client(origin));
},
finalize,
|envelope| {
let (prepared, _policy, mut attempt, clock) = envelope
.begin(ResponseEgressFramingMode::HostAutomatic)
.unwrap_or_else(|_| panic!("should prepare the document response"));
let response = prepared.into_response();
self.store.response_released();
let status = response.status();
let cookies = response
.headers()
.get_all("set-cookie")
.iter()
.map(|value| {
value
.to_str()
.expect("should decode response cookie")
.to_owned()
})
.collect();
assert!(
attempt.begin_writing(),
"should start releasing the response"
);
let body = block_on(response.into_body().into_bytes_bounded(1024 * 1024))
.expect("should consume the complete document");
assert!(
attempt.account_bytes(body.len() as u64, clock.now()),
"should account body bytes"
);
assert!(
attempt.complete(clock.now()),
"should complete the document response"
);
observation = Some(Observation {
status,
cookies,
body: body.to_vec(),
});
response_delivery(
FastlyResponseCommitState::Committed,
attempt.into_terminal_report(),
Ok(()),
)
},
)
});
handle_delivery(delivery, &counters, |_| {});
observation.expect("should deliver a finalized response")
}
}

#[test]
fn identity_conformance_fastly() {
for scenario in Scenario::FASTLY_BASELINE {
let store = RecordingStore::new(scenario == Scenario::WriteFailure);
let mut adapter = FastlyAdapter::new(store.clone());
log::info!("Running Fastly identity scenario {scenario:?}");
scenario.verify(&mut adapter, &store);
}
}
30 changes: 23 additions & 7 deletions crates/trusted-server-adapter-fastly/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mod deadline_network;
mod delivery;
mod ec_kv;
mod esi_assembly;
#[cfg(test)]
mod identity_conformance;
mod management_api;
mod middleware;
mod platform;
Expand Down Expand Up @@ -352,18 +354,28 @@ fn serve(
geo: request_geo.geo(),
counters,
};
let delivery = send_request_with_registries_and_hooks(
&retained.app,
TrustedServerApp::stores(),
req,
prepare,
|response: &mut HttpResponse| finalize_response(response, &inputs),
);
let delivery = with_finalization(&inputs, |finalize| {
send_request_with_registries_and_hooks(
&retained.app,
TrustedServerApp::stores(),
req,
prepare,
finalize,
)
});
handle_delivery(delivery, counters, |state| {
run_post_delivery(state, &queue, settings, &counters.log_context());
});
}

/// Supplies the production finalization callback to a request lifecycle.
fn with_finalization<T>(
inputs: &FinalizeInputs<'_>,
send: impl FnOnce(&mut dyn FnMut(&mut HttpResponse) -> Option<FinalizeState>) -> T,
) -> T {
send(&mut |response| finalize_response(response, inputs))
}

/// What the native work reads from the SDK client request before `EdgeZero` converts it.
///
/// Only the client request exposes the trusted forwarder, the resolved client
Expand Down Expand Up @@ -730,6 +742,10 @@ fn build_ja4_debug_response(req: &FastlyRequest) -> FastlyResponse {
}

pub(crate) fn maybe_identity_graph(settings: &Settings) -> Option<KvIdentityGraph> {
#[cfg(test)]
if let Some(graph) = identity_conformance::fixture_graph(settings) {
return Some(graph);
}
settings
.ec
.ec_store
Expand Down
Loading
Loading