Skip to content

Commit 80612f3

Browse files
committed
repl: create history file with mode 0600
Test code mostly written by Trott nodejs#3392 (comment).
1 parent 6815a3b commit 80612f3

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/internal/repl.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
9393
var writing = false;
9494
var pending = false;
9595
repl.pause();
96-
fs.open(historyPath, 'a+', oninit);
96+
// History files are conventionally not readable by others:
97+
// https://github.com/nodejs/node/issues/3392
98+
// https://github.com/nodejs/node/pull/3394
99+
fs.open(historyPath, 'a+', 0o0600, oninit);
97100

98101
function oninit(err, hnd) {
99102
if (err) {
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
// Flags: --expose_internals
3+
4+
const common = require('../common');
5+
6+
if (common.isWindows) {
7+
console.log('1..0 # Skipped: Win32 uses ACLs for file permissions, ' +
8+
'modes are always 0666 and says nothing about group/other ' +
9+
'read access.');
10+
return;
11+
}
12+
13+
const assert = require('assert');
14+
const path = require('path');
15+
const fs = require('fs');
16+
const repl = require('internal/repl');
17+
const Duplex = require('stream').Duplex;
18+
// Invoking the REPL should create a repl history file at the specified path
19+
// and mode 600.
20+
21+
var stream = new Duplex();
22+
stream.pause = stream.resume = function() {};
23+
// ends immediately
24+
stream._read = function() {
25+
this.push(null);
26+
};
27+
stream._write = function(c, e, cb) {
28+
cb();
29+
};
30+
stream.readable = stream.writable = true;
31+
32+
common.refreshTmpDir();
33+
const replHistoryPath = path.join(common.tmpDir, '.node_repl_history');
34+
35+
const checkResults = common.mustCall(function(err, r) {
36+
if (err)
37+
throw err;
38+
r.input.end();
39+
const stat = fs.statSync(replHistoryPath);
40+
assert.strictEqual(
41+
stat.mode & 0o777, 0o600,
42+
'REPL history file should be mode 0600');
43+
});
44+
45+
repl.createInternalRepl(
46+
{NODE_REPL_HISTORY: replHistoryPath},
47+
{
48+
terminal: true,
49+
input: stream,
50+
output: stream
51+
},
52+
checkResults
53+
);

0 commit comments

Comments
 (0)