Skip to content

Commit

Permalink
fix: treat "ascii" buffer encoding as "utf-8" (#7395)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Nov 12, 2024
1 parent 81d566c commit be5e7ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 13 additions & 6 deletions packages/cc/src/cc/EntryControlCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion packages/shared/src/Bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,19 @@ export class Bytes extends Uint8Array {
return uint8ArrayToBase64(this);
case "base64url":
return uint8ArrayToBase64(this, { urlSafe: true });
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");
}
}

Expand Down

0 comments on commit be5e7ed

Please sign in to comment.