From 7354aa0d08a06eac42c635670a55f858bd23c943 Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Fri, 23 Dec 2022 12:28:06 +0100 Subject: [PATCH] internal/bech32: remove data length limit Fixes #453 --- cmd/age/age_test.go | 35 ++++++++++++++++++++++++++++++++++ cmd/age/testdata/plugin.txt | 20 +++++++++++++++++++ internal/bech32/bech32.go | 6 ------ internal/bech32/bech32_test.go | 9 +++++---- 4 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 cmd/age/testdata/plugin.txt diff --git a/cmd/age/age_test.go b/cmd/age/age_test.go index 7dfd5fee..a95cdbe4 100644 --- a/cmd/age/age_test.go +++ b/cmd/age/age_test.go @@ -5,6 +5,7 @@ package main import ( + "bufio" "os" "testing" @@ -28,11 +29,45 @@ func TestMain(m *testing.M) { main() return 0 }, + "age-plugin-test": func() (exitCode int) { + // TODO: use plugin server package once it's available. + switch os.Args[1] { + case "--age-plugin=recipient-v1": + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() // add-recipient + scanner.Scan() // body + scanner.Scan() // wrap-file-key + scanner.Scan() // body + fileKey := scanner.Text() + scanner.Scan() // done + scanner.Scan() // body + os.Stdout.WriteString("-> recipient-stanza 0 test\n") + os.Stdout.WriteString(fileKey + "\n") + os.Stdout.WriteString("-> done\n\n") + return 0 + case "--age-plugin=identity-v1": + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() // add-identity + scanner.Scan() // body + scanner.Scan() // recipient-stanza + scanner.Scan() // body + fileKey := scanner.Text() + scanner.Scan() // done + scanner.Scan() // body + os.Stdout.WriteString("-> file-key 0\n") + os.Stdout.WriteString(fileKey + "\n") + os.Stdout.WriteString("-> done\n\n") + return 0 + default: + return 1 + } + }, })) } func TestScript(t *testing.T) { testscript.Run(t, testscript.Params{ Dir: "testdata", + // TODO: enable AGEDEBUG=plugin without breaking stderr checks. }) } diff --git a/cmd/age/testdata/plugin.txt b/cmd/age/testdata/plugin.txt new file mode 100644 index 00000000..01e3ed8e --- /dev/null +++ b/cmd/age/testdata/plugin.txt @@ -0,0 +1,20 @@ +# encrypt and decrypt a file with a test plugin +age -r age1test10qdmzv9q -o test.age input +age -d -i key.txt test.age +cmp stdout input +! stderr . + +# very long identity and recipient +age -R long-recipient.txt -o test.age input +age -d -i long-key.txt test.age +cmp stdout input +! stderr . + +-- input -- +test +-- key.txt -- +AGE-PLUGIN-TEST-10Q32NLXM +-- long-recipient.txt -- +age1test10pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8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S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7RC0PU8S7Q5U8SUD diff --git a/internal/bech32/bech32.go b/internal/bech32/bech32.go index c12864f0..6f8a48cd 100644 --- a/internal/bech32/bech32.go +++ b/internal/bech32/bech32.go @@ -111,9 +111,6 @@ func Encode(hrp string, data []byte) (string, error) { if err != nil { return "", err } - if len(hrp)+len(values)+7 > 90 { - return "", fmt.Errorf("too long: hrp length=%d, data length=%d", len(hrp), len(values)) - } if len(hrp) < 1 { return "", fmt.Errorf("invalid HRP: %q", hrp) } @@ -144,9 +141,6 @@ func Encode(hrp string, data []byte) (string, error) { // Decode decodes a Bech32 string. If the string is uppercase, the HRP will be uppercase. func Decode(s string) (hrp string, data []byte, err error) { - if len(s) > 90 { - return "", nil, fmt.Errorf("too long: len=%d", len(s)) - } if strings.ToLower(s) != s && strings.ToUpper(s) != s { return "", nil, fmt.Errorf("mixed case") } diff --git a/internal/bech32/bech32_test.go b/internal/bech32/bech32_test.go index 4568d62c..01ce361c 100644 --- a/internal/bech32/bech32_test.go +++ b/internal/bech32/bech32_test.go @@ -28,7 +28,7 @@ func TestBech32(t *testing.T) { str string valid bool }{ - {"A12UEL5L", true}, + {"A12UEL5L", true}, // empty {"a12uel5l", true}, {"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", true}, {"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", true}, @@ -44,11 +44,12 @@ func TestBech32(t *testing.T) { {"1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, // empty hrp // invalid character (DEL) in hrp {"spl" + string(rune(127)) + "t1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", false}, - // too long - {"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", false}, + + // long vectors that we do accept despite the spec, see Issue 453 + {"long10pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7rc0pu8s7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true}, + {"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", true}, // BIP 173 invalid vectors. - {"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", false}, {"pzry9x0s0muk", false}, {"1pzry9x0s0muk", false}, {"x1b4n0q5v", false},