c60418
From 5cea97e083448aaa2352320612541c895178b3b5 Mon Sep 17 00:00:00 2001
c60418
From: "Christoph M. Becker" <cmbecker69@gmx.de>
c60418
Date: Mon, 14 Jun 2021 13:22:27 +0200
c60418
Subject: [PATCH] Fix #81122: SSRF bypass in FILTER_VALIDATE_URL
c60418
c60418
We need to ensure that the password detected by parse_url() is actually
c60418
a valid password; we can re-use is_userinfo_valid() for that.
c60418
---
c60418
 ext/filter/logical_filters.c   |  4 +++-
c60418
 ext/filter/tests/bug81122.phpt | 21 +++++++++++++++++++++
c60418
 2 files changed, 24 insertions(+), 1 deletion(-)
c60418
 create mode 100644 ext/filter/tests/bug81122.phpt
c60418
c60418
diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c
c60418
index ba2e7e527e76..721da45d532d 100644
c60418
--- a/ext/filter/logical_filters.c
c60418
+++ b/ext/filter/logical_filters.c
c60418
@@ -632,7 +632,9 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
c60418
 		RETURN_VALIDATION_FAILED
c60418
 	}
c60418
 
c60418
-	if (url->user != NULL && !is_userinfo_valid(url->user)) {
c60418
+	if (url->user != NULL && !is_userinfo_valid(url->user)
c60418
+		|| url->pass != NULL && !is_userinfo_valid(url->pass)
c60418
+	) {
c60418
 		php_url_free(url);
c60418
 		RETURN_VALIDATION_FAILED
c60418
 
c60418
diff --git a/ext/filter/tests/bug81122.phpt b/ext/filter/tests/bug81122.phpt
c60418
new file mode 100644
c60418
index 000000000000..d89d4114a547
c60418
--- /dev/null
c60418
+++ b/ext/filter/tests/bug81122.phpt
c60418
@@ -0,0 +1,21 @@
c60418
+--TEST--
c60418
+Bug #81122 (SSRF bypass in FILTER_VALIDATE_URL)
c60418
+--SKIPIF--
c60418
+
c60418
+if (!extension_loaded('filter')) die("skip filter extension not available");
c60418
+?>
c60418
+--FILE--
c60418
+
c60418
+$urls = [
c60418
+    "https://example.com:\\@test.com/",
c60418
+    "https://user:\\epass@test.com",
c60418
+    "https://user:\\@test.com",
c60418
+];
c60418
+foreach ($urls as $url) {
c60418
+    var_dump(filter_var($url, FILTER_VALIDATE_URL));
c60418
+}
c60418
+?>
c60418
+--EXPECT--
c60418
+bool(false)
c60418
+bool(false)
c60418
+bool(false)