From fefad1e7814d37fe6565cbd5f3b50613c95b560c Mon Sep 17 00:00:00 2001 From: Matteo Pace Date: Fri, 15 Dec 2023 10:59:11 +0100 Subject: [PATCH] better comment about second DecodeString call --- internal/transformations/base64decode.go | 6 ++++-- internal/transformations/base64decode_test.go | 15 ++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/transformations/base64decode.go b/internal/transformations/base64decode.go index 52bb86baa..42eb0b5f3 100644 --- a/internal/transformations/base64decode.go +++ b/internal/transformations/base64decode.go @@ -21,8 +21,10 @@ func base64decode(data string) (string, bool, error) { // and perform a partial decoding up to that point if corrErr, ok := err.(base64.CorruptInputError); ok { illegalCharPos := int(corrErr) - // Forgiving call to DecodeString, decoding is performed up to the illegal characther - // If an error occurs, dec will still contain the decoded string up to the error + // Forgiving call (no error check) to DecodeString. Decoding is performed truncating + // the input string to the first error index. If a new decoding error occurs, + // it will not be about an illegal character but a malformed encoding of the trailing + // character because of the truncation. The dec will still contain a best effort decoded string dec, _ = base64.RawStdEncoding.DecodeString(dataNoPadding[:illegalCharPos]) } else { return data, false, nil diff --git a/internal/transformations/base64decode_test.go b/internal/transformations/base64decode_test.go index 65b43adbf..cd5ef9213 100644 --- a/internal/transformations/base64decode_test.go +++ b/internal/transformations/base64decode_test.go @@ -41,27 +41,32 @@ var b64DecodeTests = []struct { expected: "", }, { - name: "decoded up to the space (invalid caracter)", + name: "Malformed base64 encoding", + input: "PHNjcmlwd", + expected: "