emailengine-app 2.79.0 → 2.79.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/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +79 -0
- package/bin/emailengine.js +8 -1
- package/config/default.toml +6 -0
- package/data/google-crawlers.json +1 -1
- package/lib/account.js +137 -19
- package/lib/api-routes/account-routes.js +13 -1
- package/lib/api-routes/index.js +12 -0
- package/lib/api-routes/mailbox-routes.js +6 -0
- package/lib/api-routes/mcp-oauth-routes.js +259 -0
- package/lib/api-routes/mcp-routes.js +172 -0
- package/lib/api-routes/message-routes.js +125 -4
- package/lib/api-routes/outbox-routes.js +7 -0
- package/lib/api-routes/permission-map.js +101 -1
- package/lib/api-routes/route-helpers.js +87 -4
- package/lib/api-routes/route-metadata.js +24 -4
- package/lib/api-routes/settings-routes.js +37 -14
- package/lib/api-routes/submit-routes.js +41 -0
- package/lib/api-routes/template-routes.js +7 -0
- package/lib/api-routes/token-routes.js +292 -122
- package/lib/arf-detect.js +17 -4
- package/lib/bounce-detect.js +28 -17
- package/lib/consts.js +36 -1
- package/lib/db.js +6 -0
- package/lib/email-client/base-client.js +102 -117
- package/lib/email-client/gmail-client.js +91 -29
- package/lib/email-client/imap/mailbox.js +175 -101
- package/lib/email-client/imap-client.js +49 -2
- package/lib/email-client/outlook-client.js +20 -24
- package/lib/enum-descriptions.js +2 -1
- package/lib/field-usage.js +17 -1
- package/lib/lua/h-set-new-mark.lua +36 -0
- package/lib/lua/s-list-accounts.lua +5 -2
- package/lib/mcp/index.js +29 -0
- package/lib/mcp/inject.js +70 -0
- package/lib/mcp/json-schema.js +120 -0
- package/lib/mcp/listen.js +171 -0
- package/lib/mcp/oauth.js +324 -0
- package/lib/mcp/protocol.js +489 -0
- package/lib/mcp/resources.js +139 -0
- package/lib/mcp/tools.js +655 -0
- package/lib/message-port-stream.js +84 -1
- package/lib/oauth2-apps.js +84 -15
- package/lib/openapi/build-document.js +5 -1
- package/lib/openapi/index.js +3 -1
- package/lib/response-stream.js +52 -21
- package/lib/routes-ui.js +4 -0
- package/lib/schemas.js +26 -5
- package/lib/settings.js +23 -9
- package/lib/token-audit-log.js +32 -9
- package/lib/token-permission-view.js +104 -8
- package/lib/tokens.js +194 -68
- package/lib/tools.js +96 -0
- package/lib/ui-routes/account-routes.js +55 -6
- package/lib/ui-routes/admin-config-routes.js +114 -1
- package/lib/ui-routes/admin-entities-routes.js +103 -19
- package/lib/ui-routes/auth-routes.js +13 -0
- package/lib/ui-routes/mcp-consent-routes.js +330 -0
- package/lib/ui-routes/oauth-config-routes.js +7 -7
- package/lib/ui-routes/route-helpers.js +143 -1
- package/lib/ui-routes/suppression-list-routes.js +14 -9
- package/lib/utils/decode-headers.js +42 -0
- package/lib/utils/header-map.js +48 -0
- package/lib/utils/network.js +9 -1
- package/lib/utils/unsafe-key.js +59 -0
- package/lib/web-safe-html.js +61 -0
- package/lib/webhook-routing.js +6 -2
- package/lib/webhooks.js +31 -8
- package/package.json +9 -8
- package/sbom.json +1 -1
- package/scripts/README.md +25 -0
- package/scripts/build-icon.js +77 -0
- package/server.js +39 -17
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +98 -0
- package/static/emailengine.ico +0 -0
- package/static/js/ui.js +465 -1
- package/static/licenses.html +42 -22
- package/translations/messages.pot +57 -57
- package/views/accounts/account.hbs +8 -8
- package/views/accounts/browse.hbs +1 -1
- package/views/accounts/edit.hbs +2 -2
- package/views/config/mcp.hbs +361 -0
- package/views/config/oauth/index.hbs +7 -0
- package/views/gateways/edit.hbs +1 -1
- package/views/gateways/gateway.hbs +2 -2
- package/views/gateways/index.hbs +7 -7
- package/views/layout/prompt.hbs +62 -0
- package/views/mcp/authorize.hbs +136 -0
- package/views/partials/accounts_table.hbs +7 -7
- package/views/partials/head_assets.hbs +3 -3
- package/views/partials/mcp_access_levels.hbs +13 -0
- package/views/partials/mcp_account_limit.hbs +14 -0
- package/views/partials/mcp_tool_count.hbs +19 -0
- package/views/partials/side_menu.hbs +1 -0
- package/views/partials/ui/account-picker.hbs +59 -0
- package/views/partials/ui/code-block.hbs +26 -0
- package/views/partials/ui/menu-item.hbs +5 -0
- package/views/partials/ui/radio.hbs +41 -0
- package/views/partials/ui/tab.hbs +15 -10
- package/views/partials/ui/tabs.hbs +8 -5
- package/views/suppression-lists/list.hbs +1 -1
- package/views/templates/index.hbs +2 -2
- package/views/templates/template.hbs +5 -4
- package/views/tokens/index.hbs +30 -4
- package/views/tokens/new.hbs +517 -239
- package/winconf.js +148 -48
- package/workers/api.js +239 -95
- package/workers/imap.js +65 -72
- package/workers/webhooks.js +21 -1
package/lib/mcp/oauth.js
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The minimal OAuth 2.1 authorization server behind the MCP endpoint.
|
|
4
|
+
//
|
|
5
|
+
// It exists for exactly one flow: an MCP client that cannot be configured with a static access
|
|
6
|
+
// token (claude.ai web connectors and similar) discovers this server through the protected
|
|
7
|
+
// resource metadata, registers itself as a public client, sends the operator's browser to the
|
|
8
|
+
// admin consent page, and exchanges the resulting code for an EmailEngine access token with the
|
|
9
|
+
// `mcp` scope. Every credential this issues is an ordinary access token: visible on the admin
|
|
10
|
+
// tokens page, revocable there, and bounded by SURFACE_GRANTS.mcp like any other mcp-scoped
|
|
11
|
+
// token. There are no refresh tokens and no expiry by default - revocation is the lifecycle.
|
|
12
|
+
//
|
|
13
|
+
// What is implemented, spec-wise: OAuth 2.0 Dynamic Client Registration (RFC 7591, open
|
|
14
|
+
// registration for public clients only, rate limited per IP), authorization code + PKCE S256
|
|
15
|
+
// (OAuth 2.1 - PKCE is mandatory, `plain` is not accepted), single-use codes with a short TTL,
|
|
16
|
+
// exact-match redirect URIs, RFC 8707 resource validation against this instance's canonical
|
|
17
|
+
// MCP resource, and the RFC 9207 `iss` authorization-response parameter. Client secrets do not
|
|
18
|
+
// exist here: `token_endpoint_auth_method` is always `none`, which is what PKCE is for.
|
|
19
|
+
|
|
20
|
+
const crypto = require('crypto');
|
|
21
|
+
|
|
22
|
+
const { redis } = require('../db');
|
|
23
|
+
const { REDIS_PREFIX } = require('../consts');
|
|
24
|
+
const msgpack = require('../msgpack');
|
|
25
|
+
const tokens = require('../tokens');
|
|
26
|
+
const settings = require('../settings');
|
|
27
|
+
const { constantTimeEqual } = require('../tools');
|
|
28
|
+
const { mcpFeatureEnabled } = require('./index');
|
|
29
|
+
const { LOOPBACK_HOSTNAMES } = require('./protocol');
|
|
30
|
+
|
|
31
|
+
// Registered clients and pending codes are throwaway state, not configuration: an unused client
|
|
32
|
+
// registration disappears after this long, and re-registering is one unauthenticated call. The
|
|
33
|
+
// TTL is refreshed whenever the client starts an authorization, so an actively used client id
|
|
34
|
+
// stays stable.
|
|
35
|
+
const CLIENT_TTL = 30 * 24 * 3600;
|
|
36
|
+
|
|
37
|
+
// An authorization code is redeemed within seconds in a working flow; ten minutes is the
|
|
38
|
+
// conventional upper bound.
|
|
39
|
+
const CODE_TTL = 10 * 60;
|
|
40
|
+
|
|
41
|
+
// How many redirect URIs one registration may carry. Consumed by the registration route's joi
|
|
42
|
+
// schema too, so the validation layer and the policy here cannot drift apart.
|
|
43
|
+
const MAX_REDIRECT_URIS = 10;
|
|
44
|
+
|
|
45
|
+
const clientKey = clientId => `${REDIS_PREFIX}mcp:oauth:client:${clientId}`;
|
|
46
|
+
const codeKey = code => `${REDIS_PREFIX}mcp:oauth:code:${code}`;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether the OAuth flow is available at all: the MCP endpoint must be live, the feature
|
|
50
|
+
* enabled, and a serviceUrl configured - the metadata documents have to name a canonical origin,
|
|
51
|
+
* which a request-derived address cannot provide.
|
|
52
|
+
*
|
|
53
|
+
* @returns {Promise<String|false>} the canonical origin, or false
|
|
54
|
+
*/
|
|
55
|
+
async function oauthOrigin() {
|
|
56
|
+
if (!mcpFeatureEnabled) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// One HMGET for the three settings the answer depends on, not three serialized reads - this
|
|
61
|
+
// runs on every discovery request and on every /mcp 401
|
|
62
|
+
const stored = await settings.getMulti('mcpEnabled', 'mcpOAuthEnabled', 'serviceUrl');
|
|
63
|
+
if (!stored.mcpEnabled || !stored.mcpOAuthEnabled || !stored.serviceUrl) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
return new URL(stored.serviceUrl).origin;
|
|
69
|
+
} catch (err) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function mcpResource(origin) {
|
|
75
|
+
return `${origin}/mcp`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A redirect URI a public MCP client may register: https anywhere, http only on the loopback
|
|
80
|
+
* (native clients binding a local callback port), or a private-use scheme (desktop apps).
|
|
81
|
+
* Schemes that execute in a browser context are refused outright.
|
|
82
|
+
*/
|
|
83
|
+
function isAcceptableRedirectUri(value) {
|
|
84
|
+
if (typeof value !== 'string' || value.length > 2048) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let url;
|
|
89
|
+
try {
|
|
90
|
+
url = new URL(value);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (url.hash) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
switch (url.protocol) {
|
|
100
|
+
case 'https:':
|
|
101
|
+
return true;
|
|
102
|
+
case 'http:':
|
|
103
|
+
return LOOPBACK_HOSTNAMES.includes(url.hostname);
|
|
104
|
+
case 'javascript:':
|
|
105
|
+
case 'data:':
|
|
106
|
+
case 'file:':
|
|
107
|
+
case 'blob:':
|
|
108
|
+
case 'vbscript:':
|
|
109
|
+
return false;
|
|
110
|
+
default:
|
|
111
|
+
// Anything else is treated as a private-use scheme (com.example.app:/callback,
|
|
112
|
+
// RFC 8252 territory). The URL parser already guarantees scheme syntax, so beyond
|
|
113
|
+
// the browser-context schemes named above there is nothing left to filter.
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Registers a public client (RFC 7591).
|
|
120
|
+
*
|
|
121
|
+
* @param {Object} opts
|
|
122
|
+
* @param {Array} opts.redirectUris
|
|
123
|
+
* @param {String} [opts.clientName]
|
|
124
|
+
* @returns {Promise<Object>} the stored client record
|
|
125
|
+
*/
|
|
126
|
+
async function registerClient({ redirectUris, clientName }) {
|
|
127
|
+
const uris = [].concat(redirectUris || []);
|
|
128
|
+
|
|
129
|
+
if (!uris.length || uris.length > MAX_REDIRECT_URIS) {
|
|
130
|
+
throw oauthFailure('invalid_redirect_uri', `redirect_uris must list 1-${MAX_REDIRECT_URIS} redirect URIs`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Any unacceptable URI fails the whole registration (RFC 7591 3.2.2). Silently registering
|
|
134
|
+
// the acceptable subset was tried first, and it just moved the failure to authorize time,
|
|
135
|
+
// where the client sees a generic error page instead of which URI was refused and why.
|
|
136
|
+
const refused = uris.find(uri => !isAcceptableRedirectUri(uri));
|
|
137
|
+
if (refused !== undefined) {
|
|
138
|
+
throw oauthFailure(
|
|
139
|
+
'invalid_redirect_uri',
|
|
140
|
+
`Unacceptable redirect URI (must be https, loopback http, or a private-use scheme): ${String(refused).slice(0, 256)}`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const client = {
|
|
145
|
+
client_id: crypto.randomBytes(16).toString('hex'),
|
|
146
|
+
client_id_issued_at: Math.floor(Date.now() / 1000),
|
|
147
|
+
redirect_uris: uris,
|
|
148
|
+
token_endpoint_auth_method: 'none',
|
|
149
|
+
grant_types: ['authorization_code'],
|
|
150
|
+
response_types: ['code'],
|
|
151
|
+
client_name: (clientName || '').toString().slice(0, 256) || undefined
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
await redis.set(clientKey(client.client_id), msgpack.encode(client), 'EX', CLIENT_TTL);
|
|
155
|
+
|
|
156
|
+
return client;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function getClient(clientId) {
|
|
160
|
+
if (!/^[0-9a-f]{32}$/.test(clientId || '')) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const encoded = await redis.getBuffer(clientKey(clientId));
|
|
165
|
+
if (!encoded) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
return msgpack.decode(encoded);
|
|
171
|
+
} catch (err) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// A client that reached the consent page is in active use; keep its registration alive
|
|
177
|
+
async function touchClient(clientId) {
|
|
178
|
+
await redis.expire(clientKey(clientId), CLIENT_TTL);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Trailing slashes carry no meaning for a resource identifier, so every comparison below runs
|
|
182
|
+
// on the stripped form
|
|
183
|
+
function normalizeResource(resource) {
|
|
184
|
+
return typeof resource === 'string' ? resource.replace(/\/+$/, '') : resource;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Validates the resource indicator (RFC 8707) against this instance's canonical MCP resource.
|
|
189
|
+
* Absent is tolerated for interoperability; a present value has to name this instance.
|
|
190
|
+
*/
|
|
191
|
+
function isAcceptableResource(resource, origin) {
|
|
192
|
+
if (resource === undefined || resource === null || resource === '') {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (typeof resource !== 'string') {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const normalized = normalizeResource(resource);
|
|
200
|
+
|
|
201
|
+
return normalized === mcpResource(origin) || normalized === origin;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Mints a single-use authorization code after the admin approved the consent page.
|
|
206
|
+
*
|
|
207
|
+
* @param {Object} opts - clientId, redirectUri, codeChallenge, resource, account (optional
|
|
208
|
+
* binding for the token), description (for the admin tokens listing)
|
|
209
|
+
* @returns {Promise<String>} the code
|
|
210
|
+
*/
|
|
211
|
+
async function createAuthorizationCode(opts) {
|
|
212
|
+
const code = crypto.randomBytes(32).toString('base64url');
|
|
213
|
+
|
|
214
|
+
const record = {
|
|
215
|
+
clientId: opts.clientId,
|
|
216
|
+
redirectUri: opts.redirectUri,
|
|
217
|
+
codeChallenge: opts.codeChallenge,
|
|
218
|
+
resource: opts.resource || null,
|
|
219
|
+
account: opts.account || null,
|
|
220
|
+
// The narrowing the operator approved, applied to the token this code mints. Absent
|
|
221
|
+
// means unnarrowed within the mcp scope, the same shape the admin generator posts.
|
|
222
|
+
permissions: opts.permissions || null,
|
|
223
|
+
description: opts.description,
|
|
224
|
+
created: Date.now()
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
await redis.set(codeKey(code), msgpack.encode(record), 'EX', CODE_TTL);
|
|
228
|
+
|
|
229
|
+
return code;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function oauthFailure(error, description) {
|
|
233
|
+
const err = new Error(description);
|
|
234
|
+
err.oauthError = error;
|
|
235
|
+
return err;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function verifyPkce(codeVerifier, codeChallenge) {
|
|
239
|
+
// RFC 7636 verifier alphabet and length, then S256
|
|
240
|
+
if (typeof codeVerifier !== 'string' || codeVerifier.length < 43 || codeVerifier.length > 128 || !/^[A-Za-z0-9\-._~]+$/.test(codeVerifier)) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const digest = crypto.createHash('sha256').update(codeVerifier).digest('base64url');
|
|
245
|
+
|
|
246
|
+
return constantTimeEqual(digest, codeChallenge);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The token endpoint's whole job: redeem a code for an access token.
|
|
251
|
+
*
|
|
252
|
+
* @param {Object} opts - code, clientId, redirectUri, codeVerifier, resource, origin, ip
|
|
253
|
+
* @returns {Promise<Object>} OAuth token response body
|
|
254
|
+
*/
|
|
255
|
+
async function redeemAuthorizationCode({ code, clientId, redirectUri, codeVerifier, resource, origin, ip }) {
|
|
256
|
+
if (typeof code !== 'string' || code.length > 256) {
|
|
257
|
+
throw oauthFailure('invalid_grant', 'Unknown authorization code');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Single use by construction: whoever gets the record, gets it deleted
|
|
261
|
+
const encoded = await redis.getdelBuffer(codeKey(code));
|
|
262
|
+
if (!encoded) {
|
|
263
|
+
throw oauthFailure('invalid_grant', 'Unknown or expired authorization code');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let record;
|
|
267
|
+
try {
|
|
268
|
+
record = msgpack.decode(encoded);
|
|
269
|
+
} catch (err) {
|
|
270
|
+
throw oauthFailure('invalid_grant', 'Unknown or expired authorization code');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (record.clientId !== clientId) {
|
|
274
|
+
throw oauthFailure('invalid_grant', 'Authorization code was issued to another client');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (record.redirectUri !== redirectUri) {
|
|
278
|
+
throw oauthFailure('invalid_grant', 'redirect_uri does not match the authorization request');
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (!verifyPkce(codeVerifier, record.codeChallenge)) {
|
|
282
|
+
throw oauthFailure('invalid_grant', 'PKCE verification failed');
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!isAcceptableResource(resource, origin)) {
|
|
286
|
+
throw oauthFailure('invalid_target', 'The resource parameter does not name this server');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// RFC 8707: a code issued for a resource named at authorization time is redeemed for that
|
|
290
|
+
// same resource. Absence stays tolerated on either side for interoperability - only one
|
|
291
|
+
// resource exists on this server, so this catches client confusion, not a privilege step.
|
|
292
|
+
if (resource && record.resource && normalizeResource(resource) !== normalizeResource(record.resource)) {
|
|
293
|
+
throw oauthFailure('invalid_target', 'The resource parameter does not match the authorization request');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const token = await tokens.provision({
|
|
297
|
+
account: record.account || undefined,
|
|
298
|
+
description: record.description,
|
|
299
|
+
scopes: ['mcp'],
|
|
300
|
+
permissions: record.permissions || undefined,
|
|
301
|
+
ip,
|
|
302
|
+
metadata: JSON.stringify({ issuer: 'mcp-oauth', client: record.clientId })
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
access_token: token,
|
|
307
|
+
token_type: 'Bearer',
|
|
308
|
+
scope: 'mcp'
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
module.exports = {
|
|
313
|
+
oauthOrigin,
|
|
314
|
+
mcpResource,
|
|
315
|
+
registerClient,
|
|
316
|
+
getClient,
|
|
317
|
+
touchClient,
|
|
318
|
+
isAcceptableRedirectUri,
|
|
319
|
+
isAcceptableResource,
|
|
320
|
+
createAuthorizationCode,
|
|
321
|
+
redeemAuthorizationCode,
|
|
322
|
+
verifyPkce,
|
|
323
|
+
MAX_REDIRECT_URIS
|
|
324
|
+
};
|