-
-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: more forgiving base64 transformation [custom implementation] #944
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cfa39f1
more forgiving base64 transformation
M4tteoP fefad1e
better comment about second DecodeString call
M4tteoP 718e225
custom implementation
M4tteoP f7907e7
Merge branch 'main' into base64_flexible_custom
M4tteoP d3dac7a
comments
M4tteoP 552c207
address review, minor refactor
M4tteoP 8221a87
moves new line characters check
M4tteoP d600c6d
Extends tests with golang base64 tests
M4tteoP 77ce907
updates link with the permalink to the exact borrowed payloads
M4tteoP 8753b80
adds note about using custom implementation
M4tteoP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,17 +10,173 @@ import ( | |
"testing" | ||
) | ||
|
||
var b64DecodeTests = []string{ | ||
"VGVzdENhc2U=", | ||
"P.HNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==", | ||
"VGVzdABDYXNl", | ||
var b64DecodeTests = []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "Valid", | ||
input: "VGVzdENhc2U=", | ||
expected: "TestCase", | ||
}, | ||
{ | ||
name: "Valid with \u0000", | ||
input: "VGVzdABDYXNl", | ||
expected: "Test\x00Case", | ||
}, | ||
{ | ||
name: "Valid without padding", | ||
input: "VGVzdENhc2U", | ||
expected: "TestCase", | ||
}, | ||
{ | ||
name: "Valid without longer padding", | ||
input: "PA==", | ||
expected: "<", | ||
}, | ||
{ | ||
name: "valid <TEST>", | ||
input: "PFRFU1Q+", | ||
expected: "<TEST>", | ||
}, | ||
{ | ||
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 character)", | ||
input: "P.HNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==", | ||
expected: "", // Only the P character does not result in a printable character conversion. | ||
}, | ||
{ | ||
name: "decoded up to the dot (invalid character)", | ||
input: "PHNjcmlwd.D5hbGVydCgxKTwvc2NyaXB0Pg==", | ||
expected: "<scrip", | ||
}, | ||
{ | ||
name: "decoded up to the dot (invalid character)", | ||
input: "PHNjcmlwdD.5hbGVydCgxKTwvc2NyaXB0Pg==", | ||
expected: "<script", | ||
}, | ||
{ | ||
name: "decoded up to the dash (invalid character for base64, only valid for Base64url)", | ||
input: "PFRFU1Q-", | ||
expected: "<TEST", | ||
}, | ||
// The following tests are from the golang base64 decoder tests | ||
// Source: https://github.com/golang/go/blob/master/src/encoding/base64/base64_test.go | ||
{ | ||
name: "golang test - RFC 3548 examples", | ||
input: "FPucA9l+", | ||
expected: "\x14\xfb\x9c\x03\xd9\x7e", | ||
}, | ||
{ | ||
name: "golang test - RFC 3548 examples", | ||
input: "FPucA9k=", | ||
expected: "\x14\xfb\x9c\x03\xd9", | ||
}, | ||
{ | ||
name: "golang test - RFC 3548 examples", | ||
input: "FPucAw==", | ||
expected: "\x14\xfb\x9c\x03", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "Zg==", | ||
expected: "f", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "Zm8=", | ||
expected: "fo", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "Zm9v", | ||
expected: "foo", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "Zm9vYg==", | ||
expected: "foob", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "Zm9vYmE=", | ||
expected: "fooba", | ||
}, | ||
{ | ||
name: "golang test - RFC 4648 examples", | ||
input: "Zm9vYmFy", | ||
expected: "foobar", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "c3VyZS4=", | ||
expected: "sure.", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "c3VyZQ==", | ||
expected: "sure", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "c3Vy", | ||
expected: "sur", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "c3U=", | ||
expected: "su", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "bGVhc3VyZS4=", | ||
expected: "leasure.", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "ZWFzdXJlLg==", | ||
expected: "easure.", | ||
}, | ||
{ | ||
name: "golang test - Wikipedia examples", | ||
input: "YXN1cmUu", | ||
expected: "asure.", | ||
}, | ||
} | ||
|
||
func TestBase64Decode(t *testing.T) { | ||
for _, tt := range b64DecodeTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual, _, err := base64decode(tt.input) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if actual != tt.expected { | ||
t.Errorf("Expected %q, but got %q", tt.expected, actual) | ||
} | ||
}) | ||
} | ||
} | ||
func BenchmarkB64Decode(b *testing.B) { | ||
for _, tt := range b64DecodeTests { | ||
b.Run(tt, func(b *testing.B) { | ||
b.Run(tt.input, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_, _, err := base64decode(tt) | ||
_, _, err := base64decode(tt.input) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
|
@@ -31,7 +187,7 @@ func BenchmarkB64Decode(b *testing.B) { | |
|
||
func FuzzB64Decode(f *testing.F) { | ||
for _, tc := range b64DecodeTests { | ||
f.Add(tc) | ||
f.Add(tc.input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interestingly the fuzz test was still set up for this forgiving version, that's nice |
||
} | ||
f.Fuzz(func(t *testing.T, tc string) { | ||
data, _, err := base64decode(tc) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it still make sense to executer these when we
break
above on illegal character?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Early returning
dst.String()
when we break above on illegal character (so not when padding reached) leads to some failing tests:Trailing characters require to be rearranged even if that case, as if the end of the string was reached