From 3b2c36843eb94842930a247873d902de48f10ab8 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 17 Nov 2026 09:13:05 +0100 Subject: [PATCH] Take secret as plain string instead of base64 Using base64 was too complicated and had no real purpose except preventing terminal injection attacks. Instead, do this manually. Thanks to Felix Dreissig (F30) for the suggestion. --- README.adoc | 49 ++++++++++++++++++++++++++++++------------------- main.go | 39 +++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/README.adoc b/README.adoc index 439d843..c575945 100644 --- a/README.adoc +++ b/README.adoc @@ -5,7 +5,7 @@ sss-vault is a CLI tool in Go that implements Shamir's Secret Sharing (SSS). The actual implementation is copied from the shamir/ directory of -{vault}[HashiCorp's Vault]. Secrets and parts are Base64-encoded. +{vault}[HashiCorp's Vault]. The code in shamir/ is taken from {0c6c13dd38}[0c6c13dd38 (license: update headers to IBM Corp. (#10229) (#10233), 2025-10-21)]. @@ -37,41 +37,52 @@ Needs Go to build, no external dependencies == Usage .... -$ base64 -Test Message - -VGVzdCBNZXNzYWdlCg== $ ./sss-vault split 5 3 -Waiting for secret as base64 on stdin until EOF -VGVzdCBNZXNzYWdlCg== +Waiting for secret on stdin until EOF +Test +Message +! -Generated 5 parts with threshold 3 as base64 +Generated 5 parts with threshold 3 part 1: -jDfKZ5pvHA8lreTtcC4= +YKJz2uuG6/n80bzKjm5Yjw== part 2: -M7apcB/tRrF9HUDQWy8= +Es+deRZMztqSCClXozmfiw== part 3: -bOhnRkXm5Op7IBbrRAc= +B8z9cZOdVlMF/404WONuPQ== part 4: -jY1fTie6hx3PxeQWfLw= +lnqwvWBJRAcRvPj3WwW6Gw== part 5: -C6Y0TljjcEYDzMpbP9w= +6jHfMI3pqqj6MC7SJb9iBg== +.... +.... $ ./sss-vault combine -Waiting for parts as base64 on stdin until EOF (one per line) -M7apcB/tRrF9HUDQWy8= -jDfKZ5pvHA8lreTtcC4= -C6Y0TljjcEYDzMpbP9w= +Waiting for parts on stdin until EOF (one per line) +6jHfMI3pqqj6MC7SJb9iBg== +YKJz2uuG6/n80bzKjm5Yjw== +B8z9cZOdVlMF/404WONuPQ== -Recovered secret as base64: -VGVzdCBNZXNzYWdlCg== +Recovered secret: +Test +Message +! +.... + +Terminal injection is avoided by escaping if necessary: + +.... +$ ./sss-vault combine +[...] +Recovered secret (escaped): +"\x1b[31mterminal injection\x1b[0m" .... diff --git a/main.go b/main.go index b57282c..2ba8ff0 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,6 @@ // sss-vault is a CLI tool that implements Shamir's Secret Sharing (SSS). The // actual implementation is copied from the shamir/ directory of HashiCorp's -// Vault [1]. Secrets and parts are Base64-encoded. +// Vault [1]. // // [1] https://github.com/hashicorp/vault @@ -17,6 +17,8 @@ import ( "os" "strconv" "strings" + "unicode" + "unicode/utf8" "sss-vault/shamir" ) @@ -41,27 +43,22 @@ func main() { log.Fatalf("invalid threshold: %v", err) } _, err = fmt.Fprintln(os.Stderr, - "Waiting for secret as base64 on stdin until EOF") + "Waiting for secret on stdin until EOF") if err != nil { log.Fatal(err) } - stdin, err := io.ReadAll(os.Stdin) - if err != nil { - log.Fatal(err) - } - secret, err := base64.StdEncoding.DecodeString(string(stdin)) + secret, err := io.ReadAll(os.Stdin) if err != nil { log.Fatal(err) } - res, err := shamir.Split(secret, parts, threshold) if err != nil { log.Fatal(err) } _, err = fmt.Printf( - "\nGenerated %d parts with threshold %d as base64\n\n", + "\nGenerated %d parts with threshold %d\n\n", parts, threshold) if err != nil { log.Fatal(err) @@ -76,7 +73,7 @@ func main() { } else if os.Args[1] == "combine" && len(os.Args) == 2 { _, err := fmt.Fprintln(os.Stderr, - "Waiting for parts as base64 on stdin until EOF (one per line)") + "Waiting for parts on stdin until EOF (one per line)") if err != nil { log.Fatal(err) } @@ -104,8 +101,11 @@ func main() { if err != nil { log.Fatal(err) } - _, err = fmt.Printf("\nRecovered secret as base64:\n%s\n", - base64.StdEncoding.EncodeToString(secret)) + format := "\nRecovered secret:\n%s\n" + if !isTerminalSafe(string(secret)) { + format = "\nRecovered secret (escaped):\n%q\n" + } + _, err = fmt.Printf(format, string(secret)) if err != nil { log.Fatal(err) } @@ -115,4 +115,19 @@ func main() { } } +func isTerminalSafe(x string) bool { + if !utf8.ValidString(x) { + return false + } + for _, r := range x { + if r == '\n' || r == '\t' { + continue + } + if unicode.IsControl(r) || unicode.Is(unicode.Bidi_Control, r) { + return false + } + } + return true +} + // vi: set noet ts=4 sw=4 sts=4: -- 2.56.1