-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: enable gosec #35873
Merged
Merged
*: enable gosec #35873
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2445ac8
*: enable gosec
hawkingrei 47be8e6
pdutil: enable flaky test
hawkingrei 723a882
pdutil: enable flaky test
hawkingrei fe57c82
Merge branch 'master' into enable_gosec
hawkingrei af88baf
Merge branch 'master' into enable_gosec
hawkingrei 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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,16 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "gosec", | ||
srcs = ["analysis.go"], | ||
importpath = "github.com/pingcap/tidb/build/linter/gosec", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//build/linter/util", | ||
"@com_github_golangci_golangci_lint//pkg/result", | ||
"@com_github_golangci_gosec//:gosec", | ||
"@com_github_golangci_gosec//rules", | ||
"@org_golang_x_tools//go/analysis", | ||
"@org_golang_x_tools//go/loader", | ||
], | ||
) |
This file contains 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,110 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// 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 gosec | ||
|
||
import ( | ||
"fmt" | ||
"go/token" | ||
"go/types" | ||
"io/ioutil" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/golangci/golangci-lint/pkg/result" | ||
"github.com/golangci/gosec" | ||
"github.com/golangci/gosec/rules" | ||
"github.com/pingcap/tidb/build/linter/util" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/loader" | ||
) | ||
|
||
// Name is the name of the analyzer. | ||
const Name = "gosec" | ||
|
||
// Analyzer is the analyzer struct of gosec. | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: Name, | ||
Doc: "Inspects source code for security problems", | ||
Run: run, | ||
} | ||
|
||
func init() { | ||
util.SkipAnalyzer(Analyzer) | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
gasConfig := gosec.NewConfig() | ||
enabledRules := rules.Generate(func(id string) bool { | ||
if id == "G104" || id == "G103" || id == "G101" || id == "G201" { | ||
return true | ||
} | ||
return false | ||
}) | ||
logger := log.New(ioutil.Discard, "", 0) | ||
analyzer := gosec.NewAnalyzer(gasConfig, logger) | ||
analyzer.LoadRules(enabledRules.Builders()) | ||
|
||
var createdPkgs []*loader.PackageInfo | ||
createdPkgs = append(createdPkgs, util.MakeFakeLoaderPackageInfo(pass)) | ||
allPkgs := make(map[*types.Package]*loader.PackageInfo) | ||
for _, pkg := range createdPkgs { | ||
pkg := pkg | ||
allPkgs[pkg.Pkg] = pkg | ||
} | ||
prog := &loader.Program{ | ||
Fset: pass.Fset, | ||
Imported: nil, // not used without .Created in any linter | ||
Created: createdPkgs, // all initial packages | ||
AllPackages: allPkgs, // all initial packages and their depndencies | ||
} | ||
|
||
analyzer.ProcessProgram(prog) | ||
issues, _ := analyzer.Report() | ||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
severity, confidence := gosec.Low, gosec.Low | ||
issues = filterIssues(issues, severity, confidence) | ||
for _, i := range issues { | ||
fileContent, tf, err := util.ReadFile(pass.Fset, i.File) | ||
if err != nil { | ||
panic(err) | ||
} | ||
text := fmt.Sprintf("[%s] %s: %s", Name, i.RuleID, i.What) // TODO: use severity and confidence | ||
var r *result.Range | ||
line, err := strconv.Atoi(i.Line) | ||
if err != nil { | ||
r = &result.Range{} | ||
if n, rerr := fmt.Sscanf(i.Line, "%d-%d", &r.From, &r.To); rerr != nil || n != 2 { | ||
continue | ||
} | ||
line = r.From | ||
} | ||
|
||
pass.Reportf(token.Pos(tf.Base()+util.FindOffset(string(fileContent), line, 1)), text) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func filterIssues(issues []*gosec.Issue, severity, confidence gosec.Score) []*gosec.Issue { | ||
res := make([]*gosec.Issue, 0) | ||
for _, issue := range issues { | ||
if issue.Severity >= severity && issue.Confidence >= confidence { | ||
res = append(res, issue) | ||
} | ||
} | ||
return res | ||
} |
This file contains 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 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 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 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 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 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 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 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 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
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.
Some linter needs to upgrade the version of toml. so it change some error message.