Skip to content

Commit

Permalink
better comment about second DecodeString call
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tteoP committed Dec 15, 2023
1 parent cfa39f1 commit fefad1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions internal/transformations/base64decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions internal/transformations/base64decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,32 @@ var b64DecodeTests = []struct {
expected: "<TEST>",
},
{
name: "decoded up to the space (invalid caracter)",
name: "Malformed base64 encoding",
input: "PHNjcmlwd",
expected: "<scrip",
},
{
name: "decoded up to the space (invalid character)",
input: "PFR FU1Q+",
expected: "<T",
},
{
name: "decoded up to the dot (invalid caracter)",
input: "P.HNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==",
expected: "",
expected: "", // Only the P character does not result in a printable character conversion.
},
{
name: "decoded up to the dot (invalid caracter)",
name: "decoded up to the dot (invalid character)",
input: "PHNjcmlwd.D5hbGVydCgxKTwvc2NyaXB0Pg==",
expected: "<scrip",
},
{
name: "decoded up to the dot (invalid caracter)",
name: "decoded up to the dot (invalid character)",
input: "PHNjcmlwdD.5hbGVydCgxKTwvc2NyaXB0Pg==",
expected: "<script",
},
{
name: "decoded up to the dash (invalid caracter for base64.RawStdEncoding)",
name: "decoded up to the dash (invalid character for base64.RawStdEncoding)",
input: "PFRFU1Q-",
expected: "<TEST",
},
Expand Down

0 comments on commit fefad1e

Please sign in to comment.