File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 55#include " Cbor.h"
66#include " HexCoding.h"
77#include " Numeric.h"
8+ #include " rust/bindgen/WalletCoreRSBindgen.h"
89
910#include < sstream>
1011#include < cassert>
@@ -307,6 +308,9 @@ uint32_t Decode::getCompoundLength(uint32_t countMultiplier) const {
307308 break ;
308309 }
309310 uint32_t elemLen = nextElem.getTotalLen ();
311+ if (elemLen == 0 || checkAddUnsignedOverflow (len, elemLen)) {
312+ throw std::invalid_argument (" CBOR invalid element length" );
313+ }
310314 if (len + elemLen > length ()) {
311315 throw std::invalid_argument (" CBOR array data too short" );
312316 }
@@ -431,7 +435,13 @@ string Decode::dumpToStringInternal() const {
431435 break ;
432436
433437 case MT_string:
434- s << " \" " << getString () << " \" " ;
438+ {
439+ auto str = getString ();
440+ if (!Rust::tw_string_is_utf8_bytes (reinterpret_cast <const uint8_t *>(str.data ()), str.size ())) {
441+ throw std::invalid_argument (" CBOR string is not valid UTF-8" );
442+ }
443+ s << " \" " << str << " \" " ;
444+ }
435445 break ;
436446
437447 case MT_array:
Original file line number Diff line number Diff line change @@ -559,5 +559,21 @@ TEST(Cbor, BytesOverflowLength_getTotalLen) {
559559 FAIL () << " Expected exception" ;
560560}
561561
562+ TEST (Cbor, GetCompoundLengthOverflow) {
563+ Data overflow = parse_hex (" 9bffffffffffffffff" );
564+ EXPECT_FALSE (Decode (overflow).isValid ());
565+ }
566+
567+ TEST (Cbor, StringInvalidUtf8Throws) {
568+ Data invalidUtf8 = parse_hex (" 63fffefd" );
569+ Decode cbor (invalidUtf8);
570+ EXPECT_THROW (cbor.dumpToString (), std::invalid_argument);
571+ }
572+
573+ TEST (Cbor, StringValidUtf8) {
574+ Data validUtf8 = Encode::string (" hello" ).encoded ();
575+ EXPECT_EQ (" \" hello\" " , Decode (validUtf8).dumpToString ());
576+ }
577+
562578// clang-format on
563579} // namespace TW::Cbor::tests
You can’t perform that action at this time.
0 commit comments