feat(cli): escape control characters from mail, refuse the token over plain http
- printable() shows C0, DEL and C1 control characters (except tab and newline; CRLF counts as a newline) as \xNN / \uNNNN, applied to every table cell, archive show, member listings, import reports and error text, so an escape sequence in a subject cannot drive the terminal. - The client refuses to send the bearer token over http:// to a non-loopback host unless --insecure is given: usage error, exit 2. localhost, 127.0.0.0/8 and ::1 stay allowed. Offline commands (dkim) never build the client, so they are not affected. - Tests: the helper, ESC sequences in archive ls/show, held ls, queue ls, list/member names, API errors and import reports; the URL check by flag and PARSIMONY_URL. README updated.
This commit is contained in:
parent
9968c3b70f
commit
eccd82af69
3 changed files with 241 additions and 15 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
|
@ -22,7 +23,7 @@ type IO struct {
|
|||
Out, Err io.Writer
|
||||
}
|
||||
|
||||
const usageText = `Usage: parsimony [--url URL] [--json] <command> [arguments]
|
||||
const usageText = `Usage: parsimony [--url URL] [--insecure] [--json] <command> [arguments]
|
||||
|
||||
Administration (talks to the REST API):
|
||||
list ls show all lists
|
||||
|
|
@ -69,6 +70,7 @@ Environment:
|
|||
PARSIMONY_URL API address, default http://127.0.0.1:8080 (--url overrides)
|
||||
|
||||
--json prints the API's JSON instead of tables.
|
||||
--insecure allows sending the token over plain http to a host other than localhost.
|
||||
Exit status: 0 success, 1 failure, 2 usage error.
|
||||
`
|
||||
|
||||
|
|
@ -79,6 +81,7 @@ func Run(args []string, io IO, getenv func(string) string) int {
|
|||
fs := newFlagSet("parsimony")
|
||||
fs.StringVar(&c.url, "url", "", "")
|
||||
fs.BoolVar(&c.json, "json", false, "")
|
||||
fs.BoolVar(&c.insecure, "insecure", false, "")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
fmt.Fprint(io.Out, usageText)
|
||||
|
|
@ -88,7 +91,7 @@ func Run(args []string, io IO, getenv func(string) string) int {
|
|||
}
|
||||
rest := fs.Args()
|
||||
if len(rest) == 0 {
|
||||
fmt.Fprint(io.Err, "usage: parsimony [--url URL] [--json] <command> (see parsimony help)\n")
|
||||
fmt.Fprint(io.Err, "usage: parsimony [--url URL] [--insecure] [--json] <command> (see parsimony help)\n")
|
||||
return 2
|
||||
}
|
||||
if rest[0] == "help" {
|
||||
|
|
@ -124,16 +127,27 @@ type cmd struct {
|
|||
getenv func(string) string
|
||||
url string
|
||||
json bool
|
||||
cl *client
|
||||
// insecure allows the token to travel over plain http to a non-loopback host.
|
||||
insecure bool
|
||||
cl *client
|
||||
}
|
||||
|
||||
// usageErr is an error that is the caller's mistake: exit status 2.
|
||||
type usageErr string
|
||||
|
||||
func (e usageErr) Error() string { return string(e) }
|
||||
|
||||
func (c *cmd) usageError(format string, a ...any) int {
|
||||
fmt.Fprintf(c.io.Err, "error: "+format+"\n", a...)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (c *cmd) fail(err error) int {
|
||||
fmt.Fprintf(c.io.Err, "error: %v\n", err)
|
||||
fmt.Fprintf(c.io.Err, "error: %s\n", printable(err.Error()))
|
||||
var u usageErr
|
||||
if errors.As(err, &u) {
|
||||
return 2
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
|
|
@ -153,9 +167,14 @@ func (c *cmd) client() (*client, error) {
|
|||
if base == "" {
|
||||
base = "http://127.0.0.1:8080"
|
||||
}
|
||||
if _, err := url.Parse(base); err != nil {
|
||||
u, err := url.Parse(base)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid URL %q", base)
|
||||
}
|
||||
// url.Parse lowercases the scheme.
|
||||
if u.Scheme == "http" && !isLoopback(u.Hostname()) && !c.insecure {
|
||||
return nil, usageErr(fmt.Sprintf("refusing to send the token over plain http to %s: use https, or pass --insecure", u.Host))
|
||||
}
|
||||
c.cl = &client{
|
||||
base: strings.TrimRight(base, "/"), token: token,
|
||||
http: &http.Client{Timeout: 30 * time.Second},
|
||||
|
|
@ -312,10 +331,44 @@ func (c *cmd) printJSON(raw json.RawMessage) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
// isLoopback reports whether host is this machine: localhost or a loopback address.
|
||||
func isLoopback(host string) bool {
|
||||
if strings.EqualFold(strings.TrimSuffix(host, "."), "localhost") {
|
||||
return true
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
return ip != nil && ip.IsLoopback()
|
||||
}
|
||||
|
||||
// printable makes text that came from mail or from the API safe for a terminal: control
|
||||
// characters (C0, DEL, C1) other than tab and newline are shown as \xNN / \uNNNN instead of
|
||||
// being sent, so an escape sequence in a subject cannot drive the terminal. CRLF counts as a newline.
|
||||
func printable(s string) string {
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case r == '\t' || r == '\n':
|
||||
b.WriteRune(r)
|
||||
case r < 0x20 || r == 0x7f:
|
||||
fmt.Fprintf(&b, `\x%02x`, r)
|
||||
case r >= 0x80 && r <= 0x9f:
|
||||
fmt.Fprintf(&b, `\u%04x`, r)
|
||||
default:
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (c *cmd) table(rows [][]string) {
|
||||
w := tabwriter.NewWriter(c.io.Out, 0, 8, 2, ' ', 0)
|
||||
for _, r := range rows {
|
||||
fmt.Fprintln(w, strings.Join(r, "\t"))
|
||||
cells := make([]string, len(r))
|
||||
for i, cell := range r {
|
||||
cells[i] = printable(cell)
|
||||
}
|
||||
fmt.Fprintln(w, strings.Join(cells, "\t"))
|
||||
}
|
||||
w.Flush()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -824,3 +824,172 @@ func TestHeldListingWarnsWhenTheHeaderDiffersFromTheSender(t *testing.T) {
|
|||
t.Fatalf("no usable From is flagged too: %q", lines["no from"])
|
||||
}
|
||||
}
|
||||
|
||||
const esc = "\x1b[31mRED\x1b]0;pwned\x07"
|
||||
|
||||
// noControl fails when s still holds a control character other than tab and newline.
|
||||
func noControl(t *testing.T, what, s string) {
|
||||
t.Helper()
|
||||
for _, r := range s {
|
||||
if (r < 0x20 && r != '\t' && r != '\n') || (r >= 0x7f && r <= 0x9f) {
|
||||
t.Fatalf("%s prints control character %U:\n%q", what, r, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintableStripsControlCharactersButKeepsTabAndNewline(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"plain text, accents: àèì 日本": "plain text, accents: àèì 日本",
|
||||
"a\tb\nc": "a\tb\nc",
|
||||
"a\r\nb": "a\nb",
|
||||
"\x1b[31mred\x1b[0m": `\x1b[31mred\x1b[0m`,
|
||||
"us\x1f": `us\x1f`,
|
||||
"bell\x07 nul\x00 del\x7f": `bell\x07 nul\x00 del\x7f`,
|
||||
"c1 \u009b31m and \u0085": `c1 \u009b31m and \u0085`,
|
||||
"c1 edges \u0080 \u009f, kept: ~ \u00a0 \u00a1": `c1 edges \u0080 \u009f, kept: ~ ` + "\u00a0 \u00a1",
|
||||
"lone\rcarriage": `lone\x0dcarriage`,
|
||||
"": "",
|
||||
} {
|
||||
if got := printable(in); got != want {
|
||||
t.Errorf("printable(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestControlCharactersFromMailNeverReachTheTerminal(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.ok(t, "list", "add", "dev@example.org")
|
||||
l, _ := e.s.FindList("dev@example.org")
|
||||
e.s.ArchiveMessage(l.ID, store.Message{MessageID: "<e@x>", Subject: "subj " + esc, Sender: "from " + esc,
|
||||
Date: time.Unix(1_700_000_000, 0), Body: "line one\r\nbody " + esc + "\r\nlast"})
|
||||
e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob "+esc+" <bob@x.com>\r\nSubject: held "+esc+"\r\n\r\nb"), "reason "+esc)
|
||||
e.s.Enqueue([]byte("x"), []store.NewOutbound{{From: "b@x.org", Rcpt: "lost@x.com"}})
|
||||
due, _ := e.s.DueOutbox(10)
|
||||
e.s.FailOutbox(due[0].ID, "refused: 554 "+esc)
|
||||
|
||||
for _, args := range [][]string{
|
||||
{"archive", "ls", "dev@example.org"}, {"archive", "ls", "dev@example.org", "--q", "subj"},
|
||||
{"archive", "show", "1"}, {"held", "ls", "dev@example.org"}, {"queue", "ls"},
|
||||
} {
|
||||
out := e.ok(t, args...)
|
||||
noControl(t, strings.Join(args, " "), out)
|
||||
if !strings.Contains(out, `\x1b[31mRED`) {
|
||||
t.Errorf("%v: the escape sequence should stay visible as text:\n%s", args, out)
|
||||
}
|
||||
}
|
||||
if out := e.ok(t, "archive", "show", "1"); !strings.Contains(out, "\nbody ") || strings.Contains(out, `\x0d`) {
|
||||
t.Errorf("CRLF line ends are not control noise: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControlCharactersInListAndMemberNamesAreEscaped(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
addr := "a\x1b[31mb\x07@x.org"
|
||||
l, err := e.s.CreateList("a\x1b[31mb\x07", "x.org", "title "+esc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e.s.AddMember(l.ID, "m\x1b[2Jm@x.org", "name "+esc)
|
||||
mbox := "From a@x.com Tue Jan 2 15:04:05 2024\nSubject: s\n\nbody\n"
|
||||
for _, c := range []struct {
|
||||
stdin string
|
||||
args []string
|
||||
}{
|
||||
{"", []string{"list", "ls"}}, {"", []string{"list", "show", addr}},
|
||||
{"", []string{"list", "set", addr, "--title", "t"}},
|
||||
{"", []string{"member", "ls", addr}}, {"", []string{"member", "ls", addr, "--emails"}},
|
||||
{"", []string{"member", "add", addr, "new@x.com"}},
|
||||
{"new2@x.com\n", []string{"member", "import", addr, "-"}},
|
||||
{mbox, []string{"archive", "import", addr, "-"}},
|
||||
{"", []string{"member", "rm", addr, "new@x.com"}},
|
||||
{"", []string{"list", "rm", addr, "--yes"}},
|
||||
} {
|
||||
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, c.stdin, c.args...)
|
||||
if code != 0 {
|
||||
t.Fatalf("%v: exit %d %s", c.args, code, errs)
|
||||
}
|
||||
noControl(t, strings.Join(c.args, " "), out+errs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControlCharactersInImportReportsAreEscaped(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.ok(t, "list", "add", "dev@example.org")
|
||||
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL},
|
||||
"bad"+esc+"@x\n", "member", "import", "dev@example.org", "-")
|
||||
if code != 1 || !strings.Contains(errs, `line 1: bad\x1b[31mRED`) {
|
||||
t.Fatalf("member import: %d %q", code, errs)
|
||||
}
|
||||
noControl(t, "member import", out+errs)
|
||||
|
||||
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
b, _ := json.Marshal(map[string]any{"imported": 0, "failed": 1, "errors": []string{"message 1: " + esc}})
|
||||
w.Write(b)
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`[{"id":1,"address":"dev@example.org"}]`))
|
||||
}))
|
||||
defer fake.Close()
|
||||
var o, eb bytes.Buffer
|
||||
vars := map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": fake.URL}
|
||||
Run([]string{"archive", "import", "dev@example.org", "-"}, IO{In: strings.NewReader("x"), Out: &o, Err: &eb}, func(k string) string { return vars[k] })
|
||||
if !strings.Contains(eb.String(), `message 1: \x1b[31mRED`) {
|
||||
t.Fatalf("archive import errors: %q", eb.String())
|
||||
}
|
||||
noControl(t, "archive import", o.String()+eb.String())
|
||||
}
|
||||
|
||||
func TestControlCharactersInAPIErrorsAndImportReportsAreEscaped(t *testing.T) {
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
msg, _ := json.Marshal(map[string]string{"error": "relay said " + esc})
|
||||
w.Write(msg)
|
||||
}))
|
||||
defer api.Close()
|
||||
var out, errb bytes.Buffer
|
||||
vars := map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": api.URL}
|
||||
code := Run([]string{"list", "ls"}, IO{In: strings.NewReader(""), Out: &out, Err: &errb}, func(k string) string { return vars[k] })
|
||||
if code != 1 || !strings.Contains(errb.String(), `relay said \x1b[31mRED`) {
|
||||
t.Fatalf("exit %d: %q", code, errb.String())
|
||||
}
|
||||
noControl(t, "error text", errb.String())
|
||||
}
|
||||
|
||||
func TestTokenIsNotSentOverPlainHTTPToARemoteHost(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
run := func(url string, extra ...string) (int, string) {
|
||||
args := append(append([]string{"--url", url}, extra...), "list", "ls")
|
||||
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token}, "", args...)
|
||||
if out != "" {
|
||||
t.Fatalf("stdout %q", out)
|
||||
}
|
||||
return code, errs
|
||||
}
|
||||
code, errs := run("http://lists.example.org:8080")
|
||||
if code != 2 || !strings.Contains(errs, "--insecure") || !strings.Contains(errs, "http") {
|
||||
t.Fatalf("remote http must be refused with a usage error: %d %q", code, errs)
|
||||
}
|
||||
if code, errs = run("HTTP://10.1.2.3"); code != 2 {
|
||||
t.Fatalf("scheme case must not matter: %d %q", code, errs)
|
||||
}
|
||||
// Past the check the only thing left to fail is the connection (port 1 is closed, .invalid never resolves).
|
||||
for _, u := range []string{"http://localhost:1", "http://LocalHost.:1", "http://127.0.0.1:1", "http://[::1]:1", "https://nowhere.invalid"} {
|
||||
if code, errs = run(u); code != 1 || !strings.Contains(errs, "cannot reach") {
|
||||
t.Errorf("%s must be allowed: %d %q", u, code, errs)
|
||||
}
|
||||
}
|
||||
if code, errs = run("http://nowhere.invalid", "--insecure"); code != 1 || !strings.Contains(errs, "cannot reach") {
|
||||
t.Fatalf("--insecure allows plain http: %d %q", code, errs)
|
||||
}
|
||||
// The environment variable is checked like the flag.
|
||||
code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": "http://lists.example.org"}, "", "list", "ls")
|
||||
if code != 2 {
|
||||
t.Fatalf("PARSIMONY_URL: %d %q", code, errs)
|
||||
}
|
||||
// No token to protect, no URL check: offline commands never build the client.
|
||||
code, _, errs = e.runEnv(t, nil, "", "--url", "http://lists.example.org", "dkim", "record", "example.org", "--key", filepath.Join(t.TempDir(), "none"))
|
||||
if strings.Contains(errs, "--insecure") {
|
||||
t.Fatalf("offline commands must not care about the URL")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ func (c *cmd) listSet(args []string) int {
|
|||
if c.json {
|
||||
return c.printJSON(raw)
|
||||
}
|
||||
fmt.Fprintf(c.io.Out, "Updated %s\n", l.Address)
|
||||
fmt.Fprintf(c.io.Out, "Updated %s\n", printable(l.Address))
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +168,7 @@ func (c *cmd) done(msg string) int {
|
|||
if c.json {
|
||||
return c.printJSON(nil)
|
||||
}
|
||||
fmt.Fprintln(c.io.Out, msg)
|
||||
fmt.Fprintln(c.io.Out, printable(msg))
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
@ -220,7 +220,7 @@ func (c *cmd) memberLs(args []string) int {
|
|||
}
|
||||
if *emails {
|
||||
for _, m := range ms {
|
||||
fmt.Fprintln(c.io.Out, m.Address)
|
||||
fmt.Fprintln(c.io.Out, printable(m.Address))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
|
@ -259,7 +259,7 @@ func (c *cmd) memberAdd(args []string) int {
|
|||
}
|
||||
var m member
|
||||
json.Unmarshal(raw, &m)
|
||||
fmt.Fprintf(c.io.Out, "Added %s to %s\n", m.Address, l.Address)
|
||||
fmt.Fprintf(c.io.Out, "Added %s to %s\n", printable(m.Address), printable(l.Address))
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
@ -327,7 +327,7 @@ func (c *cmd) memberImport(args []string) int {
|
|||
}
|
||||
if _, err := c.addMember(l.ID, address, name); err != nil {
|
||||
failed++
|
||||
fmt.Fprintf(c.io.Err, "line %d: %s: %v\n", n, line, err)
|
||||
fmt.Fprintf(c.io.Err, "line %d: %s\n", n, printable(fmt.Sprintf("%s: %v", line, err)))
|
||||
continue
|
||||
}
|
||||
imported++
|
||||
|
|
@ -339,7 +339,7 @@ func (c *cmd) memberImport(args []string) int {
|
|||
if failed > 0 {
|
||||
msg += fmt.Sprintf(", %d failed", failed)
|
||||
}
|
||||
fmt.Fprintln(c.io.Out, msg)
|
||||
fmt.Fprintln(c.io.Out, printable(msg))
|
||||
if failed > 0 {
|
||||
return 1
|
||||
}
|
||||
|
|
@ -544,7 +544,7 @@ func (c *cmd) archiveShow(args []string) int {
|
|||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(c.io.Out, "Subject: %s\nFrom: %s\nDate: %s\n\n%s\n", m.Subject, m.Sender, formatDate(m.Date), m.Body)
|
||||
fmt.Fprintf(c.io.Out, "Subject: %s\nFrom: %s\nDate: %s\n\n%s\n", printable(m.Subject), printable(m.Sender), formatDate(m.Date), printable(m.Body))
|
||||
return nil
|
||||
}, "")
|
||||
}
|
||||
|
|
@ -601,10 +601,10 @@ func (c *cmd) archiveImport(args []string) int {
|
|||
if sum.Failed > 0 {
|
||||
msg += fmt.Sprintf(", %d failed", sum.Failed)
|
||||
}
|
||||
fmt.Fprintln(c.io.Out, msg)
|
||||
fmt.Fprintln(c.io.Out, printable(msg))
|
||||
}
|
||||
for _, e := range sum.Errors {
|
||||
fmt.Fprintln(c.io.Err, e)
|
||||
fmt.Fprintln(c.io.Err, printable(e))
|
||||
}
|
||||
if sum.Failed > len(sum.Errors) {
|
||||
fmt.Fprintf(c.io.Err, "... and %d more\n", sum.Failed-len(sum.Errors))
|
||||
|
|
|
|||
Loading…
Reference in a new issue