Skip to content

Commit 26f0240

Browse files
evanlucasMyles Borins
authored and
Myles Borins
committed
repl: make sure historyPath is trimmed
If one were to set NODE_REPL_HISTORY to a string that contains only a space (" "), then the history file would be created with that name which can cause problems are certain systems. PR-URL: #4539 Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 3d5bc69 commit 26f0240

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/api/repl.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ via the following environment variables:
3737
- `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history
3838
will be saved to the specified file rather than `.node_repl_history` in the
3939
user's home directory. Setting this value to `""` will disable persistent
40-
REPL history.
40+
REPL history. Whitespace will be trimmed from the value.
4141
- `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. Controls how many lines of
4242
history will be persisted if history is available. Must be a positive number.
4343
- `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults

lib/internal/repl.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,26 @@ function createRepl(env, opts, cb) {
5555
}
5656

5757
const repl = REPL.start(opts);
58-
if (opts.terminal && env.NODE_REPL_HISTORY !== '') {
58+
if (opts.terminal) {
5959
return setupHistory(repl, env.NODE_REPL_HISTORY,
6060
env.NODE_REPL_HISTORY_FILE, cb);
6161
}
62+
6263
repl._historyPrev = _replHistoryMessage;
6364
cb(null, repl);
6465
}
6566

6667
function setupHistory(repl, historyPath, oldHistoryPath, ready) {
68+
// Empty string disables persistent history.
69+
70+
if (typeof historyPath === 'string')
71+
historyPath = historyPath.trim();
72+
73+
if (historyPath === '') {
74+
repl._historyPrev = _replHistoryMessage;
75+
return ready(null, repl);
76+
}
77+
6778
if (!historyPath) {
6879
try {
6980
historyPath = path.join(os.homedir(), '.node_repl_history');

test/parallel/test-repl-persistent-history.js

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ const tests = [
8585
test: [UP],
8686
expected: [prompt, replDisabled, prompt]
8787
},
88+
{
89+
env: { NODE_REPL_HISTORY: ' ' },
90+
test: [UP],
91+
expected: [prompt, replDisabled, prompt]
92+
},
8893
{
8994
env: { NODE_REPL_HISTORY: '',
9095
NODE_REPL_HISTORY_FILE: enoentHistoryPath },

0 commit comments

Comments
 (0)