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

repl: make sure historyPath is trimmed #4539

Merged
merged 1 commit into from
Jan 16, 2016
Merged

Conversation

evanlucas
Copy link
Contributor

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.

Related: #4522

R= @Fishrock123?

@evanlucas evanlucas added the repl Issues and PRs related to the REPL subsystem. label Jan 5, 2016
}

if (historyPath !== '') {
return setupHistory(repl, env.NODE_REPL_HISTORY,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are anyway going to use the actual value as it is, then why not just

if (opts.terminal &&
    typeof env.NODE_REPL_HISTORY === 'string' &&
    env.NODE_REPL_HISTORY.trim() !== '') {
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because then we would have to trim the string more than once before it is passed to setupHistory

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanlucas How so? We are anyway passing env.NODE_REPL_HISTORY as it is to setupHistory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow. Not what I meant to do. Will fix shortly. I was meaning to pass the trimmed historyPath to setupHistory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@thefourtheye
Copy link
Contributor

Hmmm, I still feel that the type checking would be better here. Because, users can tamper env programmatically and we will fail at runtime if trim is not defined on that object.

@evanlucas
Copy link
Contributor Author

process.env gets coerced to a string when set though.

> process.env.test = 1
1
> process.env.test
'1'
> process.env.test = { 'name': 'test' }
{ name: 'test' }
> process.env.test
'[object Object]'
> process.env.PATH
'/Users/evan/dev/code/depot_tools:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin'

@thefourtheye
Copy link
Contributor

TIL :-) Thanks :P

In that case, we can simply do

const historyPath = env.NODE_REPL_HISTORY.trim();
if (historyPath) {
  return setupHistory(...);
}

Right?

@evanlucas
Copy link
Contributor Author

We can't do that because the key may not exist at all:

> process.env.biscuits.trim()
TypeError: Cannot read property 'trim' of undefined
    at repl:1:21
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)
> 

@thefourtheye
Copy link
Contributor

Ah, right. Thanks :) Change LGTM. Let's see what @Fishrock123 thinks. Apart from this, should we warn the user that the file name is an empty string?

if (opts.terminal) {
// Make sure we trim the history path. We don't want to create
// a file name " ".
let historyPath = env.NODE_REPL_HISTORY;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this logic, including the check for empty string might make more sense inside of setupHistory().

@cjihrig
Copy link
Contributor

cjihrig commented Jan 5, 2016

This probably warrants a tiny documentation update to mention that whitespace is stripped. Right now it only says:

Setting this value to "" will disable persistent REPL history.

After this change, that would only be partially correct.

@jasnell
Copy link
Member

jasnell commented Jan 5, 2016

LGTM. What do y'all think about landing this in v4?

@evanlucas
Copy link
Contributor Author

@cjihrig nits addressed. PTAL.

@jasnell I think it should definitely be backported to LTS. Especially considering the problems it could cause on windows as stated in #4522 (comment)

return ready(null, repl);
}

if (historyPath) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is not necessary. If the control reaches this point, then historyPath is not an empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be undefined though

@cjihrig
Copy link
Contributor

cjihrig commented Jan 6, 2016

One comment, then LGTM.

@evanlucas
Copy link
Contributor Author

Ok, updated. PTAL @cjihrig

@cjihrig
Copy link
Contributor

cjihrig commented Jan 7, 2016

@Fishrock123
Copy link
Contributor

Looking, sorry for the delay.

repl._historyPrev = _replHistoryMessage;
cb(null, repl);
}

function setupHistory(repl, historyPath, oldHistoryPath, ready) {
// Empty string disables persistent history.

if (typeof historyPath === 'string')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait what else would it be? We don't export setupHistory().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if process.env.NODE_REPL_HISTORY is undefined, then it will be undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, carry on.

@jasnell
Copy link
Member

jasnell commented Jan 7, 2016

LGTM once @Fishrock123 is happy :-)

@Fishrock123
Copy link
Contributor

Seems fine to me.

@evanlucas
Copy link
Contributor Author

Rebased after the eslint changes. @Fishrock123 @jasnell @cjihrig @thefourtheye LGTY?

@cjihrig
Copy link
Contributor

cjihrig commented Jan 14, 2016

Yep, still LGTM.

@Fishrock123
Copy link
Contributor

LGTM

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: nodejs#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]>
@evanlucas evanlucas closed this Jan 16, 2016
@evanlucas evanlucas deleted the replspace branch January 16, 2016 19:25
@evanlucas evanlucas merged commit da550aa into nodejs:master Jan 16, 2016
@evanlucas
Copy link
Contributor Author

Landed in da550aa. Thanks!

evanlucas added a commit that referenced this pull request Jan 18, 2016
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]>
MylesBorins pushed a commit that referenced this pull request Jan 28, 2016
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]>
MylesBorins pushed a commit that referenced this pull request Feb 11, 2016
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]>
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Feb 11, 2016
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: nodejs#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]>
@MylesBorins MylesBorins mentioned this pull request Feb 11, 2016
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Feb 15, 2016
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: nodejs#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]>
scovetta pushed a commit to scovetta/node that referenced this pull request Apr 2, 2016
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: nodejs#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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants