Skip to content

Commit b599ec4

Browse files
authored
Merge pull request #41 from securenative/dev
Support pii data remove from config
2 parents 55716a9 + 4379e99 commit b599ec4

File tree

8 files changed

+250
-76
lines changed

8 files changed

+250
-76
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>com.securenative.java</groupId>
77
<artifactId>securenative-java</artifactId>
88
<packaging>jar</packaging>
9-
<version>0.5.6</version>
9+
<version>0.5.7</version>
1010
<url>https://github.com/securenative/securenative-java</url>
1111

1212
<name>${project.groupId}:${project.artifactId}:${project.version}</name>

src/main/java/com/securenative/config/ConfigurationManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ private static SecureNativeOptions getOptions(Properties properties) {
105105
.withDisable(Utils.parseBooleanOrDefault(getPropertyOrEnvOrDefault(properties, "SECURENATIVE_DISABLE", defaultOptions.getDisabled()), defaultOptions.getDisabled()))
106106
.withLogLevel(getPropertyOrEnvOrDefault(properties, "SECURENATIVE_LOG_LEVEL", defaultOptions.getLogLevel()))
107107
.withFailoverStrategy(FailoverStrategy.fromString(Objects.requireNonNull(getPropertyOrEnvOrDefault(properties, "SECURENATIVE_FAILOVER_STRATEGY", defaultOptions.getFailoverStrategy())), defaultOptions.getFailoverStrategy()))
108-
.withProxyHeaders(getPropertyListOrEnvOrDefault(properties, "SECURENATIVE_PROXY_HEADERS", defaultOptions.getProxyHeaders()));
108+
.withProxyHeaders(getPropertyListOrEnvOrDefault(properties, "SECURENATIVE_PROXY_HEADERS", defaultOptions.getProxyHeaders()))
109+
.withPiiHeaders(getPropertyListOrEnvOrDefault(properties, "SECURENATIVE_PII_HEADERS", defaultOptions.getPiiHeaders()))
110+
.withPiiRegexPattern(getPropertyOrEnvOrDefault(properties, "SECURENATIVE_PII_REGEX_PATTERN", defaultOptions.getPiiRegexPattern()));
109111
return builder.build();
110112
}
111113
}

src/main/java/com/securenative/config/SecureNativeConfigurationBuilder.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public class SecureNativeConfigurationBuilder {
5555
*/
5656
private ArrayList<String> proxyHeaders;
5757

58+
/**
59+
* Pii Headers
60+
*/
61+
private ArrayList<String> piiHeaders;
62+
63+
/**
64+
* Pii Regex Pattern
65+
*/
66+
private String piiRegexPattern;
67+
5868
private SecureNativeConfigurationBuilder() {
5969
}
6070

@@ -69,7 +79,9 @@ public static SecureNativeConfigurationBuilder defaultConfigBuilder() {
6979
.withDisable(false)
7080
.withLogLevel("fatal")
7181
.withFailoverStrategy(FailoverStrategy.FAIL_OPEN)
72-
.withProxyHeaders(new ArrayList<>());
82+
.withProxyHeaders(new ArrayList<>())
83+
.withPiiHeaders(new ArrayList<>())
84+
.withPiiRegexPattern(null);
7385
}
7486

7587
public SecureNativeConfigurationBuilder withApiKey(String apiKey) {
@@ -122,7 +134,17 @@ public SecureNativeConfigurationBuilder withProxyHeaders(ArrayList<String> proxy
122134
return this;
123135
}
124136

137+
public SecureNativeConfigurationBuilder withPiiHeaders(ArrayList<String> piiHeaders) {
138+
this.piiHeaders = piiHeaders;
139+
return this;
140+
}
141+
142+
public SecureNativeConfigurationBuilder withPiiRegexPattern(String piiRegexPattern) {
143+
this.piiRegexPattern = piiRegexPattern;
144+
return this;
145+
}
146+
125147
public SecureNativeOptions build() {
126-
return new SecureNativeOptions(apiKey, apiUrl, interval, maxEvents, timeout, autoSend, disable, logLevel, failoverStrategy, proxyHeaders);
148+
return new SecureNativeOptions(apiKey, apiUrl, interval, maxEvents, timeout, autoSend, disable, logLevel, failoverStrategy, proxyHeaders, piiHeaders, piiRegexPattern);
127149
}
128150
}

src/main/java/com/securenative/config/SecureNativeOptions.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,17 @@ public class SecureNativeOptions {
5555
*/
5656
private final ArrayList<String> proxyHeaders;
5757

58-
public SecureNativeOptions(String apiKey, String apiUrl, int interval, int maxEvents, int timeout, boolean autoSend, boolean disable, String logLevel, FailoverStrategy failoverStrategy, ArrayList<String> proxyHeaders) {
58+
/**
59+
* Pii Headers
60+
*/
61+
private final ArrayList<String> piiHeaders;
62+
63+
/**
64+
* Pii Regex Pattern
65+
*/
66+
private final String piiRegexPattern;
67+
68+
public SecureNativeOptions(String apiKey, String apiUrl, int interval, int maxEvents, int timeout, boolean autoSend, boolean disable, String logLevel, FailoverStrategy failoverStrategy, ArrayList<String> proxyHeaders, ArrayList<String> piiHeaders, String piiRegexPattern) {
5969
this.apiKey = apiKey;
6070
this.apiUrl = apiUrl;
6171
this.interval = interval;
@@ -66,6 +76,8 @@ public SecureNativeOptions(String apiKey, String apiUrl, int interval, int maxEv
6676
this.logLevel = logLevel;
6777
this.failoverStrategy = failoverStrategy;
6878
this.proxyHeaders = proxyHeaders;
79+
this.piiHeaders = piiHeaders;
80+
this.piiRegexPattern = piiRegexPattern;
6981
}
7082

7183
public String getApiKey() {
@@ -107,4 +119,12 @@ public FailoverStrategy getFailoverStrategy() {
107119
public ArrayList<String> getProxyHeaders() {
108120
return proxyHeaders;
109121
}
122+
123+
public ArrayList<String> getPiiHeaders() {
124+
return piiHeaders;
125+
}
126+
127+
public String getPiiRegexPattern() {
128+
return piiRegexPattern;
129+
}
110130
}

src/main/java/com/securenative/context/SecureNativeContextBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static SecureNativeContextBuilder defaultContextBuilder() {
5454
}
5555

5656
public static SecureNativeContextBuilder fromHttpServletRequest(HttpServletRequest request, SecureNativeOptions options) {
57-
Map<String, String> headers = RequestUtils.getHeadersFromRequest(request);
57+
Map<String, String> headers = RequestUtils.getHeadersFromRequest(request, options);
5858

5959
String clientToken = RequestUtils.getCookieValueFromRequest(request, RequestUtils.SECURENATIVE_COOKIE);
6060
if (Utils.isNullOrEmpty(clientToken)) {

src/main/java/com/securenative/utils/RequestUtils.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@
55
import javax.servlet.http.Cookie;
66
import javax.servlet.http.HttpServletRequest;
77
import java.util.*;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
810

911
public class RequestUtils {
1012
public final static String SECURENATIVE_COOKIE = "_sn";
1113
public final static String SECURENATIVE_HEADER = "x-securenative";
1214
private final static List<String> ipHeaders = Arrays.asList("x-forwarded-for", "x-client-ip", "x-real-ip", "x-forwarded", "x-cluster-client-ip", "forwarded-for", "forwarded", "via");
15+
private final static List<String> piiHeaders = Arrays.asList("authorization", "access_token", "apikey", "password", "passwd", "secret", "api_key");
1316

14-
public static Map<String, String> getHeadersFromRequest(HttpServletRequest request) {
17+
public static Map<String, String> getHeadersFromRequest(HttpServletRequest request, SecureNativeOptions options) {
1518
Map<String, String> headersMap = new HashMap<>();
16-
for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements(); ) {
17-
String headerName = headerNames.nextElement();
18-
String headerValue = request.getHeader(headerName);
19-
headersMap.put(headerName, headerValue);
19+
if (options != null && options.getPiiHeaders().size() > 0) {
20+
for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements(); ) {
21+
String headerName = headerNames.nextElement();
22+
if (!options.getPiiHeaders().contains(headerName.toLowerCase()) && !options.getPiiHeaders().contains(headerName.toUpperCase())) {
23+
String headerValue = request.getHeader(headerName);
24+
headersMap.put(headerName, headerValue);
25+
}
26+
}
27+
} else if (options != null && options.getPiiRegexPattern() != null) {
28+
for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements(); ) {
29+
String headerName = headerNames.nextElement();
30+
Pattern pattern = Pattern.compile(options.getPiiRegexPattern(), Pattern.CASE_INSENSITIVE);
31+
Matcher matcher = pattern.matcher(headerName);
32+
if (!matcher.find()) {
33+
String headerValue = request.getHeader(headerName);
34+
headersMap.put(headerName, headerValue);
35+
}
36+
}
37+
} else {
38+
for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements(); ) {
39+
String headerName = headerNames.nextElement();
40+
if (!piiHeaders.contains(headerName.toLowerCase()) && !piiHeaders.contains(headerName.toUpperCase())) {
41+
String headerValue = request.getHeader(headerName);
42+
headersMap.put(headerName, headerValue);
43+
}
44+
}
2045
}
46+
2147
return headersMap;
2248
}
2349

@@ -38,7 +64,7 @@ public static String getCookieValueFromRequest(HttpServletRequest request, Strin
3864
}
3965

4066
public static String getClientIpFromRequest(HttpServletRequest request, Map<String, String> headers, SecureNativeOptions options) {
41-
if (options.getProxyHeaders().size() > 0) {
67+
if (options != null && options.getProxyHeaders().size() > 0) {
4268
for (String header : options.getProxyHeaders()) {
4369
if (headers.containsKey(header)) {
4470
String headerValue = headers.get(header);

src/test/java/com/securenative/config/ConfigurationManagerTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void ParseConfigFileCorrectlyTest() throws SecureNativeConfigException {
4242
"SECURENATIVE_DISABLE=false",
4343
"SECURENATIVE_LOG_LEVEL=fatal",
4444
"SECURENATIVE_FAILOVER_STRATEGY=fail-closed",
45-
"SECURENATIVE_PROXY_HEADERS=CF-Connecting-IP,Some-Random-Ip");
45+
"SECURENATIVE_PROXY_HEADERS=CF-Connecting-IP,Some-Random-Ip",
46+
"SECURENATIVE_PII_HEADERS=authentication,apiKey",
47+
"SECURENATIVE_PII_REGEX_PATTERN=/http_auth_/i");
4648

4749
InputStream inputStream = new ByteArrayInputStream(config.getBytes());
4850

@@ -63,6 +65,8 @@ public void ParseConfigFileCorrectlyTest() throws SecureNativeConfigException {
6365
assertThat(options.getMaxEvents()).isEqualTo(100);
6466
assertThat(options.getTimeout()).isEqualTo(1500);
6567
assertThat(options.getProxyHeaders().size() == 0);
68+
assertThat(options.getPiiHeaders().size() == 0);
69+
assertThat(options.getPiiRegexPattern()).isEqualTo("/http_auth_/i");
6670

6771
// restore resource stream
6872
ConfigurationManager.setResourceStream(new ResourceStreamImpl());
@@ -163,6 +167,8 @@ public void loadDefaultConfigTest() throws SecureNativeConfigException {
163167
assertThat(options.getLogLevel()).isEqualTo("fatal");
164168
assertThat(options.getFailoverStrategy()).isEqualTo(FailoverStrategy.FAIL_OPEN);
165169
assertThat(options.getProxyHeaders().size() == 0);
170+
assertThat(options.getPiiHeaders().size() == 0);
171+
assertThat(options.getPiiRegexPattern()).isEqualTo(null);
166172

167173
ConfigurationManager.setResourceStream(new ResourceStreamImpl());
168174
}

0 commit comments

Comments
 (0)