@@ -62,4 +62,33 @@ describe("base64", () => {
6262 // wrong number of =
6363 expect ( ( ) => fromBase64 ( "a===" ) ) . toThrow ( ) ;
6464 } ) ;
65+
66+ it ( "passes ChALkeR tests" , ( ) => {
67+ // From https://github.com/paulmillr/scure-base/issues/42, potentially containing duplicates
68+ // with above but just copy/pasted for the sake of checking the documented issue.
69+ // The non-strict case is disabled (see test below)
70+ const invalid = [
71+ "====" , // excessive padding
72+ "aQ=" , // incomplete padding
73+ "aQ===" , // excessive padding
74+ "aaaa====" , // excessive padding
75+ "a" , // invalid group of length 1
76+ "a===" , // invalid group of length 1, padded
77+ "a==" , // invalid group of length 1 + incomplete padding
78+ "aaaaa" , // invalid group of length 1 after group of length 4
79+ // "aa==", // non-strict, should be aQ==
80+ ] ;
81+ for ( const input of invalid ) {
82+ expect ( ( ) => fromBase64 ( input ) )
83+ . withContext ( `invalid input: '${ input } '` )
84+ . toThrow ( ) ;
85+ }
86+ } ) ;
87+
88+ it ( "decodes non-strict base64" , ( ) => {
89+ // There is currently no good reason to allow or forbid non-canonical base64 encodings. But since the library
90+ // always behaved like this, we better do not silently change behaviour.
91+ const data = fromBase64 ( "aa==" ) ;
92+ expect ( toBase64 ( data ) ) . toEqual ( "aQ==" ) ;
93+ } ) ;
6594} ) ;
0 commit comments