Email for professionals and teams
jmap: ignore unimplemented IETF extension capabilities in `using` (#155)
yi1118e2d165c78d1
diff --git a/src/lib/jmap/processor.ts b/src/lib/jmap/processor.tsindex 5fb9ccf..5846890 100644--- a/src/lib/jmap/processor.ts+++ b/src/lib/jmap/processor.ts@@ -36,6 +36,9 @@ const METHODS: Record<string, JmapMethodHandler> = {export const SUPPORTED_CAPABILITIES = new Set([CAPABILITY_CORE, CAPABILITY_MAIL, CAPABILITY_SUBMISSION]);+/** Every IETF JMAP capability lives in this namespace, including optional extensions. */+const JMAP_CAPABILITY_NAMESPACE = "urn:ietf:params:jmap:";+/** JSON Pointer lookup with the JMAP `*` array-flattening extension. */function resolvePointer(value: unknown, path: string): unknown {const segments = path.split("/").slice(1).map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));@@ -131,7 +134,13 @@ export function validateRequest(body: unknown): JmapRequest {const request = body as Partial<JmapRequest>;if (!Array.isArray(request.using) || !Array.isArray(request.methodCalls)) throw new JmapError("notRequest");for (const capability of request.using) {- if (!SUPPORTED_CAPABILITIES.has(String(capability))) throw new JmapError("unknownCapability", String(capability));+ const name = String(capability);+ // A client may list optional IETF extension capabilities on every request, even ones it never+ // calls into, and RFC 8620 §3.3 lets it do that. Ignoring an unimplemented extension only+ // pushes the failure to its method calls, which already answer `unknownMethod`. Capability+ // URNs from any other namespace are a real mismatch between the client and this server.+ if (SUPPORTED_CAPABILITIES.has(name) || name.startsWith(JMAP_CAPABILITY_NAMESPACE)) continue;+ throw new JmapError("unknownCapability", name);}if (request.methodCalls.length > LIMITS.maxCallsInRequest) throw new JmapError("limit", "Too many method calls", { limit: "maxCallsInRequest" });for (const call of request.methodCalls) {diff --git a/tests/jmap-capabilities.test.mjs b/tests/jmap-capabilities.test.mjsnew file mode 100644index 0000000..0d0ed2b--- /dev/null+++ b/tests/jmap-capabilities.test.mjs@@ -0,0 +1,72 @@+import assert from "node:assert/strict";+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import test, { after } from "node:test";+import { fileURLToPath, pathToFileURL } from "node:url";++import { build } from "esbuild";++const root = join(dirname(fileURLToPath(import.meta.url)), "..");++/**+ * The module under test imports through the `@/*` alias and is TypeScript, so it is bundled with the+ * esbuild already used by `scripts/build-server.mjs` rather than loaded directly. Nothing here+ * touches a Workers binding: `validateRequest` only inspects the decoded request object.+ */+const outDir = mkdtempSync(join(tmpdir(), "mailflare-jmap-capabilities-test-"));+after(() => rmSync(outDir, { recursive: true, force: true }));++await build({+ stdin: {+ contents: `export { SUPPORTED_CAPABILITIES, validateRequest } from "./src/lib/jmap/processor.ts";`,+ resolveDir: root,+ sourcefile: "jmap-capabilities-test-entry.js",+ },+ outfile: join(outDir, "entry.mjs"),+ bundle: true,+ platform: "node",+ format: "esm",+ target: "node22",+ tsconfig: join(root, "tsconfig.json"),+ logLevel: "silent",+});++const { SUPPORTED_CAPABILITIES, validateRequest } = await import(pathToFileURL(join(outDir, "entry.mjs")).href);++const request = (using) => ({ using, methodCalls: [["Core/echo", { echo: true }, "c0"]] });++test("accepts the capabilities the server advertises", () => {+ for (const capability of SUPPORTED_CAPABILITIES) {+ assert.deepEqual(validateRequest(request([capability])).using, [capability]);+ }+});++test("ignores an unimplemented IETF extension capability", () => {+ const using = [...SUPPORTED_CAPABILITIES, "urn:ietf:params:jmap:vacationresponse"];+ assert.deepEqual(validateRequest(request(using)).using, using);+});++test("ignores an unimplemented IETF extension alongside capabilities we do not implement calls for", () => {+ const request_ = {+ using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:vacationresponse"],+ methodCalls: [+ ["VacationResponse/get", {}, "c0"],+ ["Core/echo", { echo: true }, "c1"],+ ],+ };+ assert.equal(validateRequest(request_).methodCalls.length, 2);+});++test("still rejects an unknown capability from another namespace", () => {+ assert.throws(() => validateRequest(request([...SUPPORTED_CAPABILITIES, "https://example.com/apis/foobar"])), (error) => {+ assert.equal(error.type, "unknownCapability");+ assert.equal(error.message, "https://example.com/apis/foobar");+ return true;+ });+});++test("still rejects a request that is not a request object", () => {+ assert.throws(() => validateRequest({ using: ["urn:ietf:params:jmap:core"] }), { type: "notRequest" });+ assert.throws(() => validateRequest(null), { type: "notRequest" });+});\ No newline at end of file