From 1b72dfbcbae61ac6db3502135ed7b7023a6ff008 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 17 Dec 2021 22:32:15 +0200 Subject: [PATCH] Fix: 'fromGlobalId' trowing on invalide UTF code points Fixes #358 Note: it's temprorary workaround until we design a better API --- src/utils/base64.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/utils/base64.ts b/src/utils/base64.ts index 7d70cc1..ff7709d 100644 --- a/src/utils/base64.ts +++ b/src/utils/base64.ts @@ -126,29 +126,42 @@ function utf8ArrayToString(input: Array) { for (let i = 0; i < input.length; ) { const a = input[i++]; if ((a & 0x80) === 0) { - result += String.fromCodePoint(a); + result += fromCodePoint(a); continue; } const b = input[i++]; if ((a & 0xe0) === 0xc0) { - result += String.fromCodePoint(((a & 0x1f) << 6) | (b & 0x3f)); + result += fromCodePoint(((a & 0x1f) << 6) | (b & 0x3f)); continue; } const c = input[i++]; if ((a & 0xf0) === 0xe0) { - result += String.fromCodePoint( + result += fromCodePoint( ((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f), ); continue; } const d = input[i++]; - result += String.fromCodePoint( + result += fromCodePoint( ((a & 0x07) << 18) | ((b & 0x3f) << 12) | ((c & 0x3f) << 6) | (d & 0x3f), ); } return result; } + +function fromCodePoint(code: number): string { + if (code > 0x10FFFF) { + /* + * Previously we used Node's API for parsing Base64 and following code + * Buffer.from(i, 'base64').toString('utf8') + * That silently ignored incorrect input and returned empty string instead + * Let's keep this behaviour for a time being and hopefully fix it in the future. + */ + return ''; + } + return String.fromCodePoint(code); +}