From 7de47ec6d2c4978c44fff27c972e43686995fcb6 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 12 Nov 2024 20:07:37 +0100 Subject: [PATCH 1/3] fix: treat "ascii" buffer encoding as "utf-8" --- packages/shared/src/Bytes.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/shared/src/Bytes.ts b/packages/shared/src/Bytes.ts index 52bf0a0537ef..9c3c70990510 100644 --- a/packages/shared/src/Bytes.ts +++ b/packages/shared/src/Bytes.ts @@ -132,8 +132,20 @@ export class Bytes extends Uint8Array { return uint8ArrayToBase64(this); case "base64url": return uint8ArrayToBase64(this, { urlSafe: true }); + return uint8ArrayToString(this, "utf8"); + case "ucs-2": + case "ucs2": + case "utf16le": + return uint8ArrayToString(this, "utf-16le"); + case "ascii": + case "latin1": + case "binary": + // For TextDecoder, these are aliases for "windows-1252" + // which is not supported with small-icu or without ICU. + // When dealing with actual ASCII data, there is no difference + // to simply using "utf8" instead. default: - return uint8ArrayToString(this, encoding); + return uint8ArrayToString(this, "utf-8"); } } From 3f31b476c12d0737e76f0c1ac7ce2ac782cdf988 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 12 Nov 2024 20:18:20 +0100 Subject: [PATCH 2/3] fix: test --- packages/cc/src/cc/EntryControlCC.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/cc/src/cc/EntryControlCC.ts b/packages/cc/src/cc/EntryControlCC.ts index f4dcb6c5c907..e136c3cde1f8 100644 --- a/packages/cc/src/cc/EntryControlCC.ts +++ b/packages/cc/src/cc/EntryControlCC.ts @@ -459,14 +459,21 @@ export class EntryControlCCNotification extends EntryControlCC { eventDataLength === 16 || eventDataLength === 32, ); } - eventData = eventData.toString("ascii"); + // Trim 0xff padding bytes + let paddingStart = eventDataLength; + while ( + paddingStart > 0 + && eventData[paddingStart - 1] === 0xff + ) { + paddingStart--; + } + eventData = eventData.subarray(0, paddingStart).toString( + "ascii", + ); + if (!noStrictValidation) { - validatePayload( - /^[\u0000-\u007f]+[\u00ff]*$/.test(eventData), - ); + validatePayload(/^[\u0000-\u007f]+$/.test(eventData)); } - // Trim 0xff padding bytes - eventData = eventData.replace(/[\u00ff]*$/, ""); break; case EntryControlDataTypes.MD5: // MD5 16 byte binary data encoded as a MD5 hash value. From 2880c388f9e63a43c369a888b69647905af07c9d Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 12 Nov 2024 20:18:53 +0100 Subject: [PATCH 3/3] fix: unreachable code --- packages/shared/src/Bytes.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shared/src/Bytes.ts b/packages/shared/src/Bytes.ts index 9c3c70990510..e40a9af30fa0 100644 --- a/packages/shared/src/Bytes.ts +++ b/packages/shared/src/Bytes.ts @@ -132,7 +132,6 @@ export class Bytes extends Uint8Array { return uint8ArrayToBase64(this); case "base64url": return uint8ArrayToBase64(this, { urlSafe: true }); - return uint8ArrayToString(this, "utf8"); case "ucs-2": case "ucs2": case "utf16le":