Skip to content

Commit

Permalink
fix: throw error on invalid URI (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi authored Jan 8, 2025
1 parent 6086702 commit e0576a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/org/extism/sdk/chicory/HostEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,11 @@ long[] request(Instance instance, long... args) {

byte[] request(String method, URI uri, Map<String, String> headers, byte[] requestBody) {
var host = uri.getHost();
if (host == null || host.isBlank()) {
throw new ExtismHttpException("HTTP request host is invalid for URI: " + uri);
}
if (Arrays.stream(hostPatterns).noneMatch(p -> p.matches(host))) {
throw new ExtismException(String.format("HTTP request to '%s' is not allowed", host));
throw new ExtismHttpException(String.format("HTTP request to '%s' is not allowed", host));
}

return clientAdapter.request(method, uri, headers, requestBody);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/extism/sdk/chicory/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@

public class HttpTest extends TestCase {

public void testInvalidHost() {
var httpConfig = HttpConfig.defaultConfig();
var logger = new SystemLogger();

var anyHost = new String[]{"*.httpbin.org"};
var hostEnv = new HostEnv(new Kernel(), Map.of(), anyHost, httpConfig, logger);

try {
byte[] response = hostEnv.http().request(
"GET",
URI.create("httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
fail("should throw an exception");
} catch (ExtismHttpException e) {
assertEquals("HTTP request host is invalid for URI: httpbin.org/headers", e.getMessage());
}
}

public void testNoAllowedHosts() {
noAllowedHosts(HttpConfig.defaultConfig());
noAllowedHosts(HttpConfig.urlConnectionConfig());
Expand Down

0 comments on commit e0576a6

Please sign in to comment.