Skip to content

Commit

Permalink
internal/postgres: exclusions should be case insensitive
Browse files Browse the repository at this point in the history
Update exclusion logic to be case insensitive. When there is no go.mod
file, GitHub module paths may be spelled with any casing.

As a result of this change, we will no longer be able to exclude only
certain capitalizations of a module.

Change-Id: I67342cf3543e795e7795aa74081541d7e6fed18f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/576595
Reviewed-by: Jonathan Amsterdam <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
kokoro-CI: kokoro <[email protected]>
Auto-Submit: Robert Findley <[email protected]>
  • Loading branch information
findleyr authored and gopherbot committed Apr 5, 2024
1 parent 599abd8 commit b8abe08
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 9 additions & 5 deletions internal/postgres/excluded.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ func (db *DB) IsExcluded(ctx context.Context, path, version string) bool {
}

func excludes(pattern, path, version string) bool {
// Patterns with "@" must match exactly.
// Certain hosts (such as GitHub) are case insensitive.
// Therefore, we err on the side of insensitive exclusions.

// Patterns with "@" must match the full path (case insensitively).
mod, ver, found := strings.Cut(pattern, "@")
if found {
return mod == path && ver == version
return strings.EqualFold(mod, path) && ver == version
}
// Patterns without "@" can match exactly or be a componentwise prefix.
if pattern == path {
// Patterns without "@" can match the full path or be a componentwise prefix
// (case insensitively).
if strings.EqualFold(pattern, path) {
return true
}
if !strings.HasSuffix(pattern, "/") {
pattern += "/"
}
return strings.HasPrefix(path, pattern)
return strings.HasPrefix(strings.ToLower(path), strings.ToLower(pattern))
}

// InsertExcludedPattern inserts pattern into the excluded_prefixes table.
Expand Down
12 changes: 11 additions & 1 deletion internal/postgres/excluded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestIsExcluded(t *testing.T) {
defer release()
ctx := context.Background()

for _, pat := range []string{"bad", "badslash/", "[email protected]"} {
for _, pat := range []string{"bad", "badslash/", "[email protected]", "github.com/bad"} {
if err := testDB.InsertExcludedPattern(ctx, pat, "someone", "because"); err != nil {
t.Fatal(err)
}
Expand All @@ -38,6 +38,16 @@ func TestIsExcluded(t *testing.T) {
{"baddy", "v1.2.4", false},
{"baddy", "", false},
{"baddy", "v1.2.3", true},

// tests for case insensitivity
{"Bad", "", true},
{"Bad/repo", "", true},
{"baDDy", "v1.2.3", true},
{"baDDy", "v1.2.4", false},
{"github.com/Bad", "", true},
{"github.com/bad/repo", "", true},
{"github.com/bad/Repo", "", true},
{"github.com/Bad/repo", "", true},
} {
got := testDB.IsExcluded(ctx, test.path, test.version)
if got != test.want {
Expand Down

0 comments on commit b8abe08

Please sign in to comment.