-
-
Notifications
You must be signed in to change notification settings - Fork 678
feat: Support for adding taint analysis engine #1486
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
Open
ravisastryk
wants to merge
7
commits into
securego:master
Choose a base branch
from
ravisastryk:feature/taint-analysis-engine
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,270
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf684e2
feat: add taint analysis engine for data flow security
ravisastryk a603bc1
restructure with analyzer specific files
ravisastryk 451e764
remove redundant tests and update docs
ravisastryk 3725f9f
separate tests
ravisastryk ef93848
update additional sources and sinks
ravisastryk 78e704e
added more sources and sinks
ravisastryk bf5d054
address golint issue
ravisastryk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // (c) Copyright gosec's authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package analyzers | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestTaintAnalyzerConstructors tests that all taint analyzer constructors work. | ||
| func TestTaintAnalyzerConstructors(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| constructor AnalyzerBuilder | ||
| id string | ||
| description string | ||
| }{ | ||
| { | ||
| name: "SQLInjection", | ||
| constructor: newSQLInjectionAnalyzer, | ||
| id: "G701", | ||
| description: "SQL injection via taint analysis", | ||
| }, | ||
| { | ||
| name: "CommandInjection", | ||
| constructor: newCommandInjectionAnalyzer, | ||
| id: "G702", | ||
| description: "Command injection via taint analysis", | ||
| }, | ||
| { | ||
| name: "PathTraversal", | ||
| constructor: newPathTraversalAnalyzer, | ||
| id: "G703", | ||
| description: "Path traversal via taint analysis", | ||
| }, | ||
| { | ||
| name: "SSRF", | ||
| constructor: newSSRFAnalyzer, | ||
| id: "G704", | ||
| description: "SSRF via taint analysis", | ||
| }, | ||
| { | ||
| name: "XSS", | ||
| constructor: newXSSAnalyzer, | ||
| id: "G705", | ||
| description: "XSS via taint analysis", | ||
| }, | ||
| { | ||
| name: "LogInjection", | ||
| constructor: newLogInjectionAnalyzer, | ||
| id: "G706", | ||
| description: "Log injection via taint analysis", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| analyzer := tt.constructor(tt.id, tt.description) | ||
|
|
||
| if analyzer == nil { | ||
| t.Fatal("constructor returned nil") | ||
| } | ||
|
|
||
| if analyzer.Name != tt.id { | ||
| t.Errorf("analyzer Name = %s, want %s", analyzer.Name, tt.id) | ||
| } | ||
|
|
||
| if analyzer.Run == nil { | ||
| t.Error("analyzer Run function is nil") | ||
| } | ||
|
|
||
| if len(analyzer.Requires) == 0 { | ||
| t.Error("analyzer has no requirements") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDefaultAnalyzersIncludeTaint tests that default analyzers include taint rules. | ||
| func TestDefaultAnalyzersIncludeTaint(t *testing.T) { | ||
| expectedTaintIDs := []string{"G701", "G702", "G703", "G704", "G705", "G706"} | ||
|
|
||
| found := make(map[string]bool) | ||
| for _, def := range defaultAnalyzers { | ||
| found[def.ID] = true | ||
| } | ||
|
|
||
| for _, id := range expectedTaintIDs { | ||
| if !found[id] { | ||
| t.Errorf("default analyzers missing taint rule: %s", id) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestGenerateIncludesTaintAnalyzers tests that Generate includes taint analyzers. | ||
| func TestGenerateIncludesTaintAnalyzers(t *testing.T) { | ||
| analyzerList := Generate(false) | ||
|
|
||
| expectedTaintIDs := []string{"G701", "G702", "G703", "G704", "G705", "G706"} | ||
|
|
||
| for _, id := range expectedTaintIDs { | ||
| if _, ok := analyzerList.Analyzers[id]; !ok { | ||
| t.Errorf("generated analyzer list missing taint rule: %s", id) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestGenerateExcludeTaintAnalyzers tests that taint analyzers can be excluded. | ||
| func TestGenerateExcludeTaintAnalyzers(t *testing.T) { | ||
| filter := NewAnalyzerFilter(true, "G701", "G702") | ||
| analyzerList := Generate(false, filter) | ||
|
|
||
| if _, ok := analyzerList.Analyzers["G701"]; ok { | ||
| t.Error("G701 should be excluded but was found") | ||
| } | ||
|
|
||
| if _, ok := analyzerList.Analyzers["G702"]; ok { | ||
| t.Error("G702 should be excluded but was found") | ||
| } | ||
|
|
||
| // Other taint analyzers should still be present | ||
| if _, ok := analyzerList.Analyzers["G703"]; !ok { | ||
| t.Error("G703 should be present but was not found") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // (c) Copyright gosec's authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package analyzers | ||
|
|
||
| import ( | ||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/securego/gosec/v2/analyzers/taint" | ||
| ) | ||
|
|
||
| // newCommandInjectionAnalyzer creates an analyzer for detecting command injection vulnerabilities | ||
| // via taint analysis (G702) | ||
| func newCommandInjectionAnalyzer(id string, description string) *analysis.Analyzer { | ||
| config := taint.CommandInjection() | ||
| rule := taint.RuleInfo{ | ||
| ID: id, | ||
| Description: description, | ||
| Severity: "CRITICAL", | ||
| CWE: "CWE-78", | ||
| } | ||
| return taint.NewGosecAnalyzer(&rule, &config) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // (c) Copyright gosec's authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package analyzers | ||
|
|
||
| import ( | ||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/securego/gosec/v2/analyzers/taint" | ||
| ) | ||
|
|
||
| // newLogInjectionAnalyzer creates an analyzer for detecting log injection vulnerabilities | ||
| // via taint analysis (G706) | ||
| func newLogInjectionAnalyzer(id string, description string) *analysis.Analyzer { | ||
| config := taint.LogInjection() | ||
| rule := taint.RuleInfo{ | ||
| ID: id, | ||
| Description: description, | ||
| Severity: "LOW", | ||
| CWE: "CWE-117", | ||
| } | ||
| return taint.NewGosecAnalyzer(&rule, &config) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // (c) Copyright gosec's authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package analyzers | ||
|
|
||
| import ( | ||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/securego/gosec/v2/analyzers/taint" | ||
| ) | ||
|
|
||
| // newPathTraversalAnalyzer creates an analyzer for detecting path traversal vulnerabilities | ||
| // via taint analysis (G703) | ||
| func newPathTraversalAnalyzer(id string, description string) *analysis.Analyzer { | ||
| config := taint.PathTraversal() | ||
| rule := taint.RuleInfo{ | ||
| ID: id, | ||
| Description: description, | ||
| Severity: "HIGH", | ||
| CWE: "CWE-22", | ||
| } | ||
| return taint.NewGosecAnalyzer(&rule, &config) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In the developer section, it would be great to add some details which describes how to create a taint rule.