Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: treat "ascii" buffer encoding as "utf-8" #7395

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading