Skip to content

Commit 48078cb

Browse files
feat: add tls.SignatureScheme
1 parent 1b365e2 commit 48078cb

File tree

5 files changed

+165
-21
lines changed

5 files changed

+165
-21
lines changed

pkg/analyzer/analyzer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
RPCDefaultPathFlag = "rpc-default-path"
2424
OSDevNullFlag = "os-dev-null"
2525
SQLIsolationLevelFlag = "sql-isolation-level"
26+
TLSSignatureScheme = "tls-signature-scheme"
2627
)
2728

2829
// New returns new usestdlibvars analyzer.
@@ -47,6 +48,7 @@ func flags() flag.FlagSet {
4748
flags.Bool(RPCDefaultPathFlag, false, "suggest the use of rpc.DefaultXXPath")
4849
flags.Bool(OSDevNullFlag, false, "suggest the use of os.DevNull")
4950
flags.Bool(SQLIsolationLevelFlag, false, "suggest the use of sql.LevelXX.String()")
51+
flags.Bool(TLSSignatureScheme, false, "suggest the use of tls.SignatureScheme.String()")
5052
return *flags
5153
}
5254

@@ -105,6 +107,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
105107
checkSQLIsolationLevel(pass, n)
106108
}
107109

110+
if lookupFlag(pass, TLSSignatureScheme) {
111+
checkTLSSignatureScheme(pass, n)
112+
}
113+
108114
case *ast.CompositeLit:
109115
typ, ok := n.Type.(*ast.SelectorExpr)
110116
if !ok {
@@ -417,6 +423,14 @@ func checkSQLIsolationLevel(pass *analysis.Pass, basicLit *ast.BasicLit) {
417423
}
418424
}
419425

426+
func checkTLSSignatureScheme(pass *analysis.Pass, basicLit *ast.BasicLit) {
427+
currentVal := getBasicLitValue(basicLit)
428+
429+
if newVal, ok := mapping.TLSSignatureScheme[currentVal]; ok {
430+
report(pass, basicLit.Pos(), currentVal, newVal)
431+
}
432+
}
433+
420434
// getBasicLitFromArgs gets the *ast.BasicLit of a function argument.
421435
//
422436
// Arguments:

pkg/analyzer/analyzer_test.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,26 @@ func TestUseStdlibVars(t *testing.T) {
1616
"a/time",
1717
"a/os",
1818
"a/sql",
19+
"a/tls",
1920
}
2021

2122
a := analyzer.New()
2223

23-
if err := a.Flags.Set(analyzer.TimeWeekdayFlag, "true"); err != nil {
24-
t.Error(err)
25-
}
26-
if err := a.Flags.Set(analyzer.TimeMonthFlag, "true"); err != nil {
27-
t.Error(err)
28-
}
29-
if err := a.Flags.Set(analyzer.TimeLayoutFlag, "true"); err != nil {
30-
t.Error(err)
31-
}
32-
if err := a.Flags.Set(analyzer.CryptoHashFlag, "true"); err != nil {
33-
t.Error(err)
34-
}
35-
if err := a.Flags.Set(analyzer.RPCDefaultPathFlag, "true"); err != nil {
36-
t.Error(err)
37-
}
38-
if err := a.Flags.Set(analyzer.OSDevNullFlag, "true"); err != nil {
39-
t.Error(err)
40-
}
41-
if err := a.Flags.Set(analyzer.SQLIsolationLevelFlag, "true"); err != nil {
42-
t.Error(err)
43-
}
24+
mustNil(t, a.Flags.Set(analyzer.TimeWeekdayFlag, "true"))
25+
mustNil(t, a.Flags.Set(analyzer.TimeMonthFlag, "true"))
26+
mustNil(t, a.Flags.Set(analyzer.TimeLayoutFlag, "true"))
27+
mustNil(t, a.Flags.Set(analyzer.CryptoHashFlag, "true"))
28+
mustNil(t, a.Flags.Set(analyzer.RPCDefaultPathFlag, "true"))
29+
mustNil(t, a.Flags.Set(analyzer.OSDevNullFlag, "true"))
30+
mustNil(t, a.Flags.Set(analyzer.SQLIsolationLevelFlag, "true"))
31+
mustNil(t, a.Flags.Set(analyzer.TLSSignatureScheme, "true"))
4432

4533
analysistest.Run(t, analysistest.TestData(), a, pkgs...)
4634
}
35+
36+
func mustNil(t *testing.T, err error) {
37+
t.Helper()
38+
if err != nil {
39+
t.Error(err)
40+
}
41+
}

pkg/analyzer/internal/gen.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ func main() {
9595
templateName: "test-template.go.tmpl",
9696
fileName: "pkg/analyzer/testdata/src/a/sql/isolationlevel.go",
9797
},
98+
{
99+
mapping: mapping.TLSSignatureScheme,
100+
packageName: "sql_test",
101+
templateName: "test-template.go.tmpl",
102+
fileName: "pkg/analyzer/testdata/src/a/tls/signaturescheme.go",
103+
},
98104
}
99105

100106
for _, operation := range operations {

pkg/analyzer/internal/mapping/mapping.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mapping
22

33
import (
44
"crypto"
5+
"crypto/tls"
56
"database/sql"
67
"net/http"
78
"net/rpc"
@@ -176,3 +177,18 @@ var SQLIsolationLevel = map[string]string{
176177
// sql.LevelSerializable.String(): "sql.LevelSerializable.String()",
177178
// sql.LevelLinearizable.String(): "sql.LevelLinearizable.String()",
178179
}
180+
181+
var TLSSignatureScheme = map[string]string{
182+
tls.PSSWithSHA256.String(): "tls.PSSWithSHA256.String()",
183+
tls.ECDSAWithP256AndSHA256.String(): "tls.ECDSAWithP256AndSHA256.String()",
184+
tls.Ed25519.String(): "tls.Ed25519.String()",
185+
tls.PSSWithSHA384.String(): "tls.PSSWithSHA384.String()",
186+
tls.PSSWithSHA512.String(): "tls.PSSWithSHA512.String()",
187+
tls.PKCS1WithSHA256.String(): "tls.PKCS1WithSHA256.String()",
188+
tls.PKCS1WithSHA384.String(): "tls.PKCS1WithSHA384.String()",
189+
tls.PKCS1WithSHA512.String(): "tls.PKCS1WithSHA512.String()",
190+
tls.ECDSAWithP384AndSHA384.String(): "tls.ECDSAWithP384AndSHA384.String()",
191+
tls.ECDSAWithP521AndSHA512.String(): "tls.ECDSAWithP521AndSHA512.String()",
192+
tls.PKCS1WithSHA1.String(): "tls.PKCS1WithSHA1.String()",
193+
tls.ECDSAWithSHA1.String(): "tls.ECDSAWithSHA1.String()",
194+
}

pkg/analyzer/testdata/src/a/tls/signaturescheme.go

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)