Skip to content

Commit 8bf9276

Browse files
authored
Ensure that is not matching domain prefixes (#109)
resolves #108
1 parent 2453ac8 commit 8bf9276

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

depguard.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ func (ua *UncompiledAnalyzer) Compile() error {
4747
return nil
4848
}
4949

50-
func (settings LinterSettings) run(pass *analysis.Pass) (interface{}, error) {
51-
s, err := settings.compile()
50+
func (s LinterSettings) run(pass *analysis.Pass) (interface{}, error) {
51+
settings, err := s.compile()
5252
if err != nil {
5353
return nil, err
5454
}
55-
return s.run(pass)
55+
return settings.run(pass)
5656
}
5757

5858
func newAnalyzer(run func(*analysis.Pass) (interface{}, error)) *analysis.Analyzer {

settings.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ func (l LinterSettings) compile() (linterSettings, error) {
202202
return li, nil
203203
}
204204

205-
func (ls linterSettings) whichLists(fileName string) []*list {
205+
func (s linterSettings) whichLists(fileName string) []*list {
206206
var matches []*list
207-
for _, l := range ls {
207+
for _, l := range s {
208208
if l.fileMatch(fileName) {
209209
matches = append(matches, l)
210210
}
@@ -236,5 +236,13 @@ func strInPrefixList(str string, prefixList []string) (bool, int) {
236236
if ioc[len(ioc)-1] == '$' {
237237
return str == ioc[:len(ioc)-1], idx
238238
}
239-
return strings.HasPrefix(str, prefixList[idx]), idx
239+
240+
// There is no sep chars in ioc so it is a GOROOT import that is being matched to the import (str) (see $gostd expander)
241+
// AND the import contains a period which GOROOT cannot have. This eliminates the go.evil.me/pkg scenario
242+
// BUT should still allow /os/exec and ./os/exec imports which are very uncommon
243+
if !strings.ContainsAny(ioc, "./") && strings.ContainsRune(str, '.') {
244+
return false, idx
245+
}
246+
247+
return strings.HasPrefix(str, ioc), idx
240248
}

settings_test.go

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,21 @@ func TestLinterSettingsCompile(t *testing.T) {
348348

349349
var (
350350
prefixList = []string{
351-
"some/package/a",
352-
"some/package/b",
353-
"some/package/c/",
354-
"some/package/d$",
355-
"some/pkg/c",
356-
"some/pkg/d",
357-
"some/pkg/e",
351+
"willd.io/package/a",
352+
"willd.io/package/b",
353+
"willd.io/package/c/",
354+
"willd.io/package/d$",
355+
"willd.io/pkg/c",
356+
"willd.io/pkg/d",
357+
"willd.io/pkg/e",
358+
}
359+
360+
diffKindsList = []string{
361+
"./relative/path",
362+
"/absolute/path",
363+
"os/exec",
364+
"path",
365+
"willd.io/normal/package",
358366
}
359367

360368
globList = []glob.Glob{
@@ -375,19 +383,53 @@ func testStrInPrefixList(str string, expect bool, expectedIdx int) func(t *testi
375383
}
376384
}
377385

386+
func testStrInDiffPrefixList(str string, expect bool, expectedIdx int) func(t *testing.T) {
387+
return func(t *testing.T) {
388+
act, idx := strInPrefixList(str, diffKindsList)
389+
if act != expect {
390+
t.Errorf("string prefix mismatch: expected %s - got %s", strconv.FormatBool(expect), strconv.FormatBool(act))
391+
}
392+
if idx != expectedIdx {
393+
t.Errorf("string prefix index: expected %d - got %d", expectedIdx, idx)
394+
}
395+
}
396+
}
397+
378398
func TestStrInPrefixList(t *testing.T) {
379399
sort.Strings(prefixList)
380-
t.Run("full_match_start", testStrInPrefixList("some/package/a", true, 0))
381-
t.Run("full_match", testStrInPrefixList("some/package/b", true, 1))
382-
t.Run("full_match_end", testStrInPrefixList("some/pkg/e", true, 6))
383-
t.Run("no_match_end", testStrInPrefixList("zome/pkg/e", false, 6))
384-
t.Run("no_match_start", testStrInPrefixList("aome/pkg/e", false, -1))
385-
t.Run("match_start", testStrInPrefixList("some/package/a/files", true, 0))
386-
t.Run("match_middle", testStrInPrefixList("some/pkg/c/files", true, 4))
387-
t.Run("match_end", testStrInPrefixList("some/pkg/e/files", true, 6))
388-
t.Run("no_match_trailing", testStrInPrefixList("some/package/c", false, 1))
389-
t.Run("match_exact", testStrInPrefixList("some/package/d", true, 3))
390-
t.Run("no_prefix_match_exact", testStrInPrefixList("some/package/d/something", false, 3))
400+
t.Run("full_match_start", testStrInPrefixList("willd.io/package/a", true, 0))
401+
t.Run("full_match", testStrInPrefixList("willd.io/package/b", true, 1))
402+
t.Run("full_match_end", testStrInPrefixList("willd.io/pkg/e", true, 6))
403+
t.Run("no_match_end", testStrInPrefixList("zilld.io/pkg/e", false, 6))
404+
t.Run("no_match_start", testStrInPrefixList("ailld.io/pkg/e", false, -1))
405+
t.Run("match_start", testStrInPrefixList("willd.io/package/a/files", true, 0))
406+
t.Run("match_middle", testStrInPrefixList("willd.io/pkg/c/files", true, 4))
407+
t.Run("match_end", testStrInPrefixList("willd.io/pkg/e/files", true, 6))
408+
t.Run("no_match_trailing", testStrInPrefixList("willd.io/package/c", false, 1))
409+
t.Run("match_exact", testStrInPrefixList("willd.io/package/d", true, 3))
410+
t.Run("no_prefix_match_exact", testStrInPrefixList("willd.io/package/d/something", false, 3))
411+
412+
sort.Strings(diffKindsList)
413+
t.Run("match_import_with_domain_exact", testStrInDiffPrefixList("willd.io/normal/package", true, 4))
414+
t.Run("match_import_with_domain", testStrInDiffPrefixList("willd.io/normal/package/nested", true, 4))
415+
t.Run("no_match_import_with_domain", testStrInDiffPrefixList("willd.io/normal", false, 3))
416+
t.Run("match_import_relative_exact", testStrInDiffPrefixList("./relative/path", true, 0))
417+
t.Run("match_import_relative", testStrInDiffPrefixList("./relative/path/nested", true, 0))
418+
t.Run("no_match_import_relative", testStrInDiffPrefixList("./relative", false, -1))
419+
t.Run("match_import_absolute_exact", testStrInDiffPrefixList("/absolute/path", true, 1))
420+
t.Run("match_import_absolute", testStrInDiffPrefixList("/absolute/path/nested", true, 1))
421+
t.Run("no_match_import_absolute", testStrInDiffPrefixList("/absolute", false, 0))
422+
t.Run("match_gostd_single_exact", testStrInDiffPrefixList("path", true, 3))
423+
t.Run("match_gostd_single", testStrInDiffPrefixList("path/filepath", true, 3))
424+
t.Run("no_match_gostd_single", testStrInDiffPrefixList("evil", false, 1))
425+
t.Run("match_gostd_multiple_exact", testStrInDiffPrefixList("os/exec", true, 2))
426+
t.Run("match_gostd_multiple", testStrInDiffPrefixList("os/exec/fake", true, 2))
427+
t.Run("no_match_gostd_multiple", testStrInDiffPrefixList("os/evil", false, 1))
428+
429+
// "Evil Packages"
430+
t.Run("gostd_in_domain", testStrInDiffPrefixList("path.willd.io/normal/package", false, 3))
431+
t.Run("gostd_in_relative", testStrInDiffPrefixList("./os/exec", false, -1))
432+
t.Run("gostd_in_absolute", testStrInDiffPrefixList("/os/exec", false, 1))
391433
}
392434

393435
func testStrInGlobList(str string, expect bool) func(t *testing.T) {

0 commit comments

Comments
 (0)