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

readline: improve robustness against prototype mutation #45614

Merged
merged 4 commits into from Dec 10, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/internal/readline/interface.js
Expand Up @@ -22,18 +22,17 @@ const {
NumberIsNaN,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
RegExpPrototypeSymbolSplit,
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
StringPrototypeRepeat,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeTrim,
Symbol,
SymbolAsyncIterator,
SafeStringIterator,
hardenRegExp,
} = primordials;

const { codes } = require('internal/errors');
Expand Down Expand Up @@ -75,7 +74,7 @@ const kHistorySize = 30;
const kMaxUndoRedoStackSize = 2048;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
const lineEnding = /\r?\n|\r(?!\n)/;
const lineEnding = /\r?\n|\r(?!\n)/g;

const kLineObjectStream = Symbol('line object stream');
const kQuestionCancel = Symbol('kQuestionCancel');
Expand Down Expand Up @@ -589,28 +588,33 @@ class Interface extends InterfaceConstructor {
this[kSawReturnAt] &&
DateNow() - this[kSawReturnAt] <= this.crlfDelay
) {
string = RegExpPrototypeSymbolReplace(/^\n/, string, '');
if (StringPrototypeCodePointAt(string) === 10) string = StringPrototypeSlice(string, 1);
this[kSawReturnAt] = 0;
}

// Run test() on the new string chunk, not on the entire line buffer.
const newPartContainsEnding = RegExpPrototypeExec(lineEnding, string) !== null;
const newPartContainsEnding = RegExpPrototypeExec(lineEnding, string);

if (this[kLine_buffer]) {
string = this[kLine_buffer] + string;
this[kLine_buffer] = null;
}
if (newPartContainsEnding) {
if (newPartContainsEnding !== null) {
this[kSawReturnAt] = StringPrototypeEndsWith(string, '\r') ?
DateNow() :
0;

// Got one or more newlines; process into "line" events
const lines = StringPrototypeSplit(string, lineEnding);
const indexes = [0, newPartContainsEnding.index, newPartContainsEnding.index + newPartContainsEnding[0].length];
let nextMatch;
while ((nextMatch = RegExpPrototypeExec(lineEnding, string)) !== null) {
ArrayPrototypePush(indexes, nextMatch.index, nextMatch.index + nextMatch[0].length);
}
const lastIndex = indexes.length - 1;
// Either '' or (conceivably) the unfinished portion of the next line
string = ArrayPrototypePop(lines);
this[kLine_buffer] = string;
for (let n = 0; n < lines.length; n++) this[kOnLine](lines[n]);
this[kLine_buffer] = StringPrototypeSlice(string, indexes[lastIndex]);
for (let i = 1; i < lastIndex; i += 2) {
this[kOnLine](StringPrototypeSlice(string, indexes[i - 1], indexes[i]));
}
} else if (string) {
// No newlines this time, save what we have for next time
this[kLine_buffer] = string;
Expand Down Expand Up @@ -1321,7 +1325,7 @@ class Interface extends InterfaceConstructor {
// falls through
default:
if (typeof s === 'string' && s) {
const lines = RegExpPrototypeSymbolSplit(/\r\n|\n|\r/, s);
const lines = RegExpPrototypeSymbolSplit(hardenRegExp(/\r\n|\n|\r/), s);
for (let i = 0, len = lines.length; i < len; i++) {
if (i > 0) {
this[kLine]();
Expand Down