-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[Improvement] Use safe constructor with snake yaml #15758
Conversation
...i/src/main/java/org/apache/dolphinscheduler/plugin/task/api/k8s/AbstractK8sTaskExecutor.java
Fixed
Show fixed
Hide fixed
@@ -60,7 +60,7 @@ | |||
} | |||
|
|||
protected @NonNull LoopTaskYamlDefinition parseYamlConfigFile(@NonNull String yamlConfigFile) throws IOException { | |||
Yaml yaml = new Yaml(new Constructor(LoopTaskYamlDefinition.class)); | |||
Yaml yaml = new Yaml(new SafeConstructor()); |
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fix this? @EricGao888
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SbloodyS In package org.yaml.snakeyaml.constructor;
, we could see public class Constructor extends SafeConstructor
. Therefore I think there is no need to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure about it....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure about it....
I just email an4er and request PR review from him / her to see if there is some extra stuff we need to handle this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptipon()));
to avoid this warning.
https://bitbucket.org/snakeyaml/snakeyaml/wiki/Documentation#markdown-header-tutorial
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all I think the code Yaml yaml = new Yaml(new Constructor(LoopTaskYamlDefinition.class));
can't be changed to Yaml yaml = new Yaml(new SafeConstructor());
.
I would suggest to introduce the class ClassFilterConstructor.java, and then to replace the code in AbstractK8sTaskExecutor
to be changed to
import java.util.List;
Yaml yaml = new Yaml(new ClassFilterConstructor(new Class[] {
List.class
})).
This is for visibility, because here we expect the user's input to be of type list, you can also remove this map as it is already defined in the SafeConstructor
Yaml yaml = new Yaml(new ClassFilterConstructor(new Class[] {
}));
I didn't test the security of the parseYamlConfigFile function, so if you want to change it, you can also change to
Yaml yaml = new Yaml(new ClassFilterConstructor(new Class[] {
LoopTaskYamlDefinition.class
}));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fix this? @EricGao888
@SbloodyS Seems I misunderstood your point. Yes, we could fix this warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all I think the code
Yaml yaml = new Yaml(new Constructor(LoopTaskYamlDefinition.class));
can't be changed toYaml yaml = new Yaml(new SafeConstructor());
. I would suggest to introduce the class ClassFilterConstructor.java, and then to replace the code inAbstractK8sTaskExecutor
to be changed toimport java.util.List; Yaml yaml = new Yaml(new ClassFilterConstructor(new Class[] { List.class })).
This is for visibility, because here we expect the user's input to be of type list, you can also remove this map as it is already defined in the SafeConstructor
Yaml yaml = new Yaml(new ClassFilterConstructor(new Class[] { }));
I didn't test the security of the parseYamlConfigFile function, so if you want to change it, you can also change to
Yaml yaml = new Yaml(new ClassFilterConstructor(new Class[] { LoopTaskYamlDefinition.class }));
@an5er Thanks for your suggestions. It helps a lot.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #15758 +/- ##
============================================
- Coverage 40.46% 40.45% -0.01%
- Complexity 5195 5196 +1
============================================
Files 1378 1379 +1
Lines 46084 46093 +9
Branches 4923 4924 +1
============================================
+ Hits 18646 18648 +2
- Misses 25512 25518 +6
- Partials 1926 1927 +1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, please ensure that the introduction of security mechanisms does not affect the functionality of the business
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a case where unit test is helpful and should be mandatory, to verify that the nested types can be parsed without error. Can you add some?
try (FileReader fileReader = new FileReader(yamlConfigFile)) { | ||
return yaml.load(fileReader); | ||
return new Yaml(new ClassFilterConstructor(new Class[]{LoopTaskYamlDefinition.class})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kezhenxu94 In ClassFilterConstructor
, it overrides the method getClassForName
from its super class Constructor
which is called in the method getClassForNode
. The strange thing is that if you put a check point at cl = this.getClassForName(name);
, run HttpTaskDefinitionParserTest.parseYamlConfigFile
and you will find that cl = this.getClassForName(name);
only gets called once, which means the fields and the fields of the fields in LoopTaskYamlDefinition
such as LoopTaskServiceYamlDefinition
, LoopTaskQueryStateYamlDefinition
, etc. are not checked iteratively. I think whether to add these nested types or not does not make any difference and the nested types still bypass the check in this solution.
protected Class<?> getClassForNode(Node node) {
Class<? extends Object> classForTag = (Class)this.typeTags.get(node.getTag());
if (classForTag == null) {
String name = node.getTag().getClassName();
Class cl;
try {
cl = this.getClassForName(name);
} catch (ClassNotFoundException var6) {
throw new YAMLException("Class not found: " + name);
}
this.typeTags.put(node.getTag(), cl);
return cl;
} else {
return classForTag;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the method getClassForNode
must be called per yaml section node, it is impossible (to me) to get all nested classes types when starting to parse the root node, will check.
this.yaml = new Yaml(new ClassFilterConstructor(new Class[]{ | ||
List.class | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also check this, nested non-primitive types should be added too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure |
Quality Gate failedFailed conditions |
I am new to DS and have a question here. |
I think it's normal for the server to execute certain commands to the worker. However, security issues such as those in the |
No, it runs on the machine where the dolphinscheduler is deployed, not in the k8s cluster. |
@EricGao888 will you update this pr? |
Yes, but later this week. Quite busy recently. 😢 |
fd621d6
to
7478774
Compare
Tests added. |
Quality Gate failedFailed conditions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 PTAL @SbloodyS @kezhenxu94
Purpose of the pull request
Brief change log
Verify this pull request
This pull request is code cleanup without any test coverage.
(or)
This pull request is already covered by existing tests, such as (please describe tests).
(or)
This change added tests and can be verified as follows:
(or)
If your pull request contain incompatible change, you should also add it to
docs/docs/en/guide/upgrede/incompatible.md