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

rules:feat - adding rule to spring framework rce #1053

Merged
merged 1 commit into from
Apr 4, 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
4 changes: 1 addition & 3 deletions internal/services/custom_rules/custom_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (
"regexp"
"testing"

"github.com/stretchr/testify/require"

"github.com/ZupIT/horusec-devkit/pkg/enums/confidence"
"github.com/ZupIT/horusec-devkit/pkg/enums/languages"
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
"github.com/ZupIT/horusec-engine/text"
"github.com/stretchr/testify/require"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -325,7 +324,6 @@ func TestGetRuleType(t *testing.T) {
}

func TestGetExpressions(t *testing.T) {

exprs := []string{"testOne", "testTwo"}
exprOne, _ := regexp.Compile(exprs[0])
exprTwo, _ := regexp.Compile(exprs[1])
Expand Down
1 change: 1 addition & 0 deletions internal/services/engines/java/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func Rules() []engine.Rule {
// NewMessageDigest(),
NewOverlyPermissiveFilePermission(),
NewCipherGetInstanceInsecure(),
NewVulnerableRemoteCodeExecutionSpringFramework(),

// Regular rules
NewHiddenElements(),
Expand Down
18 changes: 18 additions & 0 deletions internal/services/engines/java/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2634,3 +2634,21 @@ func NewUncheckedClassInstatiation() *text.Rule {
},
}
}

func NewVulnerableRemoteCodeExecutionSpringFramework() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-JAVA-152",
Name: "Spring Framework Remote Code Execution",
Description: "It has been identified that versions prior to < 5.3.18 or < 5.2.20 of the spring framework are vulnerable to remote code execution. Please upgrade to version >= 5.3.18 or >= 5.2.20. For more information checkout the CVE-2022-22965 (https://tanzu.vmware.com/security/cve-2022-22965) advisory.",
Severity: severities.Critical.ToString(),
Confidence: confidence.Medium.ToString(),
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`<dependency.*org="org\.springframework".*\s.*(5\.[0-3]\.(1[0-7]|[0-9]\.|[0-9]"))|([0-4]\.[0-9]+\.[0-9]+).*/>`),
regexp.MustCompile(`compile.*"org\.springframework:spring-context.*((5\.[0-3]\.(1[0-7]|[0-9]\.|[0-9]"))|([0-4]\.[0-9]+\.[0-9]+).*"\))`),
regexp.MustCompile(`<groupId>\s*org\.springframework\s*</groupId>\s*<artifactId>.*\s*spring-context.*\s*</artifactId>\s*(<version>\s*((5\.[0-3]\.(1[0-7]|[0-9]\.|[0-9]"))|([0-4]\.[0-9]+\.[0-9]+)).*)\s*</version>`),
},
}
}
66 changes: 66 additions & 0 deletions internal/services/engines/java/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,54 @@ func TestRulesVulnerableCode(t *testing.T) {
},
},
},
{
Name: "HS-JAVA-152",
Rule: NewVulnerableRemoteCodeExecutionSpringFramework(),
Src: Sample1IvyVulnerableHSJAVA152,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152.1", ".test")),
Findings: []engine.Finding{
{
CodeSample: "<dependency org=\"org.springframework\"",
SourceLocation: engine.Location{
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152.1", ".test")),
Line: 2,
Column: 0,
},
},
},
},
{
Name: "HS-JAVA-152",
Rule: NewVulnerableRemoteCodeExecutionSpringFramework(),
Src: Sample2GradleVulnerableHSJAVA152,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152.2", ".test")),
Findings: []engine.Finding{
{
CodeSample: "compile(\"org.springframework:spring-context:5.3.17.RELEASE\")",
SourceLocation: engine.Location{
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152.2", ".test")),
Line: 3,
Column: 4,
},
},
},
},
{
Name: "HS-JAVA-152",
Rule: NewVulnerableRemoteCodeExecutionSpringFramework(),
Src: Sample3MavenVulnerableHSJAVA152,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152.3", ".test")),
Findings: []engine.Finding{
{
CodeSample: "<groupId>org.springframework</groupId>",
SourceLocation: engine.Location{
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152.3", ".test")),
Line: 4,
Column: 8,
},
},
},
},
}

testutil.TestVulnerableCode(t, testcases)
Expand Down Expand Up @@ -1007,6 +1055,24 @@ func TestRulesSafeCode(t *testing.T) {
Src: Sample5MavenSafeHSJAVA151,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-151", ".test")),
},
{
Name: "HS-JAVA-152",
Rule: NewVulnerableRemoteCodeExecutionSpringFramework(),
Src: Sample1IvySafeHSJAVA152,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152", ".test")),
},
{
Name: "HS-JAVA-152",
Rule: NewVulnerableRemoteCodeExecutionSpringFramework(),
Src: Sample2GradleSafeHSJAVA152,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152", ".test")),
},
{
Name: "HS-JAVA-152",
Rule: NewVulnerableRemoteCodeExecutionSpringFramework(),
Src: Sample3MavenSafeHSJAVA152,
Filename: filepath.Join(tempDir, fmt.Sprintf("%s%s", "HS-JAVA-152", ".test")),
},
}
testutil.TestSafeCode(t, testcases)
}
46 changes: 46 additions & 0 deletions internal/services/engines/java/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1444,5 +1444,51 @@ test {
</dependency>
</dependencies>
</project>
`

Sample1IvyVulnerableHSJAVA152 = `
<dependency org="org.springframework"
name="spring-core" rev="5.3.17.RELEASE" conf="compile->runtime"/>
`

Sample1IvySafeHSJAVA152 = `
<dependency org="org.springframework"
name="spring-core" rev="5.3.18.RELEASE" conf="compile->runtime"/>
`

Sample2GradleVulnerableHSJAVA152 = `
dependencies {
compile("org.springframework:spring-context:5.3.17.RELEASE")
testCompile("org.springframework:spring-test:5.3.17.RELEASE")
}
`

Sample2GradleSafeHSJAVA152 = `
dependencies {
compile("org.springframework:spring-context:5.3.18.RELEASE")
testCompile("org.springframework:spring-test:5.3.18.RELEASE")
}
`

Sample3MavenVulnerableHSJAVA152 = `
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.17.RELEASE</version>
<scope>runtime</scope>
</dependency>
</dependencies>
`

Sample3MavenSafeHSJAVA152 = `
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18.RELEASE</version>
<scope>runtime</scope>
</dependency>
</dependencies>
`
)
2 changes: 1 addition & 1 deletion internal/services/engines/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestGetRules(t *testing.T) {
{
engine: "Java",
manager: java.NewRules(),
expectedTotalRules: 182,
expectedTotalRules: 183,
},
{
engine: "Dart",
Expand Down