Skip to content

Commit e3aa0f4

Browse files
committed
disallow new in if condition attribute in config files
Signed-off-by: ceki <ceki@qos.ch>
1 parent d933cf5 commit e3aa0f4

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<x>
4+
<stack name="BEGIN"/>
5+
<if condition='new Integer(1).equals(1)'>
6+
<then>
7+
<stack name="a"/>
8+
</then>
9+
<else>
10+
<stack name="b"/>
11+
</else>
12+
</if>
13+
<stack name="END"/>
14+
</x>

logback-core-blackbox/src/test/java/ch/qos/logback/core/blackbox/joran/conditional/IfThenElseTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.junit.jupiter.api.BeforeEach;
5353
import org.junit.jupiter.api.Test;
5454

55-
import java.io.IOException;
5655
import java.util.Arrays;
5756
import java.util.HashMap;
5857
import java.util.Stack;
@@ -130,6 +129,16 @@ public void whenContextPropertyIsSet_IfThenBranchIsEvaluated() throws JoranExcep
130129
verifyConfig(new String[] { "BEGIN", "a", "END" });
131130
}
132131

132+
@Test
133+
public void ifWithNew() throws JoranException {
134+
context.putProperty(ki1, val1);
135+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifNew.xml");
136+
checker.containsMatch(Status.ERROR, IfModelHandler.NEW_OPERATOR_DISALLOWED_MSG);
137+
checker.containsMatch(Status.ERROR, IfModelHandler.NEW_OPERATOR_DISALLOWED_SEE);
138+
verifyConfig(new String[] { "BEGIN", "END" });
139+
}
140+
141+
133142
@Test
134143
public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
135144
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty.xml");

logback-core/src/main/java/ch/qos/logback/core/model/processor/conditional/IfModelHandler.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Logback: the reliable, generic, fast and flexible logging framework.
3-
* Copyright (C) 1999-2022, QOS.ch. All rights reserved.
3+
* Copyright (C) 1999-2025, QOS.ch. All rights reserved.
44
*
55
* This program and the accompanying materials are dual-licensed under
66
* either the terms of the Eclipse Public License v1.0 as published by
@@ -33,6 +33,9 @@ public class IfModelHandler extends ModelHandlerBase {
3333
public static final String MISSING_JANINO_MSG = "Could not find Janino library on the class path. Skipping conditional processing.";
3434
public static final String MISSING_JANINO_SEE = "See also " + CoreConstants.CODES_URL + "#ifJanino";
3535

36+
public static final String NEW_OPERATOR_DISALLOWED_MSG = "The 'condition' attribute may not contain the 'new' operator.";
37+
public static final String NEW_OPERATOR_DISALLOWED_SEE = "See also " + CoreConstants.CODES_URL + "#conditionNew";
38+
3639
enum Branch {IF_BRANCH, ELSE_BRANCH; }
3740

3841
IfModel ifModel = null;
@@ -75,6 +78,13 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
7578
return;
7679
}
7780

81+
// do not allow 'new' operator
82+
if(hasNew(conditionStr)) {
83+
addError(NEW_OPERATOR_DISALLOWED_MSG);
84+
addError(NEW_OPERATOR_DISALLOWED_SEE);
85+
return;
86+
}
87+
7888
try {
7989
PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(mic);
8090
pesb.setContext(context);
@@ -96,8 +106,12 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
96106
}
97107
}
98108
}
99-
100-
109+
110+
private boolean hasNew(String conditionStr) {
111+
return conditionStr.contains("new ");
112+
}
113+
114+
101115
@Override
102116
public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
103117

0 commit comments

Comments
 (0)