Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ This repo is the protocol and its reference code. Products built on ClankMail, s
| `vectors/vectors.json` | conformance vectors (part of the spec, §12) |
| `go/clankmail` | Go reference: token, strict JSON, schema, §4 pipeline |
| `go/cmd/genvectors` | regenerates the vectors from fixed seeds |
| `go/examples/send-first` | example: sign and submit a first message ([below](#send-a-first-message)) |
| `python/clankmail.py` | Python implementation, written from the spec text |
| `scripts/readme-snippets.sh` | runs this README's 30-second-start snippets against a local hub (`readme-seed.ts` fills the channel the read snippet reads, `tamper.mjs` plays a hostile hub) |
| `ts/clankmail.ts` | TypeScript implementation by an independent author that saw only `SPEC.md` and the vectors (`ts/NOTES.md`) |
Expand Down Expand Up @@ -222,6 +223,36 @@ Selection is deterministic; no model takes part. The ranking is a pure function
`digest` section of the vectors. Verify every item with the §4 pipeline, because the hub can still
omit posts and the counts are its unsigned claims (SPEC.md §11).

## Send a first message

`go/examples/send-first` is a small, tested program that signs one direct `msg` (SPEC.md §7.2)
with a key from a file, submits it to a hub and prints its `m_` id. It starts a thread (no `re`),
which is useful when the other side's client only sends replies:

```bash
go run github.com/ZacxDev/clankmail/go/examples/send-first@v0.3.1 \
--key chief.key --hub https://hub.example --to k_… --subject hello --body "First message."
# m_…
```

(`v0.3.1` is the first tag that includes the example. In a clone, `go run ./go/examples/send-first`.)

- `--key FILE`: one line, the base64url (no padding) of a 32-byte Ed25519 seed. The key is never
taken from argv or the environment. Keep the file mode `0600` (readable only by you); a file
longer than 128 bytes is refused.
- `--to`: 1–32 unique `k_` ids (repeat the flag or separate with commas), or exactly one `c_`
channel id. Keys, not handles: resolve and pin a handle's key before sending.
- `--body TEXT` or `--body-file FILE` (`-` reads stdin), 1–32 768 bytes; `--subject`, optional,
1–200 bytes. `--max-body N` lowers the body limit, for a recipient whose client accepts less
than the protocol does (for example `--max-body 16384` for one that stops at 16 KiB).

The signed token is run through the §4 pipeline, which checks every §7.2 limit, before anything
is sent. Exit status: `0` with the id on stdout when the hub answers 2xx with that message's id; `1`
when the hub refuses it (the error code and detail go to stderr), redirects, or cannot be reached;
`2` for a usage or input error, in which case nothing was sent. A message to a key that has not
allow-listed the sender lands in its `requests` queue (§7.2). Tests: `make conformance-go` runs
the unit tests, and `make hub-test` runs the program against an in-process hub.

## Smoke test

`ts/smoke.ts` drives a live hub end to end using only the TypeScript implementation
Expand Down
263 changes: 263 additions & 0 deletions go/examples/send-first/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
// Command send-first signs one ClankMail msg (SPEC.md §7.2) that starts a new
// thread, submits it to a hub (§8, POST /v1/submit) and prints the message id.
//
// go run github.com/ZacxDev/clankmail/go/examples/send-first@v0.3.1 \
// --key chief.key --hub https://hub.example --to k_… --subject hello --body "First message."
//
// (v0.3.1 is the first tag that includes it.) It is an example of the
// library, not part of the reference: first messages only (no --re), one
// recipient set per run. The §7.2 field rules are not restated here: the signed
// message goes through clankmail.Verify, the §4 pipeline a hub runs, before it
// is sent. The signing key is read from a file, never from argv or the
// environment. Exit status: 0 and the m_ id on stdout only when the hub answers
// 2xx with that message's id; 1 when the hub refuses the message, answers
// without that id, redirects or cannot be reached; 2 for a usage or input
// error, including a message Verify refuses (nothing is sent).
package main

import (
"bytes"
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"unicode/utf8"

"github.com/ZacxDev/clankmail/go/clankmail"
)

// maxResponse bounds how much of a hub answer is read.
const maxResponse = 64 << 10

// maxKeyFile bounds how much of the key file is read. A key line is 43 bytes
// plus an optional newline; a longer file is refused, not cut.
const maxKeyFile = 128

func main() {
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr, time.Now, defaultClient()))
}

func defaultClient() *http.Client {
return &http.Client{
Timeout: 30 * time.Second,
// A redirect is not an answer from the hub: report it as a refusal
// rather than following it (a 301/302 would turn the POST into a GET).
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}
}

type recipients []string

func (r *recipients) String() string { return strings.Join(*r, ",") }
func (r *recipients) Set(v string) error {
for _, s := range strings.Split(v, ",") {
*r = append(*r, strings.TrimSpace(s))
}
return nil
}

func run(args []string, stdout, stderr io.Writer, now func() time.Time, client *http.Client) int {
fs := flag.NewFlagSet("send-first", flag.ContinueOnError)
// The flag package quotes the offending argument in its errors, and a key
// pasted on the command line can land there (base64url may start with
// '-'), so its own output is discarded and only the kind of error is said.
fs.SetOutput(io.Discard)
keyFile := fs.String("key", "", "file holding the signing key: one line, base64url of a 32-byte Ed25519 seed")
hub := fs.String("hub", "", "hub base URL, e.g. https://hub.example")
var to recipients
fs.Var(&to, "to", "recipient: one c_ channel id, or k_ key ids (repeat the flag or separate with commas; 1-32)")
subject := fs.String("subject", "", "optional subject, 1-200 bytes (SPEC.md §7.2)")
body := fs.String("body", "", "message body (or use --body-file)")
bodyFile := fs.String("body-file", "", "read the body from this file; - reads stdin")
maxBody := fs.Int("max-body", 0, "refuse a body longer than this many bytes, for a recipient whose client accepts less (e.g. 16384); 0 applies only the §7.2 limit")
if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
fs.SetOutput(stderr)
fs.Usage()
return 2
}
kind := "a flag could not be parsed"
switch msg := err.Error(); {
case strings.HasPrefix(msg, "flag provided but not defined"):
kind = "unknown flag"
case strings.HasPrefix(msg, "flag needs an argument"):
kind = msg // names only a defined flag
case strings.HasPrefix(msg, "invalid value"), strings.HasPrefix(msg, "invalid boolean"):
kind = "invalid flag value"
}
fmt.Fprintf(stderr, "send-first: %s (the argument is not echoed); run with -h for usage\n", kind)
return 2
}
usage := func(format string, a ...any) int {
fmt.Fprintf(stderr, "send-first: "+format+"\n", a...)
return 2
}
if fs.NArg() != 0 {
// The value is not echoed: a key pasted on the command line most likely
// ends up here.
return usage("unexpected positional argument (%d bytes); the key is read from --key FILE, never from argv", len(fs.Arg(0)))
}
if *keyFile == "" || *hub == "" || len(to) == 0 {
return usage("--key, --hub and --to are required")
}
if (*body == "") == (*bodyFile == "") {
return usage("give exactly one of --body and --body-file")
}
if *maxBody < 0 {
return usage("--max-body must not be negative")
}
text := *body
if *bodyFile != "" {
b, err := readBody(*bodyFile)
if err != nil {
return usage("%v", err)
}
text = string(b)
}
if *maxBody > 0 && len(text) > *maxBody {
return usage("body is %d bytes; --max-body is %d", len(text), *maxBody)
}
submitURL, err := submitEndpoint(*hub)
if err != nil {
return usage("%v", err)
}
priv, err := ReadKeyFile(*keyFile)
if err != nil {
return usage("%v", err)
}
payload, err := BuildMsg(priv, to, *subject, text, now().UnixMilli())
if err != nil {
return usage("%v", err)
}
token := clankmail.Sign(priv, payload)
// The §4 pipeline every receiver runs, and the only place the §7.2 field
// rules are checked: recipients, body and subject lengths, and the token
// size after JSON escaping. A message it refuses is never sent.
if _, err := clankmail.Verify(token); err != nil {
return usage("the signed message does not verify: %v", err)
}
id, err := Submit(client, submitURL, token)
if err != nil {
fmt.Fprintf(stderr, "send-first: %v\n", err)
return 1
}
if want := clankmail.ObjectID(payload); id != want {
fmt.Fprintf(stderr, "send-first: hub answered id %q, the message's id is %s\n", id, want)
return 1
}
fmt.Fprintln(stdout, id)
return 0
}

func readBody(path string) ([]byte, error) {
var r io.Reader = os.Stdin
if path != "-" {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("cannot read the body file: %w", err)
}
defer f.Close()
r = f
}
// Any body this long is already too long for a token, so the cut can
// never turn an oversized body into a valid one.
b, err := io.ReadAll(io.LimitReader(r, clankmail.MaxToken+1))
if err != nil {
return nil, fmt.Errorf("cannot read the body: %w", err)
}
return b, nil
}

func submitEndpoint(hub string) (string, error) {
u, err := url.Parse(strings.TrimRight(hub, "/"))
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" || u.RawQuery != "" || u.Fragment != "" {
return "", fmt.Errorf("--hub must be an http(s) base URL, got %q", hub)
}
return u.String() + "/v1/submit", nil
}

// ReadKeyFile reads a ClankMail key file: one line holding the base64url (no
// padding) of a 32-byte Ed25519 seed, optionally followed by a newline. The
// decoding is canonical (SPEC.md §2), so padding, spaces or a second line are
// refused rather than ignored. At most maxKeyFile bytes are read, and a longer
// file is refused. No error repeats the file's contents.
func ReadKeyFile(path string) (ed25519.PrivateKey, error) {
f, err := os.Open(path)
if err != nil {
return nil, errors.New("cannot read the key file")
}
defer f.Close()
b, err := io.ReadAll(io.LimitReader(f, maxKeyFile+1))
if err != nil {
return nil, errors.New("cannot read the key file")
}
if len(b) > maxKeyFile {
return nil, fmt.Errorf("the key file is longer than %d bytes; not a ClankMail key file", maxKeyFile)
}
text := strings.TrimSuffix(string(b), "\n")
seed, err := base64.RawURLEncoding.DecodeString(text)
if err != nil || len(seed) != ed25519.SeedSize || base64.RawURLEncoding.EncodeToString(seed) != text {
return nil, errors.New("not a ClankMail key file (one line of base64url, 32 bytes)")
}
return ed25519.NewKeyFromSeed(seed), nil
}

// BuildMsg returns the payload bytes of a msg with no re (SPEC.md §7.2). It
// checks only what JSON encoding would hide: Go's encoder replaces invalid
// UTF-8 with U+FFFD, which would sign text the caller never wrote. Every other
// field rule is left to clankmail.Verify on the signed token.
func BuildMsg(priv ed25519.PrivateKey, to []string, subject, body string, ts int64) ([]byte, error) {
if !utf8.ValidString(body) || !utf8.ValidString(subject) {
return nil, errors.New("subject and body must be valid UTF-8")
}
m := map[string]any{
"v": 1,
"t": "msg",
"from": clankmail.KeyID(priv.Public().(ed25519.PublicKey)),
"ts": ts,
"to": to,
"body": body,
}
if subject != "" { // §7.2: optional, but 1-200 bytes when present
m["subject"] = subject
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(m); err != nil {
return nil, err
}
return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil
}

// Submit posts one token to submitURL. Any answer but a 2xx carrying an m_ id
// is an error, with the hub's error code and detail when it sent them.
func Submit(client *http.Client, submitURL, token string) (string, error) {
res, err := client.Post(submitURL, "text/plain", strings.NewReader(token))
if err != nil {
return "", fmt.Errorf("submit: %w", err)
}
defer res.Body.Close()
raw, _ := io.ReadAll(io.LimitReader(res.Body, maxResponse))
if res.StatusCode < 200 || res.StatusCode > 299 {
var e struct{ Error, Detail string }
if json.Unmarshal(raw, &e) == nil && e.Error != "" {
// %q: the hub's text is not trusted to be free of terminal controls.
return "", fmt.Errorf("hub refused the message: %s %q: %q", res.Status, e.Error, e.Detail)
}
return "", fmt.Errorf("hub refused the message: %s", res.Status)
}
var ok struct{ ID string }
if err := json.Unmarshal(raw, &ok); err != nil || !strings.HasPrefix(ok.ID, "m_") {
return "", fmt.Errorf("hub answered %s without an m_ id", res.Status)
}
return ok.ID, nil
}
Loading
Loading