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
8 changes: 6 additions & 2 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,12 @@ used again.
6. Export the tree of the tip, and the tree of the applied SHA
(`PULLGATE_OLD_TREE`).
7. Run `check` as `check_user` (section 11). If it fails, mark the SHA
"check failed", send a notice with the last lines of its output, and do
not ask. The same SHA is not checked again.
"check failed", send a notice with the end of its output, and do not
ask. The same SHA is not checked again. In that output the tree paths
(`PULLGATE_TREE`, `PULLGATE_OLD_TREE`) become `NEW` and `OLD`. The
notice keeps the last lines, at most 10 and at most 1500 characters,
so the reason (most tools print it last) is shown. A line longer than
300 characters keeps its start and its end, with `…` between.
8. Run `summary` as `check_user`, and build the message (section 12).
9. If a question is open, close it: root marks it superseded in its state,
and asks the listener to edit it ("a newer commit replaced this
Expand Down
6 changes: 4 additions & 2 deletions THREAT-MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ or during the review.
are cleaned and escaped before they are shown, so a name cannot fake a
line of the question.
- **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.
each line capped. The output of a failed `check` keeps its end, so the
reason is not cut away; root's tree paths in it become `NEW` and `OLD`.
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
Expand Down
45 changes: 45 additions & 0 deletions internal/gate/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,48 @@ func TestCheckWaiterStopsWhenRequestIsGone(t *testing.T) {
t.Fatalf("the waiter must not reply: %d messages", len(out))
}
}

// TestCheckNoticeShowsTheReason: a check that prints a long tree path
// before its reason gives a notice that holds the reason, with NEW for the
// tree path (code6x/pullgate#25).
func TestCheckNoticeShowsTheReason(t *testing.T) {
f := newFixture(t, ``, "")
f.script("check.sh", `echo "broker check: config $PULLGATE_TREE/broker.toml: [secret.x] preapprove: is not allowed with ask = \"never\""
echo "$PULLGATE_TREE" > `+f.ctrl+`/tree
exit 1
`)
f.commit("first", "one", nil)
if r := f.run(); r != CheckFailed {
t.Fatalf("run: %s", r)
}
tree := f.read("tree")
ms := f.outbox()
if tree == "" || contains(ms, tree) {
t.Fatalf("the notice must not hold the tree path %q: %+v", tree, ms)
}
if !contains(ms, "NEW/broker.toml: [secret.x] preapprove: is not allowed with ask = "never"") {
t.Fatalf("the notice must hold the reason: %+v", ms)
}
}

// TestCheckNoticeKeepsTheLastLine: output longer than the limit keeps its
// last line, and a long line keeps its end.
func TestCheckNoticeKeepsTheLastLine(t *testing.T) {
f := newFixture(t, ``, "")
f.script("check.sh", `i=0
while [ $i -lt 60 ]; do printf 'noise %0250d\n' $i; i=$((i+1)); done
printf 'start %0900d the real reason\n' 0
exit 1
`)
f.commit("first", "one", nil)
if r := f.run(); r != CheckFailed {
t.Fatalf("run: %s", r)
}
ms := f.outbox()
if len(ms) != 1 || !strings.Contains(ms[0].Text, "the real reason</pre>") || !strings.Contains(ms[0].Text, "start 000") {
t.Fatalf("the notice must keep the start and the end of the last line: %+v", ms)
}
if message.Len(ms[0].Text) > 4096 || strings.Count(ms[0].Text, "noise") < 2 {
t.Fatalf("the notice must show more than one line and fit: %d", message.Len(ms[0].Text))
}
}
16 changes: 15 additions & 1 deletion internal/gate/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/code6x/pullgate/internal/config"
Expand Down Expand Up @@ -281,7 +282,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, so it is not asked and not checked again: " + message.Code(err.Error()) + "\n" + message.Output(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(shortTrees(out, p.env.Tree, p.env.OldTree), 10))
return CheckFailed, nil
}
e.Log.Info("check passed", "sha", tip)
Expand Down Expand Up @@ -347,3 +348,16 @@ 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
}

// shortTrees replaces the long tree paths in command output with NEW and
// OLD: they say nothing to the owner and push the reason out of the
// message (code6x/pullgate#25).
func shortTrees(out, tree, oldTree string) string {
if tree != "" {
out = strings.ReplaceAll(out, tree, "NEW")
}
if oldTree != "" {
out = strings.ReplaceAll(out, oldTree, "OLD")
}
return out
}
46 changes: 36 additions & 10 deletions internal/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,53 @@ func Code(s string) string { return "<code>" + esc(s) + "</code>" }
// The full SHA is shown once per question, on the "Full:" line.
func SHA(sha string) string { return Code(Short(sha)) }

// OutputLine is the most characters of one line of command output in a
// message. A longer line keeps its start and its end, with "…" between:
// most tools print the reason last (code6x/pullgate#25).
const OutputLine = 300

// OutputMax is the most characters of command output in one message.
const OutputMax = 1500

// Output is the way to put the output of a command into a message: its
// last n lines, each cleaned and cut, in a <pre> block. It is "" for empty
// output.
// last lines (at most n, and at most OutputMax characters), each cleaned
// and cut in the middle, in a <pre> 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]) + "…"
var keep []string
used := 0
for i := len(lines) - 1; i >= 0 && len(keep) < n; i-- {
l := cutMiddle(lines[i], OutputLine)
r := Len(l) + 1 // UTF-16 units, as Telegram counts
if used+r > OutputMax && len(keep) > 0 {
break
}
lines[i] = esc(l)
used += r
keep = append([]string{html.EscapeString(l)}, keep...)
}
s := strings.Join(lines, "\n")
s := strings.Join(keep, "\n")
if strings.TrimSpace(s) == "" {
return ""
}
return "<pre>" + s + "</pre>"
}

// cutMiddle cleans one line like clean, but a line longer than max keeps
// its first max/4 and its last characters, with "…" between.
func cutMiddle(s string, max int) string {
r := []rune(s)
for i, c := range r {
if c == utf8.RuneError || unicode.IsControl(c) || isInvisible(c) {
r[i] = '?'
}
}
if len(r) > max {
head := max / 4
r = append(append(r[:head:head], '…'), r[len(r)-(max-head-1):]...)
}
return string(r)
}

// Len is the length of s in UTF-16 units, as Telegram counts.
func Len(s string) int { return len(utf16.Encode([]rune(s))) }

Expand Down
21 changes: 21 additions & 0 deletions internal/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"
"testing"
"unicode/utf8"
)

func TestClean(t *testing.T) {
Expand Down Expand Up @@ -244,3 +245,23 @@ func TestShortSHAsAndOneFullSHA(t *testing.T) {
t.Fatalf("too long: %d", Len(text))
}
}

// TestOutputKeepsTheEnd: command output keeps its last lines, and a long
// line keeps its start and its end (code6x/pullgate#25).
func TestOutputKeepsTheEnd(t *testing.T) {
if Output("a\nb", 0) != "" || Output("\n\n", 10) != "" {
t.Error("no lines, no block")
}
long := "start " + strings.Repeat("ж", 1000) + " the \"reason\"\x07"
got := Output(strings.Repeat("noise\n", 50)+long, 10)
if !strings.HasSuffix(got, " the &#34;reason&#34;?</pre>") || !strings.Contains(got, "start жж") || !strings.Contains(got, "…") {
t.Fatalf("the long line must keep its start and end, cleaned and escaped: %q", got)
}
if !utf8.ValidString(got) || strings.Count(got, "noise") != 9 {
t.Fatalf("want valid text and the last 10 lines: %q", got)
}
big := Output(strings.Repeat(strings.Repeat("x", 299)+"\n", 20)+"last", 20)
if !strings.HasSuffix(big, "\nlast</pre>") || utf8.RuneCountInString(big) > OutputMax+20 {
t.Fatalf("output over OutputMax keeps its last line and stays short: %d", utf8.RuneCountInString(big))
}
}
Loading