Skip to content

Commit 2b187a1

Browse files
author
Nicholas C. Zakas
committed
Now detect invalid padding
1 parent cd30d7e commit 2b187a1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

encodings/base64/base64.htm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ <h1>Base64 Encoding/Decoding Tests</h1>
7474

7575
_should: {
7676
error: {
77-
testInvalidChar: new Error("Not a base64-encoded string.")
77+
testInvalidChar: new Error("Not a base64-encoded string."),
78+
testInvalidString: new Error("Not a base64-encoded string."),
79+
testMissingPaddingIndicator: new Error("Not a base64-encoded string.")
7880
}
7981
},
8082

@@ -117,9 +119,14 @@ <h1>Base64 Encoding/Decoding Tests</h1>
117119
},
118120

119121
testInvalidChar: function(){
120-
base64Decode("TWF,JFU");
122+
base64Decode("aGF,0Y2g");
123+
},
124+
125+
testInvalidString: function(){
126+
base64Decode("aGF0Y2g==");
121127
}
122128

129+
123130
}));
124131

125132

encodings/base64/base64.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ function base64Encode(text){
7979
*/
8080
function base64Decode(text){
8181

82+
//ignore white space
83+
text = text.replace(/\s/g,"");
84+
8285
//first check for any unexpected input
83-
if(!(/^[a-z0-9\+\/\s]+\={0,2}$/i.test(text))){
86+
if(!(/^[a-z0-9\+\/\s]+\={0,2}$/i.test(text)) | text.length % 4 > 0){
8487
throw new Error("Not a base64-encoded string.");
8588
}
8689

@@ -91,7 +94,7 @@ function base64Decode(text){
9194
result = [];
9295

9396
//remove any whitespace and equals signs
94-
text = text.replace(/[\s=]/g, "");
97+
text = text.replace(/=/g, "");
9598

9699
//loop over each character
97100
while(i < text.length){

0 commit comments

Comments
 (0)