PushSubscription: "types": [] delivers StateChange notifications although it shouldn't

Please don’t bother for now. The bot makes a good point. I will have to confirm this before anyone wastes any time on it.

The original description was:

Issue Description

I run my own Stalwart server and I am writing my own small mail app for my iPhone and my Mac, for myself only, at least so far. It’s coming along nicely, and maybe I’ll open source it or publish it in the stores.

The app leans on push: the server pushes new mail to my phone through Apple’s notification service, and because emailpush carries the sender, subject and a preview inside the notification itself, the phone can show me what arrived without fetching anything. On iOS, the app cannot run in the background because of the operating system.

What goes wrong is that my phone also notifies me when nothing has arrived. Marking a message as read on my Mac makes my phone show “New mail”. Those are StateChange notifications — a general “something changed in your account” — and I never asked for them; I asked for new mail only. On iOS an app cannot throw away a notification once the system has handed it over, so each of these becomes a visible notification that is simply untrue.

Change an email on my Mac, marking it as read – my phone buzzes.

RFC 8620, Section 7.2 defines PushSubscription.types as the list of data types a client wants StateChange notifications for, with null (or an absent value) meaning “all types”. An empty list therefore means the client wants no StateChange notifications at all – is that right?

Stalwart 1.0.2 however does deliver StateChange notifications to a subscription created with "types": []. For a client that subscribes only for urn:ietf:params:jmap:emailpush notifications, there is currently no way to turn StateChange off.

None of this can be fixed on the client side: a UNNotificationServiceExtension is required to decrypt the payload, but it cannot discard a notification it has been handed, and the push endpoint in this setup holds no keys and so cannot tell the two kinds apart either (see Additional Context).

I am dependent on Stalwart sending the right notifications. Well, actually, I found a workaround for now: use Urgency as a marker, and let the device decide what the relay drops.

Both ends are deliberately blind — the iOS extension can decrypt but cannot discard a notification, and the relay holds no keys, so it cannot tell the two kinds apart. The one difference visible without touching the ciphertext is the Urgency header, and EmailPushConfig carries its own urgency.

So the client orders urgency: “high” for emailPush — purely as a marker, not a priority request. Measured at the relay: Urgency: high for a delivered message, Urgency: normal for a StateChange, with Topic and TTL identical. Delivery is unaffected (anything above low goes out at APNs priority 10).

The catch is that a PushVerification arrives without the marker. A relay with a hardcoded “drop anything but high” rule would swallow it, and push could then never be set up again — silently. So the rule is not in the relay: the app registers twice per launch, first with no marker (it does not yet know whether it is about to create a subscription and wait for verification), then with the urgency that may still pass once push really stands. A dropped push gets a 201 — accepted, since an error would make the server retry or discard the subscription — and every drop is logged, so a wrongly set marker shows up instead of losing mail. With no instruction stored, everything passes.

Size would separate just as cleanly (230 vs 614 bytes) and is deliberately not used: a threshold set slightly too high drops real mail, and nobody notices that.

Expected Behavior

Nothing is pushed. "types": [] selects no StateChange types, and a keyword change is not an emailPush event, since no message was delivered.

I hope I’m right here.

Actual Behavior

One StateChange notification arrives per Email/set, at the subscription’s url, encrypted as usual and decryptable with the subscription’s keys.

Measured on a real device: marking a single message as read produced one such notification, and the client’s “last notification received” diagnostic showed StateChange (Email, Mailbox).

Reproduction Steps

  1. Create a push subscription that asks for emailPush only and explicitly opts out of every StateChange type:

    {
      "using": [
        "urn:ietf:params:jmap:core",
        "urn:ietf:params:jmap:emailpush"
      ],
      "methodCalls": [
        ["PushSubscription/set", {
          "create": {
            "sub": {
              "deviceClientId": "<client-generated id>",
              "url": "https://relay.example/p/<opaque token>",
              "keys": { "p256dh": "<base64url>", "auth": "<base64url>" },
              "types": [],
              "emailPush": {
                "<accountId>": {
                  "properties": [
                    "id", "from", "subject", "preview", "threadId", "receivedAt"
                  ],
                  "filter": { "inMailbox": "<inboxId>" },
                  "urgency": "normal"
                }
              }
            }
          }
        }, "0"]
      ]
    }
    

    The server accepts all of this, including keys, types: [] and the emailPush config, and sends the PushVerification as expected.

  2. Confirm the subscription with the verification code.

  3. From any client, change a keyword on an existing message — no new message is delivered:

    ["Email/set", {
      "accountId": "<accountId>",
      "update": { "<emailId>": { "keywords/$seen": null } }
    }, "0"]
    
  4. Observe what arrives at the subscription’s url.

Relevant Log Output

Decrypted on the device (RFC 8291), the payload of step 4 is:

{
  "@type": "StateChange",
  "changed": { "<accountId>": { "Email": "<state>", "Mailbox": "<state>" } }
}

For comparison, the two kinds of notification as they arrive at the push endpoint. These are my own relay’s log lines (German; Topic ohne means no Topic header was sent). The first is the StateChange above, the second a genuinely delivered message, with urgency: "high" ordered in EmailPushConfig:

Push mit Inhalt, Urgency normal, Topic ohne, TTL 86400, 230 Byte
Push mit Inhalt, Urgency high, Topic ohne, TTL 86400, 614 Byte

Stalwart Version

v1.x

Installation Method

Binary (Linux)

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

Additional Context

  • Stalwart 1.0.2
  • Push path: Web Push (RFC 8030, RFC 8188, RFC 8291) with VAPID (RFC 9749)
  • urn:ietf:params:jmap:emailpush (draft-ietf-jmap-emailpush), advertised by the server both in capabilities and in the account’s accountCapabilities
  • Client: a personal JMAP client (iOS/macOS), not a public product

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Path glob probably relative to repo.

Find where types is populated from the subscription.

From the source I read, "types": [] should suppress StateChange, so I can’t explain what you’re seeing. I haven’t reproduced it, and I haven’t found the cause.

Your reading of RFC 8620 §7.2 matches the code I found. On create or update, an explicit types array replaces the stored set, and null selects all types (crates/jmap/src/push/set.rs). When a state change is dispatched, it is intersected with the subscription’s types bitmap, and an empty result is dropped (filter_state_change in crates/common/src/ipc.rs). With an empty types that intersection is always empty, so a keyword change on an existing message shouldn’t reach your URL. An Email/set that only changes keywords doesn’t generate an EmailPush event either.

One more path could be relevant, but I haven’t confirmed it applies to you. If an EmailPush notification arrives for an account that has no matching emailPush entry on the subscription, the server converts it into a StateChange (crates/services/src/state_manager/push.rs). That conversion is meant for new-mail events. In your repro the emailPush key is the <accountId>, so I’d double-check that it matches the account the change occurs in.

I couldn’t find where an empty types becomes “all types”. It might be something in how the [] is parsed or stored that I didn’t reach. Treat your observation as unconfirmed against the code, not as refuted.

If you want to narrow it down, fetch the subscription with PushSubscription/get and check the returned types. An empty array there means the filter is set as you intended. A populated array means the value is being rewritten somewhere on create. Please also say whether the StateChange still arrives after you remove the emailPush block, and whether it arrives for changes made by a different client or account.

A human will need to confirm whether this is a server bug.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

I think the bot makes a good point. I am checking on my end and get back to you.

Edit: That’ll take a while.

Maintainers: Don’t bother yet. Please wait until I’ve confirmed this.

Hi Marian,

An empty types list should mean no StateChange at all, and Stalwart got that wrong in two places.

When a subscription was created, Stalwart couldn’t tell an explicit "types": [] apart from an absent types, so it treated your empty list as “all types”. That’s the reason a $seen change on the Mac woke your phone. Fixing only that would have caused a second problem, though: EmailPush objects were also filtered by types, so an empty list would have silenced the new-mail pushes along with the StateChanges. Per the draft, EmailPush is sent whatever types says.

Both are fixed. With "types": [] plus your emailPush config, you’ll get EmailPush objects for matching deliveries and nothing else. The fix ships in next week’s Stalwart release. After upgrading you can drop the Urgency marker workaround, and existing subscriptions created with [] need to be created again, since they were stored with all types.