From fbd258ec812fba72ad11346195f8407af870ddf3 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Sat, 11 Jun 2022 14:39:12 +0900 Subject: [PATCH 1/4] fix(binascii): a2b_base64() raises on non padded input --- stdlib/src/binascii.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index 5dad5dce0..a84142902 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -104,6 +104,9 @@ mod decl { } else { b }; + if b.len() % 4 != 0 { + return Err(base64::DecodeError::InvalidLength) + } base64::decode(b) }) .map_err(|err| vm.new_value_error(format!("error decoding base64: {}", err))) From d2b8686de37337cacb8df2f4fdcf27e1b1f15e14 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Sat, 11 Jun 2022 14:39:45 +0900 Subject: [PATCH 2/4] fix(binascii): a2b_base64() raises binascii.Error instead of ValueError --- stdlib/src/binascii.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index a84142902..48cdf5df3 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -5,7 +5,7 @@ pub(super) use decl::crc32; #[pymodule(name = "binascii")] mod decl { use crate::vm::{ - builtins::{PyIntRef, PyTypeRef}, + builtins::{PyBaseExceptionRef, PyIntRef, PyTypeRef}, function::{ArgAsciiBuffer, ArgBytesLike, OptionalArg}, PyResult, VirtualMachine, }; @@ -93,6 +93,10 @@ mod decl { newline: bool, } + fn new_binascii_error(msg: String, vm: &VirtualMachine) -> PyBaseExceptionRef { + vm.new_exception_msg(error_type(vm), msg) + } + #[pyfunction] fn a2b_base64(s: ArgAsciiBuffer, vm: &VirtualMachine) -> PyResult> { s.with_ref(|b| { @@ -109,7 +113,7 @@ mod decl { } base64::decode(b) }) - .map_err(|err| vm.new_value_error(format!("error decoding base64: {}", err))) + .map_err(|err| new_binascii_error(format!("error decoding base64: {}", err), vm)) } #[pyfunction] From 6bd9c81626062b751181950a5188d1c52123ad82 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Sat, 11 Jun 2022 14:40:08 +0900 Subject: [PATCH 3/4] fix(binascii): enable test_b64decode_padding_error --- Lib/test/test_base64.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 634c99b17..86164dd82 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -235,8 +235,6 @@ class BaseXYTestCase(unittest.TestCase): b'\xd3V\xbeo\xf7\x1d') self.check_decode_type_errors(base64.urlsafe_b64decode) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_b64decode_padding_error(self): self.assertRaises(binascii.Error, base64.b64decode, b'abc') self.assertRaises(binascii.Error, base64.b64decode, 'abc') From 3bbd59005f470b4ef51dc9a6ecf77651ae1dfbf7 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Sat, 11 Jun 2022 15:05:51 +0900 Subject: [PATCH 4/4] style(binascii): run rustfmt --- stdlib/src/binascii.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index 48cdf5df3..3f72eee3b 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -109,7 +109,7 @@ mod decl { b }; if b.len() % 4 != 0 { - return Err(base64::DecodeError::InvalidLength) + return Err(base64::DecodeError::InvalidLength); } base64::decode(b) })