1use tr064::Service;
5
6use crate::names::{Sensitivity, sensitivity};
7use crate::policy;
8
9pub const HIDDEN: &str = "***";
11
12pub fn value(name: &str, value: &str) -> String {
16 if sensitivity(name) != Sensitivity::Plain {
17 return HIDDEN.to_owned();
18 }
19 let mut redacted = String::with_capacity(value.len());
20 let mut rest = value;
21 while let Some(position) = rest.find("sid=") {
22 let start = position + "sid=".len();
23 let parameter = rest[..position].ends_with(['?', '&']);
24 redacted.push_str(&rest[..start]);
25 rest = &rest[start..];
26 if parameter {
27 redacted.push_str(HIDDEN);
28 rest = &rest[rest.find(['&', '#']).unwrap_or(rest.len())..];
29 }
30 }
31 redacted.push_str(rest);
32 redacted
33}
34
35const ACCESS_OUTPUTS: [(&str, &str); 1] = [("X_AVM-DE_Filelinks", "NewUrl")];
38
39pub fn output(service: &Service, name: &str, value: &str) -> String {
43 if ACCESS_OUTPUTS.contains(&(service.name(), name)) {
44 HIDDEN.to_owned()
45 } else {
46 self::value(name, value)
47 }
48}
49
50#[derive(Debug, Clone, Copy, Default)]
54pub struct Log;
55
56impl tr064::Redact for Log {
57 fn argument(&self, service: &Service, action: &str, name: &str, value: &str) -> String {
58 if policy::is_personal(service, action) || policy::is_personal_output(name) {
59 format!("({} bytes of personal data)", value.len())
60 } else {
61 output(service, name, value)
62 }
63 }
64
65 fn parameter(&self, name: &str, value: &str) -> String {
66 self::value(name, value)
67 }
68}
69
70pub fn error(error: tr064::Error) -> tr064::Error {
73 match error {
74 tr064::Error::Http(error) => tr064::Error::Http(error.without_url()),
75 tr064::Error::Status { status, url } => tr064::Error::Status {
76 status,
77 url: value("", &url),
78 },
79 other => other,
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use tr064::{Client, Error, Redact as _, Url};
86
87 use super::*;
88
89 #[tokio::test]
90 async fn hides_session_ids_in_errors() {
91 let status = Error::Status {
92 status: 403,
93 url: "http://box/devicehostlist.lua?sid=0123abcd".to_owned(),
94 };
95 assert_eq!(
96 error(status).to_string(),
97 "HTTP status 403 from http://box/devicehostlist.lua?sid=***"
98 );
99 let client = Client::builder(Url::parse("http://127.0.0.1:1").unwrap())
100 .build()
101 .unwrap();
102 let http = client
103 .get("/devicehostlist.lua?sid=0123abcd")
104 .await
105 .unwrap_err();
106 assert!(http.to_string().contains("0123abcd"));
107 assert!(!error(http).to_string().contains("0123abcd"));
108 assert!(matches!(
109 error(Error::AuthenticationFailed),
110 Error::AuthenticationFailed
111 ));
112 }
113
114 fn service(name: &str) -> Service {
115 Service {
116 service_type: format!("urn:dslforum-org:service:{name}:1"),
117 service_id: format!("urn:{name}-com:serviceId:{name}1"),
118 control_url: String::new(),
119 event_sub_url: None,
120 scpd_url: String::new(),
121 }
122 }
123
124 #[test]
125 fn hides_secrets_and_personal_data_in_the_log() {
126 let wlan = service("WLANConfiguration");
127 assert_eq!(
128 Log.argument(&wlan, "GetSecurityKeys", "NewKeyPassphrase", "wlan-key"),
129 "***"
130 );
131 assert_eq!(
132 Log.argument(&wlan, "GetInfo", "NewSSID", "wlan-1"),
133 "wlan-1"
134 );
135 let ontel = service("X_AVM-DE_OnTel");
136 assert_eq!(
137 Log.argument(
138 &ontel,
139 "GetPhonebookEntry",
140 "NewPhonebookEntryData",
141 "<contact/>"
142 ),
143 "(10 bytes of personal data)"
144 );
145 let info = service("DeviceInfo");
146 assert_eq!(
147 Log.argument(&info, "GetInfo", "NewDeviceLog", "log"),
148 "(3 bytes of personal data)"
149 );
150 let links = service("X_AVM-DE_Filelinks");
151 assert_eq!(
152 Log.argument(
153 &links,
154 "GetGenericFilelinkEntry",
155 "NewUrl",
156 "https://link/a"
157 ),
158 "***"
159 );
160 assert_eq!(
161 Log.argument(&links, "GetGenericFilelinkEntry", "NewPath", "/photos"),
162 "/photos"
163 );
164 assert_eq!(
165 Log.argument(&info, "GetInfo", "NewUrl", "https://box/"),
166 "https://box/"
167 );
168 assert_eq!(Log.parameter("sid", "0123abcd"), "***");
169 assert_eq!(Log.parameter("version", "2"), "2");
170 }
171
172 #[test]
173 fn hides_secrets_and_session_ids() {
174 assert_eq!(value("NewX_AVM-DE_WPSClientPIN", "1234"), "***");
175 assert_eq!(value("NewSSID", "home"), "home");
176 assert_eq!(value("NewUserName", "provider-login@isp"), "***");
177 assert_eq!(value("NewParameterKey", "abc"), "***");
178 assert_eq!(value("NewMinCharsPSK", "8"), "8");
179 assert_eq!(
180 value("NewX_AVM-DE_HostListPath", "/hostlist.lua?sid=0123abcd"),
181 "/hostlist.lua?sid=***"
182 );
183 assert_eq!(
184 value(
185 "NewCallListURL",
186 "http://box/calllist.lua?a=1&sid=ab&max=9#x"
187 ),
188 "http://box/calllist.lua?a=1&sid=***&max=9#x"
189 );
190 assert_eq!(value("NewName", "outsid=1, sid=2"), "outsid=1, sid=2");
191 }
192}