Skip to content

Commit 3919a66

Browse files
committed
FINERACT-1872: Template request fix for 1.8.x
1 parent 7a37a47 commit 3919a66

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.apache.fineract.infrastructure.core.config;
2121

22+
import java.util.List;
2223
import lombok.Getter;
2324
import lombok.Setter;
2425
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -36,6 +37,8 @@ public class FineractProperties {
3637

3738
private FineractCorrelationProperties correlation;
3839

40+
private FineractTemplateProperties template;
41+
3942
@Getter
4043
@Setter
4144
public static class FineractTenantProperties {
@@ -72,4 +75,12 @@ public static class FineractCorrelationProperties {
7275
private boolean enabled;
7376
private String headerName;
7477
}
78+
79+
@Getter
80+
@Setter
81+
public static class FineractTemplateProperties {
82+
83+
private boolean regexWhitelistEnabled;
84+
private List<String> regexWhitelist;
85+
}
7586
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.template.exception;
20+
21+
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformException;
22+
23+
public class TemplateForbiddenException extends AbstractPlatformException {
24+
25+
public TemplateForbiddenException(final String url) {
26+
super("error.msg.template.url.forbidden", "Template with url " + url + " not allowed");
27+
}
28+
}

fineract-provider/src/main/java/org/apache/fineract/template/service/TemplateMergeService.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,24 @@
3838
import java.security.NoSuchAlgorithmException;
3939
import java.util.HashMap;
4040
import java.util.Map;
41+
import java.util.regex.Matcher;
42+
import java.util.regex.Pattern;
43+
import lombok.RequiredArgsConstructor;
44+
import lombok.extern.slf4j.Slf4j;
45+
import org.apache.fineract.infrastructure.core.config.FineractProperties;
4146
import org.apache.fineract.template.domain.Template;
4247
import org.apache.fineract.template.domain.TemplateFunctions;
43-
import org.slf4j.Logger;
44-
import org.slf4j.LoggerFactory;
48+
import org.apache.fineract.template.exception.TemplateForbiddenException;
4549
import org.springframework.security.core.context.SecurityContextHolder;
4650
import org.springframework.stereotype.Service;
4751

52+
@Slf4j
53+
@RequiredArgsConstructor
4854
@Service
4955
public class TemplateMergeService {
5056

51-
private static final Logger LOG = LoggerFactory.getLogger(TemplateMergeService.class);
57+
private final FineractProperties fineractProperties;
5258

53-
// private final FromJsonHelper fromApiJsonHelper;
5459
private Map<String, Object> scopes;
5560
private String authToken;
5661

@@ -92,7 +97,7 @@ private Map<String, Object> getCompiledMapFromMappers(final Map<String, String>
9297
try {
9398
this.scopes.put(entry.getKey(), getMapFromUrl(url));
9499
} catch (final IOException e) {
95-
LOG.error("getCompiledMapFromMappers() failed", e);
100+
log.error("getCompiledMapFromMappers() failed", e);
96101
}
97102
}
98103
}
@@ -114,6 +119,26 @@ private Map<String, Object> getMapFromUrl(final String url) throws IOException {
114119
}
115120

116121
private HttpURLConnection getConnection(final String url) {
122+
if (fineractProperties.getTemplate() != null && fineractProperties.getTemplate().isRegexWhitelistEnabled()) {
123+
boolean whitelisted = false;
124+
125+
if (fineractProperties.getTemplate().getRegexWhitelist() != null
126+
&& !fineractProperties.getTemplate().getRegexWhitelist().isEmpty()) {
127+
for (String urlPattern : fineractProperties.getTemplate().getRegexWhitelist()) {
128+
Pattern pattern = Pattern.compile(urlPattern);
129+
Matcher matcher = pattern.matcher(url);
130+
if (matcher.matches()) {
131+
whitelisted = true;
132+
break;
133+
}
134+
}
135+
}
136+
137+
if (!whitelisted) {
138+
throw new TemplateForbiddenException(url);
139+
}
140+
}
141+
117142
if (this.authToken == null) {
118143
final String name = SecurityContextHolder.getContext().getAuthentication().getName();
119144
final String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
@@ -138,7 +163,7 @@ protected PasswordAuthentication getPasswordAuthentication() {
138163
connection.setDoInput(true);
139164

140165
} catch (IOException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
141-
LOG.error("getConnection() failed, return null", e);
166+
log.error("getConnection() failed, return null", e);
142167
}
143168

144169
return connection;
@@ -158,13 +183,13 @@ private static String getStringFromInputStream(final InputStream is) {
158183
}
159184

160185
} catch (final IOException e) {
161-
LOG.error("getStringFromInputStream() failed", e);
186+
log.error("getStringFromInputStream() failed", e);
162187
} finally {
163188
if (br != null) {
164189
try {
165190
br.close();
166191
} catch (final IOException e) {
167-
LOG.error("Problem occurred in getStringFromInputStream function", e);
192+
log.error("Problem occurred in getStringFromInputStream function", e);
168193
}
169194
}
170195
}

fineract-provider/src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fineract.content.regex-whitelist=${FINERACT_CONTENT_REGEX_WHITELIST:.*\\.pdf$,.*
4848
fineract.content.mime-whitelist-enabled=${FINERACT_CONTENT_MIME_WHITELIST_ENABLED:true}
4949
fineract.content.mime-whitelist=${FINERACT_CONTENT_MIME_WHITELIST:application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,image/jpeg,image/png}
5050

51+
fineract.template.regex-whitelist-enabled=${FINERACT_TEMPLATE_REGEX_WHITELIST_ENABLED:true}
52+
fineract.template.regex-whitelist=${FINERACT_TEMPLATE_REGEX_WHITELIST:}
53+
5154
# Logging pattern for the console
5255
logging.pattern.console=${CONSOLE_LOG_PATTERN:%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(%replace([%X{correlationId}]){'\\[\\]', ''}) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}
5356

fineract-provider/src/test/java/org/apache/fineract/template/service/TemplateServiceStepDefinitions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
import org.apache.commons.io.IOUtils;
3535
import org.apache.fineract.template.domain.Template;
3636
import org.apache.fineract.template.domain.TemplateMapper;
37+
import org.springframework.beans.factory.annotation.Autowired;
3738

3839
public class TemplateServiceStepDefinitions implements En {
3940

40-
private TemplateMergeService tms = new TemplateMergeService();
41-
41+
@Autowired
42+
private TemplateMergeService tms;
4243
private String template;
4344

4445
private Map<String, Object> data;

0 commit comments

Comments
 (0)