Added alphabet check before decoding #7
@ -1,6 +1,11 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const b64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function atob(str) {
 | 
					function atob(str) {
 | 
				
			||||||
 | 
					  for (const ch of str)
 | 
				
			||||||
 | 
					    if (!b64Alphabet.includes(ch))
 | 
				
			||||||
 | 
					      throw new Error(`Invalid character '${ch}' in base64 String`);
 | 
				
			||||||
  return Buffer.from(str, 'base64').toString('binary');
 | 
					  return Buffer.from(str, 'base64').toString('binary');
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										9
									
								
								test.js
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								test.js
									
									
									
									
									
								
							@ -4,6 +4,7 @@
 | 
				
			|||||||
  var atob = require('.');
 | 
					  var atob = require('.');
 | 
				
			||||||
  var encoded = "SGVsbG8sIFdvcmxkIQ=="
 | 
					  var encoded = "SGVsbG8sIFdvcmxkIQ=="
 | 
				
			||||||
  var unencoded = "Hello, World!";
 | 
					  var unencoded = "Hello, World!";
 | 
				
			||||||
 | 
					  var malformed = "SGVsbG8s{"
 | 
				
			||||||
  /*
 | 
					  /*
 | 
				
			||||||
    , encoded = "SGVsbG8sIBZM"
 | 
					    , encoded = "SGVsbG8sIBZM"
 | 
				
			||||||
    , unencoded = "Hello, 世界"
 | 
					    , unencoded = "Hello, 世界"
 | 
				
			||||||
@ -14,5 +15,13 @@
 | 
				
			|||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const decoded = atob(malformed)
 | 
				
			||||||
 | 
					    console.log('[FAIL]', malformed, decoded);
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  } catch (_) {
 | 
				
			||||||
 | 
					    /* pass */
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log('[PASS] all tests pass');
 | 
					  console.log('[PASS] all tests pass');
 | 
				
			||||||
}());
 | 
					}());
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user