hexo-blog-encrypt 4.0.2 → 4.1.0
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 +256 -245
- package/ReadMe.zh.md +222 -230
- package/lib/hbe.blink.html +1 -1
- package/lib/hbe.bundle.js +1 -1
- package/lib/hbe.bundle.js.map +4 -4
- package/lib/hbe.default.html +1 -1
- package/lib/hbe.flip.html +1 -1
- package/lib/hbe.shrink.html +1 -1
- package/lib/hbe.style.css +50 -20
- package/lib/hbe.surge.html +1 -1
- package/lib/hbe.up.html +1 -1
- package/lib/hbe.wave.html +1 -1
- package/lib/hbe.xray.html +1 -1
- package/package.json +11 -9
- package/src/server/config.js +104 -4
- package/src/server/crypto.js +10 -5
- package/src/server/generator.js +18 -2
- package/src/server/index.js +12 -5
- package/src/server/template.js +9 -4
package/src/server/crypto.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const crypto = require('node:crypto');
|
|
4
|
+
const { promisify } = require('node:util');
|
|
5
|
+
|
|
6
|
+
const pbkdf2 = promisify(crypto.pbkdf2);
|
|
4
7
|
|
|
5
8
|
const DEFAULT_ITERATIONS = 250000;
|
|
6
9
|
const SALT_BYTES = 32;
|
|
@@ -14,10 +17,12 @@ function deriveKey(password, salt, iterations) {
|
|
|
14
17
|
const passwordBuf = typeof password === 'string'
|
|
15
18
|
? Buffer.from(password, 'utf8')
|
|
16
19
|
: Buffer.from(String(password), 'utf8');
|
|
17
|
-
|
|
20
|
+
// Async PBKDF2 (#252): runs on the libuv thread pool so a multi-post build
|
|
21
|
+
// does not block the event loop for each derivation.
|
|
22
|
+
return pbkdf2(passwordBuf, salt, iterations, KEY_BYTES, HASH);
|
|
18
23
|
}
|
|
19
24
|
|
|
20
|
-
function encrypt(plaintext, password, opts) {
|
|
25
|
+
async function encrypt(plaintext, password, opts) {
|
|
21
26
|
const iterations = (opts && Number.isInteger(opts.iterations) && opts.iterations > 0)
|
|
22
27
|
? opts.iterations
|
|
23
28
|
: DEFAULT_ITERATIONS;
|
|
@@ -31,7 +36,7 @@ function encrypt(plaintext, password, opts) {
|
|
|
31
36
|
salt = crypto.randomBytes(SALT_BYTES);
|
|
32
37
|
}
|
|
33
38
|
const nonce = crypto.randomBytes(NONCE_BYTES);
|
|
34
|
-
const dk = deriveKey(password, salt, iterations);
|
|
39
|
+
const dk = await deriveKey(password, salt, iterations);
|
|
35
40
|
const cipher = crypto.createCipheriv(CIPHER, dk, nonce);
|
|
36
41
|
const enc = Buffer.concat([
|
|
37
42
|
cipher.update(Buffer.from(String(plaintext), 'utf8')),
|
|
@@ -42,7 +47,7 @@ function encrypt(plaintext, password, opts) {
|
|
|
42
47
|
return { salt, nonce, ciphertext };
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
function decrypt(salt, nonce, ciphertext, password, opts) {
|
|
50
|
+
async function decrypt(salt, nonce, ciphertext, password, opts) {
|
|
46
51
|
const iterations = (opts && Number.isInteger(opts.iterations) && opts.iterations > 0)
|
|
47
52
|
? opts.iterations
|
|
48
53
|
: DEFAULT_ITERATIONS;
|
|
@@ -55,7 +60,7 @@ function decrypt(salt, nonce, ciphertext, password, opts) {
|
|
|
55
60
|
if (!Buffer.isBuffer(ciphertext) || ciphertext.length < TAG_BYTES) {
|
|
56
61
|
throw new Error('ciphertext must be a Buffer of length ≥ 16');
|
|
57
62
|
}
|
|
58
|
-
const dk = deriveKey(password, salt, iterations);
|
|
63
|
+
const dk = await deriveKey(password, salt, iterations);
|
|
59
64
|
const enc = ciphertext.slice(0, ciphertext.length - TAG_BYTES);
|
|
60
65
|
const tag = ciphertext.slice(ciphertext.length - TAG_BYTES);
|
|
61
66
|
const decipher = crypto.createDecipheriv(CIPHER, dk, nonce);
|
package/src/server/generator.js
CHANGED
|
@@ -9,6 +9,18 @@ function hex10(buf) {
|
|
|
9
9
|
return crypto.createHash('sha256').update(buf).digest('hex').slice(0, HASH_BYTES * 2);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
// esbuild ends the bundle with `//# sourceMappingURL=hbe.bundle.js.map`, a
|
|
13
|
+
// file that is never served (#262). Point it at the emitted hashed map, or
|
|
14
|
+
// strip it when no map route is emitted, so browsers never request a 404.
|
|
15
|
+
const SOURCEMAP_COMMENT_RE = /\n?\/\/# sourceMappingURL=\S+\s*$/;
|
|
16
|
+
|
|
17
|
+
function linkSourcemap(bytes, mapName) {
|
|
18
|
+
const text = bytes.toString('utf8');
|
|
19
|
+
if (!SOURCEMAP_COMMENT_RE.test(text)) return bytes;
|
|
20
|
+
const replacement = mapName ? `\n//# sourceMappingURL=${mapName}\n` : '\n';
|
|
21
|
+
return Buffer.from(text.replace(SOURCEMAP_COMMENT_RE, replacement), 'utf8');
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
/**
|
|
13
25
|
* Build the v4 Hexo generator function. Returns a function that, when
|
|
14
26
|
* invoked by Hexo's generator pipeline, yields:
|
|
@@ -68,8 +80,12 @@ function createGenerator(opts) {
|
|
|
68
80
|
hash: hex10(bundleBytes),
|
|
69
81
|
};
|
|
70
82
|
}
|
|
71
|
-
|
|
83
|
+
// The hash is computed over the on-disk bundle bytes; the filter and the
|
|
84
|
+
// generator share this instance, so both always agree on the route name.
|
|
85
|
+
const { bytes: rawBundleBytes, hash } = cache;
|
|
72
86
|
const jsRoutePath = `lib/hbe.${hash}.js`;
|
|
87
|
+
const hasMap = Boolean(sourcemapPath && fs.existsSync(sourcemapPath));
|
|
88
|
+
const bundleBytes = linkSourcemap(rawBundleBytes, hasMap ? `hbe.${hash}.js.map` : null);
|
|
73
89
|
|
|
74
90
|
const routes = [
|
|
75
91
|
{
|
|
@@ -82,7 +98,7 @@ function createGenerator(opts) {
|
|
|
82
98
|
},
|
|
83
99
|
];
|
|
84
100
|
|
|
85
|
-
if (
|
|
101
|
+
if (hasMap) {
|
|
86
102
|
const mapBytes = fs.readFileSync(sourcemapPath);
|
|
87
103
|
routes.push({
|
|
88
104
|
path: `lib/hbe.${hash}.js.map`,
|
package/src/server/index.js
CHANGED
|
@@ -91,7 +91,7 @@ function register(hexo) {
|
|
|
91
91
|
sourcemapPath: BUNDLE_SOURCEMAP_PATH,
|
|
92
92
|
});
|
|
93
93
|
|
|
94
|
-
hexo.extend.filter.register('after_post_render', function v4Filter(data) {
|
|
94
|
+
hexo.extend.filter.register('after_post_render', async function v4Filter(data) {
|
|
95
95
|
// Idempotence: stamp a per-instance Symbol on `data` after a successful
|
|
96
96
|
// pass so re-entry on the same object is a no-op. We deliberately do NOT
|
|
97
97
|
// use `data.encrypt === true` (a user-set "please encrypt" front-matter
|
|
@@ -109,11 +109,19 @@ function register(hexo) {
|
|
|
109
109
|
return data;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
const titleText = (typeof data.title === 'string') ? data.title.trim() : '(untitled)';
|
|
112
113
|
let effectivePostData = data;
|
|
113
114
|
if (fmPassword === undefined || fmPassword === null) {
|
|
114
115
|
const tagMatch = resolveTagPassword(hexo.config && hexo.config.encrypt, data.tags);
|
|
115
|
-
if (!tagMatch
|
|
116
|
-
|
|
116
|
+
if (!tagMatch) return data;
|
|
117
|
+
if (tagMatch.password === undefined || tagMatch.password === null) {
|
|
118
|
+
// Fail closed (#244): a matching tag entry without a password is a
|
|
119
|
+
// config mistake; publishing the post in plaintext would be silent.
|
|
120
|
+
throw new Error(
|
|
121
|
+
`hexo-blog-encrypt: post "${titleText}" matches encrypt.tags entry "${tagMatch.name}", ` +
|
|
122
|
+
'but that entry has no password. Set a password on the tag entry in _config.yml, ' +
|
|
123
|
+
'or add `password: ""` to the post front matter to publish it unencrypted.'
|
|
124
|
+
);
|
|
117
125
|
}
|
|
118
126
|
effectivePostData = Object.assign({}, data, { password: tagMatch.password });
|
|
119
127
|
effectivePostData.__tagName = tagMatch.name;
|
|
@@ -132,7 +140,6 @@ function register(hexo) {
|
|
|
132
140
|
logger.updateSilent(!!cfg.silent);
|
|
133
141
|
|
|
134
142
|
const tagName = effectivePostData.__tagName;
|
|
135
|
-
const titleText = (typeof data.title === 'string') ? data.title.trim() : '(untitled)';
|
|
136
143
|
if (tagName) {
|
|
137
144
|
logger.info(`hexo-blog-encrypt: encrypting "${titleText}" via tag "${tagName}" with theme ${cfg.theme}.`);
|
|
138
145
|
} else {
|
|
@@ -146,7 +153,7 @@ function register(hexo) {
|
|
|
146
153
|
const stableSaltEnabled = cfg.stableSalt === true
|
|
147
154
|
&& typeof data.permalink === 'string'
|
|
148
155
|
&& data.permalink.length > 0;
|
|
149
|
-
const { salt, nonce, ciphertext } = encrypt(plaintext, cfg.password, {
|
|
156
|
+
const { salt, nonce, ciphertext } = await encrypt(plaintext, cfg.password, {
|
|
150
157
|
iterations: cfg.kdf.iterations,
|
|
151
158
|
salt: stableSaltEnabled ? stableSaltFromPermalink(data.permalink) : undefined,
|
|
152
159
|
});
|
package/src/server/template.js
CHANGED
|
@@ -37,7 +37,8 @@ const PLACEHOLDERS = [
|
|
|
37
37
|
{ token: '{{hbeWrongHashMessage}}', field: 'whm', mode: 'attr' },
|
|
38
38
|
{ token: '{{hbeKdfIterations}}', field: 'kdfIterations', mode: 'attr' },
|
|
39
39
|
{ token: '{{hbeAutoSave}}', field: 'autoSave', mode: 'attr' },
|
|
40
|
-
|
|
40
|
+
// attr (#247): some themes place the message inside data-content="…".
|
|
41
|
+
{ token: '{{hbeMessage}}', field: 'message', mode: 'attr' },
|
|
41
42
|
{ token: '{{hbeButtonClass}}', field: 'buttonClass', mode: 'attr' },
|
|
42
43
|
{ token: '{{hbeButtonText}}', field: 'buttonText', mode: 'text' },
|
|
43
44
|
{ token: '{{hbeSalt}}', field: 'salt', mode: 'hex' },
|
|
@@ -45,6 +46,8 @@ const PLACEHOLDERS = [
|
|
|
45
46
|
{ token: '{{hbeEncryptedData}}', field: 'ciphertext', mode: 'hex' },
|
|
46
47
|
];
|
|
47
48
|
|
|
49
|
+
const PLACEHOLDER_RE = new RegExp(PLACEHOLDERS.map(({ token }) => token).join('|'), 'g');
|
|
50
|
+
|
|
48
51
|
function valueOf(opts, field) {
|
|
49
52
|
const v = opts[field];
|
|
50
53
|
if (field === 'kdfIterations') return String(v);
|
|
@@ -53,7 +56,7 @@ function valueOf(opts, field) {
|
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
function substitute(template, opts) {
|
|
56
|
-
|
|
59
|
+
const replacements = new Map();
|
|
57
60
|
for (const { token, field, mode } of PLACEHOLDERS) {
|
|
58
61
|
const raw = valueOf(opts, field);
|
|
59
62
|
let replacement;
|
|
@@ -67,9 +70,11 @@ function substitute(template, opts) {
|
|
|
67
70
|
}
|
|
68
71
|
replacement = s;
|
|
69
72
|
}
|
|
70
|
-
|
|
73
|
+
replacements.set(token, replacement);
|
|
71
74
|
}
|
|
72
|
-
|
|
75
|
+
// Match only the original template. Replacement text may contain tokens of
|
|
76
|
+
// its own and must never be expanded again in a different escaping context.
|
|
77
|
+
return template.replace(PLACEHOLDER_RE, (token) => replacements.get(token));
|
|
73
78
|
}
|
|
74
79
|
|
|
75
80
|
function discoverThemes(templateDir) {
|