Skip to content

Commit

Permalink
Fix code scanning alert no. 351: Server-side request forgery
Browse files Browse the repository at this point in the history
CoPilot AI generated patch

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
mprins and github-advanced-security[bot] authored Nov 12, 2024
1 parent ef324ba commit b7c6ee2
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public ResponseEntity<?> proxy(
private URI buildWMSUrl(GeoService service, HttpServletRequest request) {
final UriComponentsBuilder originalServiceUrl =
UriComponentsBuilder.fromHttpUrl(service.getUrl());
// Validate the service URL against allowed domains
if (!isValidDomain(service.getUrl())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid service URL");

Check warning on line 169 in src/main/java/org/tailormap/api/controller/GeoServiceProxyController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/GeoServiceProxyController.java#L169

Added line #L169 was not covered by tests
}
// request.getParameterMap() includes parameters from an application/x-www-form-urlencoded POST
// body
final MultiValueMap<String, String> requestParams =
Expand Down Expand Up @@ -264,4 +268,19 @@ private static ResponseEntity<?> doProxy(
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body("Bad Gateway");
}
}

private static final List<String> ALLOWED_DOMAINS = List.of(
"example.com",
"another-example.com"
);

private boolean isValidDomain(String url) {
try {
URI uri = new URI(url);
String host = uri.getHost();

Check warning on line 280 in src/main/java/org/tailormap/api/controller/GeoServiceProxyController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/GeoServiceProxyController.java#L276-L280

Added lines #L276 - L280 were not covered by tests
return ALLOWED_DOMAINS.stream().anyMatch(host::endsWith);
} catch (Exception e) {
return false;
}
}
}

0 comments on commit b7c6ee2

Please sign in to comment.