|
6 | 6 | "errors"
|
7 | 7 | "fmt"
|
8 | 8 | "os"
|
| 9 | + "regexp" |
9 | 10 | "strings"
|
10 | 11 | "sync"
|
11 | 12 | "time"
|
@@ -130,6 +131,9 @@ func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
|
130 | 131 | return
|
131 | 132 | }
|
132 | 133 |
|
| 134 | + // key entered through command line may have spaces instead of newlines, replace them |
| 135 | + token = replaceSpacesWithNewlines(token) |
| 136 | + |
133 | 137 | info, err := AnalyzePermissions(context.Background(), cfg, token)
|
134 | 138 | if err != nil {
|
135 | 139 | color.Red("[x] Error: %s", err.Error())
|
@@ -301,3 +305,26 @@ func analyzeGithubUser(ctx context.Context, parsedKey any) (*string, error) {
|
301 | 305 | func analyzeGitlabUser(ctx context.Context, parsedKey any) (*string, error) {
|
302 | 306 | return privatekey.VerifyGitLabUser(ctx, parsedKey)
|
303 | 307 | }
|
| 308 | + |
| 309 | +// replaceSpacesWithNewlines extracts the base64 part, replaces spaces with newlines if needed, and reconstructs the key. |
| 310 | +func replaceSpacesWithNewlines(privateKey string) string { |
| 311 | + // Regex pattern to extract the key content |
| 312 | + re := regexp.MustCompile(`(?i)(-----\s*BEGIN[ A-Z0-9_-]*PRIVATE KEY\s*-----)\s*([\s\S]*?)\s*(-----\s*END[ A-Z0-9_-]*PRIVATE KEY\s*-----)`) |
| 313 | + |
| 314 | + // Find matches |
| 315 | + matches := re.FindStringSubmatch(privateKey) |
| 316 | + if len(matches) != 4 { |
| 317 | + // no need to process |
| 318 | + return privateKey |
| 319 | + } |
| 320 | + |
| 321 | + header := matches[1] // BEGIN line |
| 322 | + base64Part := matches[2] // Base64 content |
| 323 | + footer := matches[3] // END line |
| 324 | + |
| 325 | + // Replace spaces with newlines |
| 326 | + formattedBase64 := strings.ReplaceAll(base64Part, " ", "\n") |
| 327 | + |
| 328 | + // Reconstruct the private key |
| 329 | + return fmt.Sprintf("%s\n%s\n%s", header, formattedBase64, footer) |
| 330 | +} |
0 commit comments