1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
45//! Functions to redact strings to remove PII before logging them
67/// Redact a URL.
8///
9/// It's tricky to redact an URL without revealing PII. We check for various known bad URL forms
10/// and report them, otherwise we just log "<URL>".
11pub fn redact_url(url: &str) -> String {
12if url.is_empty() {
13return "<URL (empty)>".to_string();
14 }
15match url.find(':') {
16None => "<URL (no scheme)>".to_string(),
17Some(n) => {
18let mut chars = url[0..n].chars();
19match chars.next() {
20// No characters in the scheme
21None => return "<URL (empty scheme)>".to_string(),
22Some(c) => {
23// First character must be alphabetic
24if !c.is_ascii_alphabetic() {
25return "<URL (invalid scheme)>".to_string();
26 }
27 }
28 }
29for c in chars {
30// Subsequent characters must be in the set ( alpha | digit | "+" | "-" | "." )
31if !(c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.') {
32return "<URL (invalid scheme)>".to_string();
33 }
34 }
35"<URL>".to_string()
36 }
37 }
38}
3940/// Redact compact jwe string (Five base64 segments, separated by `.` chars)
41pub fn redact_compact_jwe(url: &str) -> String {
42 url.replace(|ch| ch != '.', "x")
43}
4445#[cfg(test)]
46mod test {
47use super::*;
4849#[test]
50fn test_redact_url() {
51assert_eq!(redact_url("http://some.website.com/index.html"), "<URL>");
52assert_eq!(redact_url("about:config"), "<URL>");
53assert_eq!(redact_url(""), "<URL (empty)>");
54assert_eq!(redact_url("://some.website.com/"), "<URL (empty scheme)>");
55assert_eq!(redact_url("some.website.com/"), "<URL (no scheme)>");
56assert_eq!(redact_url("some.website.com/"), "<URL (no scheme)>");
57assert_eq!(
58 redact_url("abc%@=://some.website.com/"),
59"<URL (invalid scheme)>"
60);
61assert_eq!(
62 redact_url("0https://some.website.com/"),
63"<URL (invalid scheme)>"
64);
65assert_eq!(
66 redact_url("a+weird-but.lega1-SCHEME://some.website.com/"),
67"<URL>"
68);
69 }
7071#[test]
72fn test_redact_compact_jwe() {
73assert_eq!(redact_compact_jwe("abc.1234.x3243"), "xxx.xxxx.xxxxx")
74 }
75}