proxy-watch 0.3.1

Read and watch the operating system's proxy settings, layered with *_proxy, and the route each URL takes under them
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! Crate-wide error type.

use thiserror::Error;

use crate::util::{redact_offending_token, redact_userinfo};
use crate::{RejectedValue, Scheme};

/// Errors from reading OS proxy configuration, and from routing a URL through what was read.
///
/// `#[non_exhaustive]`. No `#[from] io::Error`: I/O is always wrapped with context.
/// Hand-written [`Debug`]: [`PacFetchRequired`](Error::PacFetchRequired)'s [`url::Url`]
/// would print credentials; other payloads are masked at construction or safe.
#[derive(Error)]
#[non_exhaustive]
pub enum Error {
    /// A Windows `ProxyServer` style specification could not be parsed.
    #[error("invalid proxy server specification {input:?}: {reason}")]
    InvalidProxyServer {
        /// Offending token, masked, and withheld outright when it may be a credential.
        input: String,
        /// Why parsing failed.
        reason: String,
    },

    /// A single bypass / `no_proxy` / `ProxyOverride` entry could not be parsed.
    #[error("invalid bypass pattern {input:?}: {reason}")]
    InvalidBypassPattern {
        /// Offending entry, masked like [`InvalidProxyServer`](Error::InvalidProxyServer)'s.
        input: String,
        /// Why parsing failed.
        reason: String,
    },

    /// An `AutoConfigURL`-style setting read from the OS is malformed.
    ///
    /// Not the environment: `*_proxy` values go through
    /// [`ProxyEndpoint::parse`](crate::ProxyEndpoint::parse), which answers with
    /// [`InvalidProxyServer`](Error::InvalidProxyServer) instead.
    #[error("invalid proxy URL {input:?}: {source}")]
    InvalidProxyUrl {
        /// Offending input, masked like [`InvalidProxyServer`](Error::InvalidProxyServer)'s.
        input: String,
        /// Underlying parse error.
        #[source]
        source: url::ParseError,
    },

    /// The proxy URL carried a scheme this crate does not understand.
    ///
    /// Recognised schemes are listed on [`ProxyScheme`](crate::ProxyScheme).
    #[error("unsupported proxy scheme {0:?}")]
    UnsupportedProxyScheme(String),

    /// `http_proxy` under CGI: a non-empty `REQUEST_METHOD` ⇒ forged `Proxy:` header
    /// (httpoxy, CVE-2016-5385). Refused like Go's httpproxy; not applied to
    /// `https_proxy`/`no_proxy`, whose names no request header can produce.
    ///
    /// KDE's `ProxyType = 4` raises the same error on a wider rule, because there the
    /// *file* names the variable each slot reads: any name a request header could have set
    /// (RFC 3875 §4.1.18's `HTTP_` prefix) is refused whichever slot named it, including
    /// the one holding the bypass list. `variable` is that name as written.
    #[error("refusing to use {variable} value in CGI environment")]
    CgiHttpProxy {
        /// Variable name as set in the environment.
        variable: String,
    },

    /// An I/O failure, annotated with the operation that caused it.
    ///
    /// OS error codes are carried as [`std::io::Error`], built by `from_raw_os_error`.
    #[error("{context}")]
    Io {
        /// Operation in progress when the failure occurred.
        context: String,
        /// Underlying I/O error.
        #[source]
        source: std::io::Error,
    },

    /// Linux sandbox: desktop settings unreadable (Flatpak without dconf → keyfile
    /// defaults). Portal fallback when possible; else this error, not a false Direct.
    #[error("running inside a {sandbox} sandbox: {reason}")]
    Sandboxed {
        /// Detected sandbox name.
        sandbox: String,
        /// Why no source could be read.
        reason: String,
    },

    /// No store to read. A target with no backend has none at all; on Linux neither desktop
    /// store answered, which both desktop features being off guarantees and which a build
    /// with one of them on reaches when the schema and `kioslaverc` are missing.
    ///
    /// Not the answer inside a sandbox: that route is chosen before either store is
    /// consulted and reports [`Sandboxed`](Self::Sandboxed), including when the feature that
    /// would carry the portal fallback is off.
    #[error("watching the system proxy configuration is not supported on this platform")]
    Unsupported,

    /// `resolve()` needs PAC/WPAD evaluation (`resolve_with_pac` / `pac` feature).
    /// An error, not silent Direct.
    ///
    /// `resolve_with_pac` answers with it too, for
    /// [`WpadAutoDetect`](crate::ProxyMode::WpadAutoDetect) handed no script body: this
    /// crate runs no discovery itself, so the fix there is to supply the script, or to hand
    /// discovery to the OS through `WinHttpPacResolver` / `CfNetworkPacResolver` built
    /// `with_wpad(true)`, which answer `WpadAutoDetect` with this error until they are.
    /// `mode` is `"wpad"` in every case and does not tell them apart.
    #[error("resolving this URL requires evaluating a proxy auto-config script ({mode})")]
    PacNotSupported {
        /// Auto-config mode in effect.
        mode: &'static str,
    },

    /// The system configured a proxy for this scheme, the value could not be used, and no
    /// other entry covers it.
    ///
    /// Returning `ProxyStep::Direct` here would assert something the platform does not do:
    /// KDE expands a `[$e]` value from the session environment and proxies the request, and
    /// this crate does not expand it. Only a scheme with no catch-all reaches this; a live
    /// `socksProxy` covers the drop first, as Chromium's `fallback_proxies` does. Where
    /// this reports, Chromium goes direct instead: its `ProxyList::AddProxyChain` drops a
    /// malformed entry with "Silently discard malformed inputs", leaving
    /// `MapUrlSchemeToProxyList` to answer `nullptr`.
    // `{scheme}` names the slot, so the second half says "this request" rather than
    // repeating it: for a `ws`/`wss` URL the slot is `Https` or `Socks`, and "nothing else
    // covers https" would describe a request that was not made.
    #[error("the configured {scheme} proxy could not be used and nothing else covers this request")]
    ProxyEntryUnusable {
        /// Which requests lost an answer, read off the drop's own attribution. In every
        /// mode this crate builds, the attribution and the slot are the same value because
        /// each backend files its record against the slot it dropped, so the slot decides
        /// coverage rather than the request's own scheme. A lost catch-all reports
        /// [`Scheme::All`] whatever was asked for, and a `ws`/`wss` request reports
        /// whichever of the chain it lost. A caller who hands
        /// [`ProxyMode::manual`](crate::ProxyMode::manual) a record attributed to one
        /// scheme under the key of another gets the attribution back: nothing normalises a
        /// caller-built mismatch, and the same value is what
        /// [`ProxyMode::rejected`](crate::ProxyMode::rejected) hands back.
        scheme: Scheme,
        /// First drop naming that scheme; masked at construction. The rest stay reachable
        /// through [`ProxyMode::rejected`](crate::ProxyMode::rejected) on the mode this
        /// error was resolved against, which is the caller's own `config.effective`
        /// wherever the caller supplied it. This field survives when a resolver builds a
        /// mode of its own that goes out of scope with the call: `WinHttpPacResolver`
        /// answers a WPAD miss by re-reading the registry into a `Manual`, while the caller
        /// still holds `WpadAutoDetect`, which has no list.
        rejected: RejectedValue,
    },

    /// `resolve_with_pac` + [`ProxyMode::Pac`](crate::ProxyMode::Pac) without script body.
    /// Fetch is caller's job; `url` stored verbatim, masked only in `{}`/`{:?}`.
    #[error(
        "the PAC script at {} must be fetched by the caller before it can be evaluated",
        redact_userinfo(.url.as_str())
    )]
    PacFetchRequired {
        /// Script URL.
        url: url::Url,
    },

    /// PAC run failed (syntax, throw, limits). `reason` masked/sanitized at construction.
    #[error("PAC evaluation failed: {reason}")]
    PacEvaluation {
        /// Engine message (sanitized).
        reason: String,
    },

    /// Evaluating a PAC script overran `PacPolicy::timeout`.
    #[error("PAC evaluation exceeded its {timeout:?} budget")]
    PacTimeout {
        /// Budget that was exceeded.
        timeout: std::time::Duration,
    },

    /// Every PAC evaluation slot stayed taken for the whole of `PacPolicy::timeout`, so the
    /// script never started. The PAC engines together run at most `limit` evaluation threads
    /// per process, and a thread holds its slot until its script ends, past the timeout that
    /// abandoned it. `pac-quickjs` interrupts a script at the deadline, and holds a slot past
    /// it only while a host function such as `dnsResolve` has yet to return.
    #[error("no PAC evaluation slot came free within {timeout:?} ({limit} still running)")]
    PacSaturated {
        /// Budget spent waiting.
        timeout: std::time::Duration,
        /// Evaluation threads this process allows at once.
        limit: usize,
    },

    /// `FindProxyForURL` returned nothing usable (malformed candidates skipped first).
    /// `result` masked at construction.
    #[error("PAC script returned no usable proxy candidate: {result:?}")]
    PacInvalidResult {
        /// Raw return string (masked).
        result: String,
    },

    /// `pac` on but no engine (`pac-quickjs` or a custom `pac::PacEvaluator`).
    #[error(
        "no PAC JavaScript engine is enabled; build with `pac-quickjs` (not on Android or iOS), \
         or attach a native resolver through `pac::PacResolver`"
    )]
    PacEngineUnavailable,
}

impl Error {
    // Mask `user:password` in `input` once so Display/Debug stay safe downstream, with
    // or without the `@` that would make it recognisable, since `input` is by definition
    // a token that failed to parse.
    pub(crate) fn proxy_server(input: impl AsRef<str>, reason: impl Into<String>) -> Self {
        Error::InvalidProxyServer {
            input: redact_offending_token(input.as_ref()),
            reason: reason.into(),
        }
    }

    // Same masking as [`Self::proxy_server`] (malformed bypasses can still look like userinfo).
    pub(crate) fn bypass(input: impl AsRef<str>, reason: impl Into<String>) -> Self {
        Error::InvalidBypassPattern {
            input: redact_offending_token(input.as_ref()),
            reason: reason.into(),
        }
    }

    // Mask raw text that failed `Url::parse` (cannot re-parse to strip userinfo).
    #[cfg_attr(
        not(any(target_os = "linux", target_os = "macos", windows)),
        allow(dead_code)
    )]
    pub(crate) fn invalid_proxy_url(input: impl AsRef<str>, source: url::ParseError) -> Self {
        Error::InvalidProxyUrl {
            input: redact_offending_token(input.as_ref()),
            source,
        }
    }

    // No `allow(dead_code)` for a target with no backend, unlike its neighbour above:
    // `watch::ThreadGuard`'s `Drop` builds this error when a backend thread panics, and a
    // `Drop` impl is live everywhere.
    pub(crate) fn io(context: impl Into<String>, source: std::io::Error) -> Self {
        Error::Io {
            context: context.into(),
            source,
        }
    }

    // Mask + sanitize, in that order; see
    // [`crate::util::redact_and_sanitize_untrusted`] (PAC return is attacker-chosen).
    #[cfg_attr(not(feature = "pac"), allow(dead_code))]
    pub(crate) fn pac_invalid_result(result: impl AsRef<str>) -> Self {
        Error::PacInvalidResult {
            result: crate::util::redact_and_sanitize_untrusted(result.as_ref()),
        }
    }

    // Mask + sanitize engine/`throw` text. `pac/quickjs.rs` hands over what the engine said;
    // `pac/winhttp.rs` hands over a sentence it wrote itself and still comes through here,
    // so the variant is never built any other way.
    #[cfg_attr(
        not(any(
            pac_quickjs,
            feature = "pac-subprocess",
            all(windows, feature = "pac-windows-native")
        )),
        allow(dead_code)
    )]
    pub(crate) fn pac_evaluation(reason: impl AsRef<str>) -> Self {
        Error::PacEvaluation {
            reason: crate::util::redact_and_sanitize_untrusted(reason.as_ref()),
        }
    }
}

impl std::fmt::Debug for Error {
    // See enum docs: only [`Error::PacFetchRequired`]'s URL needs special casing.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::InvalidProxyServer { input, reason } => f
                .debug_struct("InvalidProxyServer")
                .field("input", input)
                .field("reason", reason)
                .finish(),
            Error::InvalidBypassPattern { input, reason } => f
                .debug_struct("InvalidBypassPattern")
                .field("input", input)
                .field("reason", reason)
                .finish(),
            Error::InvalidProxyUrl { input, source } => f
                .debug_struct("InvalidProxyUrl")
                .field("input", input)
                .field("source", source)
                .finish(),
            Error::UnsupportedProxyScheme(scheme) => f
                .debug_tuple("UnsupportedProxyScheme")
                .field(scheme)
                .finish(),
            Error::CgiHttpProxy { variable } => f
                .debug_struct("CgiHttpProxy")
                .field("variable", variable)
                .finish(),
            Error::Io { context, source } => f
                .debug_struct("Io")
                .field("context", context)
                .field("source", source)
                .finish(),
            Error::Sandboxed { sandbox, reason } => f
                .debug_struct("Sandboxed")
                .field("sandbox", sandbox)
                .field("reason", reason)
                .finish(),
            Error::Unsupported => write!(f, "Unsupported"),
            Error::PacNotSupported { mode } => f
                .debug_struct("PacNotSupported")
                .field("mode", mode)
                .finish(),
            Error::ProxyEntryUnusable { scheme, rejected } => f
                .debug_struct("ProxyEntryUnusable")
                .field("scheme", scheme)
                .field("rejected", rejected)
                .finish(),
            Error::PacFetchRequired { url } => f
                .debug_struct("PacFetchRequired")
                .field("url", &format_args!("{}", redact_userinfo(url.as_str())))
                .finish(),
            Error::PacEvaluation { reason } => f
                .debug_struct("PacEvaluation")
                .field("reason", reason)
                .finish(),
            Error::PacTimeout { timeout } => f
                .debug_struct("PacTimeout")
                .field("timeout", timeout)
                .finish(),
            Error::PacSaturated { timeout, limit } => f
                .debug_struct("PacSaturated")
                .field("timeout", timeout)
                .field("limit", limit)
                .finish(),
            Error::PacInvalidResult { result } => f
                .debug_struct("PacInvalidResult")
                .field("result", result)
                .finish(),
            Error::PacEngineUnavailable => write!(f, "PacEngineUnavailable"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const SECRET: &str = "hunter2";

    #[test]
    fn proxy_server_masks_the_password_in_display_and_debug() {
        let input = format!("http://alice:{SECRET}@proxy.corp:99999");
        let error = Error::proxy_server(&input, "port out of range");

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        // Not vacuous: the rest of the input is still there.
        assert!(display.contains("proxy.corp"), "{display}");
        assert!(debug.contains("proxy.corp"), "{debug}");
    }

    #[test]
    fn invalid_proxy_url_masks_the_password_in_display_and_debug() {
        let input = format!("http://alice:{SECRET}@proxy.corp:99999");
        let error = Error::invalid_proxy_url(&input, url::ParseError::EmptyHost);

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        assert!(display.contains("proxy.corp"), "{display}");
        assert!(debug.contains("proxy.corp"), "{debug}");
    }

    // The payload looks like a bare scheme name, but
    // [`ProxyEndpoint::parse`](crate::ProxyEndpoint::parse) splits on the first `"://"`
    // *before* it looks for userinfo, so credentials written ahead of the scheme end up
    // inside it.
    #[test]
    fn an_unsupported_scheme_masks_credentials_written_ahead_of_it() {
        let input = format!("alice:{SECRET}@http://proxy.corp:8080");
        let error = crate::ProxyEndpoint::parse(&input, 80).unwrap_err();

        assert!(
            matches!(error, Error::UnsupportedProxyScheme(_)),
            "fixture precondition: expected an unsupported-scheme error, got {error:?}"
        );
        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        // `SafeError` only exists with the `tracing` feature, so this rendering is checked
        // conditionally: an unconditional reference stops `cargo test
        // --no-default-features` from compiling at all.
        #[cfg(feature = "tracing")]
        {
            let traced = crate::trace::SafeError(&error).to_string();
            assert!(!traced.contains(SECRET), "{traced}");
        }
        // Not vacuous: the user name and the mangled scheme still identify the mistake.
        assert!(display.contains("alice"), "{display}");
    }

    // The `url` field stays a genuine, fetchable `url::Url`, so this variant masks at
    // *display* time rather than at construction. Both rendering paths must still come
    // out clean.
    #[test]
    fn pac_fetch_required_masks_the_password_in_display_and_debug() {
        let url = url::Url::parse(&format!("https://alice:{SECRET}@wpad.corp/proxy.pac")).unwrap();
        let error = Error::PacFetchRequired { url: url.clone() };

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        assert!(display.contains("wpad.corp"), "{display}");
        assert!(debug.contains("wpad.corp"), "{debug}");

        // The field itself is untouched: the caller still needs the real URL to fetch.
        if let Error::PacFetchRequired { url: preserved } = &error {
            assert_eq!(preserved, &url);
            assert_eq!(preserved.password(), Some(SECRET));
        } else {
            unreachable!();
        }
    }

    #[test]
    fn pac_invalid_result_masks_the_password_in_display_and_debug() {
        let error = Error::pac_invalid_result(format!("PROXY alice:{SECRET}@proxy.corp:8080"));

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        assert!(display.contains("proxy.corp"), "{display}");
        assert!(debug.contains("proxy.corp"), "{debug}");
    }

    // A `FindProxyForURL` that returns an unusable string chooses that string outright,
    // so it is at least as attacker-controlled as an engine message quoting a `throw`.
    #[test]
    fn pac_invalid_result_strips_control_characters_and_truncates() {
        let error = Error::pac_invalid_result("BOGUS\nWARN a fake second log line\r\nthird");

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains('\n'), "{display}");
        assert!(!display.contains('\r'), "{display}");
        assert!(!debug.contains('\n'), "{debug}");
        assert!(!debug.contains('\r'), "{debug}");
        assert!(
            display.contains("BOGUS.WARN a fake second log line..third"),
            "{display}"
        );

        let long = Error::pac_invalid_result("y".repeat(4096));
        assert!(long.to_string().len() < 512, "{}", long.to_string().len());
        assert!(long.to_string().contains('…'), "{long}");
    }

    // `Error::proxy_server`/`Error::bypass` mask only their `input` field; `reason` is
    // trusted. `crate::util::split_host_port`'s `Err` tests that trust: on malformed
    // input it hands back the tail of the string it was splitting, which, because
    // `ProxyEndpoint::parse` cuts the authority off at the first `/`/`?`/`#` *before* it
    // looks for `@`, can be a stranded password fragment rather than a port. This drives
    // the real helper the way `endpoint.rs` does, not a made-up reason string.
    #[test]
    fn proxy_server_reason_does_not_leak_a_password_stranded_past_a_slash() {
        let reason = crate::util::split_host_port(&format!("bob:{SECRET}"))
            .expect_err("a non-numeric \"port\" must fail to parse");
        let error = Error::proxy_server(format!("http://bob:{SECRET}/x@proxy.corp:8080"), reason);

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        // Not vacuous: the rest of the address is still there, in both `input` (already
        // masked) and `reason` (now safe by construction rather than by luck).
        assert!(display.contains("proxy.corp"), "{display}");
        assert!(debug.contains("proxy.corp"), "{debug}");
    }

    // `HostPattern::parse` never processes `@` (a bypass entry is not supposed to be a
    // URL), so `bypass.rs` rejects any `@`-bearing entry outright, with a reason that
    // names the mistake instead of repeating it.
    #[test]
    fn bypass_reason_does_not_leak_a_password_before_an_at_sign() {
        let error = crate::bypass::HostPattern::parse(&format!("alice:{SECRET}@proxy.corp"))
            .expect_err("an '@'-bearing entry must be rejected, not silently misparsed");

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        assert!(display.contains("proxy.corp"), "{display}");
        assert!(debug.contains("proxy.corp"), "{debug}");
    }

    // A syntax error repeats the malformed PAC URL literal the script embedded, so the
    // engine's message can quote a `user:password@` fragment.
    #[test]
    fn pac_evaluation_masks_the_password_in_display_and_debug() {
        let error = Error::pac_evaluation(format!(
            "the PAC script failed to load: unexpected token in string literal \
             \"http://alice:{SECRET}@proxy.corp/x.pac\""
        ));

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains(SECRET), "{display}");
        assert!(!debug.contains(SECRET), "{debug}");
        assert!(display.contains("proxy.corp"), "{display}");
        assert!(debug.contains("proxy.corp"), "{debug}");
    }

    // A PAC script's own `throw` reaches `reason` verbatim via the engine's message, so a
    // script author chooses its bytes. Newlines must not survive into `Display`/`Debug`,
    // or a script could forge extra log lines wherever a caller prints the error.
    #[test]
    fn pac_evaluation_strips_control_characters_in_display_and_debug() {
        let error = Error::pac_evaluation(
            "FindProxyForURL failed: Error: forged\nWARN a fake second log line\r\nthird",
        );

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(!display.contains('\n'), "{display}");
        assert!(!display.contains('\r'), "{display}");
        assert!(!debug.contains('\n'), "{debug}");
        assert!(!debug.contains('\r'), "{debug}");
        // Not vacuous: the message survives, control characters swapped for `.`.
        assert!(
            display.contains("forged.WARN a fake second log line..third"),
            "{display}"
        );
    }

    // A pathological script (or engine) message must not make this error unbounded, even
    // without the `tracing` feature.
    #[test]
    fn pac_evaluation_truncates_a_long_reason_in_display_and_debug() {
        let error = Error::pac_evaluation(format!("FindProxyForURL failed: {}", "y".repeat(4096)));

        let display = error.to_string();
        let debug = format!("{error:?}");
        assert!(display.len() < 512, "{}", display.len());
        assert!(debug.len() < 512, "{}", debug.len());
        assert!(display.contains('…'), "{display}");
        assert!(debug.contains('…'), "{debug}");
    }

    // One row per variant. Only [`Error::PacFetchRequired`] needs the hand-written impl at
    // all; the other arms are what `derive(Debug)` would have written, copied out by hand
    // because a hand-written impl cannot delegate to a derive for the rest. This test is
    // the only thing that notices a copy drifting: `Sandboxed` printed without its `reason`
    // (the field that says *why* the sandbox left nothing readable, and the whole content
    // of that error), and `Unsupported` printed as `PacEngineUnavailable`, which leaves two
    // distinct failures indistinguishable wherever a caller logs `{:?}`.
    //
    // Exact strings, so that a label, an order or a name cannot change unseen. Where a
    // field has a `Debug` of its own the expectation defers to it rather than copying it
    // out, because this impl does not control that rendering.
    #[test]
    fn every_variant_debug_names_itself_and_keeps_its_fields() {
        let rejected = RejectedValue::new(
            crate::RejectionKind::UnsupportedMapping,
            crate::RejectionSource::ProxyServer,
            "socks",
        );
        for (error, expected) in [
            (
                Error::proxy_server("proxy.corp", "no port"),
                r#"InvalidProxyServer { input: "proxy.corp", reason: "no port" }"#.to_owned(),
            ),
            (
                Error::bypass("*.corp", "empty label"),
                r#"InvalidBypassPattern { input: "*.corp", reason: "empty label" }"#.to_owned(),
            ),
            (
                Error::invalid_proxy_url("not-a-url", url::ParseError::RelativeUrlWithoutBase),
                format!(
                    r#"InvalidProxyUrl {{ input: "not-a-url", source: {:?} }}"#,
                    url::ParseError::RelativeUrlWithoutBase
                ),
            ),
            (
                Error::UnsupportedProxyScheme("gopher".to_owned()),
                r#"UnsupportedProxyScheme("gopher")"#.to_owned(),
            ),
            (
                Error::CgiHttpProxy {
                    variable: "http_proxy".to_owned(),
                },
                r#"CgiHttpProxy { variable: "http_proxy" }"#.to_owned(),
            ),
            (
                Error::io(
                    "reading dconf",
                    std::io::Error::other("dconf exited nonzero"),
                ),
                format!(
                    r#"Io {{ context: "reading dconf", source: {:?} }}"#,
                    std::io::Error::other("dconf exited nonzero")
                ),
            ),
            (
                Error::Sandboxed {
                    sandbox: "flatpak".to_owned(),
                    reason: "dconf is not on the bus".to_owned(),
                },
                r#"Sandboxed { sandbox: "flatpak", reason: "dconf is not on the bus" }"#.to_owned(),
            ),
            (Error::Unsupported, "Unsupported".to_owned()),
            (
                Error::PacNotSupported { mode: "wpad" },
                r#"PacNotSupported { mode: "wpad" }"#.to_owned(),
            ),
            (
                Error::ProxyEntryUnusable {
                    scheme: Scheme::Https,
                    rejected: rejected.clone(),
                },
                format!(
                    "ProxyEntryUnusable {{ scheme: {:?}, rejected: {rejected:?} }}",
                    Scheme::Https
                ),
            ),
            (
                // No credentials, so that the row pins the rendering and not the masking,
                // which `pac_fetch_required_masks_the_password_in_display_and_debug` owns.
                Error::PacFetchRequired {
                    url: url::Url::parse("https://wpad.corp/proxy.pac").unwrap(),
                },
                "PacFetchRequired { url: https://wpad.corp/proxy.pac }".to_owned(),
            ),
            (
                Error::pac_evaluation("the engine refused the script"),
                r#"PacEvaluation { reason: "the engine refused the script" }"#.to_owned(),
            ),
            (
                Error::PacTimeout {
                    timeout: std::time::Duration::from_millis(1500),
                },
                "PacTimeout { timeout: 1.5s }".to_owned(),
            ),
            (
                Error::PacSaturated {
                    timeout: std::time::Duration::from_millis(1500),
                    limit: 4,
                },
                "PacSaturated { timeout: 1.5s, limit: 4 }".to_owned(),
            ),
            (
                Error::pac_invalid_result("BOGUS"),
                r#"PacInvalidResult { result: "BOGUS" }"#.to_owned(),
            ),
            (
                Error::PacEngineUnavailable,
                "PacEngineUnavailable".to_owned(),
            ),
        ] {
            assert_eq!(format!("{error:?}"), expected);
        }
    }
}