Skip to content

Commit d73be6d

Browse files
authored
fix vulnerabiity in yaml constructor (#198)
Signed-off-by: Subhobrata Dey <[email protected]>
1 parent 9579e51 commit d73be6d

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/main/java/org/opensearch/securityanalytics/rules/backend/QueryBackend.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.opensearch.securityanalytics.rules.utils.Either;
3333
import org.apache.commons.lang3.tuple.Pair;
3434
import org.yaml.snakeyaml.Yaml;
35+
import org.yaml.snakeyaml.constructor.SafeConstructor;
3536

3637
import java.io.IOException;
3738
import java.io.InputStream;
@@ -70,7 +71,7 @@ public QueryBackend(String ruleCategory, boolean convertAndAsIn, boolean enableF
7071
assert is != null;
7172
String content = new String(is.readAllBytes(), Charset.defaultCharset());
7273

73-
Yaml yaml = new Yaml();
74+
Yaml yaml = new Yaml(new SafeConstructor());
7475
Map<String, Object> fieldMappingsObj = yaml.load(content);
7576
this.fieldMappings = (Map<String, String>) fieldMappingsObj.get("fieldmappings");
7677

src/main/java/org/opensearch/securityanalytics/rules/objects/SigmaRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.opensearch.securityanalytics.rules.exceptions.SigmaLogsourceError;
1313
import org.opensearch.securityanalytics.rules.exceptions.SigmaStatusError;
1414
import org.yaml.snakeyaml.Yaml;
15+
import org.yaml.snakeyaml.constructor.SafeConstructor;
1516

1617
import java.text.SimpleDateFormat;
1718
import java.util.ArrayList;
@@ -167,7 +168,7 @@ protected static SigmaRule fromDict(Map<String, Object> rule, boolean collectErr
167168
}
168169

169170
public static SigmaRule fromYaml(String rule, boolean collectErrors) throws SigmaError {
170-
Yaml yaml = new Yaml();
171+
Yaml yaml = new Yaml(new SafeConstructor());
171172
Map<String, Object> ruleMap = yaml.load(rule);
172173
return fromDict(ruleMap, collectErrors);
173174
}

src/test/java/org/opensearch/securityanalytics/rules/objects/SigmaDetectionsTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.opensearch.securityanalytics.rules.utils.Either;
2222
import org.opensearch.test.OpenSearchTestCase;
2323
import org.yaml.snakeyaml.Yaml;
24+
import org.yaml.snakeyaml.constructor.SafeConstructor;
2425

2526
import java.util.Collections;
2627
import java.util.List;
@@ -29,7 +30,7 @@
2930
public class SigmaDetectionsTests extends OpenSearchTestCase {
3031

3132
public void testSigmaDetectionsFromDict() throws SigmaError{
32-
Yaml yaml = new Yaml();
33+
Yaml yaml = new Yaml(new SafeConstructor());
3334
Map<String, Object> detectionsMap = yaml.load(
3435
" selection:\n" +
3536
" EventID: 16\n" +
@@ -61,7 +62,7 @@ public void testSigmaDetectionsFromDict() throws SigmaError{
6162
}
6263

6364
public void testSigmaDetectionsFromDictNoDetections() {
64-
Yaml yaml = new Yaml();
65+
Yaml yaml = new Yaml(new SafeConstructor());
6566
Map<String, Object> detectionsMap = yaml.load(
6667
" condition: selection");
6768
Exception exception = assertThrows(SigmaDetectionError.class, () -> {
@@ -75,7 +76,7 @@ public void testSigmaDetectionsFromDictNoDetections() {
7576
}
7677

7778
public void testSigmaDetectionsFromDictNoCondition() {
78-
Yaml yaml = new Yaml();
79+
Yaml yaml = new Yaml(new SafeConstructor());
7980
Map<String, Object> detectionsMap = yaml.load(
8081
" selection:\n" +
8182
" EventID: 16\n" +
@@ -93,7 +94,7 @@ public void testSigmaDetectionsFromDictNoCondition() {
9394
}
9495

9596
public void testDetectionItemAllModifiedKeyPlainValuesPostProcess() throws SigmaError{
96-
Yaml yaml = new Yaml();
97+
Yaml yaml = new Yaml(new SafeConstructor());
9798
Map<String, Object> detectionsMap = yaml.load(
9899
" selection:\n" +
99100
" field|all: [\"val1\", \"val2\", 123]\n" +
@@ -111,7 +112,7 @@ public void testDetectionItemAllModifiedKeyPlainValuesPostProcess() throws Sigma
111112
}
112113

113114
public void testDetectionItemAllModifiedUnboundPlainValuesPostProcess() throws SigmaError {
114-
Yaml yaml = new Yaml();
115+
Yaml yaml = new Yaml(new SafeConstructor());
115116
Map<String, Object> detectionsMap = yaml.load(
116117
" selection:\n" +
117118
" \"|all\": [\"val1\", \"val2\", 123]\n" +
@@ -129,7 +130,7 @@ public void testDetectionItemAllModifiedUnboundPlainValuesPostProcess() throws S
129130
}
130131

131132
public void testDetectionItemAllModifiedKeySpecialValuesPostProcess() throws SigmaError {
132-
Yaml yaml = new Yaml();
133+
Yaml yaml = new Yaml(new SafeConstructor());
133134
Map<String, Object> detectionsMap = yaml.load(
134135
" selection:\n" +
135136
" field|all: [\"val1*\", \"val2\", 123]\n" +

0 commit comments

Comments
 (0)