Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix vulnerabiity in yaml constructor #198

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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