@@ -21,7 +21,7 @@ func detectSha1Usage(pass *analysis.Pass) (interface{}, error) {
2121 pkgs := []string {"jssha" , "jssha/sha1" , "jssha/dist/sha1" }
2222
2323 // Will be used to track the Encrypting library being used
24- var pkgDeclaratorVar * analysis.Variable
24+ var pkgDeclaratorVar [] * analysis.Variable
2525
2626 dfg := pass .ResultOf [DataFlowAnalyzer ].(* DataFlowGraph )
2727
@@ -68,11 +68,39 @@ func detectSha1Usage(pass *analysis.Pass) (interface{}, error) {
6868 if varName != "" {
6969 nameVar := scopeTree .GetScope (node ).Lookup (varName )
7070 if nameVar != nil {
71- pkgDeclaratorVar = nameVar
71+ pkgDeclaratorVar = append ( pkgDeclaratorVar , nameVar )
7272 }
7373 }
7474 }
7575 }
76+ if node .Type () == "import_statement" {
77+ // Handle the case for import declaration eg. import jssha from "jssha"
78+
79+ packageName := node .ChildByFieldName ("source" )
80+ if packageName != nil && packageName .Type () == "string" {
81+ packageNameContent := packageName .NamedChild (0 ).Content (pass .FileContext .Source )
82+ if packageNameContent != "jssha" {
83+ return
84+ }
85+
86+ }
87+
88+ importField := node .NamedChild (0 )
89+
90+ if importField != nil && importField .Type () == "import_clause" {
91+ importIdentifier := importField .NamedChild (0 )
92+ if importIdentifier != nil && importIdentifier .Type () == "identifier" {
93+ idName := importIdentifier .Content (pass .FileContext .Source )
94+ if idName != "" {
95+ nameVar := scopeTree .GetScope (node ).Lookup (idName )
96+ if nameVar != nil {
97+ pkgDeclaratorVar = append (pkgDeclaratorVar , nameVar )
98+ }
99+ }
100+ }
101+ }
102+
103+ }
76104 })
77105
78106 analysis .Preorder (pass , func (node * sitter.Node ) {
@@ -81,25 +109,19 @@ func detectSha1Usage(pass *analysis.Pass) (interface{}, error) {
81109 }
82110
83111 if node .Type () == "new_expression" {
84- // fmt.Println("+++++++++++++++++", node.Content(pass.FileContext.Source))
85112 ctor := node .ChildByFieldName ("constructor" )
86113 arg := node .ChildByFieldName ("arguments" )
87114 if ctor != nil && arg != nil {
88115 ctorVar := scopeTree .GetScope (ctor ).Lookup (ctor .Content (pass .FileContext .Source ))
89- // fmt.Println("++++++ctorVar+++++++", ctorVar, "++++++++++++")
90- // fmt.Println("++++++pkgDeclaratorVar+++++++", pkgDeclaratorVar, "++++++++++++")
91- if ctorVar != nil && ctorVar == pkgDeclaratorVar {
116+ if ctorVar != nil && slices .Contains (pkgDeclaratorVar , ctorVar ) {
92117 hashAlgo := arg .NamedChild (0 )
93- // fmt.Println("++++++hashAlgo+++++++", hashAlgo, "++++++++++++")
94118 if hashAlgo == nil {
95119 return
96120 }
97121
98122 hashAlgoStr := hashAlgo .NamedChild (0 )
99123 hashAlgoName := hashAlgoStr .Content (pass .FileContext .Source )
100- // fmt.Println("++++++hashAlgoName+++++++", hashAlgoName, "++++++++++++")
101124 if hashAlgoName == "SHA-1" {
102- // fmt.Println("++++++hashAlgoNameConditionTrue+++++++", hashAlgoName, "++++++++++++")
103125 pass .Report (pass , node , "SHA-1 is not recommended for cryptographic purposes" )
104126 }
105127
0 commit comments