From 2b1f368c93861ef751e32574b08ee4caa5e80c7f Mon Sep 17 00:00:00 2001 From: "David I. Lehn" Date: Mon, 7 Mar 2022 19:41:19 -0500 Subject: [PATCH] Add fallback to pretty print invalid UTF8 data. Malformed UTF8 data can cause the escaping code to fail. Capture the failures and print out a hex version with error note. --- CHANGELOG.md | 5 +++++ lib/asn1.js | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 730767d0b..075ea30a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Forge ChangeLog =============== +## 1.3.0 - 2022-XXX + +### Fixed +- [asn1] Add fallback to pretty print invalid UTF8 data. + ## 1.2.1 - 2022-01-11 ### Fixed diff --git a/lib/asn1.js b/lib/asn1.js index e0fea0e08..c142f6358 100644 --- a/lib/asn1.js +++ b/lib/asn1.js @@ -1391,7 +1391,16 @@ asn1.prettyPrint = function(obj, level, indentation) { } rval += '0x' + forge.util.bytesToHex(obj.value); } else if(obj.type === asn1.Type.UTF8) { - rval += forge.util.decodeUtf8(obj.value); + try { + rval += forge.util.decodeUtf8(obj.value); + } catch(e) { + if(e.message === 'URI malformed') { + rval += + '0x' + forge.util.bytesToHex(obj.value) + ' (malformed UTF8)'; + } else { + throw e; + } + } } else if(obj.type === asn1.Type.PRINTABLESTRING || obj.type === asn1.Type.IA5String) { rval += obj.value;