Skip to content

Commit 92d7793

Browse files
tomakehurstMahoney
authored andcommitted
Applied DNS resolver enforcement to webhooks extension
1 parent 027ddaf commit 92d7793

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

wiremock-webhooks-extension/src/main/java/org/wiremock/webhooks/Webhooks.java

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
2222
import com.fasterxml.jackson.annotation.JsonCreator;
2323
import com.github.tomakehurst.wiremock.common.NetworkAddressRules;
2424
import com.github.tomakehurst.wiremock.common.Notifier;
25+
import com.github.tomakehurst.wiremock.common.ProhibitedNetworkAddressException;
2526
import com.github.tomakehurst.wiremock.core.Admin;
2627
import com.github.tomakehurst.wiremock.extension.Parameters;
2728
import com.github.tomakehurst.wiremock.extension.PostServeAction;
2829
import com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel;
2930
import com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine;
3031
import com.github.tomakehurst.wiremock.http.HttpHeader;
32+
import com.github.tomakehurst.wiremock.http.NetworkAddressRulesAdheringDnsResolver;
3133
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
32-
import java.net.InetAddress;
33-
import java.net.URI;
34-
import java.net.UnknownHostException;
3534
import java.util.*;
3635
import java.util.concurrent.Executors;
3736
import java.util.concurrent.ScheduledExecutorService;
@@ -54,24 +53,20 @@ public class Webhooks extends PostServeAction {
5453
private final CloseableHttpClient httpClient;
5554
private final List<WebhookTransformer> transformers;
5655
private final TemplateEngine templateEngine;
57-
private final NetworkAddressRules targetAddressRules;
5856

5957
private Webhooks(
6058
ScheduledExecutorService scheduler,
6159
CloseableHttpClient httpClient,
62-
List<WebhookTransformer> transformers,
63-
NetworkAddressRules targetAddressRules) {
60+
List<WebhookTransformer> transformers) {
6461
this.scheduler = scheduler;
6562
this.httpClient = httpClient;
6663
this.transformers = transformers;
6764

6865
this.templateEngine = TemplateEngine.defaultTemplateEngine();
69-
this.targetAddressRules = targetAddressRules;
7066
}
7167

7268
private Webhooks(List<WebhookTransformer> transformers, NetworkAddressRules targetAddressRules) {
73-
this(
74-
Executors.newScheduledThreadPool(10), createHttpClient(), transformers, targetAddressRules);
69+
this(Executors.newScheduledThreadPool(10), createHttpClient(targetAddressRules), transformers);
7570
}
7671

7772
public Webhooks(NetworkAddressRules targetAddressRules) {
@@ -87,7 +82,7 @@ public Webhooks(WebhookTransformer... transformers) {
8782
this(Arrays.asList(transformers), NetworkAddressRules.ALLOW_ALL);
8883
}
8984

90-
private static CloseableHttpClient createHttpClient() {
85+
private static CloseableHttpClient createHttpClient(NetworkAddressRules targetAddressRules) {
9186
return HttpClientBuilder.create()
9287
.disableAuthCaching()
9388
.disableAutomaticRetries()
@@ -96,6 +91,7 @@ private static CloseableHttpClient createHttpClient() {
9691
.disableContentCompression()
9792
.setConnectionManager(
9893
PoolingHttpClientConnectionManagerBuilder.create()
94+
.setDnsResolver(new NetworkAddressRulesAdheringDnsResolver(targetAddressRules))
9995
.setDefaultSocketConfig(
10096
SocketConfig.custom().setSoTimeout(Timeout.ofMilliseconds(30000)).build())
10197
.setMaxConnPerRoute(1000)
@@ -125,10 +121,6 @@ public void doAction(
125121
definition = transformer.transform(serveEvent, definition);
126122
}
127123
definition = applyTemplating(definition, serveEvent);
128-
if (targetAddressProhibited(definition.getUrl())) {
129-
notifier().error("The target webhook address is denied in WireMock's configuration.");
130-
return;
131-
}
132124
request = buildRequest(definition);
133125
} catch (Exception e) {
134126
notifier().error("Exception thrown while configuring webhook", e);
@@ -146,13 +138,14 @@ public void doAction(
146138
finalDefinition.getUrl(),
147139
response.getCode(),
148140
EntityUtils.toString(response.getEntity())));
141+
} catch (ProhibitedNetworkAddressException e) {
142+
notifier.error("The target webhook address is denied in WireMock's configuration.");
149143
} catch (Exception e) {
150-
notifier()
151-
.error(
152-
String.format(
153-
"Failed to fire webhook %s %s",
154-
finalDefinition.getMethod(), finalDefinition.getUrl()),
155-
e);
144+
notifier.error(
145+
String.format(
146+
"Failed to fire webhook %s %s",
147+
finalDefinition.getMethod(), finalDefinition.getUrl()),
148+
e);
156149
}
157150
},
158151
finalDefinition.getDelaySampleMillis(),
@@ -215,19 +208,6 @@ private static ClassicHttpRequest buildRequest(WebhookDefinition definition) {
215208
return requestBuilder.build();
216209
}
217210

218-
// TODO this is duplicated in com.github.tomakehurst.wiremock.http.ProxyResponseRenderer - should
219-
// it be on NetworkAddressRules ?
220-
private boolean targetAddressProhibited(String url) {
221-
String host = URI.create(url).getHost();
222-
try {
223-
final InetAddress[] resolvedAddresses = InetAddress.getAllByName(host);
224-
return !Arrays.stream(resolvedAddresses)
225-
.allMatch(address -> targetAddressRules.isAllowed(address.getHostAddress()));
226-
} catch (UnknownHostException e) {
227-
return true;
228-
}
229-
}
230-
231211
public static WebhookDefinition webhook() {
232212
return new WebhookDefinition();
233213
}

wiremock-webhooks-extension/src/test/java/functional/WebhooksAcceptanceTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,11 @@ public void doesNotFireAWebhookWhenRequestedForDeniedTarget() throws Exception {
387387
.map(message -> message.replace("\n", "\n>>> "))
388388
.collect(Collectors.joining("\n>>> ")));
389389

390-
await()
391-
.until(
392-
() -> testNotifier.getErrorMessages(),
393-
hasItem(
394-
containsString(
395-
"The target webhook address is denied in WireMock's configuration.")));
390+
List<String> errorMessages =
391+
await().until(() -> testNotifier.getErrorMessages(), hasSize(greaterThanOrEqualTo(1)));
392+
assertThat(
393+
errorMessages.get(0),
394+
is("The target webhook address is denied in WireMock's configuration."));
396395
}
397396

398397
private void waitForRequestToTargetServer() throws Exception {

0 commit comments

Comments
 (0)