Browse Source

[php] Base64 throws upon decoding failures (fixes #10394)

Aleksandr Kuzmenko 4 years ago
parent
commit
bcbc8351d7
2 changed files with 9 additions and 2 deletions
  1. 8 2
      std/php/_std/haxe/crypto/Base64.hx
  2. 1 0
      tests/unit/src/unit/TestMisc.hx

+ 8 - 2
std/php/_std/haxe/crypto/Base64.hx

@@ -52,7 +52,10 @@ class Base64 {
 				default:
 			}
 		}
-		return Bytes.ofString(base64_decode(str, true));
+		return switch base64_decode(str, true) {
+			case false: throw new Exception("Base64.decode : invalid encoded string");
+			case s: Bytes.ofString(s);
+		}
 	}
 
 	public static inline function urlEncode(bytes:Bytes, complement = false):String {
@@ -70,6 +73,9 @@ class Base64 {
 				default:
 			}
 		}
-		return Bytes.ofString(base64_decode(str_replace(URL_62_63, NORMAL_62_63, str), true));
+		return switch base64_decode(str_replace(URL_62_63, NORMAL_62_63, str), true) {
+			case false: throw new Exception("Base64.urlDecode : invalid encoded string");
+			case s: Bytes.ofString(s);
+		}
 	}
 }

+ 1 - 0
tests/unit/src/unit/TestMisc.hx

@@ -328,6 +328,7 @@ class TestMisc extends Test {
 		eq("Héllow", haxe.crypto.Base64.decode("SMOpbGxvdw", false).toString());
 		eq("Héllo", haxe.crypto.Base64.decode("SMOpbGxv").toString());
 		eq("Héll", haxe.crypto.Base64.decode("SMOpbGw=").toString());
+		exc(() -> haxe.crypto.Base64.decode("invalid string"));
 
 		// alternative base64
 		var b = new haxe.crypto.BaseCode(haxe.io.Bytes.ofString("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"));