Skip to content

Commit

Permalink
Fixes for a few finegrained token issues (#3194)
Browse files Browse the repository at this point in the history
* Fixes a few finegrained issues

* remove some code
  • Loading branch information
dustin-decker committed Aug 7, 2024
1 parent 8b37ae1 commit fc4829a
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions pkg/analyzer/analyzers/github/finegrained/finegrained.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"log"
"os"
"sort"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -428,11 +427,6 @@ func getDependabotAlertsPermission(client *gh.Client, repo *gh.Repository, curre
// Risk: Extremely Low
// GET /repos/{owner}/{repo}/dependabot/alerts
_, resp, err := client.Dependabot.ListRepoAlerts(context.Background(), *repo.Owner.Login, *repo.Name, &gh.ListAlertsOptions{})
if err != nil {
if !strings.Contains(err.Error(), "disabled") {
return NoAccess, err
}
}
defer resp.Body.Close()

switch resp.StatusCode {
Expand Down Expand Up @@ -1320,14 +1314,14 @@ func PrintFineGrainedToken(cfg *config.Config, info *common.SecretInfo) {
common.PrintGitHubRepos(info.AccessibleRepos)

// Print out the access map
perms, ok := info.RepoAccessMap.(map[string]Permission)
perms, ok := info.RepoAccessMap.([]Permission)
if !ok {
panic("Repo Access Map is not of type Permission")
}
printFineGrainedPermissions(perms, cfg.ShowAll, true)
}

perms, ok := info.UserAccessMap.(map[string]Permission)
perms, ok := info.UserAccessMap.([]Permission)
if !ok {
panic("Repo Access Map is not of type Permission")
}
Expand All @@ -1336,31 +1330,23 @@ func PrintFineGrainedToken(cfg *config.Config, info *common.SecretInfo) {
common.PrintGists(info.Gists, cfg.ShowAll)
}

func printFineGrainedPermissions(accessMap map[string]Permission, showAll bool, repoPermissions bool) {
func printFineGrainedPermissions(accessMap []Permission, showAll bool, repoPermissions bool) {
permissionCount := 0
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Permission Type", "Permission" /* Add more column headers if needed */})

// Extract keys from accessMap into slice
keys := make([]string, 0, len(accessMap))
for k := range accessMap {
keys = append(keys, k)
}
// Sort the slice
sort.Strings(keys)

for _, key := range keys {
value := accessMap[key]
if value == Invalid {
for _, perm := range accessMap {
permStr, _ := perm.ToString()
if perm == Invalid {
// don't change permissionCount
} else {
permissionCount++
}
if !showAll && value == Invalid {
if !showAll && perm == Invalid {
continue
} else {
k, v := permissionFormatter(key, value)
k, v := permissionFormatter(permStr, perm)
t.AppendRow([]any{k, v})
}
}
Expand Down

0 comments on commit fc4829a

Please sign in to comment.