secrez 2.1.16 → 2.2.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +464 -0
- package/README.md +30 -501
- package/bin/secrez.js +20 -1
- package/package.json +14 -17
- package/src/PreCommand.js +28 -27
- package/src/Welcome.js +36 -1
- package/src/commands/Copy.js +12 -0
- package/src/commands/Edit.js +34 -101
- package/src/commands/Export.js +24 -2
- package/src/commands/Find.js +5 -1
- package/src/commands/Import.js +29 -2
- package/src/commands/Quit.js +8 -1
- package/src/commands/Shell.js +29 -2
- package/src/commands/Ssh.js +41 -13
- package/src/commands/Totp.js +37 -30
- package/src/editor/MemoryEditor.js +303 -0
- package/src/editor/onKeypress.js +138 -0
- package/src/prompts/CommandPrompt.js +2 -3
- package/src/prompts/MainPrompt.js +2 -0
- package/src/prompts/MainPromptMock.js +4 -0
- package/src/prompts/SigintManager.js +4 -0
- package/src/commands/Bash.js +0 -23
- package/src/prompts/MultiEditorPrompt.js +0 -41
package/src/PreCommand.js
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
1
|
const { chalk } = require("./utils/Logger");
|
|
2
2
|
const Crypto = require("@secrez/crypto");
|
|
3
|
+
const { editInMemory } = require("./editor/MemoryEditor");
|
|
3
4
|
|
|
4
5
|
class PreCommand {
|
|
5
6
|
async useEditor(options) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
if (this.prompt.editorProvider) {
|
|
8
|
+
return this.prompt.editorProvider(options);
|
|
9
|
+
} /* istanbul ignore next */ else {
|
|
10
|
+
// Do not pause/resume inquirer-command-prompt's cached rl: after the user
|
|
11
|
+
// submits `edit …`, that Interface is already closed. On Node 24+,
|
|
12
|
+
// rl.pause()/resume() on a closed Interface throw ERR_USE_AFTER_CLOSE
|
|
13
|
+
// ("readline was closed"). Even on Node 22, pause/resume on the dead
|
|
14
|
+
// Interface still toggles process.stdin and races the next prompt.
|
|
15
|
+
// MemoryEditor owns stdin directly instead.
|
|
16
|
+
const stdin = process.stdin;
|
|
17
|
+
const wasRaw = typeof stdin.isRaw === "boolean" ? stdin.isRaw : false;
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
return await editInMemory(options.content || "");
|
|
21
|
+
} finally {
|
|
22
|
+
if (stdin.isTTY && typeof stdin.setRawMode === "function") {
|
|
23
|
+
try {
|
|
24
|
+
stdin.setRawMode(wasRaw);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
// ignore — stdin may already be in the desired mode
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Ensure stdin is flowing again for the next inquirer/command prompt
|
|
30
|
+
if (typeof stdin.resume === "function" && stdin.isPaused()) {
|
|
31
|
+
stdin.resume();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
11
34
|
}
|
|
12
|
-
let extraMessage =
|
|
13
|
-
chalk.dim("Press <enter> to launch ") +
|
|
14
|
-
message +
|
|
15
|
-
chalk.reset(
|
|
16
|
-
options.internal
|
|
17
|
-
? chalk.green("\n Ctrl-d to save the changes. Ctrl-c to abort.")
|
|
18
|
-
: ""
|
|
19
|
-
);
|
|
20
|
-
let { result } = await this.prompt.inquirer.prompt([
|
|
21
|
-
{
|
|
22
|
-
type: "multiEditor",
|
|
23
|
-
name: "result",
|
|
24
|
-
message: "Editing...",
|
|
25
|
-
default: options.content,
|
|
26
|
-
tempDir: this.cliConfig.tmpPath,
|
|
27
|
-
validate: function (text) {
|
|
28
|
-
return true;
|
|
29
|
-
},
|
|
30
|
-
extraMessage,
|
|
31
|
-
},
|
|
32
|
-
]);
|
|
33
|
-
return result;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
async useSelect(options) {
|
package/src/Welcome.js
CHANGED
|
@@ -4,6 +4,8 @@ const fs = require("fs-extra");
|
|
|
4
4
|
const Crypto = require("@secrez/crypto");
|
|
5
5
|
const Logger = require("./utils/Logger");
|
|
6
6
|
|
|
7
|
+
const LOW_ITERATIONS_WARNING_THRESHOLD = 100000;
|
|
8
|
+
|
|
7
9
|
class Welcome {
|
|
8
10
|
async start(secrez, options) {
|
|
9
11
|
this.secrez = secrez;
|
|
@@ -65,6 +67,31 @@ Thanks.`);
|
|
|
65
67
|
return parseInt(iterations);
|
|
66
68
|
}
|
|
67
69
|
|
|
70
|
+
async confirmLowIterations(iterations) {
|
|
71
|
+
if (iterations >= LOW_ITERATIONS_WARNING_THRESHOLD) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
Logger.yellow(
|
|
75
|
+
chalk.bold(`
|
|
76
|
+
Warning: you chose fewer than ${LOW_ITERATIONS_WARNING_THRESHOLD.toLocaleString()} iterations.
|
|
77
|
+
`)
|
|
78
|
+
);
|
|
79
|
+
Logger.yellow(
|
|
80
|
+
`Secrez does not salt the KDF with the password alone: the iteration count is mixed into the salt, which makes offline attacks significantly slower than with a typical password-only salt.
|
|
81
|
+
|
|
82
|
+
We recommend 500,000–1,000,000 iterations for most setups. A random per-account salt is planned for a future release.`
|
|
83
|
+
);
|
|
84
|
+
const { proceed } = await inquirer.prompt([
|
|
85
|
+
{
|
|
86
|
+
name: "proceed",
|
|
87
|
+
type: "confirm",
|
|
88
|
+
message: "Continue signup with this iteration count?",
|
|
89
|
+
default: false,
|
|
90
|
+
},
|
|
91
|
+
]);
|
|
92
|
+
return proceed;
|
|
93
|
+
}
|
|
94
|
+
|
|
68
95
|
// chimney piano fabric forest curious black hip axis story stool spoil fold
|
|
69
96
|
async saveIterations() {
|
|
70
97
|
if (this.options.saveIterations) {
|
|
@@ -107,6 +134,11 @@ Thanks.`);
|
|
|
107
134
|
async signup() {
|
|
108
135
|
for (;;) {
|
|
109
136
|
try {
|
|
137
|
+
if (!(await this.confirmLowIterations(this.iterations))) {
|
|
138
|
+
delete this.options.iterations;
|
|
139
|
+
this.iterations = await this.getIterations();
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
110
142
|
let p = await inquirer.prompt([
|
|
111
143
|
{
|
|
112
144
|
name: "password",
|
|
@@ -152,4 +184,7 @@ Thanks.`);
|
|
|
152
184
|
}
|
|
153
185
|
}
|
|
154
186
|
|
|
155
|
-
|
|
187
|
+
const welcome = new Welcome();
|
|
188
|
+
welcome.LOW_ITERATIONS_WARNING_THRESHOLD = LOW_ITERATIONS_WARNING_THRESHOLD;
|
|
189
|
+
|
|
190
|
+
module.exports = welcome;
|
package/src/commands/Copy.js
CHANGED
|
@@ -5,6 +5,18 @@ const { isYaml, yamlParse, TRUE, sleep, playMp3 } = require("@secrez/utils");
|
|
|
5
5
|
const { Node } = require("@secrez/fs");
|
|
6
6
|
const { execSync } = require("child_process");
|
|
7
7
|
|
|
8
|
+
let clipboardQueue = Promise.resolve();
|
|
9
|
+
|
|
10
|
+
function runClipboardTask(task) {
|
|
11
|
+
const next = clipboardQueue.then(task, task);
|
|
12
|
+
clipboardQueue = next.catch(() => {});
|
|
13
|
+
return next;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function drainClipboardQueue() {
|
|
17
|
+
await clipboardQueue;
|
|
18
|
+
}
|
|
19
|
+
|
|
8
20
|
class Copy extends require("../Command") {
|
|
9
21
|
setHelpAndCompletion() {
|
|
10
22
|
this.cliConfig.completion.copy = {
|
package/src/commands/Edit.js
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
// Next line is to avoid that npm-check-unused reports it
|
|
2
|
-
require("tiny-cli-editor");
|
|
3
|
-
//
|
|
4
|
-
|
|
5
1
|
const _ = require("lodash");
|
|
6
|
-
const path = require("path");
|
|
7
|
-
const fs = require("fs-extra");
|
|
8
2
|
const { Entry } = require("@secrez/core");
|
|
9
|
-
const {
|
|
10
|
-
isYaml,
|
|
11
|
-
yamlParse,
|
|
12
|
-
yamlStringify,
|
|
13
|
-
execAsync,
|
|
14
|
-
} = require("@secrez/utils");
|
|
3
|
+
const { isYaml, yamlParse, yamlStringify } = require("@secrez/utils");
|
|
15
4
|
|
|
16
5
|
class Edit extends require("../Command") {
|
|
17
6
|
setHelpAndCompletion() {
|
|
@@ -33,16 +22,6 @@ class Edit extends require("../Command") {
|
|
|
33
22
|
defaultOption: true,
|
|
34
23
|
type: String,
|
|
35
24
|
},
|
|
36
|
-
{
|
|
37
|
-
name: "editor",
|
|
38
|
-
alias: "e",
|
|
39
|
-
type: String,
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
name: "internal",
|
|
43
|
-
alias: "i",
|
|
44
|
-
type: Boolean,
|
|
45
|
-
},
|
|
46
25
|
{
|
|
47
26
|
name: "field",
|
|
48
27
|
alias: "f",
|
|
@@ -60,46 +39,45 @@ class Edit extends require("../Command") {
|
|
|
60
39
|
help() {
|
|
61
40
|
return {
|
|
62
41
|
description: [
|
|
63
|
-
"Edits a file containing a secret.",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
42
|
+
"Edits a file containing a secret using the in-memory editor.",
|
|
43
|
+
"Secrets are never written to a temporary file on disk.",
|
|
44
|
+
"Editor commands:",
|
|
45
|
+
" Ctrl-c to cancel",
|
|
67
46
|
" Ctrl-d to save",
|
|
68
|
-
"
|
|
47
|
+
" Ctrl-k to delete the current line",
|
|
69
48
|
],
|
|
70
49
|
examples: [
|
|
71
|
-
["edit ../coins/ether2-pwd", "
|
|
72
|
-
["edit ../bitcoin/seed -i", "uses the minimalistic internal editor"],
|
|
73
|
-
[
|
|
74
|
-
"edit ../bitcoin/seed -e nano",
|
|
75
|
-
"choses the editor, in this case nano",
|
|
76
|
-
],
|
|
77
|
-
["edit ../bitcoin/seed -e vim", "uses vim"],
|
|
50
|
+
["edit ../coins/ether2-pwd", "edits a secret file"],
|
|
78
51
|
[
|
|
79
52
|
"edit gmail.yml -f password",
|
|
80
53
|
"edits only the field password of the yaml file. If the field does not exist, a new field is added",
|
|
81
54
|
],
|
|
55
|
+
[
|
|
56
|
+
"edit main:/gmail.yml -f password",
|
|
57
|
+
"edits a file using a dataset-qualified path",
|
|
58
|
+
],
|
|
82
59
|
["edit damaged.yaml -u", "edits damaged.yaml without parsing it"],
|
|
83
60
|
],
|
|
84
61
|
};
|
|
85
62
|
}
|
|
86
63
|
|
|
87
64
|
async edit(options) {
|
|
88
|
-
let
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
let sanitizedPath = Entry.sanitizePath(options.path);
|
|
96
|
-
if (sanitizedPath !== options.path) {
|
|
65
|
+
let data = await this.internalFs.getTreeIndexAndPath(options.path);
|
|
66
|
+
let file = data.path;
|
|
67
|
+
let tree = data.tree;
|
|
68
|
+
|
|
69
|
+
let sanitizedPath = Entry.sanitizePath(file);
|
|
70
|
+
if (sanitizedPath !== file) {
|
|
97
71
|
throw new Error("A filename cannot contain \\/><|:&?*^$ chars.");
|
|
98
72
|
}
|
|
73
|
+
|
|
99
74
|
let exists = false;
|
|
100
75
|
let fileData;
|
|
101
76
|
try {
|
|
102
|
-
fileData = await this.prompt.commands.cat.cat(
|
|
77
|
+
fileData = await this.prompt.commands.cat.cat(
|
|
78
|
+
{ path: options.path },
|
|
79
|
+
true
|
|
80
|
+
);
|
|
103
81
|
exists = true;
|
|
104
82
|
} catch (e) {
|
|
105
83
|
if (options.field) {
|
|
@@ -108,8 +86,9 @@ class Edit extends require("../Command") {
|
|
|
108
86
|
fileData = [{ content: "" }];
|
|
109
87
|
}
|
|
110
88
|
|
|
89
|
+
let p = tree.getNormalizedPath(file);
|
|
111
90
|
let fields = {};
|
|
112
|
-
if (exists && !options.unformatted && isYaml(
|
|
91
|
+
if (exists && !options.unformatted && isYaml(p)) {
|
|
113
92
|
fields = fileData[0].content ? yamlParse(fileData[0].content) : {};
|
|
114
93
|
if (typeof fields === "object") {
|
|
115
94
|
options.choices = Object.keys(fields);
|
|
@@ -124,7 +103,7 @@ class Edit extends require("../Command") {
|
|
|
124
103
|
} else {
|
|
125
104
|
delete options.field;
|
|
126
105
|
}
|
|
127
|
-
} else if (!isYaml(
|
|
106
|
+
} else if (!isYaml(p)) {
|
|
128
107
|
delete options.field;
|
|
129
108
|
}
|
|
130
109
|
let content = options.field
|
|
@@ -134,18 +113,18 @@ class Edit extends require("../Command") {
|
|
|
134
113
|
|
|
135
114
|
if (newContent && newContent !== content) {
|
|
136
115
|
if (exists) {
|
|
137
|
-
let node =
|
|
116
|
+
let node = tree.root.getChildFromPath(p);
|
|
138
117
|
let entry = node.getEntry();
|
|
139
118
|
if (options.field) {
|
|
140
119
|
fields[options.field] = _.trim(newContent);
|
|
141
|
-
entry.content
|
|
120
|
+
entry.set({ content: yamlStringify(fields) });
|
|
142
121
|
} else {
|
|
143
|
-
entry.content
|
|
122
|
+
entry.set({ content: newContent });
|
|
144
123
|
}
|
|
145
|
-
await
|
|
124
|
+
await tree.update(node, entry);
|
|
146
125
|
} else {
|
|
147
126
|
await this.prompt.commands.touch.touch({
|
|
148
|
-
path:
|
|
127
|
+
path: options.path,
|
|
149
128
|
content: newContent,
|
|
150
129
|
});
|
|
151
130
|
}
|
|
@@ -155,70 +134,24 @@ class Edit extends require("../Command") {
|
|
|
155
134
|
}
|
|
156
135
|
}
|
|
157
136
|
|
|
158
|
-
getTinyCliEditorBinPath() {
|
|
159
|
-
if (!this.editorBinPath) {
|
|
160
|
-
let bin = path.resolve(
|
|
161
|
-
__dirname,
|
|
162
|
-
"../../node_modules/tiny-cli-editor/bin.js"
|
|
163
|
-
);
|
|
164
|
-
if (!fs.existsSync(bin)) {
|
|
165
|
-
bin = path.resolve(
|
|
166
|
-
__dirname,
|
|
167
|
-
"../../../../node_modules/tiny-cli-editor/bin.js"
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
if (!fs.existsSync(bin)) {
|
|
171
|
-
throw new Error("Default editor not found");
|
|
172
|
-
}
|
|
173
|
-
this.editorBinPath = bin;
|
|
174
|
-
}
|
|
175
|
-
return this.editorBinPath;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
137
|
async exec(options = {}) {
|
|
179
138
|
if (options.help) {
|
|
180
139
|
return this.showHelp();
|
|
181
140
|
}
|
|
182
|
-
let currentEditor;
|
|
183
141
|
try {
|
|
184
142
|
this.validate(options, {
|
|
185
143
|
path: true,
|
|
186
144
|
});
|
|
187
145
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (!shouldProceed) {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
currentEditor = process.env.EDITOR;
|
|
196
|
-
if (options.internal) {
|
|
197
|
-
process.env.EDITOR = this.getTinyCliEditorBinPath();
|
|
198
|
-
} else if (options.editor) {
|
|
199
|
-
process.env.EDITOR = options.editor;
|
|
200
|
-
}
|
|
201
|
-
if (!process.env.EDITOR) {
|
|
202
|
-
let result = await execAsync("which", __dirname, ["nano"]);
|
|
203
|
-
if (!result.message || result.code === 1) {
|
|
204
|
-
result = await execAsync("which", __dirname, ["vim"]);
|
|
205
|
-
if (!result.message || result.code === 1) {
|
|
206
|
-
throw new Error(
|
|
207
|
-
'No text editor found. Set up the EDITOR env variable or use the -e option. Type "edit -h" for more options.'
|
|
208
|
-
);
|
|
209
|
-
} else {
|
|
210
|
-
process.env.EDITOR = "vim";
|
|
211
|
-
}
|
|
212
|
-
} else {
|
|
213
|
-
process.env.EDITOR = "nano";
|
|
214
|
-
}
|
|
146
|
+
const shouldProceed = await this.checkGitConflictsBeforeOperation();
|
|
147
|
+
if (!shouldProceed) {
|
|
148
|
+
return;
|
|
215
149
|
}
|
|
150
|
+
|
|
216
151
|
await this.edit(options);
|
|
217
152
|
} catch (e) {
|
|
218
153
|
this.Logger.red(e.message);
|
|
219
154
|
}
|
|
220
|
-
// eslint-disable-next-line require-atomic-updates
|
|
221
|
-
process.env.EDITOR = currentEditor;
|
|
222
155
|
await this.prompt.run();
|
|
223
156
|
}
|
|
224
157
|
}
|
package/src/commands/Export.js
CHANGED
|
@@ -57,6 +57,11 @@ class Export extends require("../Command") {
|
|
|
57
57
|
name: "password",
|
|
58
58
|
type: String,
|
|
59
59
|
},
|
|
60
|
+
{
|
|
61
|
+
name: "iterations",
|
|
62
|
+
alias: "i",
|
|
63
|
+
type: Number,
|
|
64
|
+
},
|
|
60
65
|
{
|
|
61
66
|
name: "include-me",
|
|
62
67
|
type: Boolean,
|
|
@@ -96,8 +101,8 @@ class Export extends require("../Command") {
|
|
|
96
101
|
'asks for a password and encrypts seed.json before exporting it. The final file will have the extension ".secrez"',
|
|
97
102
|
],
|
|
98
103
|
[
|
|
99
|
-
'export seed.json -e --password "some strong password"',
|
|
100
|
-
"uses the typed password to encrypt seed.json before exporting it",
|
|
104
|
+
'export seed.json -e --password "some strong password" -i 500000',
|
|
105
|
+
"uses the typed password and iterations to encrypt seed.json before exporting it",
|
|
101
106
|
],
|
|
102
107
|
[
|
|
103
108
|
"export seed.json -ec bob alice",
|
|
@@ -260,6 +265,23 @@ class Export extends require("../Command") {
|
|
|
260
265
|
throw new Error("The two password do not match");
|
|
261
266
|
}
|
|
262
267
|
options.password = pwd;
|
|
268
|
+
if (!options.iterations) {
|
|
269
|
+
let iterationsInput = await this.useInput({
|
|
270
|
+
message:
|
|
271
|
+
"Type the number of iterations for encrypting this export",
|
|
272
|
+
validate: (val) => {
|
|
273
|
+
const n = parseInt(val, 10);
|
|
274
|
+
return (
|
|
275
|
+
(n > 0 && String(n) === String(val).trim()) ||
|
|
276
|
+
"Must be a positive integer"
|
|
277
|
+
);
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
if (!iterationsInput) {
|
|
281
|
+
throw new Error("Operation canceled");
|
|
282
|
+
}
|
|
283
|
+
options.iterations = parseInt(iterationsInput, 10);
|
|
284
|
+
}
|
|
263
285
|
}
|
|
264
286
|
content = fileCipher.encryptFile(content, options).join(",");
|
|
265
287
|
}
|
package/src/commands/Find.js
CHANGED
|
@@ -50,7 +50,7 @@ class Find extends require("../Command") {
|
|
|
50
50
|
name: "global",
|
|
51
51
|
alias: "g",
|
|
52
52
|
type: Boolean,
|
|
53
|
-
hint: "Search in all
|
|
53
|
+
hint: "Search in all datasets from root",
|
|
54
54
|
},
|
|
55
55
|
{
|
|
56
56
|
name: "trash-too",
|
|
@@ -104,6 +104,10 @@ class Find extends require("../Command") {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
async find(options) {
|
|
107
|
+
if (options.global) {
|
|
108
|
+
options.root = true;
|
|
109
|
+
}
|
|
110
|
+
|
|
107
111
|
// Handle recent mode
|
|
108
112
|
if (options.recent) {
|
|
109
113
|
// Set default limit if not specified by user
|
package/src/commands/Import.js
CHANGED
|
@@ -76,6 +76,11 @@ class Import extends require("../Command") {
|
|
|
76
76
|
name: "password",
|
|
77
77
|
type: String,
|
|
78
78
|
},
|
|
79
|
+
{
|
|
80
|
+
name: "iterations",
|
|
81
|
+
alias: "i",
|
|
82
|
+
type: Number,
|
|
83
|
+
},
|
|
79
84
|
{
|
|
80
85
|
name: "public-key",
|
|
81
86
|
type: String,
|
|
@@ -101,8 +106,12 @@ class Import extends require("../Command") {
|
|
|
101
106
|
"copies seed.json from the disk into the current directory",
|
|
102
107
|
],
|
|
103
108
|
[
|
|
104
|
-
"import seed.json.secrez --password s8eeuhwy36534",
|
|
105
|
-
"imports seed.json and decrypts it using the specified password",
|
|
109
|
+
"import seed.json.secrez --password s8eeuhwy36534 -i 500000",
|
|
110
|
+
"imports seed.json and decrypts it using the specified password and iterations",
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
"import old-backup.secrez --password s8eeuhwy36534",
|
|
114
|
+
"imports a file encrypted with the legacy v1 format (password only; no iterations)",
|
|
106
115
|
],
|
|
107
116
|
[
|
|
108
117
|
"import seed.json.secrez",
|
|
@@ -247,6 +256,24 @@ class Import extends require("../Command") {
|
|
|
247
256
|
if (!options.contactPublicKey) {
|
|
248
257
|
options.contactsPublicKeys = contactsPublicKeys;
|
|
249
258
|
}
|
|
259
|
+
const version = String(c[2]).split(",")[0];
|
|
260
|
+
if (version === "2" && options.password && !options.iterations) {
|
|
261
|
+
let iterationsInput = await this.useInput({
|
|
262
|
+
message:
|
|
263
|
+
"Type the number of iterations used when the file was exported",
|
|
264
|
+
validate: (val) => {
|
|
265
|
+
const n = parseInt(val, 10);
|
|
266
|
+
return (
|
|
267
|
+
(n > 0 && String(n) === String(val).trim()) ||
|
|
268
|
+
"Must be a positive integer"
|
|
269
|
+
);
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
if (!iterationsInput) {
|
|
273
|
+
throw new Error("Operation canceled");
|
|
274
|
+
}
|
|
275
|
+
options.iterations = parseInt(iterationsInput, 10);
|
|
276
|
+
}
|
|
250
277
|
c[2] = fileCipher.decryptFile(c[2], options);
|
|
251
278
|
} catch (e) {
|
|
252
279
|
this.skipped.push([path.basename(c[0]), e.message]);
|
package/src/commands/Quit.js
CHANGED
|
@@ -26,10 +26,14 @@ class Quit extends require("../Command") {
|
|
|
26
26
|
if (options.help) {
|
|
27
27
|
return this.showHelp();
|
|
28
28
|
}
|
|
29
|
+
const wasLoggedIn = Boolean(this.secrez.masterKeyHash);
|
|
29
30
|
/* istanbul ignore if */
|
|
30
31
|
// eslint-disable-next-line no-constant-condition
|
|
31
32
|
if (process.env.NODE_ENV !== "test") {
|
|
32
|
-
|
|
33
|
+
if (wasLoggedIn) {
|
|
34
|
+
await this.prompt.saveHistory();
|
|
35
|
+
this.secrez.signout();
|
|
36
|
+
}
|
|
33
37
|
this.Logger.bold(
|
|
34
38
|
"Clear or close the terminal. If not, your history will be visible scrolling up."
|
|
35
39
|
);
|
|
@@ -37,6 +41,9 @@ class Quit extends require("../Command") {
|
|
|
37
41
|
/*eslint-disable-next-line*/
|
|
38
42
|
process.exit(0);
|
|
39
43
|
} else {
|
|
44
|
+
if (wasLoggedIn) {
|
|
45
|
+
this.secrez.signout();
|
|
46
|
+
}
|
|
40
47
|
this.Logger.reset("Bye bye :o)");
|
|
41
48
|
}
|
|
42
49
|
}
|
package/src/commands/Shell.js
CHANGED
|
@@ -1,4 +1,31 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { spawn } = require("child_process");
|
|
2
|
+
|
|
3
|
+
function execShell(command, cwd) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const child = spawn(command, {
|
|
6
|
+
shell: true,
|
|
7
|
+
cwd,
|
|
8
|
+
});
|
|
9
|
+
let stdout = "";
|
|
10
|
+
let stderr = "";
|
|
11
|
+
child.stdout.on("data", (data) => {
|
|
12
|
+
stdout += data.toString("utf8");
|
|
13
|
+
});
|
|
14
|
+
child.stderr.on("data", (data) => {
|
|
15
|
+
stderr += data.toString("utf8");
|
|
16
|
+
});
|
|
17
|
+
child.on("close", (code) => {
|
|
18
|
+
if (code === 0) {
|
|
19
|
+
resolve(stdout);
|
|
20
|
+
} else {
|
|
21
|
+
reject(
|
|
22
|
+
new Error(stderr.trim() || stdout.trim() || `Exit code ${code}`)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
child.on("error", reject);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
2
29
|
|
|
3
30
|
class Shell extends require("../Command") {
|
|
4
31
|
setHelpAndCompletion() {
|
|
@@ -41,7 +68,7 @@ class Shell extends require("../Command") {
|
|
|
41
68
|
})
|
|
42
69
|
);
|
|
43
70
|
}
|
|
44
|
-
return
|
|
71
|
+
return execShell(options.command, pwd);
|
|
45
72
|
}
|
|
46
73
|
|
|
47
74
|
async exec(options = {}) {
|
package/src/commands/Ssh.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
2
|
const fs = require("fs-extra");
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const utils = require("@secrez/utils");
|
|
4
|
+
|
|
5
|
+
const SSH_USER_RE = /^[a-zA-Z0-9._-]+$/;
|
|
6
|
+
const SSH_HOST_RE = /^[a-zA-Z0-9.-]+$/;
|
|
5
7
|
|
|
6
8
|
class Ssh extends require("../Command") {
|
|
7
9
|
setHelpAndCompletion() {
|
|
@@ -68,16 +70,32 @@ class Ssh extends require("../Command") {
|
|
|
68
70
|
};
|
|
69
71
|
}
|
|
70
72
|
|
|
73
|
+
validateSshUser(user) {
|
|
74
|
+
if (!SSH_USER_RE.test(user)) {
|
|
75
|
+
throw new Error(`Invalid SSH user "${user}"`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
validateSshHost(host) {
|
|
80
|
+
if (!SSH_HOST_RE.test(host)) {
|
|
81
|
+
throw new Error(`Invalid remote host "${host}"`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
71
85
|
async ssh(options = {}) {
|
|
72
|
-
|
|
86
|
+
if (!options.remoteHost) {
|
|
87
|
+
throw new Error("A remote host is required");
|
|
88
|
+
}
|
|
89
|
+
const user = options.user || "root";
|
|
90
|
+
this.validateSshUser(user);
|
|
91
|
+
this.validateSshHost(options.remoteHost);
|
|
92
|
+
|
|
93
|
+
let result = await utils.execAsync("which", __dirname, ["ttab"]);
|
|
73
94
|
if (!result.message || result.code === 1) {
|
|
74
95
|
throw new Error(
|
|
75
96
|
'On MacOS ttab is required. Run "npm i -g ttab" in another terminal to install it'
|
|
76
97
|
);
|
|
77
98
|
}
|
|
78
|
-
if (!options.remoteHost) {
|
|
79
|
-
throw new Error("A remote host is required");
|
|
80
|
-
}
|
|
81
99
|
if (!options.identity) {
|
|
82
100
|
throw new Error("A path to the identity key is required");
|
|
83
101
|
}
|
|
@@ -90,17 +108,27 @@ class Ssh extends require("../Command") {
|
|
|
90
108
|
let sshPath = path.join(this.secrez.config.tmpPath, ".ssh");
|
|
91
109
|
if (!(await fs.pathExists(sshPath))) {
|
|
92
110
|
await fs.ensureDir(sshPath);
|
|
93
|
-
await execAsync("chmod", this.secrez.config.tmpPath, [
|
|
111
|
+
await utils.execAsync("chmod", this.secrez.config.tmpPath, [
|
|
112
|
+
"700",
|
|
113
|
+
".ssh",
|
|
114
|
+
]);
|
|
94
115
|
}
|
|
95
116
|
let keyName = `id_${Math.random().toString().substring(2)}`;
|
|
96
117
|
let keyPath = path.join(sshPath, keyName);
|
|
97
118
|
await fs.writeFile(keyPath, key);
|
|
98
|
-
await execAsync("chmod", sshPath, ["600", keyName]);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
119
|
+
await utils.execAsync("chmod", sshPath, ["600", keyName]);
|
|
120
|
+
|
|
121
|
+
const args = ["ssh"];
|
|
122
|
+
if (options.ignoreHostKeyCheck) {
|
|
123
|
+
args.push("-oStrictHostKeyChecking=no");
|
|
124
|
+
}
|
|
125
|
+
args.push("-i", keyPath, `${user}@${options.remoteHost}`);
|
|
126
|
+
|
|
127
|
+
result = await utils.execAsync("ttab", __dirname, args);
|
|
128
|
+
if (result.code !== 0) {
|
|
129
|
+
throw new Error(result.error || "Failed to open SSH tab");
|
|
130
|
+
}
|
|
131
|
+
|
|
104
132
|
setTimeout(() => {
|
|
105
133
|
fs.unlink(keyPath);
|
|
106
134
|
}, 10000);
|