fix(conversion): prevent Base64Decode panic and Base64Encode input mutation - #808
fix(conversion): prevent Base64Decode panic and Base64Encode input mutation#808SAY-5 wants to merge 1 commit into
Conversation
…tation Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
Not up to standards ⛔
|
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
|
Still relevant. Both fixes here are behavioral: Base64Decode no longer panics on inputs whose length is not a multiple of 4, and Base64Encode no longer writes padding zeros into a caller slice that has spare capacity. Tests cover both cases and the diff is only in conversion/base64.go. Happy to rebase if that helps a review. |
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
Description of Change
Base64Decodeindexesinput[i+1]/input[i+2]/input[i+3]for every step of 4, so any input whose length is not a multiple of 4 panics withindex out of rangeinstead of returning. The decoder now iterates over whole 4-character groups only, so a malformed-length payload is ignored at the tail rather than reading past the end of the string.Base64Encodepadded short final groups by appending zero bytes to theinputslice. When the caller passes a slice with spare capacity (e.g.Base64Encode(buf[:n])),appendwrites those zeros into the caller's backing array and corrupts data observed through other slice headers. The padding bytes are now read locally, so the argument is left untouched.Added regression tests for both: decoding inputs of length 1, 2, 3 and 5, and encoding a sub-slice whose backing array must remain unchanged.
Closes #807
Checklist
Notes: Guard base64 decode/encode against malformed-length input and caller-slice mutation.