Skip to content

Commit 57ea090

Browse files
authored
raise PBKDF2 iterations in backward compatible way (#160)
This addresses the concerns and plan detailed in #159
1 parent e85077f commit 57ea090

File tree

11 files changed

+589
-236
lines changed

11 files changed

+589
-236
lines changed

README.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# StatiCrypt
44

5-
StatiCrypt uses AES-256 to encrypt your HTML file with your passphrase and return a static page including a password prompt and the javascript decryption logic that you can safely upload anywhere (see [what the page looks like](https://robinmoisson.github.io/staticrypt/example/example_encrypted.html)).
5+
StatiCrypt uses AES-256 to encrypt your HTML file with your long password and return a static page including a password prompt and the javascript decryption logic that you can safely upload anywhere (see [what the page looks like](https://robinmoisson.github.io/staticrypt/example/example_encrypted.html)).
66

77
This means you can **password protect the content of your _public_ static HTML file, without any back-end** - serving it over Netlify, GitHub pages, etc. (see the detail of [how it works](#how-staticrypt-works)).
88

@@ -27,41 +27,41 @@ You can then run it with `npx staticrypt ...`. You can also install globally wit
2727
**Encrypt a file:** Encrypt `test.html` and create a `test_encrypted.html` file (add `-o my_encrypted_file.html` to change the name of the output file):
2828

2929
```bash
30-
staticrypt test.html MY_PASSPHRASE
30+
staticrypt test.html MY_LONG_PASSWORD
3131
```
3232

33-
**Encrypt a file with the passphrase in an environment variable:** set your passphrase in the `STATICRYPT_PASSWORD` environment variable ([`.env` files](https://www.npmjs.com/package/dotenv#usage) are supported):
33+
**Encrypt a file with the password in an environment variable:** set your long password in the `STATICRYPT_PASSWORD` environment variable ([`.env` files](https://www.npmjs.com/package/dotenv#usage) are supported):
3434

3535
```bash
36-
# the passphrase is in the STATICRYPT_PASSWORD env variable
36+
# the password is in the STATICRYPT_PASSWORD env variable
3737
staticrypt test.html
3838
```
3939

4040
**Encrypt a file and get a shareable link containing the hashed password** - you can include your file URL or leave blank:
4141

4242
```bash
4343
# you can also pass '--share' without specifying the URL to get the `?staticrypt_pwd=...`
44-
staticrypt test.html MY_PASSPHRASE --share https://example.com/test_encrypted.html
44+
staticrypt test.html MY_LONG_PASSWORD --share https://example.com/test_encrypted.html
4545
# => https://example.com/test_encrypted.html?staticrypt_pwd=5bfbf1343c7257cd7be23ecd74bb37fa2c76d041042654f358b6255baeab898f
4646
```
4747

4848
**Encrypt all html files in a directory** and replace them with encrypted versions (`{}` will be replaced with each file name by the `find` command - if you wanted to move the encrypted files to an `encrypted/` directory, you could use `-o encrypted/{}`):
4949

5050
```bash
51-
find . -type f -name "*.html" -exec staticrypt {} MY_PASSPHRASE -o {} \;
51+
find . -type f -name "*.html" -exec staticrypt {} MY_LONG_PASSWORD -o {} \;
5252
```
5353

5454
**Encrypt all html files in a directory except** the ones ending in `_encrypted.html`:
5555

5656
```bash
57-
find . -type f -name "*.html" -not -name "*_encrypted.html" -exec staticrypt {} MY_PASSPHRASE \;
57+
find . -type f -name "*.html" -not -name "*_encrypted.html" -exec staticrypt {} MY_LONG_PASSWORD \;
5858
```
5959

6060
### CLI Reference
6161

62-
The passphrase argument is optional if `STATICRYPT_PASSWORD` is set in the environment or `.env` file.
62+
The password argument is optional if `STATICRYPT_PASSWORD` is set in the environment or `.env` file.
6363

64-
Usage: staticrypt <filename> [<passphrase>] [options]
64+
Usage: staticrypt <filename> [<password>] [options]
6565

6666
Options:
6767
--help Show help [boolean]
@@ -73,24 +73,23 @@ The passphrase argument is optional if `STATICRYPT_PASSWORD` is set in the envir
7373
-e, --embed Whether or not to embed crypto-js in the page
7474
(or use an external CDN).
7575
[boolean] [default: true]
76-
-f, --file-template Path to custom HTML template with passphrase
76+
-f, --file-template Path to custom HTML template with password
7777
prompt.
78-
[string] [default: "/lib/password_template.html"]
78+
[string] [default: "/code/staticrypt/lib/password_template.html"]
7979
-i, --instructions Special instructions to display to the user.
8080
[string] [default: ""]
8181
--label-error Error message to display on entering wrong
82-
passphrase. [string] [default: "Bad password!"]
82+
password. [string] [default: "Bad password!"]
8383
--noremember Set this flag to remove the "Remember me"
8484
checkbox. [boolean] [default: false]
8585
-o, --output File name/path for the generated encrypted file.
8686
[string] [default: null]
87-
--passphrase-placeholder Placeholder to use for the passphrase input.
87+
--passphrase-placeholder Placeholder to use for the password input.
8888
[string] [default: "Password"]
8989
-r, --remember Expiration in days of the "Remember me" checkbox
90-
that will save the (salted + hashed) passphrase
91-
in localStorage when entered by the user.
92-
Default: "0", no expiration.
93-
[number] [default: 0]
90+
that will save the (salted + hashed) password in
91+
localStorage when entered by the user. Default:
92+
"0", no expiration. [number] [default: 0]
9493
--remember-label Label to use for the "Remember me" checkbox.
9594
[string] [default: "Remember me"]
9695
-s, --salt Set the salt manually. It should be set if you
@@ -104,7 +103,10 @@ The passphrase argument is optional if `STATICRYPT_PASSWORD` is set in the envir
104103
value to append "?staticrypt_pwd=<hashed_pwd>",
105104
or leave empty to display the hash to append.
106105
[string]
106+
--short Hide the "short password" warning.
107+
[boolean] [default: false]
107108
-t, --title Title for the output HTML page.
109+
[string] [default: "Protected Page"]
108110

109111

110112
## HOW STATICRYPT WORKS
@@ -119,9 +121,11 @@ So it basically encrypts your page and puts everything in a user-friendly way to
119121

120122
### Is it secure?
121123

122-
Simple answer: your file content has been encrypted with AES-256 (CBC), a popular and strong encryption algorithm, you can now upload it in any public place and no one will be able to read it without the password. So yes, if you used a good password it should be pretty secure.
124+
Simple answer: your file content has been encrypted with AES-256 (CBC), a popular and strong encryption algorithm, you can now upload it in any public place and no one will be able to read it without the password. So if you used a long, strong password, then yes it should be pretty secure.
123125

124-
That being said, actual security always depends on a number of factors and on the threat model you want to protect against. Because your full encrypted file is accessible client side, brute-force/dictionary attacks would be trivial to do at a really fast pace: **use a long, unusual password**. You can read a discussion on CBC mode and how appropriate it is in the context of StatiCrypt in [#19](https://github.com/robinmoisson/staticrypt/issues/19).
126+
That being said, actual security always depends on a number of factors and on the threat model you want to protect against. Because your full encrypted file is accessible client side, brute-force/dictionary attacks would be easy to do at a really fast pace: **use a long, unusual password**. We recommend 16+ alphanum characters, [Bitwarden](https://bitwarden.com/) is a great open-source password manager if you don't have one already.
127+
128+
On the technical aspects: we use AES in CBC mode (see a discussion on why it's appropriate for StatiCrypt in [#19](https://github.com/robinmoisson/staticrypt/issues/19)) and 15k PBKDF2 iterations (it will be 600k when we'll switch to WebCrypto, read a detailed report on why these numbers in [#159](https://github.com/robinmoisson/staticrypt/issues/159)).
125129

126130
**Also, disclaimer:** I am not a cryptographer - the concept is simple and I try my best to implement it correctly but please adjust accordingly: if you are an at-risk activist or have sensitive crypto data to protect, you might want to use something else.
127131

@@ -149,9 +153,9 @@ The salt isn't secret, so you don't need to worry about hiding the config file.
149153

150154
### How does the "Remember me" checkbox work?
151155

152-
The CLI will add a "Remember me" checkbox on the password prompt by default (`--noremember` to disable). If the user checks it, the (salted + hashed) passphrase will be stored in their browser's localStorage and the page will attempt to auto-decrypt when they come back.
156+
The CLI will add a "Remember me" checkbox on the password prompt by default (`--noremember` to disable). If the user checks it, the (salted + hashed) password will be stored in their browser's localStorage and the page will attempt to auto-decrypt when they come back.
153157

154-
If no value is provided the stored passphrase doesn't expire, you can also give it a value in days for how long should the store value be kept with `-r NUMBER_OF_DAYS`. If the user reconnects to the page after the expiration date the stored value will be cleared.
158+
If no value is provided the stored password doesn't expire, you can also give it a value in days for how long should the store value be kept with `-r NUMBER_OF_DAYS`. If the user reconnects to the page after the expiration date the stored value will be cleared.
155159

156160
#### "Logging out"
157161

@@ -163,14 +167,14 @@ This allows encrypting multiple page on a single domain with the same password:
163167

164168
#### Is the "Remember me" checkbox secure?
165169

166-
In case the value stored in the browser becomes compromised an attacker can decrypt the page, but because it's stored salted and hashed this should still protect against password reuse attacks if you've used the passphrase on other websites (of course, please use a unique passphrase nonetheless).
170+
In case the value stored in the browser becomes compromised an attacker can decrypt the page, but because it's stored salted and hashed this should still protect against password reuse attacks if you've used the password on other websites (of course, please use a long, unique password nonetheless).
167171

168172
## Contributing
169173

170174
### 🙏 Thank you!
171175

172176
- [@AaronCoplan](https://github.com/AaronCoplan) for bringing the CLI to life
173-
- [@epicfaace](https://github.com/epicfaace) & [@thomasmarr](https://github.com/thomasmarr) for sparking the caching of the passphrase in localStorage (allowing the "Remember me" checkbox)
177+
- [@epicfaace](https://github.com/epicfaace) & [@thomasmarr](https://github.com/thomasmarr) for sparking the caching of the password in localStorage (allowing the "Remember me" checkbox)
174178
- [@hurrymaplelad](https://github.com/hurrymaplelad) for refactoring a lot of the code and making the project much more pleasant to work with
175179

176180
### Opening PRs and issues

cli/helpers.js

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ const fs = require("fs");
33
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
44
const path = require("path");
55
const {renderTemplate} = require("../lib/formater.js");
6+
const Yargs = require("yargs");
67
const { generateRandomSalt } = cryptoEngine;
78

9+
const PASSWORD_TEMPLATE_DEFAULT_PATH = path.join(__dirname, "..", "lib", "password_template.html");
10+
11+
812
/**
913
* @param {string} message
1014
*/
@@ -132,21 +136,28 @@ function convertCommonJSToBrowserJS(modulePath) {
132136
}
133137
exports.convertCommonJSToBrowserJS = convertCommonJSToBrowserJS;
134138

139+
/**
140+
* @param {string} filePath
141+
* @param {string} errorName
142+
* @returns {string}
143+
*/
144+
function readFile(filePath, errorName = file) {
145+
try {
146+
return fs.readFileSync(filePath, "utf8");
147+
} catch (e) {
148+
exitEarly(`Failure: could not read ${errorName}!`);
149+
}
150+
}
151+
135152
/**
136153
* Fill the template with provided data and writes it to output file.
137154
*
138155
* @param {Object} data
139156
* @param {string} outputFilePath
140-
* @param {string} inputFilePath
157+
* @param {string} templateFilePath
141158
*/
142-
function genFile(data, outputFilePath, inputFilePath) {
143-
let templateContents;
144-
145-
try {
146-
templateContents = fs.readFileSync(inputFilePath, "utf8");
147-
} catch (e) {
148-
exitEarly("Failure: could not read template!");
149-
}
159+
function genFile(data, outputFilePath, templateFilePath) {
160+
const templateContents = readFile(templateFilePath, "template");
150161

151162
const renderedTemplate = renderTemplate(templateContents, data);
152163

@@ -156,4 +167,128 @@ function genFile(data, outputFilePath, inputFilePath) {
156167
exitEarly("Failure: could not generate output file!");
157168
}
158169
}
159-
exports.genFile = genFile;
170+
exports.genFile = genFile;
171+
172+
/**
173+
* TODO: remove in next major version
174+
*
175+
* This method checks whether the password template support the security fix increasing PBKDF2 iterations. Users using
176+
* an old custom password_template might have logic that doesn't benefit from the fix.
177+
*
178+
* @param {string} templatePathParameter
179+
* @returns {boolean}
180+
*/
181+
function isCustomPasswordTemplateLegacy(templatePathParameter) {
182+
// if the user uses the default template, it's up to date
183+
if (templatePathParameter === PASSWORD_TEMPLATE_DEFAULT_PATH) {
184+
return false;
185+
}
186+
187+
const customTemplateContent = readFile(templatePathParameter, "template");
188+
189+
// if the template injects the crypto engine, it's up to date
190+
if (customTemplateContent.includes("js_crypto_engine")) {
191+
return false;
192+
}
193+
194+
return true;
195+
}
196+
exports.isCustomPasswordTemplateLegacy = isCustomPasswordTemplateLegacy;
197+
198+
function parseCommandLineArguments() {
199+
return Yargs.usage("Usage: staticrypt <filename> [<password>] [options]")
200+
.option("c", {
201+
alias: "config",
202+
type: "string",
203+
describe: 'Path to the config file. Set to "false" to disable.',
204+
default: ".staticrypt.json",
205+
})
206+
.option("decrypt-button", {
207+
type: "string",
208+
describe: 'Label to use for the decrypt button. Default: "DECRYPT".',
209+
default: "DECRYPT",
210+
})
211+
.option("e", {
212+
alias: "embed",
213+
type: "boolean",
214+
describe:
215+
"Whether or not to embed crypto-js in the page (or use an external CDN).",
216+
default: true,
217+
})
218+
.option("f", {
219+
alias: "file-template",
220+
type: "string",
221+
describe: "Path to custom HTML template with password prompt.",
222+
default: PASSWORD_TEMPLATE_DEFAULT_PATH,
223+
})
224+
.option("i", {
225+
alias: "instructions",
226+
type: "string",
227+
describe: "Special instructions to display to the user.",
228+
default: "",
229+
})
230+
.option("label-error", {
231+
type: "string",
232+
describe: "Error message to display on entering wrong password.",
233+
default: "Bad password!",
234+
})
235+
.option("noremember", {
236+
type: "boolean",
237+
describe: 'Set this flag to remove the "Remember me" checkbox.',
238+
default: false,
239+
})
240+
.option("o", {
241+
alias: "output",
242+
type: "string",
243+
describe: "File name/path for the generated encrypted file.",
244+
default: null,
245+
})
246+
.option("passphrase-placeholder", {
247+
type: "string",
248+
describe: "Placeholder to use for the password input.",
249+
default: "Password",
250+
})
251+
.option("r", {
252+
alias: "remember",
253+
type: "number",
254+
describe:
255+
'Expiration in days of the "Remember me" checkbox that will save the (salted + hashed) password ' +
256+
'in localStorage when entered by the user. Default: "0", no expiration.',
257+
default: 0,
258+
})
259+
.option("remember-label", {
260+
type: "string",
261+
describe: 'Label to use for the "Remember me" checkbox.',
262+
default: "Remember me",
263+
})
264+
// do not give a default option to this parameter - we want to see when the flag is included with no
265+
// value and when it's not included at all
266+
.option("s", {
267+
alias: "salt",
268+
describe:
269+
'Set the salt manually. It should be set if you want to use "Remember me" through multiple pages. It ' +
270+
"needs to be a 32-character-long hexadecimal string.\nInclude the empty flag to generate a random salt you " +
271+
'can use: "statycrypt -s".',
272+
type: "string",
273+
})
274+
// do not give a default option to this parameter - we want to see when the flag is included with no
275+
// value and when it's not included at all
276+
.option("share", {
277+
describe:
278+
'Get a link containing your hashed password that will auto-decrypt the page. Pass your URL as a value to append '
279+
+ '"?staticrypt_pwd=<hashed_pwd>", or leave empty to display the hash to append.',
280+
type: "string",
281+
})
282+
.option("short", {
283+
describe: 'Hide the "short password" warning.',
284+
type: "boolean",
285+
default: false,
286+
})
287+
.option("t", {
288+
alias: "title",
289+
type: "string",
290+
describe: "Title for the output HTML page.",
291+
default: "Protected Page",
292+
});
293+
}
294+
exports.parseCommandLineArguments = parseCommandLineArguments;

0 commit comments

Comments
 (0)