emailengine-app 2.79.9 → 2.80.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.
Files changed (146) hide show
  1. package/.release-please-manifest.json +1 -1
  2. package/CHANGELOG.md +73 -0
  3. package/README.md +6 -0
  4. package/config/default.toml +5 -1
  5. package/data/google-crawlers.json +1 -1
  6. package/encrypt.js +43 -0
  7. package/lib/account/account-state.js +62 -0
  8. package/lib/account.js +34 -2
  9. package/lib/api-routes/account-routes.js +101 -1
  10. package/lib/api-routes/blocklist-routes.js +24 -0
  11. package/lib/api-routes/delivery-test-routes.js +6 -0
  12. package/lib/api-routes/gateway-routes.js +30 -0
  13. package/lib/api-routes/index.js +1 -1
  14. package/lib/api-routes/license-routes.js +10 -0
  15. package/lib/api-routes/mcp-oauth-routes.js +3 -3
  16. package/lib/api-routes/mcp-routes.js +39 -21
  17. package/lib/api-routes/oauth2-app-routes.js +38 -1
  18. package/lib/api-routes/outbox-routes.js +6 -0
  19. package/lib/api-routes/permission-map.js +208 -43
  20. package/lib/api-routes/pubsub-routes.js +8 -0
  21. package/lib/api-routes/route-helpers.js +58 -11
  22. package/lib/api-routes/settings-routes.js +67 -6
  23. package/lib/api-routes/stats-routes.js +6 -0
  24. package/lib/api-routes/template-routes.js +21 -0
  25. package/lib/api-routes/token-routes.js +34 -2
  26. package/lib/api-routes/webhook-route-routes.js +13 -0
  27. package/lib/autodetect-imap-settings.js +349 -47
  28. package/lib/cert-handler.js +62 -0
  29. package/lib/consts.js +50 -6
  30. package/lib/db.js +12 -0
  31. package/lib/egress-fetch.js +21 -9
  32. package/lib/email-client/base-client.js +238 -67
  33. package/lib/email-client/credential-errors.js +122 -0
  34. package/lib/email-client/gmail/gmail-api.js +2 -1
  35. package/lib/email-client/gmail-client.js +70 -64
  36. package/lib/email-client/imap/connection-errors.js +52 -0
  37. package/lib/email-client/imap/listing-diff.js +47 -6
  38. package/lib/email-client/imap/login-failure.js +52 -0
  39. package/lib/email-client/imap/mailbox.js +457 -379
  40. package/lib/email-client/imap/subconnection.js +17 -12
  41. package/lib/email-client/imap/sync-operations.js +46 -59
  42. package/lib/email-client/imap-client.js +139 -101
  43. package/lib/email-client/message-builder.js +11 -11
  44. package/lib/email-client/outlook/graph-api.js +2 -1
  45. package/lib/email-client/outlook-client.js +219 -106
  46. package/lib/enum-descriptions.js +12 -2
  47. package/lib/imap-proxy-auth.js +58 -3
  48. package/lib/imapproxy/imap-server.js +53 -62
  49. package/lib/lua/h-del-if-equals.lua +39 -0
  50. package/lib/lua/h-set-if-equals.lua +39 -0
  51. package/lib/mcp/oauth.js +46 -8
  52. package/lib/mcp/protocol.js +78 -35
  53. package/lib/mcp/tools.js +53 -17
  54. package/lib/privileged-settings.js +147 -0
  55. package/lib/routes-ui.js +4 -34
  56. package/lib/schemas.js +92 -16
  57. package/lib/security-headers.js +15 -0
  58. package/lib/settings.js +13 -0
  59. package/lib/tls/catalog.js +428 -0
  60. package/lib/tls/context.js +302 -0
  61. package/lib/tls/der.js +223 -0
  62. package/lib/tls/listeners.js +62 -0
  63. package/lib/tls/provision.js +555 -0
  64. package/lib/tls/self-signed.js +282 -0
  65. package/lib/tls/status.js +480 -0
  66. package/lib/tls/store.js +667 -0
  67. package/lib/token-permission-view.js +252 -102
  68. package/lib/token-permissions.js +176 -26
  69. package/lib/tokens.js +7 -6
  70. package/lib/tools.js +73 -27
  71. package/lib/ui-routes/account-routes.js +43 -30
  72. package/lib/ui-routes/admin-config-routes.js +63 -22
  73. package/lib/ui-routes/admin-entities-routes.js +23 -13
  74. package/lib/ui-routes/auth-routes.js +16 -1
  75. package/lib/ui-routes/internals-routes.js +3 -5
  76. package/lib/ui-routes/mcp-consent-routes.js +72 -31
  77. package/lib/ui-routes/network-config-routes.js +54 -45
  78. package/lib/ui-routes/tls-config-routes.js +628 -0
  79. package/lib/utils/mailbox-path.js +51 -0
  80. package/lib/utils/severity.js +31 -0
  81. package/package.json +12 -12
  82. package/sbom.json +1 -1
  83. package/server.js +23 -1
  84. package/static/css/flyonui.css +1 -1
  85. package/static/css/src/app.tailwind.css +11 -0
  86. package/static/js/app.js +34 -0
  87. package/static/js/public.js +6 -2
  88. package/static/js/ui.js +97 -61
  89. package/static/licenses.html +341 -1252
  90. package/translations/messages.pot +62 -56
  91. package/views/account/login-redirect.hbs +22 -0
  92. package/views/account/passkeys.hbs +2 -2
  93. package/views/accounts/account.hbs +2 -2
  94. package/views/accounts/browse.hbs +1 -1
  95. package/views/accounts/edit.hbs +1 -1
  96. package/views/accounts/register/imap-server.hbs +1 -1
  97. package/views/config/ai.hbs +1 -1
  98. package/views/config/branding.hbs +2 -2
  99. package/views/config/document-store/chat.hbs +1 -1
  100. package/views/config/document-store/index.hbs +1 -1
  101. package/views/config/imap-proxy.hbs +1 -4
  102. package/views/config/license.hbs +1 -1
  103. package/views/config/mcp.hbs +88 -52
  104. package/views/config/network.hbs +1 -1
  105. package/views/config/oauth/app.hbs +2 -2
  106. package/views/config/service.hbs +1 -1
  107. package/views/config/smtp.hbs +1 -4
  108. package/views/config/tls-upload.hbs +81 -0
  109. package/views/config/tls.hbs +593 -0
  110. package/views/config/webhooks.hbs +2 -2
  111. package/views/internals/index.hbs +1 -1
  112. package/views/layout/app.hbs +6 -6
  113. package/views/layout/login.hbs +5 -5
  114. package/views/layout/main.hbs +2 -2
  115. package/views/layout/prompt.hbs +3 -3
  116. package/views/layout/public.hbs +1 -1
  117. package/views/mcp/authorize.hbs +47 -45
  118. package/views/partials/ace_assets.hbs +1 -1
  119. package/views/partials/add_account_modal.hbs +1 -1
  120. package/views/partials/filter_editor_js.hbs +2 -2
  121. package/views/partials/gateway_js.hbs +1 -1
  122. package/views/partials/mcp_access_levels.hbs +16 -9
  123. package/views/partials/mcp_tool_count.hbs +15 -14
  124. package/views/partials/oauth_form.hbs +1 -1
  125. package/views/partials/public_theme_init.hbs +1 -1
  126. package/views/partials/side_menu.hbs +1 -0
  127. package/views/partials/template_form.hbs +1 -1
  128. package/views/partials/theme_init.hbs +1 -1
  129. package/views/partials/tls_security_card.hbs +25 -53
  130. package/views/partials/ui/alert-banner.hbs +3 -2
  131. package/views/partials/ui/badge.hbs +6 -2
  132. package/views/partials/ui/btn.hbs +5 -2
  133. package/views/partials/webhooks_editor_functions.hbs +1 -1
  134. package/views/reference/index.hbs +1 -1
  135. package/views/reference/redirect.hbs +1 -1
  136. package/views/reference/tag.hbs +1 -1
  137. package/views/reference/token.hbs +1 -1
  138. package/views/templates/template.hbs +1 -1
  139. package/views/tokens/new.hbs +175 -74
  140. package/views/unsubscribe.hbs +1 -1
  141. package/views/webhooks/webhook.hbs +1 -1
  142. package/workers/api.js +102 -142
  143. package/workers/imap-proxy.js +4 -1
  144. package/workers/smtp.js +51 -44
  145. package/views/partials/tls_security_js.hbs +0 -64
  146. package/views/partials/tls_security_modal.hbs +0 -22
@@ -100,16 +100,33 @@
100
100
  <p class="helper-text ms-7">Read the Prometheus metrics endpoint and nothing else.</p>
101
101
  </li>
102
102
 
103
+ {{!-- Two MCP scopes, management first: the endpoint is one door with two tool
104
+ sets behind it, and each scope opens one of them. Both rows carry the Beta
105
+ badge the MCP config page carries. --}}
106
+ <li class="p-4">
107
+ <div class="flex items-center gap-2">
108
+ <input type="checkbox" class="checkbox checkbox-sm scope-entry scope-list" id="scopesMcpManage"
109
+ name="scopesMcpManage" data-scope="mcp-manage" {{#if values.scopesMcpManage}}checked{{/if}} />
110
+ <label class="label-text grow p-0" for="scopesMcpManage">MCP management
111
+ {{> ui/badge label="Beta" variant="info" soft=true class="badge-sm align-middle" icon="" elId=""}}</label>
112
+ <div class="text-base-content/60 code-link">["mcp-manage"]</div>
113
+ </div>
114
+ <p class="helper-text ms-7">Let an AI agent manage this instance through the MCP tools at
115
+ <code>/mcp</code>: accounts, settings, OAuth2 applications, gateways, tokens and the license.
116
+ This is not REST API access - the same token is refused by <code>/v1</code>.</p>
117
+ </li>
118
+
103
119
  <li class="p-4">
104
120
  <div class="flex items-center gap-2">
105
121
  <input type="checkbox" class="checkbox checkbox-sm scope-entry scope-list" id="scopesMcp"
106
122
  name="scopesMcp" data-scope="mcp" {{#if values.scopesMcp}}checked{{/if}} />
107
- <label class="label-text grow p-0" for="scopesMcp">MCP
123
+ <label class="label-text grow p-0" for="scopesMcp">MCP mail
108
124
  {{> ui/badge label="Beta" variant="info" soft=true class="badge-sm align-middle" icon="" elId=""}}</label>
109
125
  <div class="text-base-content/60 code-link">["mcp"]</div>
110
126
  </div>
111
- <p class="helper-text ms-7">Let an AI agent call MCP tools at <code>/mcp</code>. This is not REST
112
- API access - the same token is refused by <code>/v1</code>.</p>
127
+ <p class="helper-text ms-7">Let an AI agent work with mailbox contents through the MCP tools at
128
+ <code>/mcp</code>: list, read, organize and send mail. This is not REST API access - the same
129
+ token is refused by <code>/v1</code>.</p>
113
130
  {{!-- The endpoint is off by default, so an operator can otherwise mint a
114
131
  credential for something that is not serving and only find out in the
115
132
  client. Both halves of the gate are folded into mcpReady. Revealed by
@@ -138,7 +155,7 @@
138
155
  The record itself stays single: one `permissions` object narrows whatever surface the
139
156
  request arrives on. That is why this is one editor with two modes rather than a tab per
140
157
  scope - separate panes would promise two records that cannot exist. --}}
141
- <div id="accessCard" class="hidden" data-surfaces="{{permissionSurfacesJson}}" data-mcp-presets="{{json mcpAccessPresets}}">
158
+ <div id="accessCard" class="hidden" data-surfaces="{{permissionSurfacesJson}}" data-mcp-sections="{{json mcpSections}}">
142
159
  {{#> ui/card bodyClass="space-y-4"}}
143
160
 
144
161
  <div>
@@ -146,19 +163,29 @@
146
163
  <p id="accessAppliesTo" class="text-base-content/60 text-sm"></p>
147
164
  </div>
148
165
 
149
- {{!-- MCP-only mode. The three levels are the ones the OAuth consent prompt and the MCP config
150
- page already offer, from the same table through the same partial, because the same
151
- credential minted from three places must not mean three different things. A 52-cell
152
- matrix is not how anyone provisions an agent, so it is the fourth option rather than
153
- the only one - and unlike the API editor there is no "restrict?" toggle: read-only is
166
+ {{!-- MCP-only mode. One level per MCP scope, shown while that scope is ticked above: the
167
+ same sections and levels the OAuth consent prompt and the MCP config page offer, from
168
+ the same table through the same partial, because the same credential minted from
169
+ three places must not mean three different things. A 72-cell matrix is not how
170
+ anyone provisions an agent, so it is the opt-in behind the levels rather than the way
171
+ in - and unlike the API editor there is no "restrict?" toggle: the narrowest level is
154
172
  the default, because an agent credential that starts unrestricted is the wrong way
155
173
  round. --}}
156
174
  {{!-- Labelled by the card heading rather than carrying a legend of its own, which would
157
175
  repeat "Access level" directly under itself --}}
158
- <fieldset id="mcpLevelBlock" class="hidden space-y-2" aria-labelledby="accessCardTitle">
159
- {{> mcp_access_levels name="mcpAccess" idPrefix="mcpAccess_" selected="read"}}
160
- {{> ui/radio id="mcpAccess_custom" name="mcpAccess" value="custom" label="Custom permissions"
161
- hint="Choose the exact actions and sections yourself, starting from the level selected above."}}
176
+ <fieldset id="mcpLevelBlock" class="hidden space-y-4" aria-labelledby="accessCardTitle">
177
+ {{!-- One block per section, shown while that section's scope is ticked above; the radio
178
+ group is named mcpLevel_<key>, which is what the script and ui.js read back --}}
179
+ {{#each mcpSections}}
180
+ <div class="space-y-2 mcp-section-block" data-mcp-scope="{{scope}}">
181
+ <div class="label-text font-medium">{{label}}
182
+ <span class="text-base-content/60 code-link font-normal">["{{scope}}"]</span></div>
183
+ {{> mcp_access_levels section=this name=(concat "mcpLevel_" @key) idPrefix=(concat "mcpLevel_" @key) selected=levels.[0].value}}
184
+ </div>
185
+ {{/each}}
186
+
187
+ {{> ui/checkbox id="mcpCustom" label="Custom permissions" noName=true
188
+ hint="Choose the exact actions and sections yourself, starting from the levels selected above."}}
162
189
 
163
190
  {{!-- At the decision point, as on the MCP config page: the level chosen here is what
164
191
  decides whether instructions hidden inside received mail can act --}}
@@ -255,8 +282,9 @@
255
282
  <span class="icon-[tabler--lock] size-5 shrink-0"></span>
256
283
  <span>
257
284
  Some endpoints are never available to a restricted token, whatever you select here:
258
- settings, OAuth2 applications, licensing, access tokens and stored account
259
- credentials. A token that could reach those could grant itself more.
285
+ creating access tokens, reading an account's live OAuth2 credential, and the settings
286
+ that would let a token widen itself ({{privilegedSettingsSummary}}). A token that
287
+ could reach those could grant itself more.
260
288
  </span>
261
289
  </div>
262
290
  </div>
@@ -294,7 +322,7 @@
294
322
  </div>
295
323
  {{/ui/modal}}
296
324
 
297
- <script nonce="{{cspNonce}}">
325
+ <script data-cfasync="false" nonce="{{cspNonce}}">
298
326
  document.addEventListener('DOMContentLoaded', () => {
299
327
 
300
328
  let showTokenValueElm = document.getElementById('showTokenValue');
@@ -334,13 +362,32 @@
334
362
  };
335
363
  }
336
364
 
337
- let mcpAccessPresets = JSON.parse(accessCardElm.dataset.mcpPresets || '{}');
338
- // What the mcp scope can reach at all, which is also what the "Full access" level means:
339
- // that level stores no record, so the surface grant list is its own description.
340
- let mcpCeiling = surfaceRequirements.mcp || { actions: [], groups: [], grants: [] };
365
+ // The two MCP access sections and what each level mints, from the same table the consent
366
+ // prompt and the MCP config page mint from
367
+ let mcpSections = JSON.parse(accessCardElm.dataset.mcpSections || '{}');
368
+ let mcpCustomElm = document.getElementById('mcpCustom');
369
+
370
+ let mcpScopes = Object.values(mcpSections).map(section => section.scope);
341
371
 
342
372
  let scopeElms = Array.from(document.querySelectorAll('.scope-entry'));
343
373
  let selectedScopes = () => scopeElms.filter(elm => elm.checked && !elm.disabled).map(elm => elm.dataset.scope);
374
+ let selectedMcpScopes = () => selectedScopes().filter(scope => mcpScopes.includes(scope));
375
+
376
+ // What the ticked MCP scopes can reach at all: the union of their surface tables. Read per
377
+ // call because it follows the scopes - a token holding only the management scope has no
378
+ // business ticking Messages in the custom editor.
379
+ let mcpCeilingFor = scopes => {
380
+ let ceiling = { actions: [], groups: [] };
381
+ for (let scope of scopes) {
382
+ let need = surfaceRequirements[scope];
383
+ if (!need) {
384
+ continue;
385
+ }
386
+ ceiling.actions = ceiling.actions.concat(need.actions.filter(value => !ceiling.actions.includes(value)));
387
+ ceiling.groups = ceiling.groups.concat(need.groups.filter(value => !ceiling.groups.includes(value)));
388
+ }
389
+ return ceiling;
390
+ };
344
391
 
345
392
  // Permissions are resolved per endpoint on the REST API and per tool call on the MCP
346
393
  // endpoint, so both of those scopes can carry a narrowing. The login-time scopes (SMTP,
@@ -353,7 +400,7 @@
353
400
  // be greyed while this is open. That reads correctly: API access is granted by "All", and
354
401
  // this is how to narrow it.
355
402
  let hasApiScope = () => selectedScopes().some(scope => ['*', 'api'].includes(scope));
356
- let hasMcpScope = () => selectedScopes().some(scope => ['*', 'mcp'].includes(scope));
403
+ let hasMcpScope = () => selectedScopes().some(scope => scope === '*' || mcpScopes.includes(scope));
357
404
  let narrowableScope = () => hasApiScope() || hasMcpScope();
358
405
 
359
406
  // The two editor modes. A token that reaches the MCP endpoint and not the REST API is a
@@ -363,9 +410,18 @@
363
410
  // permissions record covers both surfaces and the API side needs all thirteen sections.
364
411
  let mcpLevelMode = () => hasMcpScope() && !hasApiScope();
365
412
 
366
- let selectedMcpLevel = () => {
367
- let elm = document.querySelector('input[name="mcpAccess"]:checked');
368
- return (elm && elm.value) || 'read';
413
+ // The level per section, read through the shared helper: a section whose scope is not
414
+ // ticked has no radio group on screen and so comes back declined - the scope checkbox is
415
+ // this form's way of switching a section off
416
+ let selectedMcpLevels = () => {
417
+ let ticked = selectedMcpScopes();
418
+ let choice = uiMcpLevelChoice(mcpSections, 'mcpLevel_');
419
+ for (let key of Object.keys(mcpSections)) {
420
+ if (!ticked.includes(mcpSections[key].scope)) {
421
+ choice[key] = 'none';
422
+ }
423
+ }
424
+ return choice;
369
425
  };
370
426
 
371
427
  // Sections the MCP surface can never reach are disabled while the level editor is showing,
@@ -396,29 +452,23 @@
396
452
  // an empty one: it means the scopes are the only bound.
397
453
  let effectiveRecord = () => {
398
454
  if (mcpLevelMode()) {
399
- let level = selectedMcpLevel();
400
- if (level !== 'custom') {
401
- let record = mcpAccessPresets[level];
402
- if (record) {
403
- return { actions: record.actions, groups: record.groups, unrestricted: false };
404
- }
405
- // The full level is the one that legitimately stores no record. Any other level
406
- // missing from the table means the table did not load, and a missing preset must
407
- // not quietly mint a wider credential than the level on screen promises - the
408
- // MCP config page's generator refuses the same way, and the consent route
409
- // defaults the same direction.
410
- return {
411
- actions: mcpCeiling.actions,
412
- groups: mcpCeiling.groups,
413
- unrestricted: true,
414
- unknownLevel: !Object.prototype.hasOwnProperty.call(mcpAccessPresets, level)
415
- };
455
+ if (!mcpCustomElm.checked) {
456
+ // The pair list the selected levels mint, through the shared helper, so a level
457
+ // mints here exactly what it mints on the consent prompt
458
+ return uiMcpLevelRecord(mcpSections, selectedMcpLevels());
416
459
  }
417
460
  } else if (!permissionsEnabledElm.checked) {
418
461
  return { actions: allActions, groups: Object.keys(axisLabels.group), unrestricted: true };
419
462
  }
420
463
 
421
- return { actions: tickedValues('.permission-action'), groups: tickedValues('.permission-group'), unrestricted: false };
464
+ return {
465
+ actions: tickedValues('.permission-action'),
466
+ groups: tickedValues('.permission-group'),
467
+ // The scope bound applies to a custom record too: the count must not promise a
468
+ // management tool to a token that only holds the mail scope
469
+ surfaces: mcpLevelMode() ? selectedMcpScopes() : undefined,
470
+ unrestricted: false
471
+ };
422
472
  };
423
473
 
424
474
  // Applies a permission record to the axes. Shared by the API presets and by the switch into
@@ -434,10 +484,11 @@
434
484
 
435
485
  let syncAxisAvailability = () => {
436
486
  let restrictToMcp = mcpLevelMode();
487
+ let ceiling = mcpCeilingFor(selectedMcpScopes());
437
488
 
438
489
  for (let elm of document.querySelectorAll('.permission-axis')) {
439
490
  let axis = elm.classList.contains('permission-action') ? 'actions' : 'groups';
440
- let reachable = !restrictToMcp || mcpCeiling[axis].includes(elm.dataset.value);
491
+ let reachable = !restrictToMcp || ceiling[axis].includes(elm.dataset.value);
441
492
 
442
493
  // Disabled rather than unticked: flipping the scopes back has to restore the
443
494
  // selection rather than quietly lose it, and a disabled box is skipped by every
@@ -479,6 +530,35 @@
479
530
  // stale count cannot survive a scope change.
480
531
  window.uiMcpToolCount(mcpToolCountElm, hasMcpScope() ? Object.assign({ account: accountElm.value }, record) : null);
481
532
 
533
+ let rest = document.createElement('div');
534
+ rest.className = 'text-base-content/60';
535
+ rest.textContent = 'It cannot reach anything else, create tokens or read stored credentials.';
536
+
537
+ if (record.grants) {
538
+ // A level per section, each read out in the words of the table it came from
539
+ if (!record.grants.length) {
540
+ let line = document.createElement('div');
541
+ line.className = 'text-warning';
542
+ line.textContent = 'No access level is selected, so this token would not be able to do anything.';
543
+ permissionOutcomeTextElm.append(line);
544
+ return;
545
+ }
546
+ let levels = selectedMcpLevels();
547
+ for (let key of Object.keys(mcpSections)) {
548
+ let level = mcpSections[key].levels.find(entry => entry.value === levels[key]);
549
+ if (!level) {
550
+ continue;
551
+ }
552
+ let line = document.createElement('div');
553
+ let label = document.createElement('strong');
554
+ label.textContent = mcpSections[key].label + ' (' + level.label + '): ';
555
+ line.append(label, document.createTextNode('can ' + level.actions + ' across ' + level.groups + '.'));
556
+ permissionOutcomeTextElm.append(line);
557
+ }
558
+ permissionOutcomeTextElm.append(rest);
559
+ return;
560
+ }
561
+
482
562
  if (!record.unrestricted && (!record.actions.length || !record.groups.length)) {
483
563
  let line = document.createElement('div');
484
564
  line.className = 'text-warning';
@@ -504,9 +584,6 @@
504
584
  permissionOutcomeTextElm.append(cannot);
505
585
  }
506
586
 
507
- let rest = document.createElement('div');
508
- rest.className = 'text-base-content/60';
509
- rest.textContent = 'It cannot reach anything else, or read credentials and settings.';
510
587
  permissionOutcomeTextElm.append(rest);
511
588
  };
512
589
 
@@ -525,15 +602,18 @@
525
602
  scopes = Object.keys(surfaceRequirements);
526
603
  }
527
604
 
605
+ let covers = ([action, group]) => uiMcpRecordAllows(record, action, group);
606
+
528
607
  let blocked = scopes
529
608
  .filter(scope => surfaceRequirements[scope])
530
609
  .filter(scope => {
531
610
  let need = surfaceRequirements[scope];
532
611
  if (need.mode === 'any') {
533
612
  // Per-request surface: usable while any one (action, group) pair survives
534
- return !need.grants.some(([action, group]) => record.actions.includes(action) && record.groups.includes(group));
613
+ return !need.grants.some(covers);
535
614
  }
536
- return !need.actions.every(value => record.actions.includes(value)) || !need.groups.every(value => record.groups.includes(value));
615
+ // Login-time surface: every pair it exercises has to survive
616
+ return !need.grants.every(covers);
537
617
  })
538
618
  .map(scope => surfaceRequirements[scope].label);
539
619
 
@@ -582,11 +662,18 @@
582
662
  apiLimitBlockElm.classList.toggle('hidden', levelMode);
583
663
  syncAxisAvailability();
584
664
 
585
- // The matrix is the detail view behind either mode: the "Custom permissions" level on
665
+ // A section is shown while its scope is ticked: the scope checkbox is how this form
666
+ // declines a section, so an unticked scope must not leave a level on screen
667
+ let ticked = selectedMcpScopes();
668
+ for (let block of document.querySelectorAll('.mcp-section-block')) {
669
+ block.classList.toggle('hidden', !ticked.includes(block.dataset.mcpScope));
670
+ }
671
+
672
+ // The matrix is the detail view behind either mode: the "Custom permissions" opt-in on
586
673
  // one side, the restriction toggle on the other. The API presets are shapes for the
587
674
  // REST API, so they stay out of the way in level mode, where the named levels above
588
675
  // are the presets.
589
- permissionSectionElm.classList.toggle('hidden', levelMode ? selectedMcpLevel() !== 'custom' : !permissionsEnabledElm.checked);
676
+ permissionSectionElm.classList.toggle('hidden', levelMode ? !mcpCustomElm.checked : !permissionsEnabledElm.checked);
590
677
  permissionPresetRowElm.classList.toggle('hidden', levelMode);
591
678
  };
592
679
 
@@ -594,7 +681,7 @@
594
681
  // ticked - the default selection carries the mcp scope with everything else, and warning
595
682
  // about a disabled endpoint on a page nobody opened for MCP is noise.
596
683
  let mcpDisabledHintElm = document.getElementById('mcpDisabledHint');
597
- let scopesMcpElm = document.getElementById('scopesMcp');
684
+ let scopesMcpElms = [document.getElementById('scopesMcpManage'), document.getElementById('scopesMcp')];
598
685
 
599
686
  let syncAll = () => {
600
687
  syncAccessCard();
@@ -602,7 +689,7 @@
602
689
  syncPermissionOutcome();
603
690
 
604
691
  if (mcpDisabledHintElm) {
605
- mcpDisabledHintElm.classList.toggle('hidden', !scopesMcpElm.checked || scopesMcpElm.disabled);
692
+ mcpDisabledHintElm.classList.toggle('hidden', !scopesMcpElms.some(elm => elm.checked && !elm.disabled));
606
693
  }
607
694
  };
608
695
 
@@ -618,19 +705,28 @@
618
705
  syncAll();
619
706
  });
620
707
 
621
- // The level the axes are seeded from when "Custom permissions" is picked, which is what its
622
- // hint promises. Starts at the default selection rather than empty.
623
- let lastNamedLevel = selectedMcpLevel();
624
- for (let elm of document.querySelectorAll('input[name="mcpAccess"]')) {
625
- elm.addEventListener('change', () => {
626
- if (elm.value === 'custom') {
627
- let record = mcpAccessPresets[lastNamedLevel];
628
- applyRecord(record ? record.actions : mcpCeiling.actions, record ? record.groups : mcpCeiling.groups);
629
- } else {
630
- lastNamedLevel = elm.value;
708
+ // Custom starts from the levels selected above, which is what its hint promises: the axes
709
+ // are seeded from the pairs those levels mint, which is the two-axis record closest to them
710
+ mcpCustomElm.addEventListener('change', () => {
711
+ if (mcpCustomElm.checked) {
712
+ let record = uiMcpLevelRecord(mcpSections, selectedMcpLevels());
713
+ let actions = [];
714
+ let groups = [];
715
+ for (let grant of record.grants) {
716
+ if (!actions.includes(grant.action)) {
717
+ actions.push(grant.action);
718
+ }
719
+ if (!groups.includes(grant.group)) {
720
+ groups.push(grant.group);
721
+ }
631
722
  }
632
- syncAll();
633
- });
723
+ applyRecord(actions, groups);
724
+ }
725
+ syncAll();
726
+ });
727
+
728
+ for (let elm of document.querySelectorAll('input[name^="mcpLevel_"]')) {
729
+ elm.addEventListener('change', syncAll);
634
730
  }
635
731
 
636
732
  for (let elm of document.querySelectorAll('.permission-axis, .scope-entry')) {
@@ -689,12 +785,12 @@
689
785
  document.getElementById('showTokenError').classList.add('hidden');
690
786
  uiModal.open('#showToken');
691
787
 
692
- // Omitting the field is what "not narrowed" means on the record, so an unchecked toggle -
693
- // and the MCP "Full access" level, which the access table stores as null for exactly that
694
- // reason - must not post an empty object. That is a different answer, and the API refuses
695
- // it.
788
+ // Omitting the field is what "not narrowed" means on the record, so an unchecked toggle
789
+ // must not post an empty object. That is a different answer, and the API refuses it. An
790
+ // MCP token minted by level always carries its pair list explicitly, so what the operator
791
+ // saw is exactly what the credential holds.
696
792
  let record = effectiveRecord();
697
- let permissions = record.unrestricted ? null : { actions: record.actions, groups: record.groups };
793
+ let permissions = record.unrestricted ? null : record.grants ? { grants: record.grants } : { actions: record.actions, groups: record.groups };
698
794
 
699
795
  let refuse = message => {
700
796
  document.getElementById('showTokenLoader').classList.add('hidden');
@@ -704,10 +800,11 @@
704
800
  uiModal.open('#showToken');
705
801
  };
706
802
 
707
- // The access table failed to load, so the level on screen cannot be resolved to the
708
- // record it promises. Refused rather than posted: what would go out is the absence of a
709
- // narrowing, which is the widest answer there is.
710
- if (record.unknownLevel) {
803
+ // The section table failed to load, so the levels on screen cannot be resolved to the
804
+ // record they promise. Refused rather than posted, as the MCP config page's generator
805
+ // refuses: what would go out is a record that grants nothing, which the API refuses
806
+ // too, but with an answer that names the wrong cause.
807
+ if (mcpLevelMode() && !mcpCustomElm.checked && !Object.keys(mcpSections).length) {
711
808
  refuse('The access levels failed to load, so the selected level cannot be applied. Reload the page and try again.');
712
809
  return;
713
810
  }
@@ -715,7 +812,11 @@
715
812
  // Said here rather than left to the schema. tokenPermissionsSchema refuses an empty axis
716
813
  // too, but a joi rejection reaches this page as a bare "HTTP error! status: 400", which
717
814
  // tells the reader nothing about the box they left unticked.
718
- if (permissions && (!permissions.actions.length || !permissions.groups.length)) {
815
+ if (permissions && permissions.grants && !permissions.grants.length) {
816
+ refuse('Select an access level for at least one MCP scope. A token that grants nothing cannot make any request.');
817
+ return;
818
+ }
819
+ if (permissions && permissions.actions && (!permissions.actions.length || !permissions.groups.length)) {
719
820
  let missing = !permissions.actions.length ? 'action' : 'section';
720
821
  refuse(`Select at least one ${missing}. A restriction that allows nothing would create a token that cannot make any request.`);
721
822
  return;
@@ -37,7 +37,7 @@ focus ring on) the corner close button --}}
37
37
  </div>
38
38
  </dialog>
39
39
 
40
- <script nonce="{{cspNonce}}">
40
+ <script data-cfasync="false" nonce="{{cspNonce}}">
41
41
  document.addEventListener('DOMContentLoaded', () => {
42
42
 
43
43
  document.getElementById('resubscribe-link').addEventListener('click', e => {
@@ -153,7 +153,7 @@ fieldName="webhook" fieldValue=webhook.id submitLabel="Delete route"}}
153
153
 
154
154
  {{> ace_assets}}
155
155
 
156
- <script nonce="{{cspNonce}}">
156
+ <script data-cfasync="false" nonce="{{cspNonce}}">
157
157
  document.addEventListener('DOMContentLoaded', () => {
158
158
 
159
159
  {{!-- useWorker is false, so the JSHint options machinery the editable