Skip to main content

zendriver_interception/
builder.rs

1//! [`InterceptBuilder`] — fluent rule + pattern registration.
2//!
3//! Two-phase API:
4//! - **Configure**: chain [`block`], [`block_hosts`], [`redirect`], [`respond`],
5//!   [`modify_request`], [`modify_response`] for declarative rules, plus
6//!   [`pattern`] / [`at_request`] / [`at_response`] / [`resource`] to control
7//!   which CDP `Fetch.RequestPattern` entries are sent on `Fetch.enable`.
8//! - **Activate**: [`start`](InterceptBuilder::start) spawns the actor task
9//!   (T6) with the registered rules + patterns, returning an
10//!   [`InterceptHandle`] for RAII teardown. Alternatively,
11//!   [`subscribe`](InterceptBuilder::subscribe) returns a
12//!   `Stream<Item = PausedRequest>` for the manual escape-hatch path —
13//!   callers drive Chrome's interception loop themselves.
14//!
15//! The `tab` field is a borrow of [`SessionHandle`] (not the full `Tab` from
16//! `zendriver` core) — this crate must not depend on `zendriver` (cycle).
17//! `Tab::intercept()` in `zendriver` constructs the builder via
18//! `InterceptBuilder::new(self.session())`.
19//!
20//! [`block`]: InterceptBuilder::block
21//! [`block_hosts`]: InterceptBuilder::block_hosts
22//! [`redirect`]: InterceptBuilder::redirect
23//! [`respond`]: InterceptBuilder::respond
24//! [`modify_request`]: InterceptBuilder::modify_request
25//! [`modify_response`]: InterceptBuilder::modify_response
26//! [`pattern`]: InterceptBuilder::pattern
27//! [`at_request`]: InterceptBuilder::at_request
28//! [`at_response`]: InterceptBuilder::at_response
29//! [`resource`]: InterceptBuilder::resource
30
31use std::sync::Arc;
32
33use futures::stream::{Stream, StreamExt};
34use serde_json::{Value, json};
35use tokio::sync::oneshot;
36use tokio_util::sync::CancellationToken;
37use tracing::warn;
38use zendriver_transport::SessionHandle;
39
40use crate::actor::{
41    InterceptHandle, RequestPausedEvent, build_request_info, build_response_info, run_actor,
42    serialize_pattern,
43};
44use crate::error::InterceptionError;
45use crate::host_matcher::HostMatcher;
46use crate::paused::PausedRequest;
47use crate::rule::Rule;
48use crate::types::{
49    RequestInfo, RequestOverrides, RequestStage, ResourceType, ResponseInfo, ResponseOverrides,
50};
51use crate::url_pattern::UrlPattern;
52
53/// A pending `Fetch.RequestPattern` entry to send on `Fetch.enable`.
54///
55/// CDP's [`Fetch.RequestPattern`] takes an optional `urlPattern`,
56/// `resourceType`, and `requestStage`. We mirror it 1:1 here. The builder
57/// accumulates these via [`InterceptBuilder::pattern`] / `at_request` /
58/// `at_response` / `resource`, mutating the last-pushed entry per chain — so
59/// `builder.pattern("*").at_response().resource(Image)` produces a single
60/// `RequestPattern` with all three fields set.
61///
62/// [`Fetch.RequestPattern`]: https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#type-RequestPattern
63#[derive(Debug, Clone, Default)]
64pub struct RequestPattern {
65    /// URL pattern in CDP wildcard syntax. `None` means "match any URL"
66    /// (CDP default).
67    pub url_pattern: Option<String>,
68    /// Resource type filter (e.g. `Image`, `XHR`). `None` means "all types".
69    pub resource_type: Option<ResourceType>,
70    /// Lifecycle stage at which to pause. `None` means CDP's default
71    /// (`Request`).
72    pub request_stage: Option<RequestStage>,
73}
74
75/// Fluent builder for rule-based interception against a single tab session.
76///
77/// Construct via `Tab::intercept()` (gated `feature = "interception"`, wired
78/// in Task 7). Chain configuration methods to register rules and declare CDP
79/// `Fetch.enable` patterns, then call [`start`](Self::start) (Task 7) to
80/// activate the background actor or [`subscribe`](Self::subscribe) (Task 7)
81/// for the stream-driven escape hatch.
82///
83/// `'tab` ties the builder's lifetime to the tab's session — the borrow lasts
84/// only until `start()` / `subscribe()` consumes the builder.
85//
86// `Debug` works because `Rule` has a hand-written `Debug` impl that renders
87// the closure variant's body as `<closure>`. Inner `Vec<Rule>` derives via
88// that.
89#[derive(Debug)]
90pub struct InterceptBuilder<'tab> {
91    tab: &'tab SessionHandle,
92    patterns: Vec<RequestPattern>,
93    rules: Vec<Rule>,
94    /// Optional proxy/server credentials. When set, `Fetch.enable` is sent
95    /// with `handleAuthRequests: true` and the actor responds to each
96    /// `Fetch.authRequired` event with `Fetch.continueWithAuth` carrying
97    /// these credentials. See cdpdriver/zendriver#208.
98    auth: Option<(String, String)>,
99}
100
101impl<'tab> InterceptBuilder<'tab> {
102    /// Construct a fresh builder bound to `tab`'s session.
103    ///
104    /// `pub` so adapter crates (e.g. `zendriver` core's `Tab::intercept()`
105    /// shim) can construct it from a `&SessionHandle` without going through
106    /// a trait. End users go through `Tab::intercept()` rather than calling
107    /// this directly.
108    ///
109    /// ```no_run
110    /// # async fn ex(tab: &zendriver_transport::SessionHandle)
111    /// #   -> Result<(), zendriver_interception::InterceptionError> {
112    /// use zendriver_interception::InterceptBuilder;
113    ///
114    /// let _handle = InterceptBuilder::new(tab)
115    ///     .block("*/tracker.js")?
116    ///     .start();
117    /// # Ok(()) }
118    /// ```
119    #[must_use]
120    pub fn new(tab: &'tab SessionHandle) -> Self {
121        Self {
122            tab,
123            patterns: Vec::new(),
124            rules: Vec::new(),
125            auth: None,
126        }
127    }
128
129    /// Auto-respond to `Fetch.authRequired` challenges with the given
130    /// credentials.
131    ///
132    /// This is the proxy-auth (and HTTP basic-auth) path: `Fetch.enable` is
133    /// sent with `handleAuthRequests: true` and every `Fetch.authRequired`
134    /// event is answered with `Fetch.continueWithAuth { authChallengeResponse:
135    /// { response: "ProvideCredentials", username, password } }`.
136    ///
137    /// Compose with rules: an `InterceptBuilder` configured with `handle_auth`
138    /// and `block` / `redirect` / `respond` rules handles both paths from the
139    /// same actor. Combine with [`BrowserBuilder::proxy_auth`] in the
140    /// `zendriver` crate if you want the wiring installed automatically on
141    /// every tab.
142    ///
143    /// See cdpdriver/zendriver#208.
144    #[must_use]
145    pub fn handle_auth(mut self, user: impl Into<String>, pass: impl Into<String>) -> Self {
146        self.auth = Some((user.into(), pass.into()));
147        self
148    }
149
150    /// Push a new pattern entry with the given URL pattern string.
151    ///
152    /// Subsequent [`at_request`](Self::at_request) /
153    /// [`at_response`](Self::at_response) / [`resource`](Self::resource) calls
154    /// mutate this newest entry, so a chain like
155    /// `.pattern("*").at_response().resource(ResourceType::XHR)` produces one
156    /// `RequestPattern` with all three fields populated.
157    #[must_use]
158    pub fn pattern(mut self, pattern: impl Into<String>) -> Self {
159        self.patterns.push(RequestPattern {
160            url_pattern: Some(pattern.into()),
161            ..RequestPattern::default()
162        });
163        self
164    }
165
166    /// Pause matching requests at the `Request` stage on the most-recently
167    /// pushed pattern.
168    ///
169    /// If no pattern has been pushed yet, this creates an empty one (matches
170    /// every URL by CDP default) and sets the stage on it.
171    #[must_use]
172    pub fn at_request(mut self) -> Self {
173        self.ensure_pattern().request_stage = Some(RequestStage::Request);
174        self
175    }
176
177    /// Pause matching requests at the `Response` stage on the most-recently
178    /// pushed pattern.
179    #[must_use]
180    pub fn at_response(mut self) -> Self {
181        self.ensure_pattern().request_stage = Some(RequestStage::Response);
182        self
183    }
184
185    /// Restrict the most-recently pushed pattern to a single resource type.
186    #[must_use]
187    pub fn resource(mut self, kind: ResourceType) -> Self {
188        self.ensure_pattern().resource_type = Some(kind);
189        self
190    }
191
192    /// Register a [`Rule::Block`] for `pattern`.
193    ///
194    /// Compiles `pattern` eagerly; an invalid pattern fails the builder chain
195    /// with [`InterceptionError::InvalidPattern`] returned as `Err(Self)` via
196    /// the `Result` wrapper.
197    pub fn block(mut self, pattern: impl Into<String>) -> Result<Self, InterceptionError> {
198        self.rules.push(Rule::Block {
199            pattern: UrlPattern::new(pattern)?,
200        });
201        Ok(self)
202    }
203
204    /// Register a [`Rule::BlockHosts`] backed by `matcher`.
205    ///
206    /// Every request whose host is in `matcher` (exact, or a parent domain on
207    /// a dot boundary) is failed with `BlockedByClient`. Composes with other
208    /// rules in registration order. `zendriver` core's tracker-blocklist
209    /// wiring uses this; most callers reach it via `BrowserBuilder::block_trackers`.
210    #[must_use]
211    pub fn block_hosts(mut self, matcher: Arc<HostMatcher>) -> Self {
212        self.rules.push(Rule::BlockHosts { matcher });
213        self
214    }
215
216    /// Register a [`Rule::Redirect`] that rewrites `from` → `to`.
217    pub fn redirect(
218        mut self,
219        from: impl Into<String>,
220        to: impl Into<String>,
221    ) -> Result<Self, InterceptionError> {
222        self.rules.push(Rule::Redirect {
223            from: UrlPattern::new(from)?,
224            to: to.into(),
225        });
226        Ok(self)
227    }
228
229    /// Register a [`Rule::Respond`] serving a synthesized response.
230    pub fn respond(
231        mut self,
232        pattern: impl Into<String>,
233        status: u16,
234        headers: Vec<(String, String)>,
235        body: Vec<u8>,
236    ) -> Result<Self, InterceptionError> {
237        self.rules.push(Rule::Respond {
238            pattern: UrlPattern::new(pattern)?,
239            status,
240            headers,
241            body,
242        });
243        Ok(self)
244    }
245
246    /// Register a [`Rule::Modify`] driven by a user closure.
247    ///
248    /// The closure runs on the actor task per matching request — it must be
249    /// `Send + Sync` and `'static`. Wrap shared state in `Arc` if needed.
250    pub fn modify_request<F>(
251        mut self,
252        pattern: impl Into<String>,
253        modify: F,
254    ) -> Result<Self, InterceptionError>
255    where
256        F: Fn(&RequestInfo) -> RequestOverrides + Send + Sync + 'static,
257    {
258        self.rules.push(Rule::Modify {
259            pattern: UrlPattern::new(pattern)?,
260            modify: Arc::new(modify),
261        });
262        Ok(self)
263    }
264
265    /// Register a [`Rule::ModifyResponse`] driven by a user closure.
266    ///
267    /// The closure rewrites an upstream response's status/headers (keeping
268    /// Chrome's body) and only fires at the `Response` stage — pair this with
269    /// [`at_response`](Self::at_response) so Chrome actually pauses there.
270    /// Header overrides are *replacement*, not merge (CDP semantics): return
271    /// every header you want forwarded.
272    ///
273    /// Like [`modify_request`](Self::modify_request), the closure runs on the
274    /// actor task per matching response, so it must be `Send + Sync` and
275    /// `'static`. Wrap shared state in `Arc` if needed.
276    pub fn modify_response<F>(
277        mut self,
278        pattern: impl Into<String>,
279        modify: F,
280    ) -> Result<Self, InterceptionError>
281    where
282        F: Fn(&ResponseInfo) -> ResponseOverrides + Send + Sync + 'static,
283    {
284        self.rules.push(Rule::ModifyResponse {
285            pattern: UrlPattern::new(pattern)?,
286            modify: Arc::new(modify),
287        });
288        Ok(self)
289    }
290
291    /// Activate the rule-based interception loop.
292    ///
293    /// Spawns the background actor task with the registered rules and CDP
294    /// `RequestPattern` list, and returns an [`InterceptHandle`] whose
295    /// [`Drop`] (or explicit [`stop`](InterceptHandle::stop)) tears the
296    /// actor down.
297    ///
298    /// If no [`pattern`](Self::pattern) entries were added, a single
299    /// match-all (`"*"`) pattern is sent so Chrome actually pauses requests
300    /// — without it, `Fetch.enable` would attach to nothing and the rule
301    /// list would never fire.
302    #[must_use = "interception stops when the handle is dropped — bind the returned InterceptHandle to keep it alive"]
303    pub fn start(mut self) -> InterceptHandle {
304        if self.patterns.is_empty() {
305            // Default to a single match-all pattern. Without it Chrome's
306            // `Fetch.enable` receives an empty `patterns` array and pauses
307            // nothing — silently making every rule a no-op. The actor still
308            // sends `handleAuthRequests: false` either way.
309            self.patterns.push(RequestPattern {
310                url_pattern: Some("*".into()),
311                ..RequestPattern::default()
312            });
313        }
314        let cancel = CancellationToken::new();
315        let (done_tx, done_rx) = oneshot::channel();
316        let actor_session = self.tab.clone();
317        let actor_cancel = cancel.clone();
318        let actor_rules = self.rules;
319        let actor_patterns = self.patterns;
320        let actor_auth = self.auth;
321        tokio::spawn(async move {
322            run_actor(
323                actor_session,
324                actor_rules,
325                actor_patterns,
326                actor_auth,
327                actor_cancel,
328                done_tx,
329            )
330            .await;
331        });
332        InterceptHandle::new(cancel, done_rx)
333    }
334
335    /// Manual escape-hatch: subscribe to raw [`PausedRequest`] events.
336    ///
337    /// Enables `Fetch` interception with the declared patterns (defaulting
338    /// to a single match-all `"*"` pattern when none were added) and returns
339    /// a [`Stream`] that yields one [`PausedRequest`] per `Fetch.requestPaused`
340    /// CDP event. Callers must dispatch one of `PausedRequest`'s terminal
341    /// methods (`continue_` / `abort` / `respond` / `modify_and_continue`)
342    /// to release each pause — Chrome holds the request open otherwise.
343    ///
344    /// Rules registered via `block` / `redirect` / `respond` / `modify_request`
345    /// are ignored on this path: stream consumers drive every paused request
346    /// themselves. Use [`start`](Self::start) when you want the actor to
347    /// apply rules automatically.
348    ///
349    /// The returned stream owns the underlying CDP subscription. Dropping
350    /// the stream tears the subscription down — Chrome's interception stays
351    /// active until the session is closed, but no further pauses surface to
352    /// the caller.
353    #[must_use = "the returned stream is the only handle on the subscription"]
354    pub fn subscribe(mut self) -> impl Stream<Item = PausedRequest> + Send + use<> {
355        if self.patterns.is_empty() {
356            self.patterns.push(RequestPattern {
357                url_pattern: Some("*".into()),
358                ..RequestPattern::default()
359            });
360        }
361        // Same ordering as the actor: subscribe BEFORE the (fire-and-forget)
362        // enable so we don't drop events Chrome emits between the enable
363        // round-trip and the subscription registration.
364        let raw = self.tab.subscribe::<Value>("Fetch.requestPaused");
365        let session = self.tab.clone();
366        let enable_session = session.clone();
367        let enable_patterns: Vec<Value> = self.patterns.iter().map(serialize_pattern).collect();
368        tokio::spawn(async move {
369            if let Err(e) = enable_session
370                .call(
371                    "Fetch.enable",
372                    json!({
373                        "patterns": enable_patterns,
374                        "handleAuthRequests": false,
375                    }),
376                )
377                .await
378            {
379                warn!(error = %e, "interception: Fetch.enable failed; subscribe() stream will be empty");
380            }
381        });
382        raw.filter_map(move |ev_value| {
383            let session = session.clone();
384            async move {
385                let ev: RequestPausedEvent = match serde_json::from_value(ev_value) {
386                    Ok(ev) => ev,
387                    Err(e) => {
388                        warn!(error = %e, "interception: skipping malformed Fetch.requestPaused event");
389                        return None;
390                    }
391                };
392                let info = build_request_info(&ev);
393                let response = build_response_info(&ev);
394                Some(PausedRequest::new(ev.request_id, info, response, session))
395            }
396        })
397    }
398
399    /// Lazily push an empty pattern if none exists, so the stage/resource
400    /// setters always have a target. Mirrors CDP's "missing fields default to
401    /// match-all" semantics.
402    fn ensure_pattern(&mut self) -> &mut RequestPattern {
403        if self.patterns.is_empty() {
404            self.patterns.push(RequestPattern::default());
405        }
406        self.patterns
407            .last_mut()
408            .expect("ensure_pattern pushed if empty")
409    }
410
411    /// Test-only accessor: number of registered rules. Used by the Task 5
412    /// builder test (and future actor tests) without exposing the rule list
413    /// as public API.
414    #[cfg(test)]
415    pub(crate) fn rules_count(&self) -> usize {
416        self.rules.len()
417    }
418}
419
420#[cfg(test)]
421#[allow(clippy::panic, clippy::unwrap_used)]
422mod tests {
423    use super::*;
424    use std::time::Duration;
425    use zendriver_transport::testing::MockConnection;
426
427    /// Register three rules (block + redirect + respond) on a fresh builder
428    /// and assert the rule list grew to length 3. Verifies the chain wiring
429    /// without touching the actor (Task 6) or CDP dispatch (Task 7).
430    #[tokio::test]
431    async fn three_rules_register_and_count() {
432        let (_mock, conn) = MockConnection::pair();
433        let sess = SessionHandle::new(conn.clone(), "S1");
434
435        let builder = InterceptBuilder::new(&sess)
436            .block("*/ads/*")
437            .unwrap()
438            .redirect("*/old/*", "https://example.com/new/")
439            .unwrap()
440            .respond(
441                "*/api/health",
442                200,
443                vec![("content-type".into(), "application/json".into())],
444                br#"{"ok":true}"#.to_vec(),
445            )
446            .unwrap();
447
448        assert_eq!(builder.rules_count(), 3);
449        conn.shutdown();
450    }
451
452    /// End-to-end on the rule-driven `start()` path: register a Block rule,
453    /// spawn the actor via `start()`, observe `Fetch.enable`, emit a matching
454    /// `Fetch.requestPaused`, and assert `Fetch.failRequest` is dispatched.
455    ///
456    /// This is the actor test from T6 reframed through the `start()` entry
457    /// point — proves the builder properly forwards rules + patterns to
458    /// `run_actor` (and that `start()` actually spawns the task).
459    #[tokio::test]
460    async fn start_spawns_actor_with_rules() {
461        let (mut mock, conn) = MockConnection::pair();
462        let sess = SessionHandle::new(conn.clone(), "S1");
463
464        let handle = InterceptBuilder::new(&sess)
465            .block("*/blocked/*")
466            .unwrap()
467            .pattern("*")
468            .start();
469
470        // The actor's `Fetch.enable` side-task fires fire-and-forget; wait
471        // for it to land so the `Fetch.requestPaused` subscription is in
472        // place before we emit an event.
473        let enable_id =
474            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
475                .await
476                .expect("actor did not send Fetch.enable within 2s");
477        let enable_params = mock.last_sent()["params"].clone();
478        assert_eq!(enable_params["handleAuthRequests"], false);
479        assert_eq!(enable_params["patterns"][0]["urlPattern"], "*");
480        mock.reply(enable_id, json!({})).await;
481
482        // Emit a paused-event whose URL matches the Block rule.
483        mock.emit_event_for_session(
484            "Fetch.requestPaused",
485            json!({
486                "requestId": "REQ-1",
487                "request": {
488                    "url": "https://example.test/blocked/banner.png",
489                    "method": "GET",
490                    "headers": {},
491                },
492                "resourceType": "Image",
493            }),
494            "S1",
495        )
496        .await;
497
498        // Actor should dispatch Fetch.failRequest with BlockedByClient.
499        let fail_id =
500            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.failRequest"))
501                .await
502                .expect("actor did not send Fetch.failRequest within 2s");
503        let fail_params = mock.last_sent()["params"].clone();
504        assert_eq!(fail_params["requestId"], "REQ-1");
505        assert_eq!(fail_params["errorReason"], "BlockedByClient");
506        mock.reply(fail_id, json!({})).await;
507
508        // Teardown via the handle: stop() cancels + awaits the oneshot the
509        // actor signals after Fetch.disable lands.
510        let stop_fut = tokio::spawn(handle.stop());
511        let disable_id =
512            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.disable"))
513                .await
514                .expect("actor did not send Fetch.disable on stop()");
515        mock.reply(disable_id, json!({})).await;
516        stop_fut
517            .await
518            .expect("stop() task panicked")
519            .expect("stop() returned Err");
520        conn.shutdown();
521    }
522
523    /// `start()` injects a match-all `"*"` pattern when the caller did not
524    /// add any via [`pattern`](InterceptBuilder::pattern) — otherwise
525    /// `Fetch.enable` would arrive with an empty patterns array and Chrome
526    /// would silently pause nothing.
527    #[tokio::test]
528    async fn start_defaults_to_match_all_pattern_when_none_registered() {
529        let (mut mock, conn) = MockConnection::pair();
530        let sess = SessionHandle::new(conn.clone(), "S1");
531
532        let handle = InterceptBuilder::new(&sess)
533            .block("*/blocked/*")
534            .unwrap()
535            .start();
536
537        let enable_id =
538            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
539                .await
540                .expect("actor did not send Fetch.enable within 2s");
541        let patterns = mock.last_sent()["params"]["patterns"].clone();
542        let arr = patterns.as_array().expect("patterns must be a JSON array");
543        assert_eq!(arr.len(), 1);
544        assert_eq!(arr[0]["urlPattern"], "*");
545        mock.reply(enable_id, json!({})).await;
546
547        // Drop the handle to tear down; we don't need to observe the disable.
548        drop(handle);
549        conn.shutdown();
550    }
551
552    /// On the `subscribe()` path: each `Fetch.requestPaused` event becomes
553    /// a `PausedRequest` yielded from the stream, with the request payload
554    /// decoded into [`RequestInfo`].
555    #[tokio::test]
556    async fn subscribe_yields_paused_request_per_event() {
557        let (mut mock, conn) = MockConnection::pair();
558        let sess = SessionHandle::new(conn.clone(), "S1");
559
560        let mut stream = Box::pin(InterceptBuilder::new(&sess).subscribe());
561
562        // Wait for the side-task's Fetch.enable to land so the subscription
563        // is in place before we emit.
564        let enable_id =
565            tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
566                .await
567                .expect("subscribe() did not send Fetch.enable within 2s");
568        mock.reply(enable_id, json!({})).await;
569
570        mock.emit_event_for_session(
571            "Fetch.requestPaused",
572            json!({
573                "requestId": "REQ-1",
574                "request": {
575                    "url": "https://example.test/widget.json",
576                    "method": "GET",
577                    "headers": {"accept": "application/json"},
578                },
579                "resourceType": "XHR",
580            }),
581            "S1",
582        )
583        .await;
584
585        let paused = tokio::time::timeout(Duration::from_secs(2), stream.next())
586            .await
587            .expect("subscribe() stream did not yield within 2s")
588            .expect("subscribe() stream closed before yielding");
589        assert_eq!(paused.request_id, "REQ-1");
590        assert_eq!(paused.request.url, "https://example.test/widget.json");
591        assert_eq!(paused.request.method, "GET");
592        assert_eq!(
593            paused
594                .request
595                .headers
596                .iter()
597                .find(|(k, _)| k == "accept")
598                .map(|(_, v)| v.as_str()),
599            Some("application/json"),
600        );
601        assert!(
602            paused.response.is_none(),
603            "request-stage event has no response"
604        );
605
606        drop(stream);
607        conn.shutdown();
608    }
609}