Skip to content

Commit b70af41

Browse files
committed
cmd/age: don't output binary plaintext to terminal
Closes #626
1 parent 4202739 commit b70af41

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

cmd/age/age.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"runtime/debug"
1919
"slices"
2020
"strings"
21+
"unicode"
2122

2223
"filippo.io/age"
2324
"filippo.io/age/agessh"
@@ -273,9 +274,24 @@ func main() {
273274
}()
274275
out = f
275276
} else if term.IsTerminal(os.Stdout) {
277+
buf := &bytes.Buffer{}
278+
defer func() {
279+
if out == buf {
280+
io.Copy(os.Stdout, buf)
281+
}
282+
}()
276283
if name != "-" {
277284
if decryptFlag {
278-
// TODO: buffer the output and check it's printable.
285+
// Buffer the output to check it's printable.
286+
out = buf
287+
defer func() {
288+
if bytes.ContainsFunc(buf.Bytes(), func(r rune) bool {
289+
return r != '\n' && r != '\r' && r != '\t' && unicode.IsControl(r)
290+
}) {
291+
errorWithHint("refusing to output binary to the terminal",
292+
`force anyway with "-o -"`)
293+
}
294+
}()
279295
} else if !armorFlag {
280296
// If the output wouldn't be armored, refuse to send binary to
281297
// the terminal unless explicitly requested with "-o -".
@@ -287,8 +303,6 @@ func main() {
287303
if in == os.Stdin && term.IsTerminal(os.Stdin) {
288304
// If the input comes from a TTY and output will go to a TTY,
289305
// buffer it up so it doesn't get in the way of typing the input.
290-
buf := &bytes.Buffer{}
291-
defer func() { io.Copy(os.Stdout, buf) }()
292306
out = buf
293307
}
294308
}

0 commit comments

Comments
 (0)