Skip to content

Commit a55e769

Browse files
authored
Fix redact processor arraycopy bug (#122640) (#122767)
1 parent 4f9c33f commit a55e769

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

docs/changelog/122640.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122640
2+
summary: Fix redact processor arraycopy bug
3+
area: Ingest Node
4+
type: bug
5+
issues: []

x-pack/plugin/redact/src/main/java/org/elasticsearch/xpack/redact/RedactProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,13 @@ public void extract(byte[] utf8Bytes, int offset, Region region) {
294294
*/
295295
String redactMatches(byte[] utf8Bytes, String redactStartToken, String redactEndToken) {
296296
var merged = mergeOverlappingReplacements(replacementPositions);
297-
int longestPatternName = merged.stream().mapToInt(r -> r.patternName.getBytes(StandardCharsets.UTF_8).length).max().getAsInt();
297+
int maxPatternNameLength = merged.stream()
298+
.mapToInt(r -> r.patternName.getBytes(StandardCharsets.UTF_8).length)
299+
.max()
300+
.getAsInt();
298301

299-
int maxPossibleLength = longestPatternName * merged.size() + utf8Bytes.length;
302+
int maxPossibleLength = (redactStartToken.length() + maxPatternNameLength + redactEndToken.length()) * merged.size()
303+
+ utf8Bytes.length;
300304
byte[] redact = new byte[maxPossibleLength];
301305

302306
int readOffset = 0;

x-pack/plugin/redact/src/test/java/org/elasticsearch/xpack/redact/RedactProcessorTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ public void testMatchRedact() throws Exception {
108108
var redacted = RedactProcessor.matchRedact(input, List.of(grok));
109109
assertEquals("<CREDIT_CARD> <CREDIT_CARD> <CREDIT_CARD> <CREDIT_CARD>", redacted);
110110
}
111+
{
112+
var config = new HashMap<String, Object>();
113+
config.put("field", "to_redact");
114+
config.put("patterns", List.of("%{NUMBER:NUMBER}"));
115+
config.put("pattern_definitions", Map.of("NUMBER", "\\d{4}"));
116+
var processor = new RedactProcessor.Factory(mockLicenseState(), MatcherWatchdog.noop()).create(null, "t", "d", config);
117+
var grok = processor.getGroks().get(0);
118+
119+
String input = "1001";
120+
var redacted = RedactProcessor.matchRedact(input, List.of(grok), "_prefix_", "_suffix_");
121+
assertEquals("_prefix_NUMBER_suffix_", redacted);
122+
}
111123
}
112124

113125
public void testMatchRedactMultipleGroks() throws Exception {

0 commit comments

Comments
 (0)