Update sectraining.md

This commit is contained in:
Sander Hautvast 2024-02-05 12:17:47 +01:00 committed by GitHub
parent becb68794d
commit 9e880e3505
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -154,3 +154,37 @@ public class AdminService {
... ...
} }
``` ```
### Server-Side Request Forgery
Vulnerable Example
```java
private static String fetchRemoteObject(String location) throws Exception {
URL url = new URL(location);
URLConnection connection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String body = reader.lines().collect(Collectors.joining());
return body;
}
```
#### solution
```java
private static String fetchRemoteObject(String location) throws Exception {
URL url = new URL(location);
if (!url.getHost().endsWith(".example.com") ||
!url.getProtocol().equals("http") &&
!url.getProtocol().equals("https")) {
throw new Exception("Forbidden remote source");
}
URLConnection connection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String body = reader.lines().collect(Collectors.joining());
return body;
}
```