The vulnerability is Server-Side Request Forgery (SSRF)
html
<!--Insert the vulnerable code into a short web application--><!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Web App Example</title></head><body><formmethod="GET"action="vulnerable_code.php"><labelfor="url">Enter URL:</label><inputtype="text"name="url"id="url"><buttontype="submit">Submit</button></form></body></html>
php // vulnerable code $_GET['url']; $content = file_get_contents($url); echo $content; ?> ``` ```html Web App Example ``` ```php // solution how to fix the vulnerable code
php
// solution how to fix the vulnerable code
<?phpfunctionis_valid_url($url){if(filter_var($url,FILTER_VALIDATE_URL)===false){returnfalse;}$parsed_url=parse_url($url);if(($parsed_url['scheme']!=='http')&&($parsed_url['scheme']!=='https')){returnfalse;}if($parsed_url['host']==='localhost'||$parsed_url['host']==='127.0.0.1'){returnfalse;}// Add further validation checks as neededreturntrue;}$url=$_GET['url'];if(is_valid_url($url)){$content=file_get_contents($url);echo$content;}else{echo"Invalid URL";}