fix: Fixes character reading in readFromKeyboard not displaying correctly.

This commit is contained in:
greysoh 2024-05-09 19:38:30 -04:00
parent 47b707e8cd
commit 422ec568e2
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37

View file

@ -18,15 +18,19 @@ export async function readFromKeyboard(
let lineIndex = 0; let lineIndex = 0;
async function eventLoop(): Promise<any> { async function eventLoop(): Promise<any> {
const readStreamData = stream.read(); const readStreamDataBuf = stream.read();
if (readStreamData == null) return setTimeout(eventLoop, pullRate); if (readStreamDataBuf == null) return setTimeout(eventLoop, pullRate);
if (readStreamData.includes("\x03")) { const readStreamData = readStreamDataBuf.toString();
// Fixes several bugs (incl. potential social eng. exploits, ssh-copy-id being broken, etc)
for (const character of readStreamData.split("")) {
if (character == "\x03") {
stream.write("^C"); stream.write("^C");
return promise(""); return promise("");
} else if (readStreamData.includes("\r") || readStreamData.includes("\n")) { } else if (character == "\r" || character == "\n") {
return promise(line.replace("\r", "")); return promise(line.replace("\r", ""));
} else if (readStreamData.includes(clientBackspace)) { } else if (character == clientBackspace) {
if (line.length == 0) return setTimeout(eventLoop, pullRate); // Here because if we do it in the parent if statement, shit breaks if (line.length == 0) return setTimeout(eventLoop, pullRate); // Here because if we do it in the parent if statement, shit breaks
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex); line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
@ -52,25 +56,25 @@ export async function readFromKeyboard(
lineIndex -= 1; lineIndex -= 1;
} }
} else if (readStreamData.includes("\x1B")) { } else if (character == "\x1B") {
if (readStreamData.includes(rightEscape)) { if (character == rightEscape) {
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, pullRate); if (lineIndex + 1 > line.length) return setTimeout(eventLoop, pullRate);
lineIndex += 1; lineIndex += 1;
} else if (readStreamData.includes(leftEscape)) { } else if (character == leftEscape) {
if (lineIndex - 1 < 0) return setTimeout(eventLoop, pullRate); if (lineIndex - 1 < 0) return setTimeout(eventLoop, pullRate);
lineIndex -= 1; lineIndex -= 1;
} else { } else {
return setTimeout(eventLoop, pullRate); return setTimeout(eventLoop, pullRate);
} }
if (!disableEcho) stream.write(readStreamData); if (!disableEcho) stream.write(character);
} else { } else {
lineIndex += readStreamData.length; lineIndex += 1;
// There isn't a splice method for String prototypes. So, ugh: // There isn't a splice method for String prototypes. So, ugh:
line = line =
line.substring(0, lineIndex - 1) + line.substring(0, lineIndex - 1) +
readStreamData + character +
line.substring(lineIndex - 1); line.substring(lineIndex - 1);
if (!disableEcho) { if (!disableEcho) {
@ -89,6 +93,7 @@ export async function readFromKeyboard(
); );
} }
} }
}
setTimeout(eventLoop, pullRate); setTimeout(eventLoop, pullRate);
}; };