domma-cms 1.28.4 → 1.29.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/CHANGELOG.md +9 -0
- package/admin/js/lib/contacts-arrange.js +1 -1
- package/docs/DommaFeatures.md +15 -9
- package/docs/plugin-development.md +1 -1
- package/package.json +4 -1
- package/plugins/_lib/admin/mail/body-menu.js +2 -0
- package/plugins/_lib/admin/mail/cms-host.js +22 -0
- package/plugins/_lib/admin/mail/compose-window.js +1027 -1021
- package/plugins/_lib/admin/mail/contacts.js +10 -8
- package/plugins/_lib/admin/mail/diagnostics-section.js +2 -0
- package/plugins/_lib/admin/mail/drag-drop.js +46 -0
- package/plugins/_lib/admin/mail/folder-tree.js +2 -0
- package/plugins/_lib/admin/mail/font-controls.js +6 -4
- package/plugins/_lib/admin/mail/fonts.js +1 -1
- package/plugins/_lib/admin/mail/host-context.js +87 -0
- package/plugins/_lib/admin/mail/identity.js +2 -0
- package/plugins/_lib/admin/mail/image-senders-section.js +2 -0
- package/plugins/_lib/admin/mail/keyboard.js +2 -0
- package/plugins/_lib/admin/mail/mail.css +1 -1
- package/plugins/_lib/admin/mail/mail.html +4 -0
- package/plugins/_lib/admin/mail/message-actions.js +2 -0
- package/plugins/_lib/admin/mail/oauth-panel.js +2 -0
- package/plugins/_lib/admin/mail/panes.js +2 -0
- package/plugins/_lib/admin/mail/reader-kit.js +2 -0
- package/plugins/_lib/admin/mail/reader-runtime.js +6534 -0
- package/plugins/_lib/admin/mail/reader-view.js +9 -6380
- package/plugins/_lib/admin/mail/resizable.js +2 -0
- package/plugins/_lib/admin/mail/restore-spam.js +13 -0
- package/plugins/_lib/admin/mail/rules.js +14 -0
- package/plugins/_lib/admin/mail/scheduling.js +4 -1
- package/plugins/_lib/admin/mail/section-kit.js +4 -1
- package/plugins/_lib/admin/mail/security-note.js +2 -0
- package/plugins/_lib/admin/mail/settings-section.js +11 -10
- package/plugins/_lib/admin/mail/templates.js +5 -2
- package/plugins/_lib/admin/mail/threads.js +2 -0
- package/plugins/_lib/admin/mail/vacation.js +16 -2
- package/plugins/_lib/admin/mail/when-closed.js +24 -2
- package/plugins/_lib/admin/ui/dates.js +2 -0
- package/plugins/_lib/admin/ui/resizable.js +7 -5
- package/plugins/_lib/contacts/mailContact.js +69 -0
- package/plugins/_lib/contacts/model.js +199 -0
- package/plugins/_lib/domma-mail-contacts.lock.json +10 -0
- package/plugins/_lib/domma-mail.lock.json +85 -0
- package/plugins/_lib/mail/accountFileStore.js +66 -0
- package/plugins/_lib/mail/accountRepository.js +617 -0
- package/plugins/_lib/mail/accountRoutes.js +130 -0
- package/plugins/_lib/mail/accounts.js +66 -940
- package/plugins/_lib/mail/automationLock.js +10 -0
- package/plugins/_lib/mail/bodyTokenProvider.js +36 -0
- package/plugins/_lib/mail/bodyTokens.js +5 -99
- package/plugins/_lib/mail/compose.js +5 -3
- package/plugins/_lib/mail/defaults.js +2 -0
- package/plugins/_lib/mail/envelope.js +3 -1
- package/plugins/_lib/mail/folderActions.js +83 -0
- package/plugins/_lib/mail/folderRuntime.js +109 -0
- package/plugins/_lib/mail/folders.js +4 -9
- package/plugins/_lib/mail/imapPool.js +11 -389
- package/plugins/_lib/mail/imapPoolRuntime.js +396 -0
- package/plugins/_lib/mail/mbox.js +2 -0
- package/plugins/_lib/mail/message-action.js +2 -0
- package/plugins/_lib/mail/messageSecurity.js +4 -111
- package/plugins/_lib/mail/messageSecurityProvider.js +116 -0
- package/plugins/_lib/mail/networkAddresses.js +77 -0
- package/plugins/_lib/mail/networkPolicy.js +62 -0
- package/plugins/_lib/mail/oauth.js +3 -451
- package/plugins/_lib/mail/oauthAccounts.js +178 -0
- package/plugins/_lib/mail/oauthRouteRuntime.js +282 -0
- package/plugins/_lib/mail/oauthRoutes.js +7 -289
- package/plugins/_lib/mail/oauthRuntime.js +456 -0
- package/plugins/_lib/mail/pollSchedule.js +2 -0
- package/plugins/_lib/mail/poller.js +4 -136
- package/plugins/_lib/mail/pollerRuntime.js +150 -0
- package/plugins/_lib/mail/priority.js +2 -0
- package/plugins/_lib/mail/publicMailbox.js +27 -0
- package/plugins/_lib/mail/readRoutes.js +14 -827
- package/plugins/_lib/mail/readRuntime.js +701 -0
- package/plugins/_lib/mail/render.js +2 -0
- package/plugins/_lib/mail/ruleRunner.js +27 -10
- package/plugins/_lib/mail/scheduler.js +11 -279
- package/plugins/_lib/mail/schedulerRuntime.js +259 -0
- package/plugins/_lib/mail/secretProvider.js +112 -0
- package/plugins/_lib/mail/secretbox.js +12 -225
- package/plugins/_lib/mail/send.js +21 -537
- package/plugins/_lib/mail/sieve.js +211 -0
- package/plugins/_lib/mail/sieveHost.js +24 -0
- package/plugins/_lib/mail/smtpService.js +553 -0
- package/plugins/_lib/mail/snoozeRuntime.js +60 -0
- package/plugins/_lib/mail/store.js +122 -64
- package/plugins/_lib/mail/subscriptions.js +11 -0
- package/plugins/_lib/mail/sync.js +64 -290
- package/plugins/_lib/mail/syncPlan.js +2 -0
- package/plugins/_lib/mail/syncRuntime.js +255 -0
- package/plugins/_lib/mail/syncSelection.js +2 -0
- package/plugins/_lib/mail/unreadBadge.js +2 -0
- package/plugins/_lib/mail/unsubscribe.js +2 -0
- package/plugins/_lib/mail/vacationRunner.js +3 -116
- package/plugins/_lib/mail/vacationRuntime.js +70 -0
- package/plugins/_lib/mail/write.js +44 -13
- package/server/routes/api/contacts.js +2 -196
- package/server/routes/api/forms.js +8 -1
- package/server/services/viewerSession.js +1 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/secretProvider.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
3
|
+
import {createCipheriv, createDecipheriv, createHash, randomBytes, scryptSync} from 'node:crypto';
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const DOMAIN = 'domma-mail-v1';
|
|
8
|
+
const MIN_SECRET_LENGTH = 32;
|
|
9
|
+
|
|
10
|
+
export class MailKeyError extends Error {
|
|
11
|
+
constructor(message, {sealedWith, availableFrom} = {}) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'MailKeyError';
|
|
14
|
+
this.sealedWith = sealedWith ?? null;
|
|
15
|
+
this.availableFrom = availableFrom ?? null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function describeSource(source) {
|
|
20
|
+
if (source === 'env-mail') return 'MAIL_SECRET environment variable';
|
|
21
|
+
if (source === 'env-instance') return 'INSTANCE_SECRET environment variable';
|
|
22
|
+
if (source === 'local') return 'plugin-local key file';
|
|
23
|
+
return 'unknown key source';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Instance-scoped implementation of the existing CMS v1 sealed-secret format. */
|
|
27
|
+
export function createSecretBox({dataDir, legacyKeyFile = null, environment = () => ({})} = {}) {
|
|
28
|
+
if (!dataDir) throw new Error('A mail data directory is required.');
|
|
29
|
+
const keyFile = path.join(dataDir, '.mailkey');
|
|
30
|
+
const keyCache = new Map();
|
|
31
|
+
|
|
32
|
+
function readKey(file) {
|
|
33
|
+
try {
|
|
34
|
+
const secret = fs.readFileSync(file, 'utf8').trim();
|
|
35
|
+
if (secret.length < MIN_SECRET_LENGTH) throw new MailKeyError('The stored mail key is invalid. Restore the key from backup.');
|
|
36
|
+
return secret;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error.code === 'ENOENT') return null;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function localKeyMaterial() {
|
|
44
|
+
const existing = readKey(keyFile);
|
|
45
|
+
if (existing) return existing;
|
|
46
|
+
const material = (legacyKeyFile && readKey(legacyKeyFile)) || randomBytes(32).toString('hex');
|
|
47
|
+
fs.mkdirSync(dataDir, {recursive: true, mode: 0o700});
|
|
48
|
+
try {
|
|
49
|
+
fs.writeFileSync(keyFile, material + '\n', {encoding: 'utf8', mode: 0o600, flag: 'wx'});
|
|
50
|
+
return material;
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error.code !== 'EEXIST') throw error;
|
|
53
|
+
return readKey(keyFile);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function resolveKeySource() {
|
|
58
|
+
const env = environment();
|
|
59
|
+
if (typeof env.MAIL_SECRET === 'string' && env.MAIL_SECRET.length >= MIN_SECRET_LENGTH) {
|
|
60
|
+
return {secret: env.MAIL_SECRET, source: 'env-mail'};
|
|
61
|
+
}
|
|
62
|
+
if (typeof env.INSTANCE_SECRET === 'string' && env.INSTANCE_SECRET.length >= MIN_SECRET_LENGTH) {
|
|
63
|
+
return {secret: env.INSTANCE_SECRET, source: 'env-instance'};
|
|
64
|
+
}
|
|
65
|
+
return {secret: localKeyMaterial(), source: 'local'};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function deriveKey(secret, source, salt) {
|
|
69
|
+
// Include the actual key material: rotating a secret under the same
|
|
70
|
+
// source name must also invalidate previously cached derived keys.
|
|
71
|
+
const fingerprint = createHash('sha256').update(secret).digest('hex');
|
|
72
|
+
const cacheKey = `${source}:${fingerprint}:${salt.toString('base64')}`;
|
|
73
|
+
if (keyCache.has(cacheKey)) return keyCache.get(cacheKey);
|
|
74
|
+
const key = scryptSync(DOMAIN + ':' + secret, salt, 32);
|
|
75
|
+
if (keyCache.size >= 256) keyCache.clear();
|
|
76
|
+
keyCache.set(cacheKey, key);
|
|
77
|
+
return key;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function seal(plaintext) {
|
|
81
|
+
const {secret, source} = resolveKeySource();
|
|
82
|
+
const salt = randomBytes(16);
|
|
83
|
+
const iv = randomBytes(12);
|
|
84
|
+
const cipher = createCipheriv('aes-256-gcm', deriveKey(secret, source, salt), iv);
|
|
85
|
+
const ciphertext = Buffer.concat([cipher.update(String(plaintext), 'utf8'), cipher.final()]);
|
|
86
|
+
return ['v1', source, salt.toString('base64url'), iv.toString('base64url'), cipher.getAuthTag().toString('base64url'), ciphertext.toString('base64url')].join('.');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function open(token) {
|
|
90
|
+
if (typeof token !== 'string' || !token.startsWith('v1.')) throw new MailKeyError('Stored password is not in a recognised format.');
|
|
91
|
+
const parts = token.split('.');
|
|
92
|
+
const [, sealedWith, saltPart, ivPart, tagPart, ctPart] = parts;
|
|
93
|
+
if (parts.length !== 6 || !saltPart || !ivPart || !tagPart || !ctPart) throw new MailKeyError('Stored password is malformed.');
|
|
94
|
+
const {secret, source} = resolveKeySource();
|
|
95
|
+
try {
|
|
96
|
+
const salt = Buffer.from(saltPart, 'base64url');
|
|
97
|
+
const iv = Buffer.from(ivPart, 'base64url');
|
|
98
|
+
const tag = Buffer.from(tagPart, 'base64url');
|
|
99
|
+
if (salt.length !== 16 || iv.length !== 12 || tag.length !== 16) throw new Error('Malformed secret');
|
|
100
|
+
const decipher = createDecipheriv('aes-256-gcm', deriveKey(secret, source, salt), iv);
|
|
101
|
+
decipher.setAuthTag(tag);
|
|
102
|
+
return Buffer.concat([decipher.update(Buffer.from(ctPart, 'base64url')), decipher.final()]).toString('utf8');
|
|
103
|
+
} catch {
|
|
104
|
+
throw new MailKeyError(sealedWith !== source
|
|
105
|
+
? `This password was saved using the ${describeSource(sealedWith)} and the server is now using the ${describeSource(source)}. Re-enter the password to save it against the new key.`
|
|
106
|
+
: 'Stored password could not be decrypted - the encryption key has changed. Re-enter the password.',
|
|
107
|
+
{sealedWith, availableFrom: source});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {resolveKeySource, seal, open};
|
|
112
|
+
}
|
|
@@ -1,229 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* IMAP is a password protocol: to reconnect without asking the user every time
|
|
5
|
-
* we have to keep their mailbox password, and keeping it in plain text on disk
|
|
6
|
-
* is not an option. Everything stored goes through seal()/open() here.
|
|
7
|
-
*
|
|
8
|
-
* ## Key resolution
|
|
9
|
-
*
|
|
10
|
-
* The key is derived from whichever of these is present, in order:
|
|
11
|
-
*
|
|
12
|
-
* 1. `MAIL_SECRET` — explicit override for anyone who wants mail
|
|
13
|
-
* credentials on their own rotatable key.
|
|
14
|
-
* 2. `INSTANCE_SECRET` — written by `npm run setup` and by manager
|
|
15
|
-
* registration. Present on fresh installs; NOT
|
|
16
|
-
* present on sites that predate it, which is why
|
|
17
|
-
* this is a chain and not a single source.
|
|
18
|
-
* 3. a generated key in `data/.mailkey` (0600) — last resort so the plugin
|
|
19
|
-
* works out of the box. It sits next to the sealed
|
|
20
|
-
* data, so it defends a leaked *backup* of
|
|
21
|
-
* accounts.json, not a compromised host.
|
|
22
|
-
*
|
|
23
|
-
* Deliberately NOT derived from JWT_SECRET: rotating that logs everyone out,
|
|
24
|
-
* which is recoverable, and silently bricking every stored mailbox password at
|
|
25
|
-
* the same time is not.
|
|
26
|
-
*
|
|
27
|
-
* The source that sealed a value is recorded in the token, so moving up the
|
|
28
|
-
* chain (adding MAIL_SECRET to a site that had been using .mailkey) fails with
|
|
29
|
-
* a sentence that says what happened instead of a bare auth error.
|
|
30
|
-
*
|
|
31
|
-
* @module _lib/mail/secretbox
|
|
32
|
-
*/
|
|
33
|
-
import {createCipheriv, createDecipheriv, randomBytes, scryptSync} from 'node:crypto';
|
|
34
|
-
import fs from 'node:fs';
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/secretbox.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
3
|
+
/** CMS compatibility entry point. Standalone hosts create their own secret box. */
|
|
35
4
|
import path from 'node:path';
|
|
36
5
|
import {fileURLToPath} from 'node:url';
|
|
6
|
+
import {createSecretBox} from './secretProvider.js';
|
|
37
7
|
|
|
38
|
-
const
|
|
8
|
+
const directory = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const secrets = createSecretBox({
|
|
10
|
+
dataDir: process.env.DOMMA_MAIL_DATA_DIR || path.join(directory, '..', 'data'),
|
|
11
|
+
legacyKeyFile: path.join(directory, '..', '..', 'mail-reader', 'data', '.mailkey'),
|
|
12
|
+
environment: () => process.env
|
|
13
|
+
});
|
|
39
14
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* secret. Deliberately NOT named after the plugin: `email-pro` is expected to
|
|
43
|
-
* open the same sealed accounts, and renaming this tag would invalidate every
|
|
44
|
-
* stored password for the sake of cosmetics.
|
|
45
|
-
*/
|
|
46
|
-
const DOMAIN = 'domma-mail-v1';
|
|
47
|
-
|
|
48
|
-
/** Minimum acceptable length for an env-supplied secret. */
|
|
49
|
-
const MIN_SECRET_LENGTH = 32;
|
|
50
|
-
|
|
51
|
-
/** Shares the override used by accounts.js - see the note there. */
|
|
52
|
-
const DATA_DIR = process.env.DOMMA_MAIL_DATA_DIR || path.join(__dirname, '..', 'data');
|
|
53
|
-
|
|
54
|
-
const KEY_FILE = path.join(DATA_DIR, '.mailkey');
|
|
55
|
-
|
|
56
|
-
/** Where the key lived while this code belonged to the mail-reader plugin. */
|
|
57
|
-
const LEGACY_KEY_FILE = path.join(__dirname, '..', '..', 'mail-reader', 'data', '.mailkey');
|
|
58
|
-
|
|
59
|
-
/** scrypt is deliberately slow; cache derived keys so a reconnect is not a CPU event. */
|
|
60
|
-
const keyCache = new Map();
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Raised when a sealed value cannot be opened with the key we can derive.
|
|
64
|
-
* Carries the two sources so the caller can explain the mismatch.
|
|
65
|
-
*/
|
|
66
|
-
export class MailKeyError extends Error {
|
|
67
|
-
/**
|
|
68
|
-
* @param {string} message
|
|
69
|
-
* @param {{sealedWith?: string, availableFrom?: string}} [detail]
|
|
70
|
-
*/
|
|
71
|
-
constructor(message, detail = {}) {
|
|
72
|
-
super(message);
|
|
73
|
-
this.name = 'MailKeyError';
|
|
74
|
-
this.sealedWith = detail.sealedWith ?? null;
|
|
75
|
-
this.availableFrom = detail.availableFrom ?? null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Read, or create, the local fallback key.
|
|
81
|
-
*
|
|
82
|
-
* Written 0600 at create time rather than chmod-ed afterwards, so there is no
|
|
83
|
-
* window where the file exists and is world-readable.
|
|
84
|
-
*
|
|
85
|
-
* @returns {string} hex key material
|
|
86
|
-
*/
|
|
87
|
-
function localKeyMaterial() {
|
|
88
|
-
try {
|
|
89
|
-
const existing = fs.readFileSync(KEY_FILE, 'utf8').trim();
|
|
90
|
-
if (existing.length >= MIN_SECRET_LENGTH) return existing;
|
|
91
|
-
} catch {
|
|
92
|
-
// Missing or unreadable - fall through.
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Adopt the key from the old mail-reader-owned location before generating
|
|
96
|
-
// a new one. Generating first would strand every password already sealed
|
|
97
|
-
// with it, and the user would have no way to tell why.
|
|
98
|
-
try {
|
|
99
|
-
const legacy = fs.readFileSync(LEGACY_KEY_FILE, 'utf8').trim();
|
|
100
|
-
if (legacy.length >= MIN_SECRET_LENGTH) {
|
|
101
|
-
fs.mkdirSync(path.dirname(KEY_FILE), {recursive: true});
|
|
102
|
-
fs.writeFileSync(KEY_FILE, legacy + '\n', {encoding: 'utf8', mode: 0o600});
|
|
103
|
-
return legacy;
|
|
104
|
-
}
|
|
105
|
-
} catch {
|
|
106
|
-
// No legacy key - fall through and generate.
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const generated = randomBytes(32).toString('hex');
|
|
110
|
-
fs.mkdirSync(path.dirname(KEY_FILE), {recursive: true});
|
|
111
|
-
fs.writeFileSync(KEY_FILE, generated + '\n', {encoding: 'utf8', mode: 0o600});
|
|
112
|
-
return generated;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Resolve the key material and say where it came from.
|
|
117
|
-
*
|
|
118
|
-
* @returns {{secret: string, source: 'env-mail'|'env-instance'|'local'}}
|
|
119
|
-
*/
|
|
120
|
-
export function resolveKeySource() {
|
|
121
|
-
const mailSecret = process.env.MAIL_SECRET;
|
|
122
|
-
if (typeof mailSecret === 'string' && mailSecret.length >= MIN_SECRET_LENGTH) {
|
|
123
|
-
return {secret: mailSecret, source: 'env-mail'};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const instanceSecret = process.env.INSTANCE_SECRET;
|
|
127
|
-
if (typeof instanceSecret === 'string' && instanceSecret.length >= MIN_SECRET_LENGTH) {
|
|
128
|
-
return {secret: instanceSecret, source: 'env-instance'};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return {secret: localKeyMaterial(), source: 'local'};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Derive a 32-byte key for a given salt, memoised per (source, salt).
|
|
136
|
-
*
|
|
137
|
-
* @param {string} secret
|
|
138
|
-
* @param {string} source
|
|
139
|
-
* @param {Buffer} salt
|
|
140
|
-
* @returns {Buffer}
|
|
141
|
-
*/
|
|
142
|
-
function deriveKey(secret, source, salt) {
|
|
143
|
-
const cacheKey = source + ':' + salt.toString('base64');
|
|
144
|
-
const cached = keyCache.get(cacheKey);
|
|
145
|
-
if (cached) return cached;
|
|
146
|
-
|
|
147
|
-
const key = scryptSync(DOMAIN + ':' + secret, salt, 32);
|
|
148
|
-
keyCache.set(cacheKey, key);
|
|
149
|
-
return key;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Encrypt a string for storage.
|
|
154
|
-
*
|
|
155
|
-
* Format: `v1.<source>.<salt>.<iv>.<tag>.<ciphertext>`, each part base64url.
|
|
156
|
-
*
|
|
157
|
-
* @param {string} plaintext
|
|
158
|
-
* @returns {string} sealed token
|
|
159
|
-
*/
|
|
160
|
-
export function seal(plaintext) {
|
|
161
|
-
const {secret, source} = resolveKeySource();
|
|
162
|
-
const salt = randomBytes(16);
|
|
163
|
-
const iv = randomBytes(12);
|
|
164
|
-
const key = deriveKey(secret, source, salt);
|
|
165
|
-
|
|
166
|
-
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
167
|
-
const ciphertext = Buffer.concat([cipher.update(String(plaintext), 'utf8'), cipher.final()]);
|
|
168
|
-
const tag = cipher.getAuthTag();
|
|
169
|
-
|
|
170
|
-
return [
|
|
171
|
-
'v1',
|
|
172
|
-
source,
|
|
173
|
-
salt.toString('base64url'),
|
|
174
|
-
iv.toString('base64url'),
|
|
175
|
-
tag.toString('base64url'),
|
|
176
|
-
ciphertext.toString('base64url')
|
|
177
|
-
].join('.');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Decrypt a sealed token.
|
|
182
|
-
*
|
|
183
|
-
* @param {string} token
|
|
184
|
-
* @returns {string} plaintext
|
|
185
|
-
* @throws {MailKeyError} when the token is malformed or the key no longer matches
|
|
186
|
-
*/
|
|
187
|
-
export function open(token) {
|
|
188
|
-
if (typeof token !== 'string' || !token.startsWith('v1.')) {
|
|
189
|
-
throw new MailKeyError('Stored password is not in a recognised format.');
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const [, sealedWith, saltPart, ivPart, tagPart, ctPart] = token.split('.');
|
|
193
|
-
if (!saltPart || !ivPart || !tagPart || !ctPart) {
|
|
194
|
-
throw new MailKeyError('Stored password is malformed.');
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const {secret, source} = resolveKeySource();
|
|
198
|
-
const key = deriveKey(secret, source, Buffer.from(saltPart, 'base64url'));
|
|
199
|
-
|
|
200
|
-
try {
|
|
201
|
-
const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(ivPart, 'base64url'));
|
|
202
|
-
decipher.setAuthTag(Buffer.from(tagPart, 'base64url'));
|
|
203
|
-
return Buffer.concat([
|
|
204
|
-
decipher.update(Buffer.from(ctPart, 'base64url')),
|
|
205
|
-
decipher.final()
|
|
206
|
-
]).toString('utf8');
|
|
207
|
-
} catch {
|
|
208
|
-
const moved = sealedWith !== source;
|
|
209
|
-
throw new MailKeyError(
|
|
210
|
-
moved
|
|
211
|
-
? `This password was saved using the ${describeSource(sealedWith)} and the server is now using the ${describeSource(source)}. Re-enter the password to save it against the new key.`
|
|
212
|
-
: 'Stored password could not be decrypted - the encryption key has changed. Re-enter the password.',
|
|
213
|
-
{sealedWith, availableFrom: source}
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Human-readable name for a key source, for error messages.
|
|
220
|
-
*
|
|
221
|
-
* @param {string} source
|
|
222
|
-
* @returns {string}
|
|
223
|
-
*/
|
|
224
|
-
export function describeSource(source) {
|
|
225
|
-
if (source === 'env-mail') return 'MAIL_SECRET environment variable';
|
|
226
|
-
if (source === 'env-instance') return 'INSTANCE_SECRET environment variable';
|
|
227
|
-
if (source === 'local') return 'plugin-local key file';
|
|
228
|
-
return 'unknown key source';
|
|
229
|
-
}
|
|
15
|
+
export {MailKeyError, describeSource} from './secretProvider.js';
|
|
16
|
+
export const {seal, open, resolveKeySource} = secrets;
|