From a39074fc7f04ebe58915492202e0d3ef40c744dd Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 26 Jan 2022 14:17:56 -0600 Subject: [PATCH] fix(parser): Detect shorter base64 values This is part of the way to #413. In that case, they aren't providing padding though. --- crates/typos/src/tokens.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index 0ccb5c2..46490f5 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -356,7 +356,11 @@ mod parser { ::Item: AsChar + Copy, { let (padding, captured) = take_while1(is_base64_digit)(input.clone())?; - if captured.input_len() < 90 { + if captured.input_len() < 90 + && captured + .iter_elements() + .all(|c| !['/', '+'].contains(&c.as_char())) + { return Err(nom::Err::Error(nom::error::Error::new( input, nom::error::ErrorKind::LengthValue, @@ -1049,7 +1053,7 @@ mod test { } #[test] - fn tokenize_ignore_base64() { + fn tokenize_ignore_base64_case_1() { let parser = TokenizerBuilder::new().build(); let input = "Good Iy9+btvut+d92V+v84444ziIqJKHK879KJH59//X1Iy9+btvut+d92V+v84444ziIqJKHK879KJH59//X122Iy9+btvut+d92V+v84444ziIqJKHK879KJH59//X12== Bye"; @@ -1063,6 +1067,18 @@ mod test { assert_eq!(expected, actual); } + #[test] + fn tokenize_ignore_base64_case_2() { + let parser = TokenizerBuilder::new().build(); + + let input = r#""ed25519:1": "Wm+VzmOUOz08Ds+0NTWb1d4CZrVsJSikkeRxh6aCcUwu6pNC78FunoD7KNWzqFn241eYHYMGCA5McEiVPdhzBA==""#; + let expected: Vec = vec![Identifier::new_unchecked("ed25519", Case::None, 1)]; + let actual: Vec<_> = parser.parse_bytes(input.as_bytes()).collect(); + assert_eq!(expected, actual); + let actual: Vec<_> = parser.parse_str(input).collect(); + assert_eq!(expected, actual); + } + #[test] fn tokenize_ignore_email() { let parser = TokenizerBuilder::new().build();