diff --git a/DESIGN.md b/DESIGN.md
index f4bf9ab..895edb1 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -41,8 +41,9 @@ These rules come from issue #1. Each one has tests (section 14).
approve and which paths get a warning come only from the host config:
`/etc/pullgate/NAME.toml` and `/etc/pullgate/listener.toml`. pullgate
never reads any setting from the repo. A commit cannot change the gate.
-2. **One exact commit.** The question shows the full SHA. Apply applies only
- that SHA, even when a newer commit arrives in between.
+2. **One exact commit.** The question shows the full SHA once, on its last
+ line. Apply applies only that SHA, even when a newer commit arrives in
+ between. The tap is bound to the question (its code), not to the text.
3. **No code from the new commit runs before Apply.** Before the tap,
pullgate only fetches the commit and reads it with git. The optional
`check` and `summary` commands may read the tree as data. They run as an
@@ -606,26 +607,43 @@ pullgate on HOST, instance NAME
⚠ The last apply (SHA) failed. (only when a SHA is held)
⚠ This commit is not a descendant of … (only with allow_older)
Apply this commit?
-FULL SHA
+SHORT SHA
First apply: there is no applied commit yet. All N files are listed.
-or: Since APPLIED SHA:
-⚠ Watched path changed: path
-⚠ Symlink out of the tree: path → target
-⚠ Submodule skipped: path
+or: Since SHORT APPLIED SHA:
+⚠ Watched path changed: path
+⚠ Symlink out of the tree: path → target
+⚠ Submodule skipped: path
Commits (N):
-• subject
+• subject
Changed files (N):
-• path
+• path
Summary:
output of the summary command+Full:
FULL SHA
```
-Text from the commit (subjects, paths, symlink targets, summary output) is
-cleaned before it is shown: control and invisible characters become `?`
-(newlines stay only in the summary output), each line is cut to 200
-characters, and then it is HTML-escaped. At most 50 commit subjects are
-read.
+**SHAs.** Every message shows a SHA as its first 12 characters, in
+``. The question, as it is sent, shows the full SHA once, on its last line
+("Full:"), so it can be copied and compared. That line stays after a cut.
+(After a tap, the listener adds its note below it.) Logs,
+the state and `pullgate status` keep the full SHA. Root applies the full
+SHA from its own state, never a SHA from the text.
+
+**Text from the repo is only shown as code.** Text from the repo, the
+commit, or a command that ran on it (paths, subjects, symlink targets,
+submodule paths, the output of `summary` and `check`, and errors that may
+hold any of them) is put into a message only by `message.Code` (one line in
+``) or `message.Output` (lines in ``). Telegram makes no link,
+mention, hashtag or bot command of text inside `` or ``, so a
+file named `README.md` or `login.example.com` stays plain text. The escape
+function is not exported, so other code cannot put repo text into a message
+another way; a test also checks the gate code for its own HTML. Before that,
+the text is cleaned: control and invisible characters (also right-to-left
+marks) become `?` (newlines stay only in the summary output), each line is
+cut to 200 characters, and then it is HTML-escaped. At most 50 commit
+subjects are read. Every message and every edit is sent with link previews
+off.
The message must fit one Telegram message (4096 characters). pullgate cuts
it to 3800 characters, counted on the escaped text in UTF-16 units: the head
diff --git a/README.md b/README.md
index 46e89ed..f823af5 100644
--- a/README.md
+++ b/README.md
@@ -8,8 +8,9 @@ root applies it, with a health check and a rollback or a hold.
- **The gate lives on the host.** What runs, who may approve and which paths
get a warning come from `/etc/pullgate`, never from the repo. A commit
cannot change the gate.
-- **One exact commit.** The question shows the full SHA. Apply applies only
- that SHA, even when a newer commit arrives in between.
+- **One exact commit.** The question shows a short SHA at the top and the
+ full SHA once, on its last line. Apply applies only that full SHA, even
+ when a newer commit arrives in between.
- **No code from the new commit runs before Apply.** Optional `check` and
`summary` commands read the commit as data, as an unprivileged user.
- **Only the listener holds the bot token.** It runs as its own user and
diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md
index ec0dcc1..10d7f7b 100644
--- a/THREAT-MODEL.md
+++ b/THREAT-MODEL.md
@@ -152,6 +152,22 @@ or during the review.
- **Commit subjects and summary output.** The same cleaning; HTML-escaped,
each line capped. The head of the question (host, instance, SHA, the
warnings) always comes first, before any text from the commit.
+- **Hostile text shown as a link in a trusted message.** A commit picks its
+ own file names, subjects and command output. Telegram turns text that
+ looks like a domain or a URL (`README.md`, `login.example.com`), an
+ `@username`, a `#tag` or a `/command` into something to tap, in a message
+ from the owner's own bot, next to the Apply button. So all text from the
+ repo is shown only inside `` or ``, where Telegram makes none
+ of these, and only through one function (DESIGN.md section 12). Link
+ previews are off for every message, so a link that slips through still
+ shows no preview. A test feeds hostile values into every field of every
+ message kind.
+- **A short SHA that looks right.** Messages show 12 characters of a SHA
+ (48 bits). Someone could grind a commit whose short SHA matches one the
+ owner expects. This does not change what is applied: root applies the
+ full SHA from its own state, and the tap is bound to the question code,
+ not to the text. The question also shows the full SHA once, on its last
+ line, and `pullgate status` shows the full applied SHA.
- **Symlinks.** They are written as symlinks and never followed by
pullgate. The tree is written with `O_NOFOLLOW|O_EXCL` into directories
that pullgate made itself, so a symlink cannot make pullgate write
diff --git a/internal/gate/apply.go b/internal/gate/apply.go
index 8832572..76cc804 100644
--- a/internal/gate/apply.go
+++ b/internal/gate/apply.go
@@ -318,4 +318,6 @@ func (e *Env) afterApply(st *state.State) {
st.Questions = keep
}
-func esc(err error) string { return message.Esc(err.Error()) }
+// esc shows an error in a message, in : it may hold text from the
+// repo (DESIGN.md section 12).
+func esc(err error) string { return message.Code(err.Error()) }
diff --git a/internal/gate/check_test.go b/internal/gate/check_test.go
index 56132bd..32163b9 100644
--- a/internal/gate/check_test.go
+++ b/internal/gate/check_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"time"
+ "github.com/code6x/pullgate/internal/message"
"github.com/code6x/pullgate/internal/spool"
)
@@ -52,7 +53,7 @@ func TestCheckRequestStartsANormalRun(t *testing.T) {
if r := f.run(); r != Nothing {
t.Fatalf("run: %s", r)
}
- if !contains(f.outbox(), "No new commit. The applied commit is "+f.state().Applied+"") {
+ if !contains(f.outbox(), "No new commit. The applied commit is "+message.Short(f.state().Applied)+"") {
t.Fatal("the reply must name the applied SHA")
}
}
diff --git a/internal/gate/gate_test.go b/internal/gate/gate_test.go
index f8b16ff..c42edd0 100644
--- a/internal/gate/gate_test.go
+++ b/internal/gate/gate_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"time"
+ "github.com/code6x/pullgate/internal/message"
"github.com/code6x/pullgate/internal/spool"
"github.com/code6x/pullgate/internal/state"
)
@@ -39,7 +40,7 @@ func TestFirstRunAsksWithAllFilesThenApplies(t *testing.T) {
if f.read("live") != "one" || f.state().Applied != sha {
t.Fatalf("not applied: live %q, state %q", f.read("live"), f.state().Applied)
}
- if !contains(f.outbox(), "✅ Applied "+sha) {
+ if !contains(f.outbox(), "✅ Applied "+message.Short(sha)+"") {
t.Error("no report")
}
// The next run has nothing to do.
@@ -56,7 +57,7 @@ func TestSecondCommitGetsOldTreeAndForwardOnly(t *testing.T) {
t.Fatalf("run: %s", r)
}
q := f.ask()
- if !strings.Contains(q.Text, "Since "+a+"") || !strings.Contains(q.Text, "old value one") || !strings.Contains(q.Text, "• second") {
+ if !strings.Contains(q.Text, "Since "+message.Short(a)+"") || !strings.Contains(q.Text, "old value one") || !strings.Contains(q.Text, "• second") {
t.Fatalf("the question must show the old tree and the commits:\n%s", q.Text)
}
f.tap(q.Code, "apply")
@@ -76,7 +77,7 @@ func TestSecondCommitGetsOldTreeAndForwardOnly(t *testing.T) {
t.Fatalf("an older commit must be refused, got %s", r)
}
ms := f.outbox()
- if !contains(ms, "Refused "+a) || contains(ms, "Apply this commit?") {
+ if !contains(ms, "Refused "+message.Short(a)+"") || contains(ms, a) || contains(ms, "Apply this commit?") {
t.Fatal("a refusal sends a notice and no question")
}
// It is not offered again.
@@ -155,7 +156,7 @@ func TestTapOnSupersededQuestionAppliesOldSHA(t *testing.T) {
t.Fatalf("run: %s", r)
}
q := f.ask()
- if !strings.Contains(q.Text, ""+b+"") || !strings.Contains(q.Text, "Since "+a) {
+ if !strings.Contains(q.Text, ""+message.Short(b)+"") || !strings.Contains(q.Text, "Since "+message.Short(a)+"") {
t.Fatalf("the new question is wrong:\n%s", q.Text)
}
}
@@ -506,7 +507,7 @@ func TestFailingApplyRollsBack(t *testing.T) {
if !strings.Contains(f.read("log"), "apply "+a+" old="+b+" rb=1 failed="+b) {
t.Fatalf("the rollback env is wrong:\n%s", f.read("log"))
}
- if !contains(f.outbox(), "Rolled back to "+a) {
+ if !contains(f.outbox(), "Rolled back to "+message.Short(a)+"") {
t.Fatal("no report")
}
// max_failed_tries = 1: not asked again.
@@ -588,7 +589,7 @@ func TestOnFailurePauseHoldsAndAsksNewer(t *testing.T) {
t.Fatalf("a newer commit is asked: %s", r)
}
q := f.ask()
- if !strings.Contains(q.Text, "⚠ The last apply ("+b+") failed.") {
+ if !strings.Contains(q.Text, "⚠ The last apply ("+message.Short(b)+") failed.") {
t.Fatalf("the warning is missing:\n%s", q.Text)
}
f.tap(q.Code, "apply")
diff --git a/internal/gate/repotext_test.go b/internal/gate/repotext_test.go
new file mode 100644
index 0000000..402db70
--- /dev/null
+++ b/internal/gate/repotext_test.go
@@ -0,0 +1,150 @@
+package gate
+
+import (
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "testing"
+
+ "github.com/code6x/pullgate/internal/message"
+ "github.com/code6x/pullgate/internal/spool"
+)
+
+var codeOrPre = regexp.MustCompile(`(?s).*?|.*?
`)
+
+// repoMarkers are parts of the hostile values below. None may be seen
+// outside and in any message (code6x/pullgate#29).
+var repoMarkers = []string{"README.md", "example.com", "example.org", "a href", "@username", "#tag", "/command"}
+
+const hostileLine = `README.md login.example.com https://example.com/x docs.example.org/setup Apply @username #tag /command`
+
+func checkRepoText(t *testing.T, ms []spool.Message) {
+ t.Helper()
+ for _, m := range ms {
+ if message.Len(m.Text) > 4096 {
+ t.Errorf("%s message too long: %d", m.Kind, message.Len(m.Text))
+ }
+ out := codeOrPre.ReplaceAllString(m.Text, "")
+ for _, k := range repoMarkers {
+ if strings.Contains(out, k) {
+ t.Errorf("%s message holds %q outside /:\n%s", m.Kind, k, m.Text)
+ }
+ }
+ }
+}
+
+// TestHostileCommitTextIsOnlyCode runs a commit with hostile file names, a
+// hostile subject and hostile command output through a failed check, a
+// question, and a failed apply, and checks every message.
+func TestHostileCommitTextIsOnlyCode(t *testing.T) {
+ f := newFixture(t, ``, "")
+ files := map[string]string{"README.md": "x", "login.example.com": "x", "policy/@username.toml": "x", "#tag": "x"}
+ // 1. A check that fails with hostile output.
+ f.script("check.sh", "echo '"+hostileLine+"'\necho '"+hostileLine+"' >&2\nexit 1\n")
+ f.commit("first "+hostileLine, "one", files)
+ if r := f.run(); r != CheckFailed {
+ t.Fatalf("run: %s", r)
+ }
+ ms := f.outbox()
+ if !contains(ms, "README.md login.example.com") {
+ t.Fatalf("the notice must show the check output: %+v", ms)
+ }
+ checkRepoText(t, ms)
+ // 2. A question with a hostile summary.
+ f.script("check.sh", "true\n")
+ f.script("summary.sh", "echo '"+hostileLine+"'\n")
+ f.commit("second "+hostileLine, "two", map[string]string{"fail-apply": "x", "docs.example.org": "x"})
+ if r := f.run(); r != Asked {
+ t.Fatalf("run: %s", r)
+ }
+ ms = f.outbox()
+ checkRepoText(t, ms)
+ var q spool.Message
+ for _, m := range ms {
+ if m.Kind == spool.KindAsk {
+ q = m
+ }
+ }
+ for _, want := range []string{"README.md", "login.example.com", "second " + strings.ReplaceAll(strings.ReplaceAll(hostileLine, "<", "<"), ">", ">")[:60]} {
+ if !strings.Contains(q.Text, want) {
+ t.Errorf("missing %q in the question:\n%s", want, q.Text)
+ }
+ }
+ // 3. A failed apply whose command prints hostile text.
+ f.script("apply.sh", "echo '"+hostileLine+"'\necho '"+hostileLine+"' >&2\nexit 2\n")
+ f.tap(q.Code, "apply")
+ if r := f.apply(); r != Held {
+ t.Fatalf("apply: %s", r)
+ }
+ ms = f.outbox()
+ if !contains(ms, "failed") {
+ t.Fatalf("no failure report: %+v", ms)
+ }
+ checkRepoText(t, ms)
+}
+
+// TestHostileSymlinkRefusalIsOnlyCode: the refused notice names a path
+// from the commit; it is inside too.
+func TestHostileSymlinkRefusalIsOnlyCode(t *testing.T) {
+ f := newFixture(t, ``, "[limits]\nrefuse_symlinks = true\n")
+ name := "login.example.com @username #tag"
+ if err := os.Symlink("../../x", filepath.Join(f.r.Work, name)); err != nil {
+ t.Fatal(err)
+ }
+ f.commit("link", "one", nil)
+ if r := f.run(); r != CheckFailed {
+ t.Fatalf("run: %s", r)
+ }
+ ms := f.outbox()
+ if !contains(ms, "login.example.com") {
+ t.Fatalf("the notice must name the path: %+v", ms)
+ }
+ checkRepoText(t, ms)
+}
+
+// TestGateBuildsNoRepoTextItself: the gate code makes no HTML from text by
+// itself; it uses message.Code, message.SHA and message.Output, which put
+// the text in or (code6x/pullgate#29).
+func TestGateBuildsNoRepoTextItself(t *testing.T) {
+ names, err := filepath.Glob("*.go")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, n := range names {
+ if strings.HasSuffix(n, "_test.go") {
+ continue
+ }
+ b, err := os.ReadFile(n)
+ if err != nil {
+ t.Fatal(err)
+ }
+ // The listener's own messages hold only instance names from its
+ // config, never repo text; root's messages are built here.
+ for _, bad := range []string{"html.EscapeString", "", "\"\"", "\"\" +", "Sprintf(\""} {
+ if strings.Contains(string(b), bad) {
+ t.Errorf("%s uses %q: put repo text in a message only with message.Code, message.SHA or message.Output", n, bad)
+ }
+ }
+ }
+}
+
+// TestNoticesShowShortSHAs: notices and replies show 12 characters of a
+// SHA, and an apply still installs the full SHA (code6x/pullgate#30).
+func TestNoticesShowShortSHAs(t *testing.T) {
+ f := newFixture(t, ``, "")
+ a := f.commit("first", "one", nil)
+ f.run()
+ q := f.ask()
+ if strings.Count(q.Text, a) != 1 || !strings.Contains(q.Text, "\nFull: "+a+"") {
+ t.Fatalf("the question shows the full SHA once:\n%s", q.Text)
+ }
+ f.tap(q.Code, "apply")
+ if r := f.apply(); r != Applied || f.state().Applied != a || !strings.Contains(f.read("log"), "apply "+a) {
+ t.Fatalf("the apply must use the full SHA: %s %q", r, f.read("log"))
+ }
+ ms := f.outbox()
+ if contains(ms, a) || !contains(ms, ""+message.Short(a)+"") {
+ t.Fatalf("the report must show only the short SHA: %+v", ms)
+ }
+}
diff --git a/internal/gate/run.go b/internal/gate/run.go
index 557df50..555014d 100644
--- a/internal/gate/run.go
+++ b/internal/gate/run.go
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
- "strings"
"time"
"github.com/code6x/pullgate/internal/config"
@@ -58,7 +57,8 @@ func (e *Env) edit(q int64, text string) {
}
}
-func code(sha string) string { return "" + sha + "" }
+// code shows a SHA in a message: short, in (DESIGN.md section 12).
+func code(sha string) string { return message.SHA(sha) }
// cutOff handles an apply in progress found at start (DESIGN.md section 10).
func (e *Env) cutOff(st *state.State) error {
@@ -270,7 +270,7 @@ func (e *Env) decide(ctx context.Context, st *state.State, tip string) (Result,
if errors.As(err, &hostile) {
// The commit itself is bad: it is not tried again.
st.SetMark(tip, state.MarkCheckFailed, now)
- e.notice("⚠ " + code(tip) + " is refused, so it is not asked: " + message.Esc(err.Error()))
+ e.notice("⚠ " + code(tip) + " is refused, so it is not asked: " + message.Code(err.Error()))
return CheckFailed, nil
}
return "", fmt.Errorf("export %s: %w", tip, err)
@@ -281,7 +281,7 @@ func (e *Env) decide(ctx context.Context, st *state.State, tip string) (Result,
if err != nil {
st.SetMark(tip, state.MarkCheckFailed, now)
e.Log.Warn("check failed; not asking", "sha", tip, "err", err)
- e.notice("⚠ The check of " + code(tip) + " failed (" + message.Esc(err.Error()) + "), so it is not asked. It is not checked again.\n" + lastLines(out, 10))
+ e.notice("⚠ The check of " + code(tip) + " failed, so it is not asked and not checked again: " + message.Code(err.Error()) + "\n" + message.Output(out, 10))
return CheckFailed, nil
}
e.Log.Info("check passed", "sha", tip)
@@ -347,23 +347,3 @@ func (e *Env) decide(ctx context.Context, st *state.State, tip string) (Result,
e.Log.Info("asked", "sha", tip, "question", nq.ID)
return Asked, nil
}
-
-// lastLines returns the last n lines of a command's output, cleaned, in a
-// block.
-func lastLines(out string, n int) string {
- lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
- if len(lines) > n {
- lines = lines[len(lines)-n:]
- }
- for i, l := range lines {
- if r := []rune(l); len(r) > 100 {
- l = string(r[:100]) + "…"
- }
- lines[i] = message.Esc(l)
- }
- s := strings.Join(lines, "\n")
- if strings.TrimSpace(s) == "" {
- return ""
- }
- return "" + s + "
"
-}
diff --git a/internal/listener/listener_test.go b/internal/listener/listener_test.go
index 715797b..2ad416e 100644
--- a/internal/listener/listener_test.go
+++ b/internal/listener/listener_test.go
@@ -256,6 +256,27 @@ func TestBadOutboxEntryThatStaysSendsNoNotice(t *testing.T) {
}
}
+// TestLinkPreviewsAreOff: every message and every edit is sent with link
+// previews off (code6x/pullgate#23, #29).
+func TestLinkPreviewsAreOff(t *testing.T) {
+ f := newL(t)
+ msg := f.ask(1, code)
+ _ = f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindReply, Question: 1, Text: "see https://example.com"})
+ _ = f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindNotice, Text: "https://example.com"})
+ _ = f.d.Send(os.Getegid(), spool.Message{Kind: spool.KindAsk, Question: 2, Code: "x", Expires: f.now.Add(time.Hour), Text: "broken"})
+ f.l.Outbox(context.Background())
+ f.tap(owner, chat, msg, "a:"+code)
+ ms := f.tg.Messages()
+ if len(ms) < 5 {
+ t.Fatalf("want a question, a reply, a notice, an expired note and an edit: %+v", ms)
+ }
+ for _, m := range ms {
+ if (m.Method == "sendMessage" || m.Method == "editMessageText") && !m.NoPreview {
+ t.Errorf("%s without link previews off: %q", m.Method, m.Text)
+ }
+ }
+}
+
func TestTokenNeverInErrors(t *testing.T) {
c := telegram.New("http://127.0.0.1:1", token)
_, err := c.SendMessage(context.Background(), 1, "x", nil, 0)
diff --git a/internal/message/message.go b/internal/message/message.go
index 9df9cad..8808348 100644
--- a/internal/message/message.go
+++ b/internal/message/message.go
@@ -20,10 +20,10 @@ const Limit = 3800
// MaxLine is the most characters of one cleaned line.
const MaxLine = 200
-// Clean makes text from the commit safe to show: control and invisible
+// clean makes text from the commit safe to show: control and invisible
// characters become "?", and the text is cut to MaxLine characters. It
-// does not escape HTML; Esc does.
-func Clean(s string) string {
+// does not escape HTML; esc does.
+func clean(s string) string {
var b strings.Builder
n := 0
for _, r := range s {
@@ -57,8 +57,41 @@ func isInvisible(r rune) bool {
return unicode.Is(unicode.Variation_Selector, r)
}
-// Esc cleans and HTML-escapes text from the commit.
-func Esc(s string) string { return html.EscapeString(Clean(s)) }
+// esc cleans and HTML-escapes text from the commit. It is not exported:
+// text from the repo leaves this package only through Code and Output, so
+// it is always inside or (code6x/pullgate#29).
+func esc(s string) string { return html.EscapeString(clean(s)) }
+
+// Code is the one way to put one line of text from the repo, the commit,
+// or a command that ran on it into a message: cleaned, HTML-escaped and
+// inside . Telegram makes no link, mention, hashtag or command of
+// text inside (code6x/pullgate#23).
+func Code(s string) string { return "" + esc(s) + "" }
+
+// SHA shows a commit as its short form, in (code6x/pullgate#30).
+// The full SHA is shown once per question, on the "Full:" line.
+func SHA(sha string) string { return Code(Short(sha)) }
+
+// Output is the way to put the output of a command into a message: its
+// last n lines, each cleaned and cut, in a block. It is "" for empty
+// output.
+func Output(out string, n int) string {
+ lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
+ if len(lines) > n {
+ lines = lines[len(lines)-n:]
+ }
+ for i, l := range lines {
+ if r := []rune(l); len(r) > 100 {
+ l = string(r[:100]) + "…"
+ }
+ lines[i] = esc(l)
+ }
+ s := strings.Join(lines, "\n")
+ if strings.TrimSpace(s) == "" {
+ return ""
+ }
+ return "" + s + "
"
+}
// Len is the length of s in UTF-16 units, as Telegram counts.
func Len(s string) int { return len(utf16.Encode([]rune(s))) }
@@ -114,28 +147,33 @@ func Watched(patterns, files []string) []string {
return out
}
-// Fit joins head and body lines with newlines. The head always stays. Body
-// lines are added while the text fits Limit; when some are left out, the
-// last line says how many. The lines must be escaped already; the cut is on
+// Fit joins head and body lines with newlines, then tail, if it is not
+// "". The head and the tail always stay. Body lines are added while the
+// text fits Limit; when some are left out, a line says how many. The lines must be escaped already; the cut is on
// whole lines, so it never splits a tag. preOpen is the index of a body
// line that opens a block that ends with a line "
"; when the
// cut falls inside it, the block is closed (-1: none).
-func Fit(head, body []string, preOpen int) string {
+func Fit(head, body []string, tail string, preOpen int) string {
text := strings.Join(head, "\n")
used := Len(text)
reserve := Len("\n\n… 99999 more lines left out")
+ end := ""
+ if tail != "" {
+ end = "\n" + tail
+ reserve += Len(end)
+ }
for i, l := range body {
if used+1+Len(l)+reserve > Limit {
left := len(body) - i
if preOpen >= 0 && i > preOpen && !closedBefore(body, preOpen, i) {
text += "\n"
}
- return text + fmt.Sprintf("\n… %d more lines left out", left)
+ return text + fmt.Sprintf("\n… %d more lines left out", left) + end
}
text += "\n" + l
used += 1 + Len(l)
}
- return text
+ return text + end
}
func closedBefore(body []string, open, i int) bool {
diff --git a/internal/message/message_test.go b/internal/message/message_test.go
index dd57d77..baf508f 100644
--- a/internal/message/message_test.go
+++ b/internal/message/message_test.go
@@ -2,6 +2,7 @@ package message
import (
"fmt"
+ "regexp"
"strings"
"testing"
)
@@ -17,11 +18,11 @@ func TestClean(t *testing.T) {
string([]byte{0xff, 'x'}): "?x",
strings.Repeat("x", 300): strings.Repeat("x", 200) + "…",
} {
- if got := Clean(in); got != want {
- t.Errorf("Clean(%q) = %q, want %q", in, got, want)
+ if got := clean(in); got != want {
+ t.Errorf("clean(%q) = %q, want %q", in, got, want)
}
}
- if got := Esc("&"); got != "<b>&</b>" {
+ if got := esc("&"); got != "<b>&</b>" {
t.Errorf("Esc: %q", got)
}
}
@@ -143,10 +144,103 @@ func TestFirstApplyAndHeld(t *testing.T) {
if !strings.Contains(text, "First apply: there is no applied commit yet. All 2 files are listed.") {
t.Error("the first question must say so")
}
- if !strings.Contains(text, "⚠ The last apply ("+q.Held+") failed.") {
+ if !strings.Contains(text, "⚠ The last apply ("+Short(q.Held)+") failed.") {
t.Error("the held SHA warning is missing")
}
if strings.Index(text, "The last apply") > strings.Index(text, "Apply this commit?") {
t.Error("the held warning must be at the top")
}
}
+
+// hostile are values that a commit may hold to make Telegram show a link,
+// a mention, a hashtag, a command or markup in a trusted message
+// (code6x/pullgate#23, #29).
+var hostile = []string{
+ "README.md",
+ "login.example.com",
+ "docs.example.org/setup",
+ "https://example.com/x",
+ "Apply",
+ "@username",
+ "#tag",
+ "/command",
+ "\u202Edm.EMDAER",
+ strings.Repeat("long.example.net ", 300),
+}
+
+// markers must never be seen outside and .
+var markers = []string{"README.md", "example.com", "example.org", "example.net", "a href", "@username", "#tag", "/command", "EMDAER"}
+
+var codeOrPre = regexp.MustCompile(`(?s).*?|.*?
`)
+
+// outsideCode returns what is left of text without its and
+// parts.
+func outsideCode(text string) string { return codeOrPre.ReplaceAllString(text, "") }
+
+// TestRepoTextIsOnlyInsideCode feeds hostile values into every field of the
+// question that comes from the repo.
+func TestRepoTextIsOnlyInsideCode(t *testing.T) {
+ for _, failed := range []bool{false, true} {
+ q := baseQuestion()
+ q.Watch = []string{"**"}
+ for _, h := range hostile {
+ q.Subjects = append(q.Subjects, h)
+ q.Files = append(q.Files, h)
+ q.Symlinks = append(q.Symlinks, Symlink{Path: h, Target: h})
+ q.Submodules = append(q.Submodules, h)
+ }
+ q.TotalCommits = len(q.Subjects)
+ q.Summary = strings.Join(hostile, "\n")
+ if failed {
+ q.SummaryFailed = strings.Join(hostile, " ")
+ }
+ for _, cut := range []bool{false, true} {
+ if cut {
+ q.Files = append(q.Files, make([]string, 500)...) // force a cut
+ }
+ text := q.Text()
+ if Len(text) > 4096 {
+ t.Fatalf("too long for Telegram: %d", Len(text))
+ }
+ out := outsideCode(text)
+ for _, m := range markers {
+ if strings.Contains(out, m) {
+ t.Errorf("failed=%v cut=%v: %q is outside /:\n%s", failed, cut, m, out)
+ }
+ }
+ if strings.ContainsRune(text, '') {
+ t.Error("a right-to-left mark must not reach the message")
+ }
+ }
+ }
+ for _, h := range hostile {
+ for _, s := range []string{Code(h), Output(h+"\n"+h, 10)} {
+ if out := outsideCode(s); out != "" {
+ t.Errorf("Code/Output leave text outside: %q", out)
+ }
+ }
+ }
+}
+
+// TestShortSHAsAndOneFullSHA: the question shows short SHAs, and the full
+// SHA of the commit once, on the last line (code6x/pullgate#30).
+func TestShortSHAsAndOneFullSHA(t *testing.T) {
+ q := baseQuestion()
+ q.Held = strings.Repeat("c", 40)
+ q.Files = make([]string, 2000) // the last line stays after a cut
+ text := q.Text()
+ if strings.Count(text, q.SHA) != 1 || !strings.HasSuffix(text, "\nFull: "+q.SHA+"") {
+ t.Fatalf("the full SHA must be on the last line, once:\n%s", text)
+ }
+ for _, s := range []string{q.Applied, q.Held} {
+ if strings.Contains(text, s) || !strings.Contains(text, ""+Short(s)+"") {
+ t.Errorf("only the short form of %s must be shown", Short(s))
+ }
+ }
+ if !strings.Contains(text, "Apply this commit?\n"+Short(q.SHA)+"") {
+ t.Error("the short SHA must follow the question line")
+ }
+ if Len(text) > Limit {
+ t.Fatalf("too long: %d", Len(text))
+ }
+}
diff --git a/internal/message/question.go b/internal/message/question.go
index 51443e1..0bc3307 100644
--- a/internal/message/question.go
+++ b/internal/message/question.go
@@ -42,25 +42,25 @@ func Short(sha string) string {
func (q *Question) Text() string {
head := []string{Head(q.Host, q.Instance)}
if q.Held != "" {
- head = append(head, fmt.Sprintf("⚠ The last apply (%s) failed.", q.Held))
+ head = append(head, "⚠ The last apply ("+SHA(q.Held)+") failed.")
}
if q.NotDescendant {
head = append(head, "⚠ This commit is not a descendant of the applied commit.")
}
- head = append(head, "Apply this commit?", ""+q.SHA+"")
+ head = append(head, "Apply this commit?", SHA(q.SHA))
if q.Applied == "" {
head = append(head, fmt.Sprintf("First apply: there is no applied commit yet. All %d files are listed.", len(q.Files)))
} else {
- head = append(head, "Since "+q.Applied+":")
+ head = append(head, "Since "+SHA(q.Applied)+":")
}
for _, f := range Watched(q.Watch, q.Files) {
- head = append(head, "⚠ Watched path changed: "+Esc(f)+"")
+ head = append(head, "⚠ Watched path changed: "+Code(f))
}
for _, s := range q.Symlinks {
- head = append(head, "⚠ Symlink out of the tree: "+Esc(s.Path)+" → "+Esc(s.Target)+"")
+ head = append(head, "⚠ Symlink out of the tree: "+Code(s.Path)+" → "+Code(s.Target))
}
for _, s := range q.Submodules {
- head = append(head, "⚠ Submodule skipped: "+Esc(s)+"")
+ head = append(head, "⚠ Submodule skipped: "+Code(s))
}
// The head holds text from the commit too (watched paths); keep it
// within the limit on its own.
@@ -70,30 +70,32 @@ func (q *Question) Text() string {
var body []string
body = append(body, "", fmt.Sprintf("Commits (%d):", q.TotalCommits))
for _, s := range q.Subjects {
- body = append(body, "• "+Esc(s))
+ body = append(body, "• "+Code(s))
}
if q.TotalCommits > len(q.Subjects) {
body = append(body, fmt.Sprintf("… and %d more", q.TotalCommits-len(q.Subjects)))
}
body = append(body, fmt.Sprintf("Changed files (%d):", len(q.Files)))
for _, f := range q.Files {
- body = append(body, "• "+Esc(f))
+ body = append(body, "• "+Code(f))
}
pre := -1
switch {
case q.SummaryFailed != "":
- body = append(body, "⚠ The summary command failed: "+Esc(q.SummaryFailed))
+ body = append(body, "⚠ The summary command failed: "+Code(q.SummaryFailed))
case strings.TrimSpace(q.Summary) != "":
body = append(body, "Summary:")
pre = len(body)
lines := strings.Split(strings.TrimRight(q.Summary, "\n"), "\n")
- body = append(body, ""+Esc(lines[0]))
+ body = append(body, ""+esc(lines[0]))
for _, l := range lines[1:] {
- body = append(body, Esc(l))
+ body = append(body, esc(l))
}
body = append(body, "")
}
- return Fit(head, body, pre)
+ // The full SHA once, at the end, to copy and compare. The tap is bound
+ // to the question, not to this text.
+ return Fit(head, body, "Full: "+Code(q.SHA), pre)
}
// cutHead keeps the fixed lines and as many warnings as fit.
diff --git a/internal/telegram/faketg/faketg.go b/internal/telegram/faketg/faketg.go
index 781c23a..3af8e41 100644
--- a/internal/telegram/faketg/faketg.go
+++ b/internal/telegram/faketg/faketg.go
@@ -20,6 +20,8 @@ type Sent struct {
Text string
Buttons []telegram.Button
ReplyTo int64
+ // NoPreview is true when the request turned link previews off.
+ NoPreview bool
}
// Server is the fake server.
@@ -121,6 +123,9 @@ func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
}
}
}
+ if lp, ok := req["link_preview_options"].(map[string]any); ok {
+ sent.NoPreview, _ = lp["is_disabled"].(bool)
+ }
if rp, ok := req["reply_parameters"].(map[string]any); ok {
f, _ := rp["message_id"].(float64)
sent.ReplyTo = int64(f)
diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go
index c517249..388c906 100644
--- a/test/e2e/e2e_test.go
+++ b/test/e2e/e2e_test.go
@@ -122,7 +122,7 @@ api_url = %q
}
return false
})
- if !strings.Contains(q.Text, ""+second+"") || !strings.Contains(q.Text, "Watched path changed") {
+ if !strings.Contains(q.Text, "Full: "+second+"") || !strings.Contains(q.Text, "Watched path changed") {
t.Fatalf("the question:\n%s", q.Text)
}
if strings.Contains(q.Buttons[0].CallbackData, second[:10]) {
@@ -150,7 +150,7 @@ api_url = %q
}
wait(t, "the report", func() bool {
for _, m := range tg.Messages() {
- if strings.Contains(m.Text, "✅ Applied "+second) {
+ if strings.Contains(m.Text, "✅ Applied "+second[:12]+"") {
return true
}
}
@@ -174,7 +174,7 @@ api_url = %q
checking, none := false, false
for _, m := range tg.Messages() {
checking = checking || strings.Contains(m.Text, "Checking config-repo")
- none = none || strings.Contains(m.Text, "No new commit. The applied commit is "+second)
+ none = none || strings.Contains(m.Text, "No new commit. The applied commit is "+second[:12]+"")
}
return checking && none
})