|
| 1 | +//go:generate generate_permissions permissions.yaml permissions.go airtable |
| 2 | +package airtable |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/fatih/color" |
| 12 | + "github.com/jedib0t/go-pretty/table" |
| 13 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" |
| 14 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config" |
| 15 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 16 | +) |
| 17 | + |
| 18 | +var _ analyzers.Analyzer = (*Analyzer)(nil) |
| 19 | + |
| 20 | +type Analyzer struct { |
| 21 | + Cfg *config.Config |
| 22 | +} |
| 23 | + |
| 24 | +func (Analyzer) Type() analyzers.AnalyzerType { return analyzers.AnalyzerTypeAirtable } |
| 25 | + |
| 26 | +type AirtableUserInfo struct { |
| 27 | + ID string `json:"id"` |
| 28 | + Email *string `json:"email,omitempty"` |
| 29 | + Scopes []string `json:"scopes"` |
| 30 | +} |
| 31 | + |
| 32 | +type AirtableBases struct { |
| 33 | + Bases []struct { |
| 34 | + ID string `json:"id"` |
| 35 | + Name string `json:"name"` |
| 36 | + } `json:"bases"` |
| 37 | +} |
| 38 | + |
| 39 | +func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analyzers.AnalyzerResult, error) { |
| 40 | + token, ok := credInfo["token"] |
| 41 | + if !ok { |
| 42 | + return nil, errors.New("token not found in credInfo") |
| 43 | + } |
| 44 | + |
| 45 | + userInfo, err := fetchAirtableUserInfo(token) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + var basesInfo *AirtableBases |
| 51 | + if hasScope(userInfo.Scopes, PermissionStrings[SchemaBasesRead]) { |
| 52 | + basesInfo, _ = fetchAirtableBases(token) |
| 53 | + } |
| 54 | + |
| 55 | + return mapToAnalyzerResult(userInfo, basesInfo), nil |
| 56 | +} |
| 57 | + |
| 58 | +func AnalyzeAndPrintPermissions(cfg *config.Config, token string) { |
| 59 | + userInfo, err := fetchAirtableUserInfo(token) |
| 60 | + if err != nil { |
| 61 | + color.Red("[x] Error : %s", err.Error()) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + color.Green("[!] Valid Airtable OAuth2 Access Token\n\n") |
| 66 | + printUserAndPermissions(userInfo) |
| 67 | + |
| 68 | + if hasScope(userInfo.Scopes, PermissionStrings[SchemaBasesRead]) { |
| 69 | + var basesInfo *AirtableBases |
| 70 | + basesInfo, _ = fetchAirtableBases(token) |
| 71 | + printBases(basesInfo) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func fetchAirtableUserInfo(token string) (*AirtableUserInfo, error) { |
| 76 | + url := "https://api.airtable.com/v0/meta/whoami" |
| 77 | + req, err := http.NewRequest("GET", url, nil) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 82 | + |
| 83 | + resp, err := http.DefaultClient.Do(req) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + defer resp.Body.Close() |
| 88 | + |
| 89 | + if resp.StatusCode != http.StatusOK { |
| 90 | + return nil, fmt.Errorf("failed to fetch Airtable user info, status: %d", resp.StatusCode) |
| 91 | + } |
| 92 | + |
| 93 | + var userInfo AirtableUserInfo |
| 94 | + if err := json.NewDecoder(resp.Body).Decode(&userInfo); err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + return &userInfo, nil |
| 99 | +} |
| 100 | + |
| 101 | +func fetchAirtableBases(token string) (*AirtableBases, error) { |
| 102 | + url := "https://api.airtable.com/v0/meta/bases" |
| 103 | + req, err := http.NewRequest("GET", url, nil) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 108 | + |
| 109 | + resp, err := http.DefaultClient.Do(req) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + defer resp.Body.Close() |
| 114 | + |
| 115 | + if resp.StatusCode != http.StatusOK { |
| 116 | + return nil, fmt.Errorf("failed to fetch Airtable bases, status: %d", resp.StatusCode) |
| 117 | + } |
| 118 | + |
| 119 | + var basesInfo AirtableBases |
| 120 | + if err := json.NewDecoder(resp.Body).Decode(&basesInfo); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return &basesInfo, nil |
| 125 | +} |
| 126 | + |
| 127 | +func hasScope(scopes []string, target string) bool { |
| 128 | + for _, scope := range scopes { |
| 129 | + if scope == target { |
| 130 | + return true |
| 131 | + } |
| 132 | + } |
| 133 | + return false |
| 134 | +} |
| 135 | + |
| 136 | +func mapToAnalyzerResult(userInfo *AirtableUserInfo, basesInfo *AirtableBases) *analyzers.AnalyzerResult { |
| 137 | + if userInfo == nil { |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + result := analyzers.AnalyzerResult{ |
| 142 | + AnalyzerType: analyzers.AnalyzerTypeAirtable, |
| 143 | + } |
| 144 | + var permissions []analyzers.Permission |
| 145 | + for _, scope := range userInfo.Scopes { |
| 146 | + permissions = append(permissions, analyzers.Permission{Value: scope}) |
| 147 | + } |
| 148 | + userResource := analyzers.Resource{ |
| 149 | + Name: userInfo.ID, |
| 150 | + FullyQualifiedName: userInfo.ID, |
| 151 | + Type: "user", |
| 152 | + Metadata: map[string]any{}, |
| 153 | + } |
| 154 | + |
| 155 | + if userInfo.Email != nil { |
| 156 | + userResource.Metadata["email"] = *userInfo.Email |
| 157 | + } |
| 158 | + |
| 159 | + result.Bindings = analyzers.BindAllPermissions(userResource, permissions...) |
| 160 | + |
| 161 | + if basesInfo != nil { |
| 162 | + for _, base := range basesInfo.Bases { |
| 163 | + resource := analyzers.Resource{ |
| 164 | + Name: base.Name, |
| 165 | + FullyQualifiedName: base.ID, |
| 166 | + Type: "base", |
| 167 | + } |
| 168 | + result.UnboundedResources = append(result.UnboundedResources, resource) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return &result |
| 173 | +} |
| 174 | + |
| 175 | +func printUserAndPermissions(info *AirtableUserInfo) { |
| 176 | + color.Yellow("[i] User:") |
| 177 | + t1 := table.NewWriter() |
| 178 | + email := "N/A" |
| 179 | + if info.Email != nil { |
| 180 | + email = *info.Email |
| 181 | + } |
| 182 | + t1.SetOutputMirror(os.Stdout) |
| 183 | + t1.AppendHeader(table.Row{"ID", "Email"}) |
| 184 | + t1.AppendRow(table.Row{color.GreenString(info.ID), color.GreenString(email)}) |
| 185 | + t1.SetOutputMirror(os.Stdout) |
| 186 | + t1.Render() |
| 187 | + |
| 188 | + color.Yellow("\n[i] Scopes:") |
| 189 | + t2 := table.NewWriter() |
| 190 | + t2.SetOutputMirror(os.Stdout) |
| 191 | + t2.AppendHeader(table.Row{"Scope", "Permission"}) |
| 192 | + for _, scope := range info.Scopes { |
| 193 | + for i, permission := range scope_mapping[scope] { |
| 194 | + scope_string := "" |
| 195 | + if i == 0 { |
| 196 | + scope_string = scope |
| 197 | + } |
| 198 | + t2.AppendRow(table.Row{color.GreenString(scope_string), color.GreenString(permission)}) |
| 199 | + } |
| 200 | + } |
| 201 | + t2.Render() |
| 202 | + fmt.Printf("%s: https://airtable.com/developers/web/api/scopes\n", color.GreenString("Ref")) |
| 203 | +} |
| 204 | + |
| 205 | +func printBases(bases *AirtableBases) { |
| 206 | + color.Yellow("\n[i] Bases:") |
| 207 | + t := table.NewWriter() |
| 208 | + t.SetOutputMirror(os.Stdout) |
| 209 | + if len(bases.Bases) > 0 { |
| 210 | + t.AppendHeader(table.Row{"ID", "Name"}) |
| 211 | + for _, base := range bases.Bases { |
| 212 | + t.AppendRow(table.Row{color.GreenString(base.ID), color.GreenString(base.Name)}) |
| 213 | + } |
| 214 | + } else { |
| 215 | + fmt.Printf("%s\n", color.GreenString("No bases associated with token")) |
| 216 | + } |
| 217 | + t.Render() |
| 218 | +} |
0 commit comments