@secrez/fs 1.0.8 → 1.1.1

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/README.md CHANGED
@@ -20,6 +20,13 @@ API documentation
20
20
 
21
21
  ## History
22
22
 
23
+ **1.1.0**
24
+
25
+ - strengthen password-based export encryption in `FileCipher`: new **v2** format (PBKDF2, random salt in file, iterations out of band)
26
+ - write vault blobs and cache files with mode `0o600` via `VaultPermissions` (FS-1)
27
+ - add `fileCipherLegacy` module and `FileCipherLegacy` export for **v1** password-encrypted files (import compatibility and test fixtures)
28
+ - require Node.js 22.23.4 or later (`engines.node >=22.23.4`)
29
+
23
30
  **1.0.3**
24
31
 
25
32
  - Move `encryptFile` and `decryptFile` from ExternalFs to FileCipher
@@ -31,21 +38,22 @@ API documentation
31
38
  ## Test coverage
32
39
 
33
40
  ```
34
- 107 passing (44s)
41
+ 108 passing (2s)
35
42
 
36
43
  -----------------------|---------|----------|---------|---------|-----------------------------------
37
44
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
38
45
  -----------------------|---------|----------|---------|---------|-----------------------------------
39
- All files | 81.78 | 67.77 | 86.27 | 81.44 |
40
- DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
46
+ All files | 81.97 | 67.87 | 86.87 | 81.65 |
47
+ DataCache.js | 89.53 | 74.07 | 90.9 | 89.41 | 18-24,123-124,150
41
48
  ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
42
- FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
49
+ FileCipher.js | 88.05 | 73.8 | 100 | 88.05 | 9,39,76,85,91,95,131,136
43
50
  FsUtils.js | 100 | 100 | 100 | 100 |
44
51
  GitConflictChecker.js | 72.81 | 61.9 | 90 | 72.81 | ...95,204,222,254,259,277,289,308
45
52
  InternalFs.js | 85.09 | 71.87 | 85.18 | 84.98 | ...33,344,364,423-428,460,470-487
46
53
  Messages.js | 100 | 100 | 100 | 100 |
47
54
  Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
48
- Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...39,466,476,530,622,630,661-672
55
+ Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...44,471,481,535,627,635,666-677
56
+ fileCipherLegacy.js | 100 | 100 | 100 | 100 |
49
57
  -----------------------|---------|----------|---------|---------|-----------------------------------
50
58
  ```
51
59
 
package/coverage.report CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- > @secrez/fs@1.0.8 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
2
+ > @secrez/fs@1.0.7 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
3
3
  > cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit
4
4
 
5
5
 
@@ -67,10 +67,10 @@
67
67
  ✓ should not detect conflict risk in non-git repository
68
68
  with real GitHub repository
69
69
  ✓ should detect git repository
70
- ✓ should get git status when up to date with remote (3228ms)
71
- ✓ should detect conflict risk when behind remote (5175ms)
70
+ ✓ should get git status when up to date with remote (3471ms)
71
+ ✓ should detect conflict risk when behind remote (5794ms)
72
72
  ✓ should get warning message for conflict risk
73
- ✓ should not show alert again after user proceeds (3137ms)
73
+ ✓ should not show alert again after user proceeds (3582ms)
74
74
  ✓ should reset cache
75
75
 
76
76
  #InternalFs
@@ -157,29 +157,29 @@
157
157
  getEntryDetails
158
158
  ✓ should return the entry details of a node
159
159
  #Fix
160
- ✓ should load a tree with tags
160
+ ✓ should load a tree with tags (52ms)
161
161
  1) should simulate a conflict in the repo and recover lost entries when a valid tree exists
162
- ✓ should simulate a lost index in the repo and recover the entries when no valid tree is found (45ms)
162
+ ✓ should simulate a lost index in the repo and recover the entries when no valid tree is found (73ms)
163
163
  Multi data sets
164
164
  ✓ should load the trees of two data sets
165
165
 
166
166
 
167
- 106 passing (39s)
167
+ 106 passing (44s)
168
168
  1 failing
169
169
 
170
170
  1) #Tree
171
171
  #Fix
172
172
  should simulate a conflict in the repo and recover lost entries when a valid tree exists:
173
173
 
174
- AssertionError: expected [ '/B/D/g', '/E/L/N', '/E/c', …(1) ] to deeply equal [ '/B/D/g', '/B/b', '/E/L/N', '/E/c' ]
174
+ AssertionError: expected [ '/B/D/g', '/B/b', '/E/L/N', …(1) ] to deeply equal [ '/B/D/g', '/B/b', '/E/L/N', '/E/c' ]
175
175
  + expected - actual
176
176
 
177
177
  [
178
178
  "/B/D/g"
179
- + "/B/b"
179
+ "/B/b"
180
180
  "/E/L/N"
181
- "/E/c"
182
- - "/REC_20251010150924/d"
181
+ - "/REC_20251009101127/d"
182
+ + "/E/c"
183
183
  ]
184
184
 
185
185
  at Context.<anonymous> (test/Tree.test.js:306:14)
@@ -189,15 +189,15 @@
189
189
  -----------------------|---------|----------|---------|---------|-----------------------------------
190
190
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
191
191
  -----------------------|---------|----------|---------|---------|-----------------------------------
192
- All files | 80.57 | 67.11 | 86.27 | 80.21 |
192
+ All files | 81.85 | 67.77 | 86.27 | 81.52 |
193
193
  DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
194
194
  ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
195
195
  FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
196
196
  FsUtils.js | 100 | 100 | 100 | 100 |
197
197
  GitConflictChecker.js | 72.81 | 61.9 | 90 | 72.81 | ...95,204,222,254,259,277,289,308
198
- InternalFs.js | 82.35 | 70.83 | 85.18 | 82.21 | ...64,377-385,423-428,460,470-487
198
+ InternalFs.js | 85.09 | 71.87 | 85.18 | 84.98 | ...33,344,364,423-428,460,470-487
199
199
  Messages.js | 100 | 100 | 100 | 100 |
200
200
  Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
201
- Tree.js | 87.46 | 71.68 | 86.48 | 87.11 | ...39,466,476,530,622,630,661-672
201
+ Tree.js | 90.19 | 74.09 | 86.48 | 89.91 | ...39,466,476,530,622,630,661-672
202
202
  -----------------------|---------|----------|---------|---------|-----------------------------------
203
203
   ELIFECYCLE  Test failed. See above for more details.
package/package.json CHANGED
@@ -1,24 +1,23 @@
1
1
  {
2
2
  "name": "@secrez/fs",
3
- "version": "1.0.8",
3
+ "version": "1.1.1",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "dependencies": {
9
- "@secrez/core": "~1.0.6",
10
- "@secrez/crypto": "~1.0.5",
11
- "@secrez/utils": "~1.0.5",
9
+ "@secrez/core": "~1.1.1",
10
+ "@secrez/crypto": "~1.1.1",
11
+ "@secrez/utils": "~1.1.1",
12
12
  "command-line-args": "^5.2.1",
13
13
  "fs-extra": "^9.1.0",
14
14
  "lodash": "^4.17.21"
15
15
  },
16
16
  "devDependencies": {
17
- "@secrez/test-helpers": "~2.0.1",
17
+ "@secrez/test-helpers": "~2.0.4",
18
18
  "chai": "^4.5.0",
19
19
  "cross-env": "^7.0.3",
20
- "dotenv": "^17.2.2",
21
- "mocha": "^7.2.0",
20
+ "mocha": "^10.8.2",
22
21
  "nyc": "^15.1.0"
23
22
  },
24
23
  "description": "The Secrez fs library.",
@@ -32,7 +31,7 @@
32
31
  "email": "francesco@sullo.co"
33
32
  },
34
33
  "engines": {
35
- "node": ">=10.0.0"
34
+ "node": ">=22.23.4"
36
35
  },
37
36
  "scripts": {
38
37
  "all-tests": "find test/** -name '*.test.js' | xargs ./node_modules/.bin/mocha -R spec",
package/src/DataCache.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const path = require("path");
2
2
  const fs = require("fs-extra");
3
+ const { VaultPermissions } = require("@secrez/core");
3
4
 
4
5
  class DataCache {
5
6
  constructor(dataPath, secrez) {
@@ -8,7 +9,7 @@ class DataCache {
8
9
  this.encrypted = {};
9
10
  if (dataPath) {
10
11
  this.dataPath = dataPath;
11
- fs.ensureDirSync(dataPath);
12
+ VaultPermissions.ensureDirSync(dataPath);
12
13
  this.ensured = {};
13
14
  }
14
15
  }
@@ -102,7 +103,7 @@ class DataCache {
102
103
 
103
104
  ensure(key) {
104
105
  if (!this.ensured[key]) {
105
- fs.ensureDirSync(path.join(this.dataPath, key));
106
+ VaultPermissions.ensureDirSync(path.join(this.dataPath, key));
106
107
  this.ensured[key] = true;
107
108
  }
108
109
  }
@@ -124,7 +125,7 @@ class DataCache {
124
125
  }
125
126
  }
126
127
  if (changed) {
127
- await fs.writeFile(p, content);
128
+ await VaultPermissions.writeFile(p, content);
128
129
  }
129
130
  if (this.encrypted[key]) {
130
131
  data.encryptedValue = value;
package/src/FileCipher.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const Crypto = require("@secrez/crypto");
2
+ const legacy = require("./fileCipherLegacy");
2
3
 
3
4
  class FileCipher {
4
5
  constructor(secrez) {
@@ -15,33 +16,56 @@ class FileCipher {
15
16
  return pk.split("$")[0].substring(0, 16);
16
17
  }
17
18
 
18
- getUint8ArrayPassword(password) {
19
- return Crypto.bufferToUint8Array(Crypto.SHA3(password));
19
+ deriveExportPasswordKey(password, iterations, randomSaltB64) {
20
+ const passwordKey = Crypto.SHA3(password);
21
+ const salt = Crypto.SHA3(
22
+ passwordKey + iterations.toString() + randomSaltB64
23
+ );
24
+ return Crypto.bufferToUint8Array(
25
+ Crypto.deriveKey(passwordKey, salt, iterations, 32)
26
+ );
20
27
  }
21
28
 
22
29
  encryptFile(content, options) {
23
- let key = Crypto.generateKey();
24
- let result = [
25
- "1", //version
30
+ if (options.password) {
31
+ return this.encryptFileWithPassword(content, options);
32
+ }
33
+ return this.encryptFileWithPublicKeys(content, options);
34
+ }
35
+
36
+ encryptFileWithPassword(content, options) {
37
+ const iterations = parseInt(options.iterations, 10);
38
+ if (!iterations || iterations < 1) {
39
+ throw new Error("A positive number of iterations is required");
40
+ }
41
+ const randomSaltB64 = Crypto.bs64.encode(Crypto.generateKey());
42
+ const key = Crypto.generateKey();
43
+ const derivedPassword = this.deriveExportPasswordKey(
44
+ options.password,
45
+ iterations,
46
+ randomSaltB64
47
+ );
48
+ return [
49
+ "2",
26
50
  Crypto.encrypt(content, key),
51
+ randomSaltB64,
52
+ Crypto.encrypt(key, derivedPassword),
27
53
  ];
28
- if (options.password) {
29
- result[2] = Crypto.encrypt(
30
- key,
31
- this.getUint8ArrayPassword(options.password)
32
- );
33
- } else {
34
- const myShort = this.shortPublicKey(this.secrez.getPublicKey());
35
- result[2] = myShort;
36
- if (options.publicKeys) {
37
- for (let publicKey of options.publicKeys) {
38
- let short = this.shortPublicKey(publicKey);
39
- let encKey =
40
- short === myShort
41
- ? this.secrez.encryptData(key) // encrypted for themself
42
- : this.secrez.encryptSharedData(key, publicKey);
43
- result.push(short + encKey);
44
- }
54
+ }
55
+
56
+ encryptFileWithPublicKeys(content, options) {
57
+ const key = Crypto.generateKey();
58
+ const result = ["1", Crypto.encrypt(content, key)];
59
+ const myShort = this.shortPublicKey(this.secrez.getPublicKey());
60
+ result[2] = myShort;
61
+ if (options.publicKeys) {
62
+ for (let publicKey of options.publicKeys) {
63
+ let short = this.shortPublicKey(publicKey);
64
+ let encKey =
65
+ short === myShort
66
+ ? this.secrez.encryptData(key)
67
+ : this.secrez.encryptSharedData(key, publicKey);
68
+ result.push(short + encKey);
45
69
  }
46
70
  }
47
71
  return result;
@@ -51,48 +75,72 @@ class FileCipher {
51
75
  if (typeof encryptedContent === "string") {
52
76
  encryptedContent = encryptedContent.split(",");
53
77
  }
54
- const [version, content, passwordOrShortPublicKey, ...keys] =
55
- encryptedContent;
78
+ const version = encryptedContent[0];
79
+ if (version === "2") {
80
+ return this.decryptFileV2(encryptedContent, options);
81
+ }
56
82
  if (version === "1") {
57
- if (keys && keys.length) {
58
- let myShort = this.shortPublicKey(this.secrez.getPublicKey());
59
- let contactPublicKey = options.contactPublicKey;
60
- if (!contactPublicKey) {
61
- for (let pk of options.contactsPublicKeys || []) {
62
- if (this.shortPublicKey(pk) === passwordOrShortPublicKey) {
63
- contactPublicKey = pk;
64
- break;
65
- }
66
- }
67
- }
68
- let key;
69
- for (let item of keys) {
70
- let short = item.substring(0, 16);
71
- item = item.substring(16);
72
- if (myShort === short) {
73
- key =
74
- myShort === passwordOrShortPublicKey
75
- ? this.secrez.decryptData(item)
76
- : this.secrez.decryptSharedData(item, contactPublicKey, true);
83
+ return this.decryptFileV1(encryptedContent, options);
84
+ }
85
+ throw new Error("Unsupported version");
86
+ }
87
+
88
+ decryptFileV2(encryptedContent, options) {
89
+ const [, content, randomSaltB64, encryptedKey] = encryptedContent;
90
+ if (!options.password) {
91
+ throw new Error("A password is required");
92
+ }
93
+ const iterations = parseInt(options.iterations, 10);
94
+ if (!iterations || iterations < 1) {
95
+ throw new Error("A positive number of iterations is required");
96
+ }
97
+ const derivedPassword = this.deriveExportPasswordKey(
98
+ options.password,
99
+ iterations,
100
+ randomSaltB64
101
+ );
102
+ const key = Crypto.decrypt(encryptedKey, derivedPassword);
103
+ return Crypto.decrypt(content, key, options.returnUint8Array);
104
+ }
105
+
106
+ decryptFileV1(encryptedContent, options) {
107
+ const [, content, passwordOrShortPublicKey, ...keys] = encryptedContent;
108
+ if (keys && keys.length) {
109
+ let myShort = this.shortPublicKey(this.secrez.getPublicKey());
110
+ let contactPublicKey = options.contactPublicKey;
111
+ if (!contactPublicKey) {
112
+ for (let pk of options.contactsPublicKeys || []) {
113
+ if (this.shortPublicKey(pk) === passwordOrShortPublicKey) {
114
+ contactPublicKey = pk;
115
+ break;
77
116
  }
78
117
  }
79
- if (!key) {
80
- throw new Error("The sender didn't encrypt the data for you");
81
- }
82
- return Crypto.decrypt(content, key);
83
- } else {
84
- if (!options.password) {
85
- throw new Error("A password is required");
118
+ }
119
+ let key;
120
+ for (let item of keys) {
121
+ let short = item.substring(0, 16);
122
+ item = item.substring(16);
123
+ if (myShort === short) {
124
+ key =
125
+ myShort === passwordOrShortPublicKey
126
+ ? this.secrez.decryptData(item)
127
+ : this.secrez.decryptSharedData(item, contactPublicKey, true);
86
128
  }
87
- let key = Crypto.decrypt(
88
- passwordOrShortPublicKey,
89
- this.getUint8ArrayPassword(options.password)
90
- );
91
- return Crypto.decrypt(content, key, options.returnUint8Array);
92
129
  }
93
- } else {
94
- throw new Error("Unsupported version");
130
+ if (!key) {
131
+ throw new Error("The sender didn't encrypt the data for you");
132
+ }
133
+ return Crypto.decrypt(content, key);
134
+ }
135
+ if (!options.password) {
136
+ throw new Error("A password is required");
95
137
  }
138
+ return legacy.decryptWithPassword(
139
+ content,
140
+ passwordOrShortPublicKey,
141
+ options.password,
142
+ options.returnUint8Array
143
+ );
96
144
  }
97
145
  }
98
146
 
package/src/Tree.js CHANGED
@@ -1,7 +1,12 @@
1
1
  const fs = require("fs-extra");
2
2
  const path = require("path");
3
3
  const Node = require("./Node");
4
- const { config, Entry, ConfigUtils } = require("@secrez/core");
4
+ const {
5
+ config,
6
+ Entry,
7
+ ConfigUtils,
8
+ VaultPermissions,
9
+ } = require("@secrez/core");
5
10
  const Crypto = require("@secrez/crypto");
6
11
 
7
12
  class Tree {
@@ -454,7 +459,7 @@ class Tree {
454
459
  ? (entry.encryptedContent || "") +
455
460
  (entry.extraName ? "$" + entry.extraName : "")
456
461
  : "";
457
- await fs.writeFile(fullPath, encryptedContent);
462
+ await VaultPermissions.writeFile(fullPath, encryptedContent);
458
463
  return entry;
459
464
  }
460
465
 
@@ -637,7 +642,7 @@ class Tree {
637
642
  type: config.types.NAME,
638
643
  });
639
644
  let encryptedEntry = this.secrez.encryptEntry(entry);
640
- await fs.writeFile(
645
+ await VaultPermissions.writeFile(
641
646
  path.join(this.dataPath, encryptedEntry.encryptedName),
642
647
  ""
643
648
  );
@@ -0,0 +1,31 @@
1
+ const Crypto = require("@secrez/crypto");
2
+
3
+ function getV1PasswordKey(password) {
4
+ return Crypto.bufferToUint8Array(Crypto.SHA3(password));
5
+ }
6
+
7
+ function encryptWithPassword(content, password) {
8
+ const key = Crypto.generateKey();
9
+ return [
10
+ "1",
11
+ Crypto.encrypt(content, key),
12
+ Crypto.encrypt(key, getV1PasswordKey(password)),
13
+ ];
14
+ }
15
+
16
+ function decryptWithPassword(
17
+ content,
18
+ encryptedKey,
19
+ password,
20
+ returnUint8Array
21
+ ) {
22
+ const key = Crypto.decrypt(encryptedKey, getV1PasswordKey(password));
23
+ return Crypto.decrypt(content, key, returnUint8Array);
24
+ }
25
+
26
+ module.exports = {
27
+ VERSION: "1",
28
+ getV1PasswordKey,
29
+ encryptWithPassword,
30
+ decryptWithPassword,
31
+ };
package/src/index.js CHANGED
@@ -6,6 +6,7 @@ module.exports = {
6
6
  Node: require("./Node"),
7
7
  DataCache: require("./DataCache"),
8
8
  FileCipher: require("./FileCipher"),
9
+ FileCipherLegacy: require("./fileCipherLegacy"),
9
10
  GitConflictChecker: require("./GitConflictChecker"),
10
11
  version: require("../package").version,
11
12
  };