| 1 |
use sequoia_openpgp as openpgp; |
| 2 |
use openpgp::packet::UserID; |
| 3 |
|
| 4 |
/// A canonical user ID. |
| 5 |
#[derive(thiserror::Error, Debug)] |
| 6 |
pub enum UserIDLint { |
| 7 |
/// Not UTF-8 Encoded. |
| 8 |
#[error("{:?} is not UTF-8 encoded", |
| 9 |
String::from_utf8_lossy(.0.value()))] |
| 10 |
NotUTF8Encoded(UserID, #[source] std::str::Utf8Error), |
| 11 |
|
| 12 |
/// Not in canonical form. |
| 13 |
/// |
| 14 |
/// Canonical form is: `Display Name (Comment) |
| 15 |
/// <[email protected]>` where either the display name, comment or |
| 16 |
/// email address is required. |
| 17 |
#[error("The user ID {:?} is not in canonical form{}", |
| 18 |
String::from_utf8_lossy(.0.value()), |
| 19 |
.1.as_ref() |
| 20 |
.map(|u| { |
| 21 |
format!(", try {:?}", |
| 22 |
String::from_utf8_lossy(u.value())) |
| 23 |
}) |
| 24 |
.unwrap_or_else(|| "".to_string()))] |
| 25 |
NotCanonical(UserID, Option<UserID>), |
| 26 |
|
| 27 |
/// Bare emails are technically in canonical form, but are not |
| 28 |
/// advisable. |
| 29 |
/// |
| 30 |
/// Bare emails are problematic, because regular expressions that |
| 31 |
/// match on email addresses usually assume the email address is |
| 32 |
/// in angle brackets. |
| 33 |
#[error("{:?} is a bare email address, try \"<{}>\"", |
| 34 |
String::from_utf8_lossy(.0.value()), |
| 35 |
String::from_utf8_lossy(.0.value()))] |
| 36 |
BareEmail(UserID), |
| 37 |
|
| 38 |
/// The name has excessive space characters at the beginning or |
| 39 |
/// end. |
| 40 |
#[error("{:?} has spaces at the beginning or the end, try {:?}", |
| 41 |
.0, .0.trim())] |
| 42 |
NameHasExcessSpaces(String), |
| 43 |
|
| 44 |
/// The name contains a comment. |
| 45 |
#[error("{:?} contains a comment {:?}, remove it or use --userid", |
| 46 |
.0, .1)] |
| 47 |
NameContainsComment(String, String), |
| 48 |
|
| 49 |
/// The name contains an email address. |
| 50 |
#[error("{:?} contains an email address {:?}, remove it or use --userid", |
| 51 |
.0, .1)] |
| 52 |
NameContainsEmail(String, String), |
| 53 |
|
| 54 |
/// The name is a bare email address. |
| 55 |
#[error("{:?} is a bare email address, use --email", .0)] |
| 56 |
NameIsBareEmail(String), |
| 57 |
|
| 58 |
/// The email has excessive space characters at the beginning or |
| 59 |
/// end. |
| 60 |
#[error("{:?} is not a bare email address; it has spaces at the \ |
| 61 |
beginning or the end, try {:?}", |
| 62 |
.0, .0.trim())] |
| 63 |
EmailHasExcessSpaces(String), |
| 64 |
|
| 65 |
/// The email contains a comment. |
| 66 |
#[error("{:?} is not a bare email address: it contains a comment {:?}; \ |
| 67 |
remove it or use --userid", .0, .1)] |
| 68 |
EmailContainsComment(String, String), |
| 69 |
|
| 70 |
/// The email contains a name. |
| 71 |
#[error("{:?} is not a bare email address: it contains a name {:?}; \ |
| 72 |
remove it or use --userid", |
| 73 |
.0, .1)] |
| 74 |
EmailContainsName(String, String), |
| 75 |
|
| 76 |
/// The email is not a bare email address. |
| 77 |
#[error("{:?} is not a bare email address, try {:?}", |
| 78 |
.0, &.0[1..(.0.len()-1)])] |
| 79 |
EmailIsNotBare(String), |
| 80 |
} |
| 81 |
|
| 82 |
/// Returns an error if the user ID is not in canonical form. |
| 83 |
pub(crate) fn lint_userid(uid: &UserID) |
| 84 |
-> std::result::Result<(), UserIDLint> |
| 85 |
{ |
| 86 |
let mut components = Vec::new(); |
| 87 |
|
| 88 |
let map_err = |err: anyhow::Error| { |
| 89 |
if let Ok(err) = err.downcast::<std::str::Utf8Error>() { |
| 90 |
return UserIDLint::NotUTF8Encoded(uid.clone(), err); |
| 91 |
} |
| 92 |
|
| 93 |
UserIDLint::NotCanonical(uid.clone(), None) |
| 94 |
}; |
| 95 |
|
| 96 |
// Returning the name (or comment or email address) means |
| 97 |
// checking that the user ID is in canonical form. Thus, |
| 98 |
// if this fails, the user ID is not in canonical form. |
| 99 |
if let Some(name) = uid.name().map_err(map_err)? { |
| 100 |
components.push(name.to_string()); |
| 101 |
} |
| 102 |
|
| 103 |
if let Some(comment) = uid.comment().map_err(map_err)? { |
| 104 |
components.push(format!("({})", comment)); |
| 105 |
} |
| 106 |
|
| 107 |
let email = if let Some(email) = uid.email().map_err(map_err)? { |
| 108 |
components.push(format!("<{}>", email)); |
| 109 |
Some(email) |
| 110 |
} else { |
| 111 |
None |
| 112 |
}; |
| 113 |
|
| 114 |
let userid_canonical = UserID::from(components.join(" ")); |
| 115 |
if &userid_canonical != uid { |
| 116 |
if email.map(|e| e.as_bytes()) == Some(&uid.value()) { |
| 117 |
// Bare email address. |
| 118 |
return Err(UserIDLint::BareEmail(uid.clone())); |
| 119 |
} else { |
| 120 |
return Err(UserIDLint::NotCanonical( |
| 121 |
uid.clone(), Some(userid_canonical))); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
Ok(()) |
| 126 |
} |
| 127 |
|
| 128 |
/// Lints user IDs and displays the lints. |
| 129 |
/// |
| 130 |
/// Returns an error if any of the user IDs have lints. |
| 131 |
pub(crate) fn lint_userids(uids: &[UserID]) -> Result<(), anyhow::Error> { |
| 132 |
let mut non_canonical = Vec::new(); |
| 133 |
for uid in uids.into_iter() { |
| 134 |
if let Err(err) = lint_userid(&uid) { |
| 135 |
non_canonical.push(err) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if non_canonical.is_empty() { |
| 140 |
Ok(()) |
| 141 |
} else { |
| 142 |
if non_canonical.len() == 1 { |
| 143 |
weprintln!("{}.", non_canonical[0]); |
| 144 |
weprintln!(); |
| 145 |
} else { |
| 146 |
weprintln!("The following user IDs are not in canonical form:"); |
| 147 |
weprintln!(); |
| 148 |
for err in non_canonical.iter() { |
| 149 |
weprintln!(initial_indent = " - ", "{}", err); |
| 150 |
} |
| 151 |
weprintln!(); |
| 152 |
} |
| 153 |
|
| 154 |
let bare_email = non_canonical.iter() |
| 155 |
.filter_map(|err| { |
| 156 |
if let UserIDLint::BareEmail(uid) = err { |
| 157 |
Some(uid.clone()) |
| 158 |
} else { |
| 159 |
None |
| 160 |
} |
| 161 |
}) |
| 162 |
.next(); |
| 163 |
|
| 164 |
weprintln!("Canonical user IDs are of the form \ |
| 165 |
`Name <[email protected]>`. {}\ |
| 166 |
Consider fixing the user IDs or passing \ |
| 167 |
`--allow-non-canonical-userids`.", |
| 168 |
if let Some(uid) = bare_email { |
| 169 |
format!("Bare email addresses should be wrapped in angle \ |
| 170 |
brackets like so `<{}>`. ", |
| 171 |
String::from_utf8_lossy(uid.value())) |
| 172 |
} else { |
| 173 |
"".to_string() |
| 174 |
}); |
| 175 |
weprintln!(); |
| 176 |
|
| 177 |
Err(anyhow::anyhow!("\ |
| 178 |
Some user IDs are not in canonical form")) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/// Returns an error if the given name is anything but a name. |
| 183 |
pub fn lint_name(n: &str) -> Result<(), UserIDLint> { |
| 184 |
let u = UserID::from(n); |
| 185 |
|
| 186 |
// Check for various problems for which we can give concrete |
| 187 |
// advice. |
| 188 |
let e = UserID::from(format!("x <{}>", n)); |
| 189 |
if e.email().ok().flatten() == Some(n) { |
| 190 |
return Err(UserIDLint::NameIsBareEmail(n.into())); |
| 191 |
} |
| 192 |
|
| 193 |
if u.name().ok().flatten() == Some(n.trim()) && n != n.trim() { |
| 194 |
return Err(UserIDLint::NameHasExcessSpaces(n.into())); |
| 195 |
} |
| 196 |
|
| 197 |
let c = UserID::from(format!("x {}", n)); |
| 198 |
if let Some(comment) = c.comment().ok().flatten() { |
| 199 |
return Err(UserIDLint::NameContainsComment(n.into(), comment.into())); |
| 200 |
} |
| 201 |
|
| 202 |
if let Some(email) = u.email().ok().flatten() { |
| 203 |
return Err(UserIDLint::NameContainsEmail(n.into(), email.into())); |
| 204 |
} |
| 205 |
|
| 206 |
// This is the only acceptable path, really. |
| 207 |
if u.name().ok().flatten() == Some(n) { |
| 208 |
return Ok(()); |
| 209 |
} |
| 210 |
|
| 211 |
// For a final sanity check, defer to lint_userid. |
| 212 |
lint_userid(&u) |
| 213 |
} |
| 214 |
|
| 215 |
/// Lints names and displays the lints. |
| 216 |
/// |
| 217 |
/// Returns an error if any of the names have lints. |
| 218 |
pub fn lint_names(names: &[String]) -> Result<(), anyhow::Error> { |
| 219 |
let non_canonical = |
| 220 |
names.iter().filter_map(|n| lint_name(&n).err()).collect::<Vec<_>>(); |
| 221 |
|
| 222 |
if non_canonical.is_empty() { |
| 223 |
Ok(()) |
| 224 |
} else { |
| 225 |
if non_canonical.len() == 1 { |
| 226 |
weprintln!("{}.", non_canonical[0]); |
| 227 |
weprintln!(); |
| 228 |
} else { |
| 229 |
weprintln!("The following names have issues:"); |
| 230 |
weprintln!(); |
| 231 |
for err in non_canonical.iter() { |
| 232 |
weprintln!(initial_indent = " - ", "{}", err); |
| 233 |
} |
| 234 |
weprintln!(); |
| 235 |
} |
| 236 |
|
| 237 |
Err(anyhow::anyhow!("Some names had issues")) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/// Returns an error if the given email is anything but a email. |
| 242 |
pub fn lint_email(n: &str) -> Result<(), UserIDLint> { |
| 243 |
let u = UserID::from(format!("<{}>", n)); |
| 244 |
|
| 245 |
// Check for various problems for which we can give concrete |
| 246 |
// advice. |
| 247 |
if n.starts_with('<') && n.ends_with('>') { |
| 248 |
return Err(UserIDLint::EmailIsNotBare(n.into())); |
| 249 |
} |
| 250 |
|
| 251 |
if n != n.trim() { |
| 252 |
return Err(UserIDLint::EmailHasExcessSpaces(n.into())); |
| 253 |
} |
| 254 |
|
| 255 |
let c = UserID::from(format!("x {}", n)); |
| 256 |
if let Some(comment) = c.comment().ok().flatten() { |
| 257 |
return Err(UserIDLint::EmailContainsComment(n.into(), comment.into())); |
| 258 |
} |
| 259 |
|
| 260 |
let un = UserID::from(n); |
| 261 |
if let Some(name) = un.name().ok().flatten() { |
| 262 |
return Err(UserIDLint::EmailContainsName(n.into(), name.into())); |
| 263 |
} |
| 264 |
|
| 265 |
// This is the only acceptable path, really. |
| 266 |
if u.email().ok().flatten() == Some(n) { |
| 267 |
return Ok(()); |
| 268 |
} |
| 269 |
|
| 270 |
// For a final sanity check, defer to lint_userid. |
| 271 |
lint_userid(&u) |
| 272 |
} |
| 273 |
|
| 274 |
/// Lints email addresses and displays the lints. |
| 275 |
/// |
| 276 |
/// Returns an error if any of the emails have lints. |
| 277 |
pub fn lint_emails(emails: &[String]) -> Result<(), anyhow::Error> { |
| 278 |
let non_canonical = |
| 279 |
emails.iter().filter_map(|n| lint_email(&n).err()).collect::<Vec<_>>(); |
| 280 |
|
| 281 |
if non_canonical.is_empty() { |
| 282 |
Ok(()) |
| 283 |
} else { |
| 284 |
if non_canonical.len() == 1 { |
| 285 |
weprintln!("{}.", non_canonical[0]); |
| 286 |
weprintln!(); |
| 287 |
} else { |
| 288 |
weprintln!("The following email addresses have issues:"); |
| 289 |
weprintln!(); |
| 290 |
for err in non_canonical.iter() { |
| 291 |
weprintln!(initial_indent = " - ", "{}", err); |
| 292 |
} |
| 293 |
weprintln!(); |
| 294 |
} |
| 295 |
|
| 296 |
Err(anyhow::anyhow!("Some email addresses had issues")) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
#[cfg(test)] |
| 301 |
mod test { |
| 302 |
use super::*; |
| 303 |
|
| 304 |
#[test] |
| 305 |
fn userid_lint() { |
| 306 |
// Invalid UTF-8. |
| 307 |
match lint_userid(&UserID::from(vec![0, 159, 146, 150])) { |
| 308 |
Err(UserIDLint::NotUTF8Encoded(_, _)) => (), |
| 309 |
Err(err) => { |
| 310 |
panic!("User ID with invalid UTF-8 resulted in wrong error: {}", |
| 311 |
err); |
| 312 |
} |
| 313 |
Ok(()) => { |
| 314 |
panic!("User ID with invalid UTF-8 was not rejected"); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// Bare email. |
| 319 |
match lint_userid(&UserID::from("[email protected]")) { |
| 320 |
Err(UserIDLint::BareEmail(_)) => (), |
| 321 |
Err(err) => { |
| 322 |
panic!("User ID with bare email address \ |
| 323 |
resulted in wrong error: {}", |
| 324 |
err); |
| 325 |
} |
| 326 |
Ok(()) => { |
| 327 |
panic!("User ID with bare email address was not rejected"); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
for uid in [ |
| 332 |
"<foo@[email protected]>", |
| 333 |
"<[email protected]", |
| 334 |
].into_iter() { |
| 335 |
match lint_userid(&UserID::from(uid)) { |
| 336 |
Err(UserIDLint::NotCanonical(_, _)) => (), |
| 337 |
Err(err) => { |
| 338 |
panic!("Non-canonical User ID ({}) \ |
| 339 |
resulted in wrong error: {}", |
| 340 |
uid, err); |
| 341 |
} |
| 342 |
Ok(()) => { |
| 343 |
panic!("Non-canonical User ID ({}) was not rejected", |
| 344 |
uid); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn name_lint() { |
| 352 |
use UserIDLint::*; |
| 353 |
// XXX: Use assert_matches! once available. |
| 354 |
assert!(matches!(dbg!(lint_name("Foo Bar")), Ok(()))); |
| 355 |
assert!(matches!(dbg!(lint_name("Foo Bar ")), |
| 356 |
Err(NameHasExcessSpaces(..)))); |
| 357 |
assert!(matches!(dbg!(lint_name(" Foo Bar")), |
| 358 |
Err(NameHasExcessSpaces(..)))); |
| 359 |
assert!(matches!(dbg!(lint_name("Foo Bar (comment)")), |
| 360 |
Err(NameContainsComment(..)))); |
| 361 |
assert!(matches!(dbg!(lint_name("Foo Bar <[email protected]>")), |
| 362 |
Err(NameContainsEmail(..)))); |
| 363 |
assert!(matches!(dbg!(lint_name("<[email protected]>")), |
| 364 |
Err(NameContainsEmail(..)))); |
| 365 |
assert!(matches!(dbg!(lint_name("[email protected]")), |
| 366 |
Err(NameIsBareEmail(..)))); |
| 367 |
assert!(matches!(dbg!(lint_name("(comment)")), |
| 368 |
Err(NameContainsComment(..)))); |
| 369 |
assert!(matches!(dbg!(lint_name("(comment) <[email protected]>")), |
| 370 |
Err(NameContainsComment(..)))); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn email_lint() { |
| 375 |
use UserIDLint::*; |
| 376 |
// XXX: Use assert_matches! once available. |
| 377 |
assert!(matches!(dbg!(lint_email("[email protected]")), Ok(()))); |
| 378 |
assert!(matches!(dbg!(lint_email("[email protected] ")), |
| 379 |
Err(EmailHasExcessSpaces(..)))); |
| 380 |
assert!(matches!(dbg!(lint_email(" [email protected]")), |
| 381 |
Err(EmailHasExcessSpaces(..)))); |
| 382 |
assert!(matches!(dbg!(lint_email("[email protected] (comment)")), |
| 383 |
Err(EmailContainsComment(..)))); |
| 384 |
assert!(matches!(dbg!(lint_email("Foo Bar <[email protected]>")), |
| 385 |
Err(EmailContainsName(..)))); |
| 386 |
assert!(matches!(dbg!(lint_email("[email protected] <[email protected]>")), |
| 387 |
Err(EmailContainsName(..)))); |
| 388 |
assert!(matches!(dbg!(lint_email("<[email protected]>")), |
| 389 |
Err(EmailIsNotBare(..)))); |
| 390 |
assert!(matches!(dbg!(lint_email("(comment)")), |
| 391 |
Err(EmailContainsComment(..)))); |
| 392 |
assert!(matches!(dbg!(lint_email("(comment) <[email protected]>")), |
| 393 |
Err(EmailContainsComment(..)))); |
| 394 |
} |
| 395 |
} |