Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -384,17 +384,19 @@ protected void processEngine(FlowData flowData, DeviceDataHash deviceData) {
List<String> keys = evidenceKeys;
for (Map.Entry<String, Object> evidenceItem :
flowData.getEvidence().asKeyMap().entrySet()) {
boolean containsKey = false;
String matchedKey = null;
for (String key : keys) {
if (key.equalsIgnoreCase(evidenceItem.getKey())) {
containsKey = true;
matchedKey = key;
break;
}
}
if (containsKey) {
if (matchedKey != null) {
// The native code matches the prefix with case, so it
// is given the key as the native code spells it.
relevantEvidence
.addFromBytes(
Swig.asBytes(evidenceItem.getKey()),
Swig.asBytes(matchedKey),
Swig.asBytes(evidenceItem.getValue().toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public void Process_Hash_CaseInsensitiveKeys() throws Exception {
ProcessTests.caseInsensitiveEvidenceKeys(getWrapper(), new DataValidatorHash(getWrapper().getEngine()));
}

@Test
public void Process_Hash_CaseInsensitivePrefix() throws Exception {
ProcessTests.caseInsensitiveEvidencePrefix(getWrapper(), new DataValidatorHash(getWrapper().getEngine()));
}

@Test
public void Process_Hash_ProfileOverride() throws Exception {
ProcessTests.profileOverride(getWrapper(), new DataValidatorHash(getWrapper().getEngine()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public static void caseInsensitiveEvidenceKeys(
}
}

/**
* Evidence keys ignore case, the prefix as well as the field, so a
* User-Agent given with its prefix in capitals is used.
*/
public static void caseInsensitiveEvidencePrefix(
Wrapper wrapper,
DataValidator validator) throws Exception {
try (FlowData data = wrapper.getPipeline().createFlowData()) {
data
.addEvidence("HEADER.User-Agent", Constants.ChromeUserAgent)
.process();
validator.validateData(data, true);
}
}

public static void profileOverride(
Wrapper wrapper,
DataValidator validator) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ public class EvidenceKeyFilterWhitelist implements EvidenceKeyFilter {

/**
* Construct a new instance using the list of evidence keys provided.
* Keys are compared ignoring case, as Evidence keys are.
* By default, all keys will have the same order of precedence.
* @param whitelist evidence keys to add to the whitelist
*/
public EvidenceKeyFilterWhitelist(List<String> whitelist) {
this.whitelist = new TreeMap<>();
addValues(whitelist);
this.comparator = null;
this(whitelist, String.CASE_INSENSITIVE_ORDER);
}

/**
Expand All @@ -66,12 +65,11 @@ public EvidenceKeyFilterWhitelist(
* precedence provided.
* The order of precedence of each key is given by the value of the
* key/value pair.
* Keys are compared ignoring case, as Evidence keys are.
* @param whitelist evidence keys to add to the whitelist
*/
public EvidenceKeyFilterWhitelist(Map<String, Integer> whitelist) {
this.whitelist = new TreeMap<>();
addValues(whitelist);
this.comparator = null;
this(whitelist, String.CASE_INSENSITIVE_ORDER);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -76,15 +77,15 @@ public void EvidenceKeyFilterAggregator_Include_Aggregated() {
}

/**
* Check that aggregated case-sensitive and case-insensitive filters
* still work as expected.
* Check that an aggregated case-insensitive filter, and one given a
* comparator that compares with case, still work as expected.
*/
@Test
public void EvidenceKeyFilterAggregator_CaseSensitivity() {
EvidenceKeyFilter filter1 = new EvidenceKeyFilterWhitelist(
Arrays.asList("key1"), String.CASE_INSENSITIVE_ORDER);
EvidenceKeyFilter filter2 = new EvidenceKeyFilterWhitelist(
Arrays.asList("key2"));
Arrays.asList("key2"), Comparator.naturalOrder());

EvidenceKeyFilterAggregator filter = new EvidenceKeyFilterAggregator();
filter.addFilter(filter1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
Expand All @@ -43,6 +44,27 @@ public void EvidenceKeyFilterWhitelist_Include_CheckKeys() {
assertFalse(filter.include("key3"));
}

/**
* Evidence keys ignore case, so a whitelist made without a comparator
* ignores case too, whether it is made from a list or from a map.
*/
@Test
public void EvidenceKeyFilterWhitelist_Default_IgnoresCase() {
filter = new EvidenceKeyFilterWhitelist(
Arrays.asList("header.user-agent"));

assertTrue(filter.include("Header.User-Agent"));
assertTrue(filter.include("HEADER.USER-AGENT"));
assertFalse(filter.include("header.user-agent2"));

Map<String, Integer> keys = new HashMap<>();
keys.put("query.Session-Id", 2);
filter = new EvidenceKeyFilterWhitelist(keys);

assertTrue(filter.include("query.session-id"));
assertEquals(Integer.valueOf(2), filter.order("QUERY.SESSION-ID"));
}

@Test
public void EvidenceKeyFilterWhitelist_List() {
filter = new EvidenceKeyFilterWhitelist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,12 @@ private Map<String, String> getParameters(FlowData data) throws UnsupportedEncod
.asKeyMap();

for(Map.Entry<String, Object> entry : queryEvidence.entrySet()){
if(entry.getKey().startsWith(EVIDENCE_QUERY_PREFIX) &&
EXCLUDED_PARAMETERS.contains(entry.getKey()) == false){
// Evidence keys ignore case, so the prefix and the excluded
// keys are matched whatever the case the key was added in.
if(entry.getKey().regionMatches(true, 0,
EVIDENCE_QUERY_PREFIX, 0, EVIDENCE_QUERY_PREFIX.length()) &&
EXCLUDED_PARAMETERS.contains(
entry.getKey().toLowerCase(Locale.ROOT)) == false){
String key = entry.getKey().substring(entry.getKey().indexOf(EVIDENCE_SEPERATOR) + 1);
key = URLEncoder.encode(key, "UTF-8");
String value = URLEncoder.encode(entry.getValue().toString(), "UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,50 @@ public void Url_CarriesNoQueryEvidence() throws Exception {
"The parameters object lost the query evidence: " + parameters);
}

/**
* Evidence keys ignore case, so query Evidence whose prefix is not in
* lower case reaches the parameters object, and the session id and
* sequence stay out of it whatever their case.
*/
@Test
public void Parameters_QueryPrefixInAnyCase() throws Exception {
JavaScriptBuilderElement jsElement =
new JavaScriptBuilderElementBuilder(loggerFactory)
.setEndpoint("/json")
.setHost("example.com")
.setProtocol("https")
.setMinify(false)
.build();
String javaScript;
try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
.addFlowElement(new SequenceElementBuilder(loggerFactory).build())
.addFlowElement(new JsonBuilderElementBuilder(loggerFactory).build())
.addFlowElement(jsElement)
.build();
FlowData flowData = pipeline.createFlowData()) {
flowData.addEvidence("Query.Extra", "one");
flowData.addEvidence("QUERY.Other", "two");
flowData.addEvidence("Query.Session-Id", "a-session");
flowData.process();
javaScript = flowData.getFromElement(jsElement).getJavaScript();
}
String parameters = null;
for (String line : javaScript.split("\\r?\\n")) {
if (line.toLowerCase().contains("parameters =")
&& line.contains("{")) {
parameters = line;
break;
}
}
assertNotNull(parameters, "No parameters object in the script.");
assertTrue(parameters.contains("\"Extra\":\"one\""),
"Query.Extra is not in the parameters: " + parameters);
assertTrue(parameters.contains("\"Other\":\"two\""),
"QUERY.Other is not in the parameters: " + parameters);
assertFalse(parameters.toLowerCase().contains("session-id"),
"The session id is in the parameters: " + parameters);
}

/**
* A context root still sits between the host and the endpoint, and the
* URL still carries no query string.
Expand Down
Loading