Skip to content
Draft

Dlp #1909

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
bcc7dcb
add dlp
russelmrcl Jun 23, 2025
497a43c
add comments
russelmrcl Jun 23, 2025
f0119d3
wip
russelmrcl Jun 23, 2025
5f84eb6
refactor code
russelmrcl Jun 23, 2025
e2536fe
wip
russelmrcl Jun 23, 2025
6ebb9c4
wip
russelmrcl Jun 23, 2025
9d2e722
wip
russelmrcl Jun 23, 2025
699b226
docs: minor
predic8 Jun 23, 2025
7ffee70
wip
russelmrcl Jun 23, 2025
559a62b
wip
russelmrcl Jun 23, 2025
de3ed95
wip
russelmrcl Jun 23, 2025
4c3ff8f
wip: dlp
russelmrcl Jun 26, 2025
8cfb161
wip: dlp
russelmrcl Jun 26, 2025
b17b14a
wip: dlp
russelmrcl Jun 26, 2025
099ec70
add test
russelmrcl Jun 26, 2025
02d75ec
wip
russelmrcl Jun 30, 2025
2be464b
add strategy pattern
russelmrcl Jun 30, 2025
450204b
wip test
russelmrcl Jun 30, 2025
75b56d9
wip
russelmrcl Jun 30, 2025
8adedb9
wip
russelmrcl Jun 30, 2025
e7cc895
refactor code
russelmrcl Jun 30, 2025
0f673bc
add tests
russelmrcl Jun 30, 2025
6a0bed1
wip
russelmrcl Jun 30, 2025
469256e
resolve conversations
russelmrcl Jun 30, 2025
53bc2f8
convert to json parse
russelmrcl Jun 30, 2025
7d04071
Merge branch 'master' into dlp
christiangoerdes Jun 30, 2025
ff15443
add path
russelmrcl Jun 30, 2025
ea1e179
wip
russelmrcl Jun 30, 2025
dce7806
add mask
russelmrcl Jun 30, 2025
e2eaa48
wip
russelmrcl Jul 7, 2025
13711b1
wip
russelmrcl Jul 17, 2025
7aa7e95
wip
russelmrcl Jul 17, 2025
60a13df
fix
russelmrcl Jul 17, 2025
0bf1268
refactor
russelmrcl Jul 17, 2025
21c3489
wip
russelmrcl Jul 17, 2025
20ed318
refactor code
russelmrcl Jul 21, 2025
c20c70d
improve log
russelmrcl Jul 21, 2025
528cc14
add docs
russelmrcl Jul 21, 2025
8d75f7c
edit docs
russelmrcl Jul 21, 2025
c3ec071
wip
russelmrcl Aug 1, 2025
e9a6286
refactor code
russelmrcl Aug 1, 2025
8b1a278
refactor code
russelmrcl Aug 1, 2025
77022dd
refactor code
russelmrcl Aug 26, 2025
5008ee7
Merge branch 'master' into dlp
russelmrcl Sep 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.predic8.membrane.annot.MCAttribute;

public abstract class Action implements DLPAction {

private String field;

public String getField() {
return field;
}

@MCAttribute
public void setField(String field) {
this.field = field;
}
Comment on lines +13 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for the field parameter.

The setter should validate that the field is not empty and is a valid JSONPath expression to prevent runtime errors.

 @MCAttribute
 public void setField(String field) {
+    if (field != null && field.trim().isEmpty()) {
+        throw new IllegalArgumentException("field cannot be empty");
+    }
     this.field = field;
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In core/src/main/java/com/predic8/membrane/core/interceptor/dlp/Action.java
around lines 15 to 18, the setField method lacks validation for the input
parameter. Add validation to ensure the field parameter is not null or empty and
verify it is a valid JSONPath expression before assigning it to the field
variable. If validation fails, throw an appropriate exception to prevent runtime
errors.


@Override
public abstract String apply(DLPContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.predic8.membrane.core.interceptor.dlp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.*;

public class CsvFieldConfiguration implements FieldConfiguration {

private static final Logger log = LoggerFactory.getLogger(CsvFieldConfiguration.class);

private final Map<String, String> riskLevels = new HashMap<>();
private final Map<String, String> categories = new HashMap<>();

@Override
public Map<String, String> getFields(String fileName) {
try (InputStream inputStream = getResourceAsStream(fileName)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
boolean isHeader = true;

while ((line = reader.readLine()) != null) {
if (isHeader) {
isHeader = false;
continue;
}

line = line.trim();
if (line.isEmpty() || line.startsWith("#")) continue;

String[] parts = line.split(",", -1);
if (parts.length < 3) {
log.warn("Skipping invalid line (less than 3 columns): {}", line);
continue;
}

String field = parts[0].trim().toLowerCase(Locale.ROOT);
String category = parts[1].trim();
String riskLevel = parts[2].trim().toLowerCase(Locale.ROOT);

if (!isValidRiskLevel(riskLevel)) {
log.warn("Invalid risk level '{}' for field '{}'. Defaulting to 'unclassified'", riskLevel, field);
riskLevel = "unclassified";
}

riskLevels.put(field, riskLevel);
categories.put(field, category);
}

} catch (IOException e) {
throw new RuntimeException("Error reading CSV field configuration: " + fileName, e);
}

return riskLevels;
}

public Map<String, String> getFieldCategories() {
return categories;
}

private InputStream getResourceAsStream(String fileName) {
InputStream is = CsvFieldConfiguration.class.getClassLoader().getResourceAsStream(fileName);
if (is == null) {
String msg = "Could not find CSV config file: " + fileName;
log.error(msg);
throw new IllegalArgumentException(msg);
}
return is;
}

private boolean isValidRiskLevel(String level) {
return switch (level) {
case "high", "medium", "low", "unclassified" -> true;
default -> false;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.predic8.membrane.core.interceptor.dlp;

public interface DLPAction {
String apply(DLPContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.predic8.membrane.core.http.Message;

import java.io.InputStream;
import java.util.*;

public class DLPAnalyzer {

private static final JsonFactory JSON_FACTORY = new JsonFactoryBuilder()
.configure(JsonReadFeature.ALLOW_TRAILING_COMMA, true)
.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS, false)
.streamReadConstraints(StreamReadConstraints.builder()
.maxNestingDepth(64)
.maxStringLength(16 * 1024)
.build())
.build();

private static final ObjectMapper MAPPER = new ObjectMapper(JSON_FACTORY);

private final Map<String, String> riskDict;
private final Map<String, String> categoryMap;

public DLPAnalyzer(Map<String, String> rawRiskMap, Map<String, String> categoryMap) {
this.riskDict = normalizeRiskLevels(rawRiskMap);
this.categoryMap = categoryMap;
}

private Map<String, String> normalizeRiskLevels(Map<String, String> raw) {
Map<String, String> result = new HashMap<>();
raw.forEach((key, value) -> result.put(key, normalizeLevel(value)));
return result;
}

private String normalizeLevel(String level) {
switch (level.toLowerCase()) {
case "high":
case "medium":
case "low":
return level.toLowerCase();
default:
return "unknown";
}
}

public RiskReport analyze(Message msg) {
try (InputStream is = msg.getBodyAsStreamDecoded()) {
JsonNode root = MAPPER.readTree(is);
RiskReport report = new RiskReport();
traverse(root, new ArrayDeque<>(), report);
return report;
} catch (Exception e) {
throw new RuntimeException("Failed to analyze message", e);
}
}

private void traverse(JsonNode node, Deque<String> path, RiskReport report) {
if (node.isObject()) {
node.fieldNames().forEachRemaining(fieldName -> {
path.addLast(fieldName);
traverse(node.get(fieldName), path, report);
path.removeLast();
});
} else if (node.isArray()) {
for (JsonNode child : node) {
traverse(child, path, report);
}
} else {
String fullPath = String.join(".", path);
String lastSegment = path.peekLast() != null ? path.peekLast() : "";

String riskLevel = classify(fullPath, lastSegment);
String category = categoryMap.getOrDefault(fullPath, categoryMap.getOrDefault(lastSegment, "Unknown"));

report.recordField(fullPath, riskLevel, category);
}
}

private String classify(String fullPath, String simpleName) {
return Optional.ofNullable(riskDict.get(fullPath))
.or(() -> Optional.ofNullable(riskDict.get(simpleName)))
.orElse("unknown");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.predic8.membrane.core.interceptor.dlp;

public record DLPContext(String body, RiskReport riskReport) {

public boolean hasRiskReport() {
return riskReport != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Message;
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.Outcome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;

/**
* @description <p>Interceptor for Data Loss Prevention (DLP) in JSON-based request and response bodies.</p>
*
* <p>This plugin supports three main actions on sensitive fields:</p>
* <ul>
* <li> Mask ? Masks sensitive fields, leaving a configurable number of trailing characters visible.</li>
* <li> Filter? Removes specified fields entirely from the payload.</li>
* <li> Report ? Logs the risk level and category of specified fields (if configured).</li>
* </ul>
*
* @topic 3. Security and Validation
*/
@MCElement(name = "dlp")
public class DLPInterceptor extends AbstractInterceptor {

private static final Logger log = LoggerFactory.getLogger(DLPInterceptor.class);

private DLPAnalyzer dlpAnalyzer;
private String fieldsConfig;

private List<Mask> masks = new ArrayList<>();
private List<Filter> filters = new ArrayList<>();
private List<Report> reports = new ArrayList<>();

private final List<DLPAction> actions = new ArrayList<>();

@Override
public void init() {
if (fieldsConfig != null) {
CsvFieldConfiguration csv = new CsvFieldConfiguration();
Map<String, String> levels = csv.getFields(fieldsConfig);
Map<String, String> cats = csv.getFieldCategories();
this.dlpAnalyzer = new DLPAnalyzer(levels, cats);
} else {
this.dlpAnalyzer = new DLPAnalyzer(Map.of(), Map.of());
}
actions.addAll(masks);
actions.addAll(filters);
actions.addAll(reports);
super.init();
}

@Override
public Outcome handleRequest(Exchange exc) {
return handleInternal(exc.getRequest());
}

private Outcome handleInternal(Message msg) {
try {
if (actions.isEmpty()) {
log.info("No actions configured. Skipping.");
return CONTINUE;
}
String body = msg.getBodyAsStringDecoded();
RiskReport report = dlpAnalyzer.analyze(msg);
log.info("{}", report.getFormattedSummaryLog());

for (DLPAction action : actions) {
body = action.apply(new DLPContext(body, report));
}

msg.setBodyContent(body.getBytes(StandardCharsets.UTF_8));
return CONTINUE;

} catch (Exception e) {
log.error("{}", e);
return Outcome.ABORT;
}
}

public String getFieldsConfig() {
return fieldsConfig;
}

/**
* @description Optionally, fields can be classified based on a CSV configuration. This file maps JSON paths or field names to risk levels and categories.
* @example fieldsConfig="dlp-fields.csv"
*/
@MCAttribute
public void setFieldsConfig(String fieldsConfig) {
this.fieldsConfig = fieldsConfig;
}

public List<Mask> getMasks() {
return masks;
}

@MCChildElement
public DLPInterceptor setMasks(List<Mask> masks) {
this.masks = masks;
return this;
}

public List<Filter> getFilters() {
return filters;
}

@MCChildElement(order = 1)
public DLPInterceptor setFilters(List<Filter> filters) {
this.filters = filters;
return this;
}

public List<Report> getReports() {
return reports;
}

@MCChildElement(order = 2)
public DLPInterceptor setReports(List<Report> reports) {
this.reports = reports;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.predic8.membrane.core.interceptor.dlp;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;

@MCElement(name = "field")
public class Field {

private String jsonpath;

public String getJsonpath() {
return jsonpath;
}

@MCAttribute
public void setJsonpath(String jsonpath) {
this.jsonpath = jsonpath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.predic8.membrane.core.interceptor.dlp;

import java.util.Map;

public interface FieldConfiguration {
Map<String, String> getFields(String fileName);
}
Loading