2023-03-07 17:16:47 +01:00
|
|
|
== How to fix it in Core PHP
|
|
|
|
|
|
|
|
=== Code examples
|
2022-11-25 17:34:37 +01:00
|
|
|
The following code is vulnerable to SSRF as it opens a URL defined by untrusted data.
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,php,diff-id=1,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
$host = $_GET['host'];
|
|
|
|
$url = "https://$host/.well-known/openid-configuration";
|
|
|
|
|
2022-12-09 15:18:11 +01:00
|
|
|
$ch = curl_init($url); // Noncompliant
|
|
|
|
curl_exec($ch);
|
2022-11-25 17:34:37 +01:00
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,php,diff-id=1,diff-type=compliant]
|
|
|
|
----
|
|
|
|
$allowedHosts = ["trusted1" => "trusted1.example.com", "trusted2" => "trusted2.example.com"];
|
|
|
|
$host = $allowedHosts[$_GET['host']];
|
|
|
|
$url = "https://$host/.well-known/openid-configuration";
|
|
|
|
|
2022-12-09 15:18:11 +01:00
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_exec($ch);
|
2022-11-25 17:34:37 +01:00
|
|
|
----
|
|
|
|
|
2023-08-21 10:51:21 +02:00
|
|
|
=== How does this work?
|
2022-11-25 17:34:37 +01:00
|
|
|
|
2023-08-21 10:51:21 +02:00
|
|
|
include::../../common/fix/pre-approved-list.adoc[]
|
2022-11-25 17:34:37 +01:00
|
|
|
|
2024-10-30 15:57:46 +01:00
|
|
|
include::../../common/fix/blacklist.adoc[]
|
|
|
|
|
2022-11-25 17:34:37 +01:00
|
|
|
=== Pitfalls
|
|
|
|
|
|
|
|
include::../../common/pitfalls/starts-with.adoc[]
|
2024-10-30 15:57:46 +01:00
|
|
|
|
|
|
|
include::../../common/pitfalls/blacklist-toctou.adoc[]
|