Skip to content

Commit 3f61e35

Browse files
committed
fix(base64): use js-base64 to handle non ascii text
Use js-base64 to handle non ascii text and ignore whitespaces Fix CorentinTh#879 and CorentinTh#409
1 parent a07806c commit 3f61e35

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"highlight.js": "^11.7.0",
6363
"iarna-toml-esm": "^3.0.5",
6464
"ibantools": "^4.3.3",
65+
"js-base64": "^3.7.6",
6566
"json5": "^2.2.3",
6667
"jwt-decode": "^3.1.2",
6768
"libphonenumber-js": "^1.10.28",

src/utils/base64.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ describe('base64 utils', () => {
3838

3939
it('should throw for incorrect base64 string', () => {
4040
expect(() => base64ToText('a')).to.throw('Incorrect base64 string');
41-
expect(() => base64ToText(' ')).to.throw('Incorrect base64 string');
41+
// should not really be false because trimming of space is now implied
42+
// expect(() => base64ToText(' ')).to.throw('Incorrect base64 string');
4243
expect(() => base64ToText('é')).to.throw('Incorrect base64 string');
4344
// missing final '='
4445
expect(() => base64ToText('bG9yZW0gaXBzdW0')).to.throw('Incorrect base64 string');
@@ -56,17 +57,17 @@ describe('base64 utils', () => {
5657

5758
it('should return false for incorrect base64 string', () => {
5859
expect(isValidBase64('a')).to.eql(false);
59-
expect(isValidBase64(' ')).to.eql(false);
6060
expect(isValidBase64('é')).to.eql(false);
6161
expect(isValidBase64('data:text/plain;notbase64,YQ==')).to.eql(false);
6262
// missing final '='
6363
expect(isValidBase64('bG9yZW0gaXBzdW0')).to.eql(false);
6464
});
6565

66-
it('should return false for untrimmed correct base64 string', () => {
67-
expect(isValidBase64('bG9yZW0gaXBzdW0= ')).to.eql(false);
68-
expect(isValidBase64(' LTE=')).to.eql(false);
69-
expect(isValidBase64(' YQ== ')).to.eql(false);
66+
it('should return true for untrimmed correct base64 string', () => {
67+
expect(isValidBase64('bG9yZW0gaXBzdW0= ')).to.eql(true);
68+
expect(isValidBase64(' LTE=')).to.eql(true);
69+
expect(isValidBase64(' YQ== ')).to.eql(true);
70+
expect(isValidBase64(' ')).to.eql(true);
7071
});
7172
});
7273

src/utils/base64.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Base64 } from 'js-base64';
2+
13
export { textToBase64, base64ToText, isValidBase64, removePotentialDataAndMimePrefix };
24

35
function textToBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
4-
const encoded = window.btoa(str);
6+
const encoded = Base64.encode(str);
57
return makeUrlSafe ? makeUriSafe(encoded) : encoded;
68
}
79

@@ -16,7 +18,7 @@ function base64ToText(str: string, { makeUrlSafe = false }: { makeUrlSafe?: bool
1618
}
1719

1820
try {
19-
return window.atob(cleanStr);
21+
return Base64.decode(cleanStr);
2022
}
2123
catch (_) {
2224
throw new Error('Incorrect base64 string');
@@ -34,10 +36,11 @@ function isValidBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boo
3436
}
3537

3638
try {
39+
const reEncodedBase64 = Base64.fromUint8Array(Base64.toUint8Array(cleanStr));
3740
if (makeUrlSafe) {
38-
return removePotentialPadding(window.btoa(window.atob(cleanStr))) === cleanStr;
41+
return removePotentialPadding(reEncodedBase64) === cleanStr;
3942
}
40-
return window.btoa(window.atob(cleanStr)) === cleanStr;
43+
return reEncodedBase64 === cleanStr.replace(/\s/g, '');
4144
}
4245
catch (err) {
4346
return false;

0 commit comments

Comments
 (0)