From ca1df581db3630bbdd11081c2be0da0625b8460d Mon Sep 17 00:00:00 2001 From: "Pascal S. de Kloe" Date: Fri, 12 Mar 2021 14:30:24 +0100 Subject: [PATCH] Limit mqttc input size. --- cmd/mqttc/main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/mqttc/main.go b/cmd/mqttc/main.go index 11403ef..44c6042 100644 --- a/cmd/mqttc/main.go +++ b/cmd/mqttc/main.go @@ -18,6 +18,8 @@ import ( "github.com/pascaldekloe/mqtt" ) +const messageMax = 256 * 1024 * 1024 + // ANSI escape codes for markup. const ( bold = "\x1b[1m" @@ -148,9 +150,12 @@ func main() { go func() { if *publishFlag != "" { // publish standard input - message, err := io.ReadAll(os.Stdin) - if err != nil { - log.Fatal(err) + message, err := io.ReadAll(io.LimitReader(os.Stdin, messageMax)) + switch { + case err != nil: + log.Fatal(name, ": ", err) + case len(message) >= messageMax: + log.Fatalf("%s: standard input reached %d byte limit", name, messageMax) } ctx, cancel := context.WithTimeout(context.Background(), *timeoutFlag)