Skip to content

Commit

Permalink
fix vulnerabiity in yaml constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
  • Loading branch information
sbcd90 committed Dec 14, 2022
1 parent 9579e51 commit c75e1be
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.opensearch.securityanalytics.rules.utils.Either;
import org.apache.commons.lang3.tuple.Pair;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

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

Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> fieldMappingsObj = yaml.load(content);
this.fieldMappings = (Map<String, String>) fieldMappingsObj.get("fieldmappings");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.securityanalytics.rules.exceptions.SigmaLogsourceError;
import org.opensearch.securityanalytics.rules.exceptions.SigmaStatusError;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

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

public static SigmaRule fromYaml(String rule, boolean collectErrors) throws SigmaError {
Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> ruleMap = yaml.load(rule);
return fromDict(ruleMap, collectErrors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opensearch.securityanalytics.rules.utils.Either;
import org.opensearch.test.OpenSearchTestCase;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.util.Collections;
import java.util.List;
Expand All @@ -29,7 +30,7 @@
public class SigmaDetectionsTests extends OpenSearchTestCase {

public void testSigmaDetectionsFromDict() throws SigmaError{
Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> detectionsMap = yaml.load(
" selection:\n" +
" EventID: 16\n" +
Expand Down Expand Up @@ -61,7 +62,7 @@ public void testSigmaDetectionsFromDict() throws SigmaError{
}

public void testSigmaDetectionsFromDictNoDetections() {
Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> detectionsMap = yaml.load(
" condition: selection");
Exception exception = assertThrows(SigmaDetectionError.class, () -> {
Expand All @@ -75,7 +76,7 @@ public void testSigmaDetectionsFromDictNoDetections() {
}

public void testSigmaDetectionsFromDictNoCondition() {
Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> detectionsMap = yaml.load(
" selection:\n" +
" EventID: 16\n" +
Expand All @@ -93,7 +94,7 @@ public void testSigmaDetectionsFromDictNoCondition() {
}

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

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

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

0 comments on commit c75e1be

Please sign in to comment.