Mobile app (Android):
- Sign-in to any server, with authenticator codes. The server address and
token are kept in secure storage; a dropped connection never signs out.
- Workspaces, recent pages, spaces and a foldable page tree; search; the
inbox with mark-read.
- Pages open read-only in the shared editor from their saved content and
join the live document to edit (with the native toolbar). Page links in
the text open in the app (new "navigate" bridge message).
- Comments: threads, replies, resolve.
- "Save to Docwell" in the Android share sheet makes a page from shared
text, with web addresses as links.
- Offline reading: every answer the app reads is kept on the phone, and
used when the server can't be reached.
- Push through UnifiedPush (ntfy, NextPush, ...). The app uses the Android
connector directly; the umbrella package pulls in a native crypto build
for Linux that breaks `dart run` and tests.
Server:
- /me/devices registers a phone's UnifiedPush endpoint and Web Push keys
(new push_devices collection). Each notification is also pushed,
encrypted with RFC 8291 aes128gcm so the distributor can't read it; the
app drops messages that don't decrypt. Endpoints answering 404/410 are
forgotten. Endpoints follow the webhook address rules.
- /collab accepts the apps' own origins ("null" for the mobile editor page,
wails:// for the desktop app). Sign-in is a token in the first message,
not a cookie, so this opens no cross-site hijacking.
Tried on an Android 15 emulator: sign-in, browsing, reading, live editing,
sharing and offline reading. Not covered: push end to end (no distributor
on the emulator), iOS, and YouTube embeds, which refuse to play from the
app's local editor page (error 153).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package push
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/ecdh"
|
|
"crypto/hkdf"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"errors"
|
|
)
|
|
|
|
// Web Push message encryption (RFC 8291, "aes128gcm" from RFC 8188), which
|
|
// UnifiedPush uses: only the app, which holds the private key, can read a
|
|
// message, so the distributor's server (ntfy.sh, …) sees nothing but its
|
|
// size. The app drops messages that don't decrypt, so knowing an endpoint
|
|
// is not enough to send someone a fake notification.
|
|
|
|
// recordSize is the RFC 8188 record size; a message fits in one record.
|
|
const recordSize = 4096
|
|
|
|
// decodeKey reads a base64url key (padded or not).
|
|
func decodeKey(s string) ([]byte, error) {
|
|
if b, err := base64.RawURLEncoding.DecodeString(s); err == nil {
|
|
return b, nil
|
|
}
|
|
return base64.URLEncoding.DecodeString(s)
|
|
}
|
|
|
|
// checkKeys validates a phone's public key (uncompressed P-256) and auth secret.
|
|
func checkKeys(p256dh, auth string) error {
|
|
pub, err := decodeKey(p256dh)
|
|
if err != nil {
|
|
return errors.New("bad public key")
|
|
}
|
|
if _, err := ecdh.P256().NewPublicKey(pub); err != nil {
|
|
return errors.New("bad public key")
|
|
}
|
|
secret, err := decodeKey(auth)
|
|
if err != nil || len(secret) != 16 {
|
|
return errors.New("bad auth secret")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// encrypt seals plaintext for the phone with public key p256dh and auth
|
|
// secret auth, returning the aes128gcm body.
|
|
func encrypt(plaintext []byte, p256dh, auth string) ([]byte, error) {
|
|
uaPub, err := decodeKey(p256dh)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
secret, err := decodeKey(auth)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ua, err := ecdh.P256().NewPublicKey(uaPub)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
as, err := ecdh.P256().GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
shared, err := as.ECDH(ua)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
asPub := as.PublicKey().Bytes()
|
|
|
|
// RFC 8291 section 3.4: mix the auth secret and both public keys in.
|
|
keyInfo := append(append([]byte("WebPush: info\x00"), uaPub...), asPub...)
|
|
ikm, err := hkdf.Key(sha256.New, shared, secret, string(keyInfo), 32)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
salt := make([]byte, 16)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return nil, err
|
|
}
|
|
cek, err := hkdf.Key(sha256.New, ikm, salt, "Content-Encoding: aes128gcm\x00", 16)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nonce, err := hkdf.Key(sha256.New, ikm, salt, "Content-Encoding: nonce\x00", 12)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
block, err := aes.NewCipher(cek)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(plaintext)+1+gcm.Overhead() > recordSize {
|
|
return nil, errors.New("message too long")
|
|
}
|
|
// One record, so it is the last: padding delimiter 0x02.
|
|
sealed := gcm.Seal(nil, nonce, append(plaintext, 2), nil)
|
|
|
|
header := make([]byte, 0, 16+4+1+len(asPub))
|
|
header = append(header, salt...)
|
|
header = binary.BigEndian.AppendUint32(header, recordSize)
|
|
header = append(header, byte(len(asPub)))
|
|
header = append(header, asPub...)
|
|
return append(header, sealed...), nil
|
|
}
|