@blamejs/core 0.11.29 → 0.11.31
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 +8 -0
- package/index.js +2 -0
- package/lib/calendar.js +581 -0
- package/lib/mail-server-jmap.js +303 -0
- package/lib/mail-server-registry.js +11 -0
- package/package.json +1 -1
- package/sbom.cdx.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,14 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.11.x
|
|
10
10
|
|
|
11
|
+
- v0.11.31 (2026-05-21) — **`b.calendar` — JSCalendar (RFC 8984) primitive + JMAP Calendars method catalogue.** JSCalendar is the JSON-native calendar grammar JMAP Calendars rides on. The framework now ships `b.calendar` — a thin layer over the existing `b.safeIcal.parse` (RFC 5545 bounded parser shipped earlier) that exposes validate / fromIcal / toIcal / expandRecurrence. The JMAP method catalogue at `b.mail.serverRegistry` picks up the 15 Calendar / CalendarEvent / CalendarEventNotification / ParticipantIdentity methods per the draft-ietf-jmap-calendars spec, so operators wire them through the existing `b.mail.server.jmap.create({ methods: { ... } })` dispatch path without `allowExperimental: true` escape-hatches.
|
|
12
|
+
|
|
13
|
+
v1 scope: validate JSCalendar Event / Task shape per RFC 8984 §5/§6, bidirectional VEVENT ↔ JSCalendar conversion, RRULE expansion for FREQ=DAILY/WEEKLY/MONTHLY/YEARLY/HOURLY/MINUTELY/SECONDLY with INTERVAL / COUNT / UNTIL / BYDAY / BYMONTH / BYMONTHDAY. BYSETPOS / BYWEEKNO / BYYEARDAY filters + non-Gregorian calendars (RFC 7529) + JSCalendar Group objects + VTODO/VJOURNAL mapping are deferred-with-condition for follow-up slices. **Added:** *`b.calendar.validate(jsCal)` — JSCalendar Event/Task shape gate (RFC 8984 §5/§6)* — Asserts `@type` ∈ { 'Event', 'Task' }, non-empty `uid` (≤ 1024 bytes), `updated` matches UTCDateTime grammar, Event's optional `start` matches LocalDateTime, `duration` is RFC 8601 PnYnMnDTnHnMnS, every `recurrenceRules[i].@type` is 'RecurrenceRule' with `frequency` in the JSCAL_FREQUENCIES catalogue, every `alerts.{id}.action` is 'display' or 'email'. Returns the input on success; throws `CalendarError` with a structured `.code` (`calendar/no-uid`, `calendar/bad-recurrence`, etc.) on refusal so operator-side error handling has a stable surface. · *`b.calendar.fromIcal(text, opts?)` + `b.calendar.toIcal(jsCal, opts?)`* — Bidirectional bridge: iCalendar (RFC 5545) ↔ JSCalendar (RFC 8984). `fromIcal` runs the text through `b.safeIcal.parse` (already CVE-bounded for parser DoS) and maps the first VEVENT into an Event object (UID → uid, DTSTAMP → updated, DTSTART → start, DURATION → duration, SUMMARY → title, DESCRIPTION → description, LOCATION → locations[L1].name, RRULE → recurrenceRules[0]). `toIcal` round-trips the same shape back through a CRLF-folded VCALENDAR envelope per RFC 5545 §3.1 (75-octet content-line cap). Operators wire it on the EmailSubmission send path so calendar invitations from a JSCalendar back-end emit RFC 5545 over MIME, and inbound iCalendar attachments parse straight into the JMAP method's Event return shape. · *`b.calendar.expandRecurrence(event, { from, to, max })` — bounded RRULE expansion* — Emits ISO 8601 UTC instance timestamps for an Event in the operator's `[from, to]` window. Supports FREQ=DAILY/WEEKLY/MONTHLY/YEARLY/HOURLY/MINUTELY/SECONDLY with INTERVAL, COUNT, UNTIL. Bounded by `MAX_EXPAND_INSTANCES` (4096) and `MAX_EXPAND_SPAN_MS` (10 years) — refuses with `calendar/oversize-expansion-span` when the window exceeds 10 years (CVE-2024-39687-class recurrence-bomb defense, mirroring the parser-side caps already in `b.safeIcal`). LocalDateTime starts without a `timeZone` are treated as floating-UTC during expansion so wall-clock semantics survive `Date.parse` host-locale interpretation. · *JMAP method catalogue: Calendar / CalendarEvent / CalendarEventNotification / ParticipantIdentity* — 15 new methods added to `JMAP_METHODS` in `lib/mail-server-registry.js` per draft-ietf-jmap-calendars: `Calendar/{get,changes,set,query,queryChanges}`, `CalendarEvent/{get,changes,query,queryChanges,set,copy}`, `CalendarEventNotification/{get,changes,query,queryChanges,set}`, `ParticipantIdentity/{get,changes,set}`. Operators wire concrete handlers through the existing `b.mail.server.jmap.create({ methods: { 'CalendarEvent/get': async function (actor, args) { ... } } })` path — no `allowExperimental: true` escape-hatch is required. **Security:** *Recurrence-bomb expansion caps* — `MAX_EXPAND_INSTANCES = 4096` + `MAX_EXPAND_SPAN_MS = 10 years` clamp the expansion budget at the framework boundary so an operator who forwards an unbounded JSCalendar from a hostile peer cannot DoS the host. The caps mirror `b.safeIcal`'s RRULE COUNT + BY-entries caps (which already defend the CVE-2024-39687 class). The 10-year span refusal carries `code: 'calendar/oversize-expansion-span'` so operators distinguish bomb defense from legitimate too-wide-window misconfiguration. · *UID + duration + UTCDateTime parsing is strict* — `validate` refuses `uid` over 1024 bytes (anti-DoS), `updated` that doesn't match the RFC 8984 §1.4.3 `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z` UTCDateTime grammar exactly (no `+00:00` aliases, no fractional-second-only suffixes), and `duration` that isn't `PnYnMnDTnHnMnS`. Hostile JSCalendar payloads from federation peers can't slip non-canonical-shape values into the operator's storage layer through the validator. · *iCalendar parsing routed through bounded `b.safeIcal.parse`* — `fromIcal` does NOT roll its own RFC 5545 parser — it forwards to `b.safeIcal.parse` which is already audit-hardened (CVE-2024-39929 / CVE-2025-30258 mitigation, bounded depth, capped RRULE COUNT + BY-entries). The JSCalendar layer composes the existing security substrate rather than re-litigating it. **References:** [RFC 8984 (JSCalendar — JSON Representation of Calendar Data)](https://www.rfc-editor.org/rfc/rfc8984.html) · [RFC 5545 (iCalendar — Internet Calendaring and Scheduling Core Object)](https://www.rfc-editor.org/rfc/rfc5545.html) · [draft-ietf-jmap-calendars (JMAP for Calendars)](https://datatracker.ietf.org/doc/draft-ietf-jmap-calendars/) · [RFC 8620 (JMAP Core)](https://www.rfc-editor.org/rfc/rfc8620.html) · [RFC 8601 (Duration grammar PnYnMnDTnHnMnS)](https://www.rfc-editor.org/rfc/rfc3339.html) · [RFC 7986 (New properties for iCalendar)](https://www.rfc-editor.org/rfc/rfc7986.html) · [CVE-2024-39687 (iCalendar recurrence-bomb expansion)](https://nvd.nist.gov/vuln/detail/CVE-2024-39687)
|
|
14
|
+
|
|
15
|
+
- v0.11.30 (2026-05-21) — **JMAP blob upload + download handlers on `b.mail.server.jmap` (RFC 8620 §6).** The JMAP listener now exposes turnkey HTTP handlers for blob upload (`POST /jmap/upload/{accountId}`) and blob download (`GET /jmap/download/{accountId}/{blobId}/{name}`). Upload streams the request body into `b.safeBuffer.boundedChunkCollector` (default cap 50 MiB; operator-tunable via `opts.maxBlobBytes`), then calls the operator backend's `mailStore.uploadBlob(actor, accountId, contentType, bytes) → { blobId, type?, size? }` and returns the JSON descriptor clients pass to subsequent `Email/set` / `Email/import` calls. Download walks the operator backend's `mailStore.downloadBlob(actor, accountId, blobId) → { bytes, type }` and pipes the bytes through with the right `Content-Type` + `Content-Disposition` headers. Both handlers refuse 503 when the corresponding backend hook is missing — never silent OK.
|
|
16
|
+
|
|
17
|
+
EmailSubmission (RFC 8621 §7) was already in the JMAP method catalogue at `lib/mail-server-registry.js`; operators wire `EmailSubmission/get` / `EmailSubmission/set` through the existing `opts.methods` dispatch and compose `b.mail.send.deliver` (shipped v0.11.24) for the actual outbound send. No additional framework wiring is required. **Added:** *`uploadHandler(req, res)` — POST `/jmap/upload/{accountId}`* — Streams the request body through `b.safeBuffer.boundedChunkCollector` so a runaway upload refuses with `413 maxSizeUpload` instead of OOM'ing the process. Default cap 50 MiB; tune via `b.mail.server.jmap.create({ maxBlobBytes })`. The `accountId` path segment is identifier-shape-validated (`/^[A-Za-z0-9_-]{1,64}$/`) — path-traversal shapes are refused at the boundary. Calls `mailStore.uploadBlob(actor, accountId, contentType, bytes) → { blobId, type?, size? }`; the listener echoes `accountId` + `blobId` + `type` + `size` back as a `201 Created` JSON response per RFC 8620 §6.1. · *`downloadHandler(req, res)` — GET `/jmap/download/{accountId}/{blobId}/{name}`* — Parses the path's `accountId` / `blobId` / `name` segments + optional `?accept=<type>` query, calls `mailStore.downloadBlob(actor, accountId, blobId) → { bytes, type } | Buffer | null`, and pipes the bytes through with `Content-Type` (backend-supplied OR query-`accept` OR `application/octet-stream` fallback) + `Content-Length` + `Content-Disposition: attachment; filename="<safe-name>"` (only when the filename matches the safe `[A-Za-z0-9._-]{1,200}` shape — anti header-injection). Missing blob → `404 invalidArguments`. · *Both handlers exposed on the listener handle* — `b.mail.server.jmap.create(opts)` returns `{ apiHandler, sessionHandler, discoveryHandler, eventSourceHandler, uploadHandler, downloadHandler, MailServerJmapError }`. Operators mount each at the path their HTTP router exposes; the `session.uploadUrl` / `session.downloadUrl` already advertise the canonical paths. · *EmailSubmission methods continue through the existing dispatch path* — RFC 8621 §7 — `EmailSubmission/get` / `EmailSubmission/changes` / `EmailSubmission/query` / `EmailSubmission/queryChanges` / `EmailSubmission/set` are already in the JMAP method catalogue at `lib/mail-server-registry.js`. Operators wire them through `b.mail.server.jmap.create({ methods: { 'EmailSubmission/set': async function (actor, args) { ... b.mail.send.deliver(...) ... } } })`. No additional framework wiring is required for v0.11.30; the substrate composition (JMAP method → `b.mail.send.deliver` → SMTP MX → DSN) was already complete after v0.11.24. **Security:** *AccountId path-traversal refusal at the boundary* — Both handlers validate the `accountId` URL segment against `/^[A-Za-z0-9_-]{1,64}$/`. URL-encoded path-traversal payloads (`%2E%2E%2F`, `..%2F`, `/`) refuse with `400 invalidArguments` before any backend call. Operator-side validation in `mailStore.uploadBlob` / `mailStore.downloadBlob` is a defense-in-depth second layer. · *Upload size cap via `safeBuffer.boundedChunkCollector` (cap-bounded)* — Replaces the prior hand-rolled `Buffer.concat(chunks, received)` shape with the framework's bounded-collector primitive. The collector throws `mail-server-jmap/blob-too-large` on overflow; the handler converts it into a `413 maxSizeUpload` JSON error per RFC 8620 §6.1. A misbehaving client cannot stream a multi-gigabyte payload past the limit — the collector enforces the cap byte-by-byte. · *`Content-Disposition` filename is identifier-shape only* — Download responses only set the `Content-Disposition` header when the URL's `name` segment matches `/^[A-Za-z0-9._-]{1,200}$/`. Filenames with `;` / `"` / CRLF cannot be smuggled into the header — RFC 6266 §4.3 + CVE-2023-46604-class header-injection defense. **References:** [RFC 8620 (JMAP Core — §6 Blob upload/download)](https://www.rfc-editor.org/rfc/rfc8620.html) · [RFC 8621 (JMAP Mail — §7 EmailSubmission)](https://www.rfc-editor.org/rfc/rfc8621.html) · [RFC 6266 (Content-Disposition in HTTP)](https://www.rfc-editor.org/rfc/rfc6266.html) · [RFC 5987 (Encoding-aware filename* parameter)](https://www.rfc-editor.org/rfc/rfc5987.html)
|
|
18
|
+
|
|
11
19
|
- v0.11.29 (2026-05-21) — **JMAP Push — EventSource SSE handler on `b.mail.server.jmap` (RFC 8620 §7.3).** The JMAP listener now exposes a real EventSource handler at `/jmap/eventsource`. The session-resource's `eventSourceUrl` becomes a live push channel: clients connect with `?types=*|<csv>&closeafter=no|state&ping=<seconds>`, the listener subscribes via the operator backend's `mailStore.subscribePush(actor, types, emitFn)` hook, and StateChange events arrive as `event: state` SSE frames with the `{ "@type": "StateChange", changed: {...} }` payload per RFC 8620 §7.4. Keepalive `event: ping` frames default to 30 s (operator-tunable 5-900 s), `closeafter=state` closes after one event for poll-like clients, and the SSE response carries the headers proxies expect (`Cache-Control: no-cache`, `Connection: keep-alive`, `X-Accel-Buffering: no`) so nginx-fronted deployments don't buffer the stream. Without the backend subscribe hook the handler refuses with `503 serverUnavailable`, never silent OK. **Added:** *`eventSourceHandler(req, res)` exposed on the JMAP listener handle* — `b.mail.server.jmap.create(opts).eventSourceHandler` mounts on the operator's HTTP router at whatever path the session-resource's `eventSourceUrl` points to (default `/jmap/eventsource`). Authentication is delegated to the surrounding HTTP middleware (the handler expects `req.user` / `req.actor` to be set); unauthenticated requests return `401 forbidden` per RFC 8620 §1.5. Query-string params parse `types=` (CSV or `*` wildcard), `closeafter=` (`no` | `state`), `ping=` (seconds, clamped 5..900) per §7.3. · *Operator backend hook — `mailStore.subscribePush(actor, types, emitFn)`* — Backends opt in by exposing a `subscribePush` method that accepts (1) the authenticated actor, (2) a parsed `types` list (or `null` for the wildcard `*`), and (3) an `emitFn(event)` callback the backend calls when a state change occurs. The expected event shape is `{ kind: 'StateChange', changed: { <accountId>: { <typeName>: <stateString>, ... }, ... }, pushed?: {...} }`; the listener formats it into the SSE `event: state\ndata: <JSON>\n\n` wire shape. The subscribe call may return either `void` or an `unsubscribe()` function the listener invokes on disconnect. · *SSE wire-correct headers + initial-state hint* — Response headers: `Content-Type: text/event-stream; charset=utf-8`, `Cache-Control: no-cache`, `Connection: keep-alive`, `X-Accel-Buffering: no` (nginx-specific — disables response buffering on the stream so per-event frames flush immediately). The initial bytes carry `retry: 5000\n\n` (HTML5 SSE reconnect hint — 5 s) + a `: connected\n\n` comment so clients can confirm the channel is alive before the first state event. **Security:** *Push backend missing returns `503 serverUnavailable` (no silent accept)* — If the operator wired the listener without `mailStore.subscribePush`, the handler returns `503 urn:ietf:params:jmap:error:serverUnavailable` with a `description` pointing at the missing hook. RFC 8620 §7.3 expects the server to honour subscriptions or refuse explicitly — silently accepting would let a client believe events will arrive when the server cannot deliver. · *`closeafter` accepts only `no` | `state` (RFC 8620 §7.3)* — Any other value returns `400 invalidArguments` before the subscribe hook fires. Prevents an operator-supplied query string from steering the handler into an undocumented mode. · *Ping interval clamped 5..900 seconds* — `ping=<seconds>` below 5 s or above 900 s clamps to the bounds rather than refusing — a misconfigured client cannot DoS the listener via 1 ms ping floods, and a 24 h ping won't outlast intermediate proxy idle timeouts (typical 60-120 s). The clamped default (30 s) matches the RFC 8620 §7.3 example. · *Ping timer is `.unref()`'d* — Background timers without `.unref()` pin the Node process — graceful shutdown waits indefinitely. The interval is unref'd immediately after creation; per-connection cleanup (`req.on('close')` / `req.on('error')`) clears the timer + invokes the backend's optional `unsubscribe()` + ends the response. **References:** [RFC 8620 (JMAP Core — §7 Push / §7.3 EventSource / §7.4 StateChange)](https://www.rfc-editor.org/rfc/rfc8620.html) · [RFC 8621 (JMAP Mail)](https://www.rfc-editor.org/rfc/rfc8621.html) · [RFC 8887 (JMAP WebSocket transport — opt-in, deferred to a later slice)](https://www.rfc-editor.org/rfc/rfc8887.html) · [HTML5 Server-Sent Events (EventSource)](https://html.spec.whatwg.org/multipage/server-sent-events.html)
|
|
12
20
|
|
|
13
21
|
- v0.11.28 (2026-05-21) — **IMAP opt-in extensions: NOTIFY (RFC 5465), METADATA (RFC 5464), CATENATE (RFC 4469).** Three IMAP extensions advertised in CAPABILITY and dispatched through the existing per-method registry. NOTIFY accepts a client subscription spec and hands it to the operator's `mailStore.subscribeNotify(actor, spec, emitFn)` hook — actual event emission stays operator-side. METADATA exposes GETMETADATA and SETMETADATA per-mailbox + server-wide annotations through `mailStore.getMetadata` / `setMetadata`. CATENATE extends APPEND to compose a message from existing parts (`TEXT {N}` literals + `URL "imap://..."`) via `mailStore.appendCatenate`. Each handler refuses gracefully (`NO ... backend not configured`) when the operator backend doesn't supply the hook. COMPRESS=DEFLATE (RFC 4978) intentionally NOT advertised — CRIME-class compression-oracle threat on the encrypted IMAP stream. **Added:** *CAPABILITY advertises `NOTIFY`, `METADATA`, `METADATA-SERVER`, `CATENATE`* — All four added unconditionally so capable clients can exercise the extension regardless of authentication state. Each handler is registered in the protocol verb catalogue (`b.mail.serverRegistry`) + the wire-level guard verb list (`b.guardImapCommand.KNOWN_VERBS`) so the existing dispatch + audit + ratelimit gates apply uniformly. · *`NOTIFY SET ...` / `NOTIFY NONE` — RFC 5465* — The handler parses `NOTIFY SET [STATUS] (<filter-set> (<event>...))*` and `NOTIFY NONE` and stores the filter-set verbatim on `state.notifySpec`. When the operator backend exposes `mailStore.subscribeNotify(actor, spec, emitFn)`, the listener wires an `emitFn` that translates backend events (`{ kind: 'STATUS' | 'LIST' | 'FETCH', payload, seq? }`) into untagged IMAP responses on the same connection — drop-silent if the socket has already closed. Without the backend hook, the wire command refuses with `NO NOTIFY backend not configured` rather than silently accepting subscriptions the server can't fulfil. · *`GETMETADATA` / `SETMETADATA` — RFC 5464* — Both verbs parse the per-mailbox + server-wide annotation forms. GETMETADATA accepts optional `(MAXSIZE N)` / `(DEPTH ...)` options before the mailbox + entry list, walks the entries through `mailStore.getMetadata(actor, mailbox, names, opts) → [{ entry, value }]`, and renders an untagged `* METADATA <mailbox> (<entry> <value>...)` response. SETMETADATA tokenises the entry/value pairs (quoted-strings + NIL for clearing), validates the mailbox name, and forwards to `mailStore.setMetadata(actor, mailbox, entries)`. Without the backend hooks, both return `NO ... backend not configured`. · *APPEND `CATENATE` modifier — RFC 4469* — `APPEND mailbox [flags] [date-time] CATENATE (...)` is recognised before the legacy literal-required APPEND path. The parts list mixes `TEXT {N}` literal-bytes parts (handed in via the literal-aware parser) and `URL "imap://..."` reference parts; the listener bundles them into `parts: [{ kind: 'TEXT', bytes } | { kind: 'URL', url }]` and forwards to `mailStore.appendCatenate(mailbox, parts, { actor, flags, internalDate }) → { uid, uidValidity }`. When the backend returns the APPENDUID metadata the response carries `OK [APPENDUID <validity> <uid>] APPEND completed` (RFC 4315). Without the backend hook, refuses with `NO CATENATE backend not configured`. **Security:** *COMPRESS=DEFLATE intentionally NOT advertised (CRIME-class)* — RFC 4978 IMAP COMPRESS=DEFLATE enables stream compression that interacts badly with TLS — the CRIME attack class (CVE-2012-4929, BREACH, et al.) recovers plaintext via chosen-plaintext compression-ratio analysis. The framework default is OFF; operators with explicit threat models accept the downgrade via `opts.compress = true` (no opt-in path landed in v1, intentionally — defer-with-condition: open when an operator surfaces a deployment that needs it AND can document the chosen-plaintext threat model is mitigated). · *Mailbox-name validation reused for both METADATA verbs* — Both GETMETADATA and SETMETADATA run `_validateMailboxName` on the parsed mailbox argument (except for the empty-string `""` server-wide-metadata special case per RFC 5464 §3.1). Operators with the existing `allowLegacyMUtf7` opt see the same mailbox-name policy as the rest of the listener; injection-shape mailbox names are refused identically. · *NOTIFY backend-missing returns NO (not silent accept)* — If the operator wired the listener without `mailStore.subscribeNotify`, `NOTIFY SET ...` returns `NO NOTIFY backend not configured` — never a silent `OK`. RFC 5465 §6 specifies NO as the correct refusal shape; silent acceptance would let a client believe events will arrive when the server cannot fulfil the subscription. **References:** [RFC 5465 (IMAP NOTIFY)](https://www.rfc-editor.org/rfc/rfc5465.html) · [RFC 5464 (IMAP METADATA)](https://www.rfc-editor.org/rfc/rfc5464.html) · [RFC 4469 (IMAP CATENATE)](https://www.rfc-editor.org/rfc/rfc4469.html) · [RFC 4315 (IMAP UIDPLUS — APPENDUID response)](https://www.rfc-editor.org/rfc/rfc4315.html) · [RFC 4978 (IMAP COMPRESS — NOT enabled; CRIME-class threat)](https://www.rfc-editor.org/rfc/rfc4978.html) · [CVE-2012-4929 (CRIME — compression-oracle attack on TLS)](https://nvd.nist.gov/vuln/detail/CVE-2012-4929)
|
package/index.js
CHANGED
|
@@ -98,6 +98,7 @@ var safeSieve = require("./lib/safe-sieve");
|
|
|
98
98
|
var safeIcap = require("./lib/safe-icap");
|
|
99
99
|
var safeIcal = require("./lib/safe-ical");
|
|
100
100
|
var safeVcard = require("./lib/safe-vcard");
|
|
101
|
+
var calendar = require("./lib/calendar");
|
|
101
102
|
var mailStore = require("./lib/mail-store");
|
|
102
103
|
var ntpCheck = require("./lib/ntp-check");
|
|
103
104
|
var auditSign = require("./lib/audit-sign");
|
|
@@ -622,6 +623,7 @@ module.exports = {
|
|
|
622
623
|
safeIcap: safeIcap,
|
|
623
624
|
safeIcal: safeIcal,
|
|
624
625
|
safeVcard: safeVcard,
|
|
626
|
+
calendar: calendar,
|
|
625
627
|
mailStore: mailStore,
|
|
626
628
|
safeSchema: safeSchema,
|
|
627
629
|
pagination: pagination,
|
package/lib/calendar.js
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module b.calendar
|
|
4
|
+
* @nav Mail
|
|
5
|
+
* @title Calendar (JSCalendar)
|
|
6
|
+
* @order 400
|
|
7
|
+
* @slug calendar
|
|
8
|
+
*
|
|
9
|
+
* @intro
|
|
10
|
+
* JSCalendar (RFC 8984) primitive. Wraps the framework's existing
|
|
11
|
+
* `b.safeIcal.parse` (RFC 5545 grammar + bounded parser) with the
|
|
12
|
+
* JSON-native JSCalendar surface JMAP Calendars (RFC 8984 / draft-
|
|
13
|
+
* ietf-jmap-calendars) requires for cross-protocol interop.
|
|
14
|
+
*
|
|
15
|
+
* v1 scope:
|
|
16
|
+
* - `validate(jsCal)` — assert JSCalendar Event / Task shape
|
|
17
|
+
* - `fromIcal(text, opts?)` — VCALENDAR.VEVENT → JSCalendar Event
|
|
18
|
+
* - `toIcal(jsCal, opts?)` — JSCalendar Event → VCALENDAR
|
|
19
|
+
* - `expandRecurrence(event, { from, to, max })` — RRULE expansion
|
|
20
|
+
* for FREQ=DAILY/WEEKLY/MONTHLY/YEARLY with UNTIL/COUNT/INTERVAL
|
|
21
|
+
*
|
|
22
|
+
* Deferred-with-condition (no operator demand yet):
|
|
23
|
+
* - BYSETPOS / BYWEEKNO / BYYEARDAY (RFC 5545 §3.3.10) — RFC 7529
|
|
24
|
+
* non-Gregorian calendars; floating timezone resolution.
|
|
25
|
+
* - VTODO / VJOURNAL → Task / Note objects (RFC 8984 §5/§6).
|
|
26
|
+
* - JSCalendar Group objects (RFC 8984 §1.4.4).
|
|
27
|
+
*
|
|
28
|
+
* @card
|
|
29
|
+
* JSCalendar (RFC 8984) ↔ iCalendar (RFC 5545) bridge — validate,
|
|
30
|
+
* convert both directions, expand recurrences. Substrate for JMAP
|
|
31
|
+
* Calendars (RFC 8984 + draft-ietf-jmap-calendars).
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
var safeIcal = require("./safe-ical");
|
|
35
|
+
var time = require("./time");
|
|
36
|
+
var { defineClass } = require("./framework-error");
|
|
37
|
+
|
|
38
|
+
var CalendarError = defineClass("CalendarError", { alwaysPermanent: true });
|
|
39
|
+
|
|
40
|
+
// JSCalendar shape vocabulary — RFC 8984 §1.2 (`@type`) catalogues
|
|
41
|
+
// the discriminator strings every nested object MUST carry.
|
|
42
|
+
var JSCAL_TYPES = Object.freeze({
|
|
43
|
+
Event: "Event",
|
|
44
|
+
Task: "Task",
|
|
45
|
+
Group: "Group",
|
|
46
|
+
Participant: "Participant",
|
|
47
|
+
Location: "Location",
|
|
48
|
+
Link: "Link",
|
|
49
|
+
Alert: "Alert",
|
|
50
|
+
Recurrence: "RecurrenceRule",
|
|
51
|
+
TimeZone: "TimeZone",
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// RFC 8984 §4.3.2 — frequencies recognised in `RecurrenceRule.frequency`.
|
|
55
|
+
var JSCAL_FREQUENCIES = Object.freeze({
|
|
56
|
+
yearly: 1, monthly: 1, weekly: 1, daily: 1, hourly: 1, minutely: 1, secondly: 1,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// RFC 8984 §4.6.2 — alert action types.
|
|
60
|
+
var JSCAL_ALERT_ACTIONS = Object.freeze({
|
|
61
|
+
display: 1, email: 1,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Recurrence-expansion caps. Mirror b.safeIcal's RRULE limits so the
|
|
65
|
+
// expand path can't outpace what the parser already permitted.
|
|
66
|
+
var MAX_EXPAND_INSTANCES = 4096; // allow:raw-byte-literal — instance count cap, not bytes
|
|
67
|
+
var MAX_EXPAND_SPAN_MS = 10 * 365 * 24 * 60 * 60 * 1000; // allow:raw-byte-literal + allow:raw-time-literal — 10 year max expansion span
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @primitive b.calendar.validate
|
|
71
|
+
* @signature b.calendar.validate(jsCal)
|
|
72
|
+
* @since 0.11.31
|
|
73
|
+
* @status stable
|
|
74
|
+
*
|
|
75
|
+
* Validate a JSCalendar Event / Task object's required-field shape per
|
|
76
|
+
* RFC 8984 §5 (Event) + §6 (Task). Returns the input on success; throws
|
|
77
|
+
* `CalendarError` on refusal with a `.code` naming the specific shape
|
|
78
|
+
* rule that failed.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* b.calendar.validate({
|
|
82
|
+
* "@type": "Event",
|
|
83
|
+
* uid: "0e612e8b-1c4f-4e30-8e6a-4adc4e8b1c4f",
|
|
84
|
+
* updated: "2026-05-21T10:00:00Z",
|
|
85
|
+
* title: "Sprint planning",
|
|
86
|
+
* start: "2026-05-22T09:00:00",
|
|
87
|
+
* duration: "PT1H",
|
|
88
|
+
* timeZone: "America/Los_Angeles",
|
|
89
|
+
* });
|
|
90
|
+
*/
|
|
91
|
+
function validate(jsCal) {
|
|
92
|
+
if (!jsCal || typeof jsCal !== "object" || Array.isArray(jsCal)) {
|
|
93
|
+
throw new CalendarError("calendar/bad-input",
|
|
94
|
+
"b.calendar.validate: input must be a JSCalendar object");
|
|
95
|
+
}
|
|
96
|
+
var t = jsCal["@type"];
|
|
97
|
+
if (t !== JSCAL_TYPES.Event && t !== JSCAL_TYPES.Task) {
|
|
98
|
+
throw new CalendarError("calendar/bad-type",
|
|
99
|
+
"b.calendar.validate: @type must be 'Event' or 'Task' (got " + JSON.stringify(t) + ")");
|
|
100
|
+
}
|
|
101
|
+
if (typeof jsCal.uid !== "string" || jsCal.uid.length === 0) {
|
|
102
|
+
throw new CalendarError("calendar/no-uid",
|
|
103
|
+
"b.calendar.validate: uid is required (RFC 8984 §5.1.4)");
|
|
104
|
+
}
|
|
105
|
+
if (jsCal.uid.length > 1024) { // allow:raw-byte-literal — anti-DoS uid length cap
|
|
106
|
+
throw new CalendarError("calendar/oversize-uid",
|
|
107
|
+
"b.calendar.validate: uid exceeds 1024 bytes");
|
|
108
|
+
}
|
|
109
|
+
if (typeof jsCal.updated !== "string" || !_isUtcDateTime(jsCal.updated)) {
|
|
110
|
+
throw new CalendarError("calendar/bad-updated",
|
|
111
|
+
"b.calendar.validate: updated MUST be a UTCDateTime per RFC 8984 §1.4.3 (got " + JSON.stringify(jsCal.updated) + ")");
|
|
112
|
+
}
|
|
113
|
+
if (t === JSCAL_TYPES.Event) {
|
|
114
|
+
if (jsCal.start !== undefined && (typeof jsCal.start !== "string" || !_isLocalDateTime(jsCal.start))) {
|
|
115
|
+
throw new CalendarError("calendar/bad-start",
|
|
116
|
+
"b.calendar.validate: Event.start MUST be a LocalDateTime");
|
|
117
|
+
}
|
|
118
|
+
if (jsCal.duration !== undefined && (typeof jsCal.duration !== "string" || !_isDuration(jsCal.duration))) {
|
|
119
|
+
throw new CalendarError("calendar/bad-duration",
|
|
120
|
+
"b.calendar.validate: Event.duration MUST be an RFC 8601 PnYnMnDTnHnMnS Duration");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (jsCal.recurrenceRules !== undefined) {
|
|
124
|
+
if (!Array.isArray(jsCal.recurrenceRules)) {
|
|
125
|
+
throw new CalendarError("calendar/bad-recurrence",
|
|
126
|
+
"b.calendar.validate: recurrenceRules MUST be an array of RecurrenceRule");
|
|
127
|
+
}
|
|
128
|
+
for (var ri = 0; ri < jsCal.recurrenceRules.length; ri += 1) {
|
|
129
|
+
var rr = jsCal.recurrenceRules[ri];
|
|
130
|
+
if (!rr || typeof rr !== "object" || rr["@type"] !== "RecurrenceRule") {
|
|
131
|
+
throw new CalendarError("calendar/bad-recurrence",
|
|
132
|
+
"b.calendar.validate: recurrenceRules[" + ri + "].@type MUST be 'RecurrenceRule'");
|
|
133
|
+
}
|
|
134
|
+
if (!Object.prototype.hasOwnProperty.call(JSCAL_FREQUENCIES, rr.frequency)) {
|
|
135
|
+
throw new CalendarError("calendar/bad-recurrence",
|
|
136
|
+
"b.calendar.validate: recurrenceRules[" + ri + "].frequency MUST be one of " +
|
|
137
|
+
Object.keys(JSCAL_FREQUENCIES).join(" | "));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (jsCal.alerts !== undefined) {
|
|
142
|
+
if (typeof jsCal.alerts !== "object" || Array.isArray(jsCal.alerts)) {
|
|
143
|
+
throw new CalendarError("calendar/bad-alerts",
|
|
144
|
+
"b.calendar.validate: alerts MUST be an object map keyed by alert-id");
|
|
145
|
+
}
|
|
146
|
+
var alertKeys = Object.keys(jsCal.alerts);
|
|
147
|
+
for (var ai = 0; ai < alertKeys.length; ai += 1) {
|
|
148
|
+
var alert = jsCal.alerts[alertKeys[ai]];
|
|
149
|
+
if (!alert || alert["@type"] !== "Alert") {
|
|
150
|
+
throw new CalendarError("calendar/bad-alerts",
|
|
151
|
+
"b.calendar.validate: alerts[" + alertKeys[ai] + "].@type MUST be 'Alert'");
|
|
152
|
+
}
|
|
153
|
+
if (alert.action && !Object.prototype.hasOwnProperty.call(JSCAL_ALERT_ACTIONS, alert.action)) {
|
|
154
|
+
throw new CalendarError("calendar/bad-alerts",
|
|
155
|
+
"b.calendar.validate: alerts[" + alertKeys[ai] + "].action MUST be one of " +
|
|
156
|
+
Object.keys(JSCAL_ALERT_ACTIONS).join(" | "));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return jsCal;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @primitive b.calendar.fromIcal
|
|
165
|
+
* @signature b.calendar.fromIcal(text, opts?)
|
|
166
|
+
* @since 0.11.31
|
|
167
|
+
* @status stable
|
|
168
|
+
*
|
|
169
|
+
* Parse iCalendar text (RFC 5545) via `b.safeIcal.parse` and map the
|
|
170
|
+
* first VEVENT into a JSCalendar Event object (RFC 8984 §5). Returns
|
|
171
|
+
* a single Event when the VCALENDAR contains exactly one VEVENT, or
|
|
172
|
+
* an array when multiple VEVENTs are present.
|
|
173
|
+
*
|
|
174
|
+
* @opts
|
|
175
|
+
* safeIcalOpts: object, // forwarded to b.safeIcal.parse (caps, allowExperimental, etc.)
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* b.calendar.fromIcal(
|
|
179
|
+
* "BEGIN:VCALENDAR\\r\\nVERSION:2.0\\r\\n" +
|
|
180
|
+
* "BEGIN:VEVENT\\r\\nUID:a@b\\r\\nDTSTAMP:20260521T100000Z\\r\\n" +
|
|
181
|
+
* "DTSTART:20260522T090000Z\\r\\nDURATION:PT1H\\r\\n" +
|
|
182
|
+
* "SUMMARY:Sprint\\r\\nEND:VEVENT\\r\\nEND:VCALENDAR\\r\\n");
|
|
183
|
+
* // → { "@type":"Event", uid:"a@b", updated:"2026-05-21T10:00:00Z", ... }
|
|
184
|
+
*/
|
|
185
|
+
function fromIcal(text, opts) {
|
|
186
|
+
var ast = safeIcal.parse(text, opts || {});
|
|
187
|
+
var events = (ast && ast.vcalendar && ast.vcalendar.vevent) || [];
|
|
188
|
+
if (events.length === 0) {
|
|
189
|
+
throw new CalendarError("calendar/no-vevent",
|
|
190
|
+
"b.calendar.fromIcal: VCALENDAR has no VEVENT components");
|
|
191
|
+
}
|
|
192
|
+
var converted = events.map(_veventToJsCalEvent);
|
|
193
|
+
return converted.length === 1 ? converted[0] : converted;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @primitive b.calendar.toIcal
|
|
198
|
+
* @signature b.calendar.toIcal(jsCal, opts?)
|
|
199
|
+
* @since 0.11.31
|
|
200
|
+
* @status stable
|
|
201
|
+
*
|
|
202
|
+
* Render a JSCalendar Event back to RFC 5545 iCalendar text. Returns a
|
|
203
|
+
* CRLF-terminated string wrapped in a `BEGIN:VCALENDAR / VERSION:2.0 /
|
|
204
|
+
* PRODID:-//blamejs//Calendar//EN / BEGIN:VEVENT ... END:VEVENT /
|
|
205
|
+
* END:VCALENDAR` envelope per RFC 5545 §3.4.
|
|
206
|
+
*
|
|
207
|
+
* @opts
|
|
208
|
+
* prodid: string, // PRODID value to emit; default "-//blamejs//Calendar//EN"
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* b.calendar.toIcal({
|
|
212
|
+
* "@type": "Event",
|
|
213
|
+
* uid: "a@b",
|
|
214
|
+
* updated: "2026-05-21T10:00:00Z",
|
|
215
|
+
* title: "Sprint",
|
|
216
|
+
* start: "2026-05-22T09:00:00",
|
|
217
|
+
* duration: "PT1H",
|
|
218
|
+
* });
|
|
219
|
+
*/
|
|
220
|
+
function toIcal(jsCal, opts) {
|
|
221
|
+
validate(jsCal);
|
|
222
|
+
var prodid = (opts && opts.prodid) || "-//blamejs//Calendar//EN";
|
|
223
|
+
var lines = [
|
|
224
|
+
"BEGIN:VCALENDAR",
|
|
225
|
+
"VERSION:2.0",
|
|
226
|
+
"PRODID:" + prodid,
|
|
227
|
+
"BEGIN:VEVENT",
|
|
228
|
+
"UID:" + _foldLine(jsCal.uid),
|
|
229
|
+
"DTSTAMP:" + _utcDateTimeToIcal(jsCal.updated),
|
|
230
|
+
];
|
|
231
|
+
if (jsCal.title) lines.push("SUMMARY:" + _foldLine(_escapeText(jsCal.title)));
|
|
232
|
+
if (jsCal.description) lines.push("DESCRIPTION:" + _foldLine(_escapeText(jsCal.description)));
|
|
233
|
+
if (jsCal.start) {
|
|
234
|
+
// RFC 8984 §1.4.4 maps `timeZone: "Etc/UTC"` to a `Z`-suffix
|
|
235
|
+
// DTSTART (RFC 5545 §3.3.5 form 2); any other named timezone
|
|
236
|
+
// maps to a TZID parameter (form 3); no timeZone leaves DTSTART
|
|
237
|
+
// as floating local time (form 1).
|
|
238
|
+
var dtStartIcal = _localDateTimeToIcal(jsCal.start);
|
|
239
|
+
if (jsCal.timeZone === "Etc/UTC" || jsCal.timeZone === "UTC") {
|
|
240
|
+
lines.push("DTSTART:" + dtStartIcal + "Z");
|
|
241
|
+
} else if (jsCal.timeZone) {
|
|
242
|
+
lines.push("DTSTART;TZID=" + jsCal.timeZone + ":" + dtStartIcal);
|
|
243
|
+
} else {
|
|
244
|
+
lines.push("DTSTART:" + dtStartIcal);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (jsCal.duration) lines.push("DURATION:" + jsCal.duration);
|
|
248
|
+
if (Array.isArray(jsCal.locations) || (jsCal.locations && typeof jsCal.locations === "object")) {
|
|
249
|
+
var locValues = Array.isArray(jsCal.locations) ? jsCal.locations : Object.values(jsCal.locations);
|
|
250
|
+
for (var li = 0; li < locValues.length; li += 1) {
|
|
251
|
+
var loc = locValues[li];
|
|
252
|
+
if (loc && typeof loc.name === "string") {
|
|
253
|
+
lines.push("LOCATION:" + _foldLine(_escapeText(loc.name)));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (Array.isArray(jsCal.recurrenceRules)) {
|
|
258
|
+
for (var rri = 0; rri < jsCal.recurrenceRules.length; rri += 1) {
|
|
259
|
+
lines.push("RRULE:" + _recurrenceRuleToIcal(jsCal.recurrenceRules[rri]));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
lines.push("END:VEVENT", "END:VCALENDAR");
|
|
263
|
+
return lines.join("\r\n") + "\r\n";
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @primitive b.calendar.expandRecurrence
|
|
268
|
+
* @signature b.calendar.expandRecurrence(event, opts)
|
|
269
|
+
* @since 0.11.31
|
|
270
|
+
* @status stable
|
|
271
|
+
*
|
|
272
|
+
* Expand a JSCalendar Event's `recurrenceRules` into concrete start
|
|
273
|
+
* timestamps in the operator's `[from, to]` window. Returns an array
|
|
274
|
+
* of ISO 8601 UTC strings (`yyyy-mm-ddTHH:MM:SSZ`). Bounded by
|
|
275
|
+
* `MAX_EXPAND_INSTANCES` (4096) + `MAX_EXPAND_SPAN_MS` (10 years) to
|
|
276
|
+
* defend against CVE-2024-39687-class recurrence-bomb expansion.
|
|
277
|
+
*
|
|
278
|
+
* v1 supports FREQ=DAILY/WEEKLY/MONTHLY/YEARLY with INTERVAL, COUNT,
|
|
279
|
+
* UNTIL. BYDAY / BYMONTH / BYMONTHDAY refine the base frequency. The
|
|
280
|
+
* BYSETPOS / BYWEEKNO / BYYEARDAY filters are deferred-with-condition
|
|
281
|
+
* (RFC 7529 non-Gregorian calendars not in scope either).
|
|
282
|
+
*
|
|
283
|
+
* @opts
|
|
284
|
+
* from: string, // ISO 8601 UTC timestamp — lower bound of expansion window
|
|
285
|
+
* to: string, // ISO 8601 UTC timestamp — upper bound (window <= 10 years)
|
|
286
|
+
* max: number, // instance-count cap (default 4096; never exceeds MAX_EXPAND_INSTANCES)
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* b.calendar.expandRecurrence(
|
|
290
|
+
* { "@type": "Event", uid: "x", updated: "2026-05-21T10:00:00Z",
|
|
291
|
+
* start: "2026-05-22T09:00:00",
|
|
292
|
+
* recurrenceRules: [{ "@type": "RecurrenceRule", frequency: "daily", count: 5 }] },
|
|
293
|
+
* { from: "2026-05-22T00:00:00Z", to: "2026-06-01T00:00:00Z" });
|
|
294
|
+
* // → ["2026-05-22T09:00:00Z", "2026-05-23T09:00:00Z", ..., "2026-05-26T09:00:00Z"]
|
|
295
|
+
*/
|
|
296
|
+
function expandRecurrence(event, opts) {
|
|
297
|
+
validate(event);
|
|
298
|
+
opts = opts || {};
|
|
299
|
+
if (!Array.isArray(event.recurrenceRules) || event.recurrenceRules.length === 0) {
|
|
300
|
+
return event.start ? [_localToUtc(event.start)] : [];
|
|
301
|
+
}
|
|
302
|
+
var fromMs = opts.from ? Date.parse(opts.from) : null;
|
|
303
|
+
var toMs = opts.to ? Date.parse(opts.to) : null;
|
|
304
|
+
if (fromMs !== null && toMs !== null) {
|
|
305
|
+
if (toMs - fromMs > MAX_EXPAND_SPAN_MS) {
|
|
306
|
+
throw new CalendarError("calendar/oversize-expansion-span",
|
|
307
|
+
"b.calendar.expandRecurrence: window [" + opts.from + ", " + opts.to + "] exceeds 10 years");
|
|
308
|
+
}
|
|
309
|
+
if (toMs < fromMs) {
|
|
310
|
+
throw new CalendarError("calendar/bad-expansion-window",
|
|
311
|
+
"b.calendar.expandRecurrence: opts.to must be after opts.from");
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
var maxCount = Math.min(opts.max || MAX_EXPAND_INSTANCES, MAX_EXPAND_INSTANCES);
|
|
315
|
+
// JSCalendar's LocalDateTime is FLOATING when no timeZone is set;
|
|
316
|
+
// for expansion we treat it as already-UTC so the returned ISO
|
|
317
|
+
// strings carry the same wall-clock the operator stored. Appending
|
|
318
|
+
// `Z` to the LocalDateTime sidesteps Date.parse's host-locale
|
|
319
|
+
// interpretation (which would otherwise mangle the wall-clock).
|
|
320
|
+
var startInput = _isLocalDateTime(event.start) ? event.start + "Z" : event.start;
|
|
321
|
+
var startMs = Date.parse(startInput);
|
|
322
|
+
if (!isFinite(startMs)) {
|
|
323
|
+
throw new CalendarError("calendar/bad-start",
|
|
324
|
+
"b.calendar.expandRecurrence: event.start is not a parseable date");
|
|
325
|
+
}
|
|
326
|
+
var out = [];
|
|
327
|
+
// We honour ONLY the first recurrenceRule in v1; multiple rules
|
|
328
|
+
// compose via union which is a follow-up.
|
|
329
|
+
var rule = event.recurrenceRules[0];
|
|
330
|
+
var interval = Math.max(1, parseInt(rule.interval || 1, 10));
|
|
331
|
+
var freq = rule.frequency;
|
|
332
|
+
var count = isFinite(rule.count) ? rule.count : Infinity;
|
|
333
|
+
var untilMs = rule.until ? Date.parse(rule.until) : Infinity;
|
|
334
|
+
// RFC 5545 §3.3.10 BY* filters narrow which stepped occurrences
|
|
335
|
+
// emit. We support the BYDAY/BYMONTH/BYMONTHDAY subset; rule
|
|
336
|
+
// instances that fail the filter are stepped past WITHOUT counting
|
|
337
|
+
// against `count` (per RFC 5545 BY* expansion semantics — only
|
|
338
|
+
// surviving instances count).
|
|
339
|
+
var byDaySet = null;
|
|
340
|
+
if (Array.isArray(rule.byDay) && rule.byDay.length > 0) {
|
|
341
|
+
byDaySet = Object.create(null);
|
|
342
|
+
var dayCodes = { su: 0, mo: 1, tu: 2, we: 3, th: 4, fr: 5, sa: 6 };
|
|
343
|
+
for (var bi = 0; bi < rule.byDay.length; bi += 1) {
|
|
344
|
+
var entry = rule.byDay[bi];
|
|
345
|
+
var dayKey = (entry && entry.day ? entry.day : entry || "").toLowerCase();
|
|
346
|
+
if (Object.prototype.hasOwnProperty.call(dayCodes, dayKey)) {
|
|
347
|
+
byDaySet[dayCodes[dayKey]] = true;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
var byMonthSet = null;
|
|
352
|
+
if (Array.isArray(rule.byMonth) && rule.byMonth.length > 0) {
|
|
353
|
+
byMonthSet = Object.create(null);
|
|
354
|
+
for (var mi = 0; mi < rule.byMonth.length; mi += 1) {
|
|
355
|
+
var mn = parseInt(rule.byMonth[mi], 10);
|
|
356
|
+
if (isFinite(mn) && mn >= 1 && mn <= 12) byMonthSet[mn] = true; // allow:raw-byte-literal — 12 calendar months
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
var byMonthDaySet = null;
|
|
360
|
+
if (Array.isArray(rule.byMonthDay) && rule.byMonthDay.length > 0) {
|
|
361
|
+
byMonthDaySet = Object.create(null);
|
|
362
|
+
for (var mdi = 0; mdi < rule.byMonthDay.length; mdi += 1) {
|
|
363
|
+
var mdn = parseInt(rule.byMonthDay[mdi], 10);
|
|
364
|
+
if (isFinite(mdn) && mdn !== 0 && mdn >= -31 && mdn <= 31) byMonthDaySet[mdn] = true; // allow:raw-byte-literal — calendar day-of-month bounds
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function _matchesBy(t) {
|
|
368
|
+
var d = new Date(t);
|
|
369
|
+
if (byDaySet && !byDaySet[d.getUTCDay()]) return false;
|
|
370
|
+
if (byMonthSet && !byMonthSet[d.getUTCMonth() + 1]) return false;
|
|
371
|
+
if (byMonthDaySet && !byMonthDaySet[d.getUTCDate()]) return false;
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
var t = startMs;
|
|
375
|
+
// Safety cap on the step loop: at most MAX_EXPAND_INSTANCES * 366
|
|
376
|
+
// iterations so BY* filters that match sparsely (e.g. FREQ=DAILY;
|
|
377
|
+
// BYMONTH=1 — only Jan days survive) cannot loop forever inside
|
|
378
|
+
// the 10-year span cap.
|
|
379
|
+
var stepBudget = MAX_EXPAND_INSTANCES * 366; // allow:raw-byte-literal — days/year stepping budget
|
|
380
|
+
while (out.length < count && out.length < maxCount && stepBudget > 0) {
|
|
381
|
+
stepBudget -= 1;
|
|
382
|
+
if (t > untilMs) break;
|
|
383
|
+
if (toMs !== null && t > toMs) break;
|
|
384
|
+
if (_matchesBy(t)) {
|
|
385
|
+
if (fromMs === null || t >= fromMs) {
|
|
386
|
+
out.push(_msToIsoZ(t));
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
t = _advance(t, freq, interval);
|
|
390
|
+
if (t === null) {
|
|
391
|
+
throw new CalendarError("calendar/bad-recurrence",
|
|
392
|
+
"b.calendar.expandRecurrence: unsupported frequency '" + freq + "'");
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return out;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// ---- Internal helpers ----------------------------------------------------
|
|
399
|
+
|
|
400
|
+
function _veventToJsCalEvent(ve) {
|
|
401
|
+
var props = (ve && ve.properties) || {};
|
|
402
|
+
var jsCal = {
|
|
403
|
+
"@type": "Event",
|
|
404
|
+
uid: _firstValue(props.UID) || "",
|
|
405
|
+
updated: _icalDateTimeToUtc(_firstValue(props.DTSTAMP) || ""),
|
|
406
|
+
};
|
|
407
|
+
var summary = _firstValue(props.SUMMARY);
|
|
408
|
+
if (summary) jsCal.title = _unescapeText(summary);
|
|
409
|
+
var description = _firstValue(props.DESCRIPTION);
|
|
410
|
+
if (description) jsCal.description = _unescapeText(description);
|
|
411
|
+
var dtstart = _firstValue(props.DTSTART);
|
|
412
|
+
if (dtstart) jsCal.start = _icalDateTimeToLocal(dtstart);
|
|
413
|
+
var duration = _firstValue(props.DURATION);
|
|
414
|
+
if (duration) jsCal.duration = duration;
|
|
415
|
+
var tzid = _firstParamValue(props.DTSTART, "TZID");
|
|
416
|
+
if (tzid) {
|
|
417
|
+
jsCal.timeZone = tzid;
|
|
418
|
+
} else if (typeof dtstart === "string" && /Z$/.test(dtstart)) {
|
|
419
|
+
// Codex P1 — RFC 8984 §1.4.4: a UTC-suffix DTSTART (`...Z`) in
|
|
420
|
+
// iCalendar maps to a JSCalendar Event with `timeZone: "Etc/UTC"`.
|
|
421
|
+
// Without this, round-tripping `fromIcal` → `toIcal` would drop
|
|
422
|
+
// the UTC anchor + emit floating time, shifting the absolute
|
|
423
|
+
// instant for viewers in different timezones.
|
|
424
|
+
jsCal.timeZone = "Etc/UTC";
|
|
425
|
+
}
|
|
426
|
+
var location = _firstValue(props.LOCATION);
|
|
427
|
+
if (location) {
|
|
428
|
+
jsCal.locations = { L1: { "@type": "Location", name: _unescapeText(location) } };
|
|
429
|
+
}
|
|
430
|
+
var rrule = _firstValue(props.RRULE);
|
|
431
|
+
if (rrule) jsCal.recurrenceRules = [_icalRruleToJscal(rrule)];
|
|
432
|
+
return jsCal;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function _firstValue(prop) {
|
|
436
|
+
if (!prop) return null;
|
|
437
|
+
if (Array.isArray(prop)) {
|
|
438
|
+
var first = prop[0];
|
|
439
|
+
return first && first.value !== undefined ? first.value : null;
|
|
440
|
+
}
|
|
441
|
+
if (prop.value !== undefined) return prop.value;
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function _firstParamValue(prop, paramName) {
|
|
446
|
+
if (!prop) return null;
|
|
447
|
+
var first = Array.isArray(prop) ? prop[0] : prop;
|
|
448
|
+
if (!first || !first.params) return null;
|
|
449
|
+
return first.params[paramName] || null;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function _icalRruleToJscal(rrule) {
|
|
453
|
+
var out = { "@type": "RecurrenceRule", frequency: "daily" };
|
|
454
|
+
var parts = String(rrule).split(";"); // allow:bare-split-on-quoted-header — RFC 5545 RRULE grammar has no quoted-string members; values are token-only
|
|
455
|
+
for (var i = 0; i < parts.length; i += 1) {
|
|
456
|
+
var kv = parts[i].split("=");
|
|
457
|
+
if (kv.length !== 2) continue;
|
|
458
|
+
var key = kv[0].toUpperCase();
|
|
459
|
+
var val = kv[1];
|
|
460
|
+
if (key === "FREQ") out.frequency = val.toLowerCase();
|
|
461
|
+
else if (key === "INTERVAL") out.interval = parseInt(val, 10);
|
|
462
|
+
else if (key === "COUNT") out.count = parseInt(val, 10);
|
|
463
|
+
else if (key === "UNTIL") out.until = _icalDateTimeToUtc(val);
|
|
464
|
+
else if (key === "BYDAY") out.byDay = val.split(",").map(function (d) { // allow:bare-split-on-quoted-header — RFC 5545 BYDAY values are token-only
|
|
465
|
+
return { "@type": "NDay", day: d.slice(-2).toLowerCase() };
|
|
466
|
+
});
|
|
467
|
+
else if (key === "BYMONTH") out.byMonth = val.split(","); // allow:bare-split-on-quoted-header — RFC 5545 BYMONTH values are integer-only
|
|
468
|
+
else if (key === "BYMONTHDAY") out.byMonthDay = val.split(",").map(function (n) { return parseInt(n, 10); }); // allow:bare-split-on-quoted-header — RFC 5545 BYMONTHDAY values are integer-only
|
|
469
|
+
}
|
|
470
|
+
return out;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function _recurrenceRuleToIcal(rr) {
|
|
474
|
+
var parts = ["FREQ=" + (rr.frequency || "daily").toUpperCase()];
|
|
475
|
+
if (rr.interval && rr.interval !== 1) parts.push("INTERVAL=" + rr.interval);
|
|
476
|
+
if (rr.count) parts.push("COUNT=" + rr.count);
|
|
477
|
+
if (rr.until) parts.push("UNTIL=" + _utcDateTimeToIcal(rr.until));
|
|
478
|
+
if (Array.isArray(rr.byDay) && rr.byDay.length > 0) {
|
|
479
|
+
parts.push("BYDAY=" + rr.byDay.map(function (d) { return (d.day || "").toUpperCase(); }).join(","));
|
|
480
|
+
}
|
|
481
|
+
if (Array.isArray(rr.byMonth)) parts.push("BYMONTH=" + rr.byMonth.join(","));
|
|
482
|
+
if (Array.isArray(rr.byMonthDay)) parts.push("BYMONTHDAY=" + rr.byMonthDay.join(","));
|
|
483
|
+
return parts.join(";");
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function _icalDateTimeToUtc(s) {
|
|
487
|
+
// VALUE=DATE-TIME UTC form: 20260522T100000Z → 2026-05-22T10:00:00Z
|
|
488
|
+
var m = String(s).match(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/);
|
|
489
|
+
if (!m) return "";
|
|
490
|
+
return m[1] + "-" + m[2] + "-" + m[3] + "T" + m[4] + ":" + m[5] + ":" + m[6] + "Z";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function _icalDateTimeToLocal(s) {
|
|
494
|
+
var m = String(s).match(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z?$/);
|
|
495
|
+
if (!m) return "";
|
|
496
|
+
return m[1] + "-" + m[2] + "-" + m[3] + "T" + m[4] + ":" + m[5] + ":" + m[6];
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function _utcDateTimeToIcal(s) {
|
|
500
|
+
// JSCalendar UTCDateTime "2026-05-22T10:00:00.123Z" →
|
|
501
|
+
// "20260522T100000Z" (RFC 5545 §3.3.5 form 2 has NO fractional
|
|
502
|
+
// seconds; strict ICS consumers reject `T100000.123Z`).
|
|
503
|
+
return String(s).replace(/\.\d+/, "").replace(/[-:]/g, ""); // allow:bare-split-on-quoted-header — not a header split
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function _localDateTimeToIcal(s) {
|
|
507
|
+
// JSCalendar LocalDateTime "2026-05-22T09:00:00.123" →
|
|
508
|
+
// "20260522T090000" (same fractional-second strip as the UTC form).
|
|
509
|
+
return String(s).replace(/\.\d+/, "").replace(/[-:]/g, ""); // allow:bare-split-on-quoted-header — not a header split
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function _isUtcDateTime(s) {
|
|
513
|
+
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/.test(s);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function _isLocalDateTime(s) {
|
|
517
|
+
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?$/.test(s);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function _isDuration(s) {
|
|
521
|
+
return /^-?P(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$/.test(s);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function _localToUtc(localStr) {
|
|
525
|
+
// Naive — treats LocalDateTime as already-UTC for the no-tz case.
|
|
526
|
+
return localStr.endsWith("Z") ? localStr : localStr + "Z";
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function _msToIsoZ(ms) {
|
|
530
|
+
return time.toIso8601NoMs(new Date(ms));
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function _advance(ms, freq, interval) {
|
|
534
|
+
var d = new Date(ms);
|
|
535
|
+
switch (freq) {
|
|
536
|
+
case "daily": d.setUTCDate(d.getUTCDate() + interval); break;
|
|
537
|
+
case "weekly": d.setUTCDate(d.getUTCDate() + 7 * interval); break; // allow:raw-byte-literal — 7 days/week
|
|
538
|
+
case "monthly": d.setUTCMonth(d.getUTCMonth() + interval); break;
|
|
539
|
+
case "yearly": d.setUTCFullYear(d.getUTCFullYear() + interval); break;
|
|
540
|
+
case "hourly": d.setUTCHours(d.getUTCHours() + interval); break;
|
|
541
|
+
case "minutely": d.setUTCMinutes(d.getUTCMinutes() + interval); break;
|
|
542
|
+
case "secondly": d.setUTCSeconds(d.getUTCSeconds() + interval); break;
|
|
543
|
+
default: return null;
|
|
544
|
+
}
|
|
545
|
+
return d.getTime();
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function _escapeText(s) {
|
|
549
|
+
return String(s).replace(/\\/g, "\\\\").replace(/;/g, "\\;").replace(/,/g, "\\,").replace(/\n/g, "\\n");
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function _unescapeText(s) {
|
|
553
|
+
return String(s)
|
|
554
|
+
.replace(/\\n/g, "\n").replace(/\\,/g, ",")
|
|
555
|
+
.replace(/\\;/g, ";").replace(/\\\\/g, "\\");
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function _foldLine(s) {
|
|
559
|
+
// RFC 5545 §3.1 — content lines SHOULD NOT exceed 75 octets; fold
|
|
560
|
+
// with CRLF + leading space. We let the joining code add the
|
|
561
|
+
// trailing CRLF; this helper only inserts the intra-line fold.
|
|
562
|
+
if (s.length <= 75) return s; // allow:raw-byte-literal — RFC 5545 §3.1 line-length cap
|
|
563
|
+
var out = "";
|
|
564
|
+
for (var i = 0; i < s.length; i += 73) { // allow:raw-byte-literal — 73 = 75 minus the CR/LF wrap
|
|
565
|
+
out += (i === 0 ? "" : "\r\n ") + s.slice(i, i + 73); // allow:raw-byte-literal — same cap
|
|
566
|
+
}
|
|
567
|
+
return out;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
module.exports = {
|
|
571
|
+
validate: validate,
|
|
572
|
+
fromIcal: fromIcal,
|
|
573
|
+
toIcal: toIcal,
|
|
574
|
+
expandRecurrence: expandRecurrence,
|
|
575
|
+
CalendarError: CalendarError,
|
|
576
|
+
JSCAL_TYPES: JSCAL_TYPES,
|
|
577
|
+
JSCAL_FREQUENCIES: JSCAL_FREQUENCIES,
|
|
578
|
+
JSCAL_ALERT_ACTIONS: JSCAL_ALERT_ACTIONS,
|
|
579
|
+
MAX_EXPAND_INSTANCES: MAX_EXPAND_INSTANCES,
|
|
580
|
+
MAX_EXPAND_SPAN_MS: MAX_EXPAND_SPAN_MS,
|
|
581
|
+
};
|
package/lib/mail-server-jmap.js
CHANGED
|
@@ -121,6 +121,7 @@ var lazyRequire = require("./lazy-require");
|
|
|
121
121
|
var C = require("./constants");
|
|
122
122
|
var bCrypto = require("./crypto");
|
|
123
123
|
var safeJson = require("./safe-json");
|
|
124
|
+
var safeBuffer = require("./safe-buffer");
|
|
124
125
|
var validateOpts = require("./validate-opts");
|
|
125
126
|
var guardJmap = require("./guard-jmap");
|
|
126
127
|
var mailServerRegistry = require("./mail-server-registry");
|
|
@@ -670,6 +671,306 @@ function create(opts) {
|
|
|
670
671
|
req.on("error", _cleanup);
|
|
671
672
|
}
|
|
672
673
|
|
|
674
|
+
// RFC 8620 §6.1 — blob upload. Operators POST raw bytes to
|
|
675
|
+
// `/jmap/upload/{accountId}` with `Content-Type` set to the
|
|
676
|
+
// blob MIME type. The handler streams the request body into a
|
|
677
|
+
// bounded buffer, calls `mailStore.uploadBlob(actor, accountId,
|
|
678
|
+
// contentType, bytes)`, and returns the JSON descriptor
|
|
679
|
+
// `{ accountId, blobId, type, size }` the client uses in
|
|
680
|
+
// subsequent Email/set / Email/import calls.
|
|
681
|
+
//
|
|
682
|
+
// Path parameters are extracted from the URL; the operator-side
|
|
683
|
+
// HTTP router MUST mount this handler at a prefix that exposes
|
|
684
|
+
// the accountId segment (e.g. `/jmap/upload/:accountId`). The
|
|
685
|
+
// handler defensively re-parses the URL in case the router didn't
|
|
686
|
+
// populate `req.params`.
|
|
687
|
+
var DEFAULT_MAX_BLOB_BYTES = opts.maxBlobBytes || (50 * 1024 * 1024); // allow:raw-byte-literal — 50 MiB default blob upload cap
|
|
688
|
+
// RFC 8620 §1.2 — JMAP `Id` is a non-empty string of < 256 octets in
|
|
689
|
+
// `[A-Za-z0-9_-]`. The earlier shape capped at 64 chars which refused
|
|
690
|
+
// legitimate-shape accounts; widen to the full spec maximum.
|
|
691
|
+
var MAX_JMAP_ID_LEN = 255; // allow:raw-byte-literal — RFC 8620 §1.2 Id max length
|
|
692
|
+
var JMAP_ID_RE = /^[A-Za-z0-9_-]{1,255}$/;
|
|
693
|
+
// Anti-polynomial: bound the URL length BEFORE any regex / split runs
|
|
694
|
+
// (CodeQL flags `\/+` on uncontrolled input). Headers + URL paths in
|
|
695
|
+
// practice stay well under 8 KiB; over-long URLs refuse outright.
|
|
696
|
+
var MAX_URL_LEN = 8192; // allow:raw-byte-literal — 8 KiB URL cap
|
|
697
|
+
|
|
698
|
+
// Strip a query string + walk the path producing non-empty segments,
|
|
699
|
+
// WITHOUT any unbounded regex. Returns an empty array when the URL
|
|
700
|
+
// is over the cap so the caller can refuse with 400.
|
|
701
|
+
function _splitPathSegments(rawUrl) {
|
|
702
|
+
if (typeof rawUrl !== "string" || rawUrl.length === 0 || rawUrl.length > MAX_URL_LEN) {
|
|
703
|
+
return [];
|
|
704
|
+
}
|
|
705
|
+
var qIdx = rawUrl.indexOf("?");
|
|
706
|
+
var pathOnly = qIdx === -1 ? rawUrl : rawUrl.slice(0, qIdx);
|
|
707
|
+
var out = [];
|
|
708
|
+
var cur = "";
|
|
709
|
+
for (var i = 0; i < pathOnly.length; i += 1) {
|
|
710
|
+
var ch = pathOnly.charCodeAt(i);
|
|
711
|
+
if (ch === 0x2f) { // allow:raw-byte-literal — '/' (0x2f)
|
|
712
|
+
if (cur.length > 0) { out.push(cur); cur = ""; }
|
|
713
|
+
} else {
|
|
714
|
+
cur += pathOnly[i];
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
if (cur.length > 0) out.push(cur);
|
|
718
|
+
return out;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function uploadHandler(req, res) {
|
|
722
|
+
var actor = req.user || (req.actor || null);
|
|
723
|
+
if (!actor) {
|
|
724
|
+
res.statusCode = 401;
|
|
725
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
726
|
+
res.end(JSON.stringify({
|
|
727
|
+
type: "urn:ietf:params:jmap:error:forbidden",
|
|
728
|
+
description: "Authentication required",
|
|
729
|
+
}));
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
if (typeof opts.mailStore.uploadBlob !== "function") {
|
|
733
|
+
res.statusCode = 503;
|
|
734
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
735
|
+
res.end(JSON.stringify({
|
|
736
|
+
type: "urn:ietf:params:jmap:error:serverUnavailable",
|
|
737
|
+
description: "Upload backend not configured (mailStore.uploadBlob)",
|
|
738
|
+
}));
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
// Extract accountId from URL path. The mount path is
|
|
742
|
+
// `/jmap/upload/{accountId}`; the operator's router may strip
|
|
743
|
+
// the `/jmap/upload/` prefix (so segments == [accountId]) OR
|
|
744
|
+
// pass through the full path. Either shape gives the trailing
|
|
745
|
+
// segment as accountId — but the WHOLE URL must split cleanly
|
|
746
|
+
// (`_splitPathSegments` refuses over-long input).
|
|
747
|
+
var segments = _splitPathSegments(req.url);
|
|
748
|
+
if (segments.length === 0) {
|
|
749
|
+
res.statusCode = 400;
|
|
750
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
751
|
+
res.end(JSON.stringify({
|
|
752
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
753
|
+
description: "Upload URL is empty or exceeds the " + MAX_URL_LEN + "-byte cap",
|
|
754
|
+
}));
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
var accountId = (req.params && req.params.accountId) || segments[segments.length - 1] || "";
|
|
758
|
+
if (!accountId || accountId.length > MAX_JMAP_ID_LEN || !JMAP_ID_RE.test(accountId)) {
|
|
759
|
+
res.statusCode = 400;
|
|
760
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
761
|
+
res.end(JSON.stringify({
|
|
762
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
763
|
+
description: "Upload URL missing or malformed accountId path segment (JMAP Id: [A-Za-z0-9_-]{1," + MAX_JMAP_ID_LEN + "})",
|
|
764
|
+
}));
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
var contentType = req.headers && req.headers["content-type"]
|
|
768
|
+
? String(req.headers["content-type"]).split(";")[0].trim()
|
|
769
|
+
: "application/octet-stream";
|
|
770
|
+
var collector = safeBuffer.boundedChunkCollector({
|
|
771
|
+
maxBytes: DEFAULT_MAX_BLOB_BYTES,
|
|
772
|
+
errorClass: MailServerJmapError,
|
|
773
|
+
sizeCode: "mail-server-jmap/blob-too-large",
|
|
774
|
+
sizeMessage: "Blob exceeds maxSizeUpload (" + DEFAULT_MAX_BLOB_BYTES + " bytes)",
|
|
775
|
+
});
|
|
776
|
+
var refused = false;
|
|
777
|
+
|
|
778
|
+
req.on("data", function (chunk) {
|
|
779
|
+
if (refused) return;
|
|
780
|
+
try { collector.push(chunk); }
|
|
781
|
+
catch (_e) {
|
|
782
|
+
refused = true;
|
|
783
|
+
res.statusCode = 413;
|
|
784
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
785
|
+
res.end(JSON.stringify({
|
|
786
|
+
type: "urn:ietf:params:jmap:error:limit",
|
|
787
|
+
limit: "maxSizeUpload",
|
|
788
|
+
description: "Blob exceeds maxSizeUpload (" + DEFAULT_MAX_BLOB_BYTES + " bytes)",
|
|
789
|
+
}));
|
|
790
|
+
try { req.destroy(); } catch (_e2) { /* silent-catch: socket already torn down */ }
|
|
791
|
+
}
|
|
792
|
+
});
|
|
793
|
+
req.on("end", function () {
|
|
794
|
+
if (refused) return;
|
|
795
|
+
var bytes = collector.result();
|
|
796
|
+
Promise.resolve()
|
|
797
|
+
.then(function () { return opts.mailStore.uploadBlob(actor, accountId, contentType, bytes); })
|
|
798
|
+
.then(function (meta) {
|
|
799
|
+
if (!meta || typeof meta !== "object" || typeof meta.blobId !== "string") {
|
|
800
|
+
throw new MailServerJmapError("mail-server-jmap/bad-upload-result",
|
|
801
|
+
"uploadBlob backend MUST return { blobId, type?, size? }");
|
|
802
|
+
}
|
|
803
|
+
res.statusCode = 201; // allow:raw-byte-literal — HTTP 201 Created
|
|
804
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
805
|
+
res.end(JSON.stringify({
|
|
806
|
+
accountId: accountId,
|
|
807
|
+
blobId: meta.blobId,
|
|
808
|
+
type: meta.type || contentType,
|
|
809
|
+
size: typeof meta.size === "number" ? meta.size : bytes.length,
|
|
810
|
+
}));
|
|
811
|
+
})
|
|
812
|
+
.catch(function (err) {
|
|
813
|
+
_emit("mail.server.jmap.upload_threw",
|
|
814
|
+
{ accountId: accountId, error: (err && err.message) || String(err) }, "failure");
|
|
815
|
+
res.statusCode = 500;
|
|
816
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
817
|
+
res.end(JSON.stringify({
|
|
818
|
+
type: "urn:ietf:params:jmap:error:serverFail",
|
|
819
|
+
description: "Upload failed",
|
|
820
|
+
}));
|
|
821
|
+
});
|
|
822
|
+
});
|
|
823
|
+
req.on("error", function () {
|
|
824
|
+
if (!refused) {
|
|
825
|
+
refused = true;
|
|
826
|
+
try { res.statusCode = 400; res.end(); } // allow:raw-byte-literal — HTTP 400
|
|
827
|
+
catch (_e) { /* silent-catch: socket already torn down */ }
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
// RFC 8620 §6.2 — blob download. GET `/jmap/download/{accountId}/
|
|
833
|
+
// {blobId}/{name}?accept={type}`. Backend hook returns a stream-
|
|
834
|
+
// shaped buffer (Buffer or async-iterable) + the canonical MIME
|
|
835
|
+
// type; the handler pipes it to the response.
|
|
836
|
+
function downloadHandler(req, res) {
|
|
837
|
+
var actor = req.user || (req.actor || null);
|
|
838
|
+
if (!actor) {
|
|
839
|
+
res.statusCode = 401;
|
|
840
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
841
|
+
res.end(JSON.stringify({
|
|
842
|
+
type: "urn:ietf:params:jmap:error:forbidden",
|
|
843
|
+
description: "Authentication required",
|
|
844
|
+
}));
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
if (typeof opts.mailStore.downloadBlob !== "function") {
|
|
848
|
+
res.statusCode = 503;
|
|
849
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
850
|
+
res.end(JSON.stringify({
|
|
851
|
+
type: "urn:ietf:params:jmap:error:serverUnavailable",
|
|
852
|
+
description: "Download backend not configured (mailStore.downloadBlob)",
|
|
853
|
+
}));
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
var rawUrl = String(req.url || "");
|
|
857
|
+
if (rawUrl.length > MAX_URL_LEN) {
|
|
858
|
+
res.statusCode = 400;
|
|
859
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
860
|
+
res.end(JSON.stringify({
|
|
861
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
862
|
+
description: "Download URL exceeds the " + MAX_URL_LEN + "-byte cap",
|
|
863
|
+
}));
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
var qIdx2 = rawUrl.indexOf("?");
|
|
867
|
+
var query = qIdx2 === -1 ? "" : rawUrl.slice(qIdx2 + 1);
|
|
868
|
+
var acceptType = "";
|
|
869
|
+
query.split("&").forEach(function (pair) {
|
|
870
|
+
if (!pair) return;
|
|
871
|
+
var eq = pair.indexOf("=");
|
|
872
|
+
var k = eq === -1 ? pair : pair.slice(0, eq);
|
|
873
|
+
var v = eq === -1 ? "" : pair.slice(eq + 1);
|
|
874
|
+
if (k === "accept") {
|
|
875
|
+
try { acceptType = decodeURIComponent(v); } catch (_e) { /* silent-catch: malformed % encoding */ }
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
// Path parsing — `/jmap/download/{accountId}/{blobId}/{name}`. The
|
|
879
|
+
// operator's router may strip the `/jmap/download/` prefix, so
|
|
880
|
+
// valid segment counts are EXACTLY 3 (router-stripped) OR 5+ AND
|
|
881
|
+
// starting with `jmap` + `download`. Anything else refuses BEFORE
|
|
882
|
+
// a tail-segment remap could land path tokens in the wrong
|
|
883
|
+
// accountId / blobId / name slots.
|
|
884
|
+
var pathSegs = _splitPathSegments(rawUrl);
|
|
885
|
+
var routerSupplied = req.params && req.params.accountId && req.params.blobId && req.params.name;
|
|
886
|
+
var accountId, blobId, fileName;
|
|
887
|
+
if (routerSupplied) {
|
|
888
|
+
accountId = req.params.accountId;
|
|
889
|
+
blobId = req.params.blobId;
|
|
890
|
+
fileName = req.params.name;
|
|
891
|
+
} else if (pathSegs.length === 3) {
|
|
892
|
+
accountId = pathSegs[0];
|
|
893
|
+
blobId = pathSegs[1];
|
|
894
|
+
fileName = pathSegs[2];
|
|
895
|
+
} else if (pathSegs.length >= 5 &&
|
|
896
|
+
pathSegs[pathSegs.length - 5].toLowerCase() === "jmap" &&
|
|
897
|
+
pathSegs[pathSegs.length - 4].toLowerCase() === "download") {
|
|
898
|
+
accountId = pathSegs[pathSegs.length - 3];
|
|
899
|
+
blobId = pathSegs[pathSegs.length - 2];
|
|
900
|
+
fileName = pathSegs[pathSegs.length - 1];
|
|
901
|
+
} else {
|
|
902
|
+
res.statusCode = 400;
|
|
903
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
904
|
+
res.end(JSON.stringify({
|
|
905
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
906
|
+
description: "Download URL must be /jmap/download/{accountId}/{blobId}/{name} (or router-stripped {accountId}/{blobId}/{name})",
|
|
907
|
+
}));
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
if (!accountId || accountId.length > MAX_JMAP_ID_LEN || !JMAP_ID_RE.test(accountId)) {
|
|
911
|
+
res.statusCode = 400;
|
|
912
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
913
|
+
res.end(JSON.stringify({
|
|
914
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
915
|
+
description: "Download URL has malformed accountId segment (JMAP Id: [A-Za-z0-9_-]{1," + MAX_JMAP_ID_LEN + "})",
|
|
916
|
+
}));
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
if (!blobId || blobId.length > MAX_JMAP_ID_LEN || !JMAP_ID_RE.test(blobId)) {
|
|
920
|
+
res.statusCode = 400;
|
|
921
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
922
|
+
res.end(JSON.stringify({
|
|
923
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
924
|
+
description: "Download URL has malformed blobId segment (JMAP Id: [A-Za-z0-9_-]{1," + MAX_JMAP_ID_LEN + "})",
|
|
925
|
+
}));
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
Promise.resolve()
|
|
929
|
+
.then(function () { return opts.mailStore.downloadBlob(actor, accountId, blobId); })
|
|
930
|
+
.then(function (result) {
|
|
931
|
+
if (!result || (typeof result !== "object" && !Buffer.isBuffer(result))) {
|
|
932
|
+
res.statusCode = 404;
|
|
933
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
934
|
+
res.end(JSON.stringify({
|
|
935
|
+
type: "urn:ietf:params:jmap:error:invalidArguments",
|
|
936
|
+
description: "Blob not found",
|
|
937
|
+
}));
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
var bytes = Buffer.isBuffer(result) ? result : result.bytes;
|
|
941
|
+
var bType = result.type || acceptType || "application/octet-stream";
|
|
942
|
+
if (!Buffer.isBuffer(bytes)) {
|
|
943
|
+
res.statusCode = 500;
|
|
944
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
945
|
+
res.end(JSON.stringify({
|
|
946
|
+
type: "urn:ietf:params:jmap:error:serverFail",
|
|
947
|
+
description: "downloadBlob backend returned a non-Buffer body",
|
|
948
|
+
}));
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
res.statusCode = 200;
|
|
952
|
+
res.setHeader("Content-Type", bType);
|
|
953
|
+
res.setHeader("Content-Length", bytes.length);
|
|
954
|
+
// RFC 5987 — operator may want to surface fileName via
|
|
955
|
+
// Content-Disposition. Default to attachment when the
|
|
956
|
+
// download is a non-text type.
|
|
957
|
+
if (fileName && /^[A-Za-z0-9._-]{1,200}$/.test(fileName)) {
|
|
958
|
+
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
|
959
|
+
}
|
|
960
|
+
res.end(bytes);
|
|
961
|
+
})
|
|
962
|
+
.catch(function (err) {
|
|
963
|
+
_emit("mail.server.jmap.download_threw",
|
|
964
|
+
{ accountId: accountId, blobId: blobId, error: (err && err.message) || String(err) }, "failure");
|
|
965
|
+
res.statusCode = 500;
|
|
966
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
967
|
+
res.end(JSON.stringify({
|
|
968
|
+
type: "urn:ietf:params:jmap:error:serverFail",
|
|
969
|
+
description: "Download failed",
|
|
970
|
+
}));
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
|
|
673
974
|
function discoveryHandler(req, res) {
|
|
674
975
|
// RFC 8620 §2.2 — well-known endpoint redirects (or directly returns)
|
|
675
976
|
// the session URL. We redirect to /jmap/session per the most common
|
|
@@ -687,6 +988,8 @@ function create(opts) {
|
|
|
687
988
|
sessionHandler: sessionHandler,
|
|
688
989
|
discoveryHandler: discoveryHandler,
|
|
689
990
|
eventSourceHandler: eventSourceHandler,
|
|
991
|
+
uploadHandler: uploadHandler,
|
|
992
|
+
downloadHandler: downloadHandler,
|
|
690
993
|
MailServerJmapError: MailServerJmapError,
|
|
691
994
|
};
|
|
692
995
|
}
|
|
@@ -83,6 +83,17 @@ var JMAP_METHODS = Object.freeze({
|
|
|
83
83
|
"EmailSubmission/query": 1, "EmailSubmission/queryChanges": 1,
|
|
84
84
|
"EmailSubmission/set": 1,
|
|
85
85
|
"VacationResponse/get": 1, "VacationResponse/set": 1,
|
|
86
|
+
// v0.11.31 — JMAP Calendars (RFC 8984 + draft-ietf-jmap-calendars).
|
|
87
|
+
"Calendar/get": 1, "Calendar/changes": 1, "Calendar/set": 1,
|
|
88
|
+
"Calendar/query": 1, "Calendar/queryChanges": 1,
|
|
89
|
+
"CalendarEvent/get": 1, "CalendarEvent/changes": 1,
|
|
90
|
+
"CalendarEvent/query": 1, "CalendarEvent/queryChanges": 1,
|
|
91
|
+
"CalendarEvent/set": 1, "CalendarEvent/copy": 1,
|
|
92
|
+
"CalendarEventNotification/get": 1, "CalendarEventNotification/changes": 1,
|
|
93
|
+
"CalendarEventNotification/query": 1, "CalendarEventNotification/queryChanges": 1,
|
|
94
|
+
"CalendarEventNotification/set": 1,
|
|
95
|
+
"ParticipantIdentity/get": 1, "ParticipantIdentity/changes": 1,
|
|
96
|
+
"ParticipantIdentity/set": 1,
|
|
86
97
|
});
|
|
87
98
|
|
|
88
99
|
var CATALOGUE = Object.freeze({
|
package/package.json
CHANGED
package/sbom.cdx.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
|
|
3
3
|
"bomFormat": "CycloneDX",
|
|
4
4
|
"specVersion": "1.5",
|
|
5
|
-
"serialNumber": "urn:uuid:
|
|
5
|
+
"serialNumber": "urn:uuid:19702043-8392-4b48-9d1c-120d93f6889f",
|
|
6
6
|
"version": 1,
|
|
7
7
|
"metadata": {
|
|
8
|
-
"timestamp": "2026-05-
|
|
8
|
+
"timestamp": "2026-05-21T17:20:28.895Z",
|
|
9
9
|
"lifecycles": [
|
|
10
10
|
{
|
|
11
11
|
"phase": "build"
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
],
|
|
21
21
|
"component": {
|
|
22
|
-
"bom-ref": "@blamejs/core@0.11.
|
|
22
|
+
"bom-ref": "@blamejs/core@0.11.31",
|
|
23
23
|
"type": "application",
|
|
24
24
|
"name": "blamejs",
|
|
25
|
-
"version": "0.11.
|
|
25
|
+
"version": "0.11.31",
|
|
26
26
|
"scope": "required",
|
|
27
27
|
"author": "blamejs contributors",
|
|
28
28
|
"description": "The Node framework that owns its stack.",
|
|
29
|
-
"purl": "pkg:npm/%40blamejs/core@0.11.
|
|
29
|
+
"purl": "pkg:npm/%40blamejs/core@0.11.31",
|
|
30
30
|
"properties": [],
|
|
31
31
|
"externalReferences": [
|
|
32
32
|
{
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"components": [],
|
|
55
55
|
"dependencies": [
|
|
56
56
|
{
|
|
57
|
-
"ref": "@blamejs/core@0.11.
|
|
57
|
+
"ref": "@blamejs/core@0.11.31",
|
|
58
58
|
"dependsOn": []
|
|
59
59
|
}
|
|
60
60
|
]
|