File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed
Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments