11/*
2- * Copyright (C) 2021 Thomas Akehurst
2+ * Copyright (C) 2021-2023 Thomas Akehurst
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2020import static java .util .stream .Collectors .toList ;
2121
2222import com .fasterxml .jackson .annotation .JsonCreator ;
23+ import com .github .tomakehurst .wiremock .common .NetworkAddressRules ;
2324import com .github .tomakehurst .wiremock .common .Notifier ;
2425import com .github .tomakehurst .wiremock .core .Admin ;
2526import com .github .tomakehurst .wiremock .extension .Parameters ;
2829import com .github .tomakehurst .wiremock .extension .responsetemplating .TemplateEngine ;
2930import com .github .tomakehurst .wiremock .http .HttpHeader ;
3031import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
32+ import java .net .InetAddress ;
33+ import java .net .URI ;
34+ import java .net .UnknownHostException ;
3135import java .util .*;
3236import java .util .concurrent .Executors ;
3337import java .util .concurrent .ScheduledExecutorService ;
@@ -50,25 +54,37 @@ public class Webhooks extends PostServeAction {
5054 private final CloseableHttpClient httpClient ;
5155 private final List <WebhookTransformer > transformers ;
5256 private final TemplateEngine templateEngine ;
57+ private final NetworkAddressRules targetAddressRules ;
5358
5459 private Webhooks (
5560 ScheduledExecutorService scheduler ,
5661 CloseableHttpClient httpClient ,
57- List <WebhookTransformer > transformers ) {
62+ List <WebhookTransformer > transformers ,
63+ NetworkAddressRules targetAddressRules ) {
5864 this .scheduler = scheduler ;
5965 this .httpClient = httpClient ;
6066 this .transformers = transformers ;
6167
6268 this .templateEngine = new TemplateEngine (Collections .emptyMap (), null , Collections .emptySet ());
69+ this .targetAddressRules = targetAddressRules ;
70+ }
71+
72+ private Webhooks (List <WebhookTransformer > transformers , NetworkAddressRules targetAddressRules ) {
73+ this (
74+ Executors .newScheduledThreadPool (10 ), createHttpClient (), transformers , targetAddressRules );
75+ }
76+
77+ public Webhooks (NetworkAddressRules targetAddressRules ) {
78+ this (new ArrayList <>(), targetAddressRules );
6379 }
6480
6581 @ JsonCreator
6682 public Webhooks () {
67- this (Executors . newScheduledThreadPool ( 10 ), createHttpClient (), new ArrayList <>() );
83+ this (NetworkAddressRules . ALLOW_ALL );
6884 }
6985
7086 public Webhooks (WebhookTransformer ... transformers ) {
71- this (Executors . newScheduledThreadPool ( 10 ), createHttpClient (), Arrays .asList (transformers ));
87+ this (Arrays .asList (transformers ), NetworkAddressRules . ALLOW_ALL );
7288 }
7389
7490 private static CloseableHttpClient createHttpClient () {
@@ -109,6 +125,10 @@ public void doAction(
109125 definition = transformer .transform (serveEvent , definition );
110126 }
111127 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+ }
112132 request = buildRequest (definition );
113133 } catch (Exception e ) {
114134 notifier ().error ("Exception thrown while configuring webhook" , e );
@@ -195,6 +215,19 @@ private static ClassicHttpRequest buildRequest(WebhookDefinition definition) {
195215 return requestBuilder .build ();
196216 }
197217
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+
198231 public static WebhookDefinition webhook () {
199232 return new WebhookDefinition ();
200233 }
0 commit comments