Skip to content

Commit

Permalink
Painless: Apply true regex limit factor with FIND and MATCH operation
Browse files Browse the repository at this point in the history
  • Loading branch information
stu-elastic committed Feb 20, 2024
1 parent 5920c91 commit 9628718
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,23 @@ public void setRegexLimitFactor(int regexLimitFactor) {
}

/**
* What is the limit factor for regexes?
*/
public int getRegexLimitFactor() {
return regexLimitFactor;
* What is the effective limit factor for regexes?
*/
public int getAppliedRegexLimitFactor() {
return switch (regexesEnabled) {
case TRUE -> Augmentation.UNLIMITED_PATTERN_FACTOR;
case FALSE -> Augmentation.DISABLED_PATTERN_FACTOR;
case LIMITED -> regexLimitFactor;
};
}

/**
* Get compiler settings as a map. This is used to inject compiler settings into augmented methods with the {@code @inject_constant}
* annotation.
*/
public Map<String, Object> asMap() {
int regexLimitFactorToApply = this.regexLimitFactor;
if (regexesEnabled == RegexEnabled.TRUE) {
regexLimitFactorToApply = Augmentation.UNLIMITED_PATTERN_FACTOR;
} else if (regexesEnabled == RegexEnabled.FALSE) {
regexLimitFactorToApply = Augmentation.DISABLED_PATTERN_FACTOR;
}
Map<String, Object> map = new HashMap<>();
map.put("regex_limit_factor", regexLimitFactorToApply);
map.put("regex_limit_factor", getAppliedRegexLimitFactor());

// for testing only
map.put("testInject0", testInject0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private CompilerSettings buildCompilerSettings(Map<String, String> params) {
// Except regexes enabled - this is a node level setting and can't be changed in the request.
compilerSettings.setRegexesEnabled(defaultCompilerSettings.areRegexesEnabled());

compilerSettings.setRegexLimitFactor(defaultCompilerSettings.getRegexLimitFactor());
compilerSettings.setRegexLimitFactor(defaultCompilerSettings.getAppliedRegexLimitFactor());

Map<String, String> copy = new HashMap<>(params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ public void visitBinary(EBinary userBinaryNode, ScriptScope scriptScope) {
irBinaryMathNode.attachDecoration(new IRDOperation(operation));

if (operation == Operation.MATCH || operation == Operation.FIND) {
irBinaryMathNode.attachDecoration(new IRDRegexLimit(scriptScope.getCompilerSettings().getRegexLimitFactor()));
irBinaryMathNode.attachDecoration(new IRDRegexLimit(scriptScope.getCompilerSettings().getAppliedRegexLimitFactor()));
}

irBinaryMathNode.attachDecoration(new IRDBinaryType(binaryType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public void testMethodRegexInject_Ref_Matcher() {
assertTrue(cbe.getMessage().contains(regexCircuitMessage));
}

public void testInjectBinary() {
String script = "Pattern p = /.*a.*b.*c.*/; return 'abcxyz123abc' =~ p;";
Settings settings = Settings.builder()
.put(CompilerSettings.REGEX_LIMIT_FACTOR.getKey(), 1)
.put(CompilerSettings.REGEX_ENABLED.getKey(), "true")
.build();
scriptEngine = new PainlessScriptEngine(settings, scriptContexts());
assertEquals(Boolean.TRUE, exec(script));
}

public void testRegexInject_DefMethodRef_Matcher() {
String script = "boolean isMatch(Function func) { func.apply("
+ charSequence
Expand Down

0 comments on commit 9628718

Please sign in to comment.