]> ruderich.org/simon Gitweb - sss-vault/sss-vault.git/commitdiff
Take secret as plain string instead of base64
authorSimon Ruderich <simon@ruderich.org>
Tue, 17 Nov 2026 08:13:05 +0000 (09:13 +0100)
committerSimon Ruderich <simon@ruderich.org>
Tue, 17 Nov 2026 08:13:05 +0000 (09:13 +0100)
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
main.go

index 439d84362637b7889501bb47c67ec5c28f10a557..c5759459c167edfca7091cfbd71ebd6e6e05e7ce 100644 (file)
@@ -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
-<Ctrl-D>
-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
+!
 <Ctrl-D>
 
-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==
 <Ctrl-D>
 
-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 b57282c7611d8eed37d57dc56311a04ddc738910..2ba8ff02432f039342f308cf272f45b2d0e482f3 100644 (file)
--- 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: