url-sanitize 2.4.1

Native CLI to remove tracking parameters (utm_*, fbclid, gclid) and unwrap tracking redirects from URLs and text. ClearURLs, Brave and Firefox rules baked in.
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
use std::io::{self, BufRead, IsTerminal, Write};
use std::process::ExitCode;
use std::sync::OnceLock;

use url_sanitize_core::{Catalog, SanitizeResult, Sanitizer, SanitizerOptions};

const EMBEDDED_CATALOG_JSON: &str = include_str!("../catalog/catalog.json");
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");

static CATALOG: OnceLock<Catalog> = OnceLock::new();

fn catalog() -> &'static Catalog {
    CATALOG.get_or_init(|| {
        Catalog::from_json(EMBEDDED_CATALOG_JSON).expect("embedded catalog must parse")
    })
}

#[derive(PartialEq)]
enum Action {
    Run,
    Help,
    Version,
}

struct Args {
    action: Action,
    json: bool,
    explain: bool,
    text: bool,
    options: SanitizerOptions,
    urls: Vec<String>,
    read_stdin: bool,
}

fn print_help(w: &mut dyn Write) {
    let _ = writeln!(
        w,
        "url-sanitize {} (catalog {} {})

Strip tracking parameters from URLs with the merged multi-source catalog.

USAGE:
    url-sanitize [OPTIONS] [URL]...
    url-sanitize [OPTIONS] -          read URLs from stdin (one per line)
    url-sanitize [OPTIONS] < file     same
    url-sanitize [OPTIONS] -- [URL]... treat every later argument as a URL
    url-sanitize --text [OPTIONS] < file   clean every URL inside text

OPTIONS:
    --json                  Emit one JSON SanitizeResult per input line
    --explain               Default mode, plus a `# matched: ...` line on stderr
    --text                  Clean the URLs inside text (stdin or each argument);
                            copy the rest as is. With --json: one result per URL
    --strip-referral        Strip affiliate / referral marketing params
    --keep-referral         Explicitly keep referral params (default)
    --unwrap-redirects      Unwrap redirector URLs (default)
    --no-unwrap-redirects   Leave redirector URLs untouched
    --block-domains         Emit `blocked` for domain-blocked URLs
    --version               Print version + catalog identity and generated date
    -h, --help              Show this help

EXIT CODES:
    0   all URLs processed; none blocked
    1   usage error
    2   at least one URL was blocked

Full spec: https://github.com/antonio-orionus/url-sanitize/blob/main/docs/spec.md",
        CRATE_VERSION,
        catalog().catalog_identity(),
        catalog().generated_at()
    );
}

fn parse_args(raw: Vec<String>) -> Result<Args, String> {
    let mut json = false;
    let mut explain = false;
    let mut text = false;
    let mut strip_referral = false;
    let mut keep_referral_explicit = false;
    let mut unwrap_redirects: Option<bool> = None;
    let mut domain_blocking = false;
    let mut urls = Vec::new();
    let mut read_stdin = false;
    let mut action = Action::Run;
    let mut end_of_options = false;

    for arg in raw {
        // After `--` every argument is a URL, so `url-sanitize -- "$untrusted"`
        // can't be turned into a flag.
        if end_of_options {
            urls.push(arg);
            continue;
        }
        match arg.as_str() {
            "--" => end_of_options = true,
            "-h" | "--help" => action = Action::Help,
            "--version" => {
                if action != Action::Help {
                    action = Action::Version;
                }
            }
            "--json" => json = true,
            "--explain" => explain = true,
            "--text" => text = true,
            "--strip-referral" => strip_referral = true,
            "--keep-referral" => keep_referral_explicit = true,
            "--unwrap-redirects" => unwrap_redirects = Some(true),
            "--no-unwrap-redirects" => unwrap_redirects = Some(false),
            "--block-domains" => domain_blocking = true,
            "-" => read_stdin = true,
            s if s.starts_with("--") || (s.starts_with('-') && s.len() > 1) => {
                return Err(format!("unknown flag: {}", s));
            }
            _ => urls.push(arg),
        }
    }

    if keep_referral_explicit {
        strip_referral = false;
    }

    Ok(Args {
        action,
        json,
        explain,
        text,
        options: SanitizerOptions {
            strip_referral_marketing: strip_referral,
            unwrap_redirects,
            domain_blocking,
        },
        urls,
        read_stdin,
    })
}

/// Percent-encode control and bidi-override characters for text output: an
/// unparseable input is echoed as given, and `\x1b[...` or U+202E in it would
/// reach the terminal. Must match `printable()` in the TS CLI.
fn printable(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        let unprintable = matches!(ch, '\u{0}'..='\u{1f}' | '\u{7f}'..='\u{9f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}');
        if unprintable {
            let mut buf = [0u8; 4];
            for b in ch.encode_utf8(&mut buf).bytes() {
                out.push_str(&format!("%{:02X}", b));
            }
        } else {
            out.push(ch);
        }
    }
    out
}

/// Writes one result; returns whether it was blocked. A write error is
/// passed up so the run stops instead of reporting success.
fn emit(
    res: &SanitizeResult,
    json: bool,
    explain: bool,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
) -> io::Result<bool> {
    if json {
        // serde_json::to_string is infallible on owned types here.
        let s = serde_json::to_string(res).expect("serialize SanitizeResult");
        writeln!(stdout, "{}", s)?;
        return Ok(matches!(res, SanitizeResult::Blocked { .. }));
    }
    match res {
        SanitizeResult::Unchanged { url }
        | SanitizeResult::Cleaned { url, .. }
        | SanitizeResult::Redirected { url, .. } => writeln!(stdout, "{}", printable(url))?,
        SanitizeResult::Blocked { .. } => {}
    }
    report(res, explain, stderr)
}

/// Writes a result's stderr line: the match with `--explain`, and every
/// block. Returns whether it was blocked.
fn report(res: &SanitizeResult, explain: bool, stderr: &mut dyn Write) -> io::Result<bool> {
    match res {
        SanitizeResult::Unchanged { .. } => Ok(false),
        SanitizeResult::Cleaned {
            url,
            original,
            matched_rules,
            ..
        } => {
            if explain {
                let providers: Vec<_> = matched_rules.iter().map(|m| m.provider.as_str()).collect();
                writeln!(
                    stderr,
                    "# cleaned: {} -> {} [{}]",
                    printable(original),
                    printable(url),
                    printable(&providers.join(","))
                )?;
            }
            Ok(false)
        }
        SanitizeResult::Redirected { url, original, via } => {
            if explain {
                writeln!(
                    stderr,
                    "# redirected: {} -> {} [{}]",
                    printable(original),
                    printable(url),
                    printable(&via.provider)
                )?;
            }
            Ok(false)
        }
        SanitizeResult::Blocked { original, via } => {
            writeln!(
                stderr,
                "blocked: {} (via {})",
                printable(original),
                printable(&via.provider)
            )?;
            Ok(true)
        }
    }
}

/// `--text`: writes one piece of text with its URLs cleaned, or with
/// `--json` one result per URL. Returns whether a URL was blocked.
fn emit_text(
    input: &str,
    sanitizer: &Sanitizer,
    args: &Args,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
) -> io::Result<bool> {
    let result = sanitizer.sanitize_text(input);
    if !args.json {
        stdout.write_all(result.text.as_bytes())?;
    }
    let mut any_blocked = false;
    for url in &result.urls {
        any_blocked |= if args.json {
            emit(url, true, false, stdout, stderr)?
        } else {
            report(url, args.explain, stderr)?
        };
    }
    Ok(any_blocked)
}

/// A reader that closes early (`| head -1`) ends the run quietly with the
/// exit code so far; any other write error exits 1.
fn write_failed(err: &io::Error, any_blocked: bool) -> ExitCode {
    if err.kind() == io::ErrorKind::BrokenPipe {
        return if any_blocked {
            ExitCode::from(2)
        } else {
            ExitCode::SUCCESS
        };
    }
    eprintln!("error: writing output: {}", err);
    ExitCode::from(1)
}

fn run() -> ExitCode {
    let raw: Vec<String> = std::env::args().skip(1).collect();

    let args = match parse_args(raw) {
        Ok(a) => a,
        Err(msg) => {
            eprintln!("error: {}", msg);
            eprintln!("run `url-sanitize --help` for usage");
            return ExitCode::from(1);
        }
    };

    if args.action == Action::Help {
        print_help(&mut io::stdout().lock());
        return ExitCode::SUCCESS;
    }
    if args.action == Action::Version {
        println!(
            "url-sanitize {} (catalog {} {})",
            CRATE_VERSION,
            catalog().catalog_identity(),
            catalog().generated_at()
        );
        return ExitCode::SUCCESS;
    }

    let sanitizer: Sanitizer = catalog().compile(args.options);

    let mut stdout_lock = io::stdout().lock();
    let mut stderr_lock = io::stderr().lock();
    let mut any_blocked = false;

    let use_stdin = args.read_stdin || (args.urls.is_empty() && !io::stdin().is_terminal());

    if args.urls.is_empty() && !use_stdin {
        print_help(&mut stderr_lock);
        return ExitCode::from(1);
    }

    if args.text {
        return run_text(
            &args,
            &sanitizer,
            use_stdin,
            &mut stdout_lock,
            &mut stderr_lock,
        );
    }

    for url in &args.urls {
        let res = sanitizer.sanitize(url);
        match emit(
            &res,
            args.json,
            args.explain,
            &mut stdout_lock,
            &mut stderr_lock,
        ) {
            Ok(blocked) => any_blocked |= blocked,
            Err(err) => return write_failed(&err, any_blocked),
        }
    }

    if use_stdin {
        let stdin = io::stdin();
        let mut reader = stdin.lock();
        let mut buf = Vec::new();
        loop {
            buf.clear();
            match reader.read_until(b'\n', &mut buf) {
                Ok(0) => break,
                Ok(_) => {}
                Err(err) => {
                    eprintln!("error: reading stdin: {}", err);
                    return ExitCode::from(1);
                }
            }
            // Invalid UTF-8 becomes U+FFFD, as in the TS CLI, instead of
            // silently ending the batch.
            let line = String::from_utf8_lossy(&buf);
            let trimmed = line.trim_end_matches(['\r', '\n']);
            if trimmed.is_empty() {
                continue;
            }
            let res = sanitizer.sanitize(trimmed);
            match emit(
                &res,
                args.json,
                args.explain,
                &mut stdout_lock,
                &mut stderr_lock,
            ) {
                Ok(blocked) => any_blocked |= blocked,
                Err(err) => return write_failed(&err, any_blocked),
            }
        }
    }

    if let Err(err) = stdout_lock.flush() {
        return write_failed(&err, any_blocked);
    }

    if any_blocked {
        ExitCode::from(2)
    } else {
        ExitCode::SUCCESS
    }
}

/// `--text`: copies each argument, then stdin, to stdout with every URL in it
/// cleaned. Unlike URL mode, lines are copied as is: blank lines, `\r` and
/// control characters included.
fn run_text(
    args: &Args,
    sanitizer: &Sanitizer,
    use_stdin: bool,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
) -> ExitCode {
    let mut any_blocked = false;
    for arg in &args.urls {
        match emit_text(&format!("{}\n", arg), sanitizer, args, stdout, stderr) {
            Ok(blocked) => any_blocked |= blocked,
            Err(err) => return write_failed(&err, any_blocked),
        }
    }
    if use_stdin {
        let mut reader = io::stdin().lock();
        let mut buf = Vec::new();
        loop {
            buf.clear();
            match reader.read_until(b'\n', &mut buf) {
                Ok(0) => break,
                Ok(_) => {}
                Err(err) => {
                    eprintln!("error: reading stdin: {}", err);
                    return ExitCode::from(1);
                }
            }
            // Invalid UTF-8 becomes U+FFFD, as in the TS CLI.
            let line = String::from_utf8_lossy(&buf);
            match emit_text(&line, sanitizer, args, stdout, stderr) {
                Ok(blocked) => any_blocked |= blocked,
                Err(err) => return write_failed(&err, any_blocked),
            }
        }
    }
    if let Err(err) = stdout.flush() {
        return write_failed(&err, any_blocked);
    }
    if any_blocked {
        ExitCode::from(2)
    } else {
        ExitCode::SUCCESS
    }
}

fn main() -> ExitCode {
    run()
}