Skip to content

Commit

Permalink
Merge pull request #2543 from brendandburns/r0.15
Browse files Browse the repository at this point in the history
[Cherry-pick] Add a rule to block empty YAML constructors.
  • Loading branch information
k8s-ci-robot authored Jan 30, 2023
2 parents bd7ea09 + d4fe343 commit 8e53bda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,17 @@
<replacement>INVALID IMPORTS (GUAVA)</replacement>
</replaceRegex>
</format>
<!-- prevents empty SnakeYaml constructor -->
<format>
<includes>
<include>src/**/*.java</include>
</includes>
<replaceRegex>
<name>Forbids new Yaml()</name>
<searchRegex>^.*new Yaml\(\).*$</searchRegex>
<replacement>INVALID CONSTRUCTOR (SNAKEYAML)</replacement>
</replaceRegex>
</format>
</formats>
<java>
<removeUnusedImports /> <!-- self-explanatory -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public class FilePersister implements ConfigPersister {
File configFile;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void save(
// Note this is imperfect, should protect against other processes writing this file too...
synchronized (configFile) {
try (FileWriter fw = new FileWriter(configFile)) {
Yaml yaml = new Yaml();
Yaml yaml = new Yaml(new SafeConstructor());
yaml.dump(config, fw);
fw.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
import io.kubernetes.client.openapi.JSON;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public class Dynamics {

static final JSON internalJSONCodec = new JSON();
static final Yaml internalYamlCodec = new Yaml();
static final Yaml internalYamlCodec = new Yaml(new SafeConstructor());

public static DynamicKubernetesObject newFromJson(String jsonContent) {
return newFromJson(internalJSONCodec.getGson(), jsonContent);
Expand Down

0 comments on commit 8e53bda

Please sign in to comment.