1
1
/*
2
- * Copyright (C) 2021 Thomas Akehurst
2
+ * Copyright (C) 2021-2023 Thomas Akehurst
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import static java .util .stream .Collectors .toList ;
21
21
22
22
import com .fasterxml .jackson .annotation .JsonCreator ;
23
+ import com .github .tomakehurst .wiremock .common .NetworkAddressRules ;
23
24
import com .github .tomakehurst .wiremock .common .Notifier ;
24
25
import com .github .tomakehurst .wiremock .core .Admin ;
25
26
import com .github .tomakehurst .wiremock .extension .Parameters ;
28
29
import com .github .tomakehurst .wiremock .extension .responsetemplating .TemplateEngine ;
29
30
import com .github .tomakehurst .wiremock .http .HttpHeader ;
30
31
import com .github .tomakehurst .wiremock .stubbing .ServeEvent ;
32
+ import java .net .InetAddress ;
33
+ import java .net .URI ;
34
+ import java .net .UnknownHostException ;
31
35
import java .util .*;
32
36
import java .util .concurrent .Executors ;
33
37
import java .util .concurrent .ScheduledExecutorService ;
@@ -50,25 +54,37 @@ public class Webhooks extends PostServeAction {
50
54
private final CloseableHttpClient httpClient ;
51
55
private final List <WebhookTransformer > transformers ;
52
56
private final TemplateEngine templateEngine ;
57
+ private final NetworkAddressRules targetAddressRules ;
53
58
54
59
private Webhooks (
55
60
ScheduledExecutorService scheduler ,
56
61
CloseableHttpClient httpClient ,
57
- List <WebhookTransformer > transformers ) {
62
+ List <WebhookTransformer > transformers ,
63
+ NetworkAddressRules targetAddressRules ) {
58
64
this .scheduler = scheduler ;
59
65
this .httpClient = httpClient ;
60
66
this .transformers = transformers ;
61
67
62
68
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 );
63
79
}
64
80
65
81
@ JsonCreator
66
82
public Webhooks () {
67
- this (Executors . newScheduledThreadPool ( 10 ), createHttpClient (), new ArrayList <>() );
83
+ this (NetworkAddressRules . ALLOW_ALL );
68
84
}
69
85
70
86
public Webhooks (WebhookTransformer ... transformers ) {
71
- this (Executors . newScheduledThreadPool ( 10 ), createHttpClient (), Arrays .asList (transformers ));
87
+ this (Arrays .asList (transformers ), NetworkAddressRules . ALLOW_ALL );
72
88
}
73
89
74
90
private static CloseableHttpClient createHttpClient () {
@@ -109,6 +125,10 @@ public void doAction(
109
125
definition = transformer .transform (serveEvent , definition );
110
126
}
111
127
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
+ }
112
132
request = buildRequest (definition );
113
133
} catch (Exception e ) {
114
134
notifier ().error ("Exception thrown while configuring webhook" , e );
@@ -195,6 +215,19 @@ private static ClassicHttpRequest buildRequest(WebhookDefinition definition) {
195
215
return requestBuilder .build ();
196
216
}
197
217
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
+
198
231
public static WebhookDefinition webhook () {
199
232
return new WebhookDefinition ();
200
233
}
0 commit comments