|
| 1 | +//go:generate generate_permissions permissions.yaml permissions.go figma |
| 2 | + |
| 3 | +package figma |
| 4 | + |
| 5 | +import ( |
| 6 | + _ "embed" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "os" |
| 13 | + "regexp" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "github.com/fatih/color" |
| 17 | + "github.com/jedib0t/go-pretty/v6/table" |
| 18 | + |
| 19 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" |
| 20 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config" |
| 21 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 22 | +) |
| 23 | + |
| 24 | +var _ analyzers.Analyzer = (*Analyzer)(nil) |
| 25 | + |
| 26 | +type Analyzer struct { |
| 27 | + Cfg *config.Config |
| 28 | +} |
| 29 | + |
| 30 | +func (Analyzer) Type() analyzers.AnalyzerType { return analyzers.AnalyzerTypeFigma } |
| 31 | + |
| 32 | +type ScopeStatus string |
| 33 | + |
| 34 | +const ( |
| 35 | + StatusError ScopeStatus = "Error" |
| 36 | + StatusGranted ScopeStatus = "Granted" |
| 37 | + StatusDenied ScopeStatus = "Denied" |
| 38 | + StatusUnverified ScopeStatus = "Unverified" |
| 39 | +) |
| 40 | + |
| 41 | +func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analyzers.AnalyzerResult, error) { |
| 42 | + token, ok := credInfo["token"] |
| 43 | + if !ok { |
| 44 | + return nil, errors.New("token not found in credInfo") |
| 45 | + } |
| 46 | + info, err := AnalyzePermissions(a.Cfg, token) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + return MapToAnalyzerResult(info), nil |
| 51 | +} |
| 52 | + |
| 53 | +func AnalyzeAndPrintPermissions(cfg *config.Config, token string) { |
| 54 | + info, err := AnalyzePermissions(cfg, token) |
| 55 | + if err != nil { |
| 56 | + color.Red("[x] Error : %s", err.Error()) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + color.Green("[!] Valid Figma Personal Access Token\n\n") |
| 61 | + PrintUserAndPermissions(info) |
| 62 | +} |
| 63 | + |
| 64 | +func AnalyzePermissions(cfg *config.Config, token string) (*secretInfo, error) { |
| 65 | + client := analyzers.NewAnalyzeClient(cfg) |
| 66 | + allScopes := getAllScopes() |
| 67 | + scopeToEndpoints, err := getScopeEndpointsMap() |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + var info = &secretInfo{Scopes: map[Scope]ScopeStatus{}} |
| 73 | + for _, scope := range allScopes { |
| 74 | + info.Scopes[scope] = StatusUnverified |
| 75 | + } |
| 76 | + |
| 77 | + for _, scope := range orderedScopeList { |
| 78 | + endpoint, err := getScopeEndpoint(scopeToEndpoints, scope) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + resp, err := callAPIEndpoint(client, token, endpoint) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + defer resp.Body.Close() |
| 87 | + body, err := io.ReadAll(resp.Body) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + scopeStatus := determineScopeStatus(resp.StatusCode, endpoint) |
| 93 | + if scopeStatus == StatusGranted { |
| 94 | + if scope == ScopeFilesRead { |
| 95 | + if err := json.Unmarshal(body, &info.UserInfo); err != nil { |
| 96 | + return nil, fmt.Errorf("error decoding user info from response %v", err) |
| 97 | + } |
| 98 | + } |
| 99 | + info.Scopes[scope] = StatusGranted |
| 100 | + } |
| 101 | + // If the token does NOT have the scope, response will include all the scopes it does have |
| 102 | + if scopeStatus == StatusDenied { |
| 103 | + scopes, ok := extractScopesFromError(body) |
| 104 | + if !ok { |
| 105 | + return nil, fmt.Errorf("could not extract scopes from error message") |
| 106 | + } |
| 107 | + for scope := range info.Scopes { |
| 108 | + info.Scopes[scope] = StatusDenied |
| 109 | + } |
| 110 | + for _, scope := range scopes { |
| 111 | + info.Scopes[scope] = StatusGranted |
| 112 | + } |
| 113 | + // We have enough info to finish analysis |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | + return info, nil |
| 118 | +} |
| 119 | + |
| 120 | +// determineScopeStatus takes the API response status code and uses it along with the expected |
| 121 | +// status codes to dermine whether the access token has the required scope to perform that action. |
| 122 | +// It returns a ScopeStatus which can be Granted, Denied, or Unverified. |
| 123 | +func determineScopeStatus(statusCode int, endpoint endpoint) ScopeStatus { |
| 124 | + if statusCode == endpoint.ExpectedStatusCodeWithScope || statusCode == http.StatusOK { |
| 125 | + return StatusGranted |
| 126 | + } |
| 127 | + |
| 128 | + if statusCode == endpoint.ExpectedStatusCodeWithoutScope { |
| 129 | + return StatusDenied |
| 130 | + } |
| 131 | + |
| 132 | + // Can not determine scope as the expected error is unknown |
| 133 | + return StatusUnverified |
| 134 | +} |
| 135 | + |
| 136 | +// Matches API response body with expected message pattern in case the token is missing a scope |
| 137 | +// If the responses match, we can extract all available scopes from the response msg |
| 138 | +func extractScopesFromError(body []byte) ([]Scope, bool) { |
| 139 | + filteredBody := filterErrorResponseBody(string(body)) |
| 140 | + re := regexp.MustCompile(`Invalid scope(?:\(s\))?: ([a-zA-Z_:, ]+)\. This endpoint requires.*`) |
| 141 | + matches := re.FindStringSubmatch(filteredBody) |
| 142 | + if len(matches) > 1 { |
| 143 | + scopes := strings.Split(matches[1], ", ") |
| 144 | + return getScopesFromScopeStrings(scopes), true |
| 145 | + } |
| 146 | + return nil, false |
| 147 | +} |
| 148 | + |
| 149 | +// The filterErrorResponseBody function cleans the provided "invalid permission" API |
| 150 | +// response message by removing the characters '"', '[', ']', '\', and '"'. |
| 151 | +func filterErrorResponseBody(msg string) string { |
| 152 | + result := strings.ReplaceAll(msg, "\\", "") |
| 153 | + result = strings.ReplaceAll(result, "\"", "") |
| 154 | + result = strings.ReplaceAll(result, "[", "") |
| 155 | + return strings.ReplaceAll(result, "]", "") |
| 156 | +} |
| 157 | + |
| 158 | +func MapToAnalyzerResult(info *secretInfo) *analyzers.AnalyzerResult { |
| 159 | + if info == nil { |
| 160 | + return nil |
| 161 | + } |
| 162 | + |
| 163 | + result := analyzers.AnalyzerResult{ |
| 164 | + AnalyzerType: analyzers.AnalyzerTypeFigma, |
| 165 | + } |
| 166 | + var permissions []analyzers.Permission |
| 167 | + for scope, status := range info.Scopes { |
| 168 | + if status != StatusGranted { |
| 169 | + continue |
| 170 | + } |
| 171 | + permissions = append(permissions, analyzers.Permission{Value: string(scope)}) |
| 172 | + } |
| 173 | + userResource := analyzers.Resource{ |
| 174 | + Name: info.UserInfo.Handle, |
| 175 | + FullyQualifiedName: info.UserInfo.ID, |
| 176 | + Type: "user", |
| 177 | + Metadata: map[string]any{ |
| 178 | + "email": info.UserInfo.Email, |
| 179 | + "img_url": info.UserInfo.ImgURL, |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + result.Bindings = analyzers.BindAllPermissions(userResource, permissions...) |
| 184 | + return &result |
| 185 | +} |
| 186 | + |
| 187 | +func PrintUserAndPermissions(info *secretInfo) { |
| 188 | + color.Yellow("[i] User Info:") |
| 189 | + t1 := table.NewWriter() |
| 190 | + t1.SetOutputMirror(os.Stdout) |
| 191 | + t1.AppendHeader(table.Row{"ID", "Handle", "Email", "Image URL"}) |
| 192 | + t1.AppendRow(table.Row{ |
| 193 | + color.GreenString(info.UserInfo.ID), |
| 194 | + color.GreenString(info.UserInfo.Handle), |
| 195 | + color.GreenString(info.UserInfo.Email), |
| 196 | + color.GreenString(info.UserInfo.ImgURL), |
| 197 | + }) |
| 198 | + t1.SetOutputMirror(os.Stdout) |
| 199 | + t1.Render() |
| 200 | + |
| 201 | + color.Yellow("\n[i] Scopes:") |
| 202 | + t2 := table.NewWriter() |
| 203 | + t2.AppendHeader(table.Row{"Scope", "Status", "Actions"}) |
| 204 | + for scope, status := range info.Scopes { |
| 205 | + actions := getScopeActions(scope) |
| 206 | + rows := []table.Row{} |
| 207 | + for i, action := range actions { |
| 208 | + var scopeCell string |
| 209 | + var statusCell string |
| 210 | + if i == 0 { |
| 211 | + scopeCell = color.GreenString(string(scope)) |
| 212 | + statusCell = color.GreenString(string(status)) |
| 213 | + } |
| 214 | + rows = append(rows, table.Row{scopeCell, statusCell, color.GreenString(action)}) |
| 215 | + } |
| 216 | + t2.AppendRows(rows) |
| 217 | + t2.AppendSeparator() |
| 218 | + } |
| 219 | + t2.SetOutputMirror(os.Stdout) |
| 220 | + t2.Render() |
| 221 | + fmt.Printf("%s: https://www.figma.com/developers/api\n\n", color.GreenString("Ref")) |
| 222 | +} |
0 commit comments