-
Notifications
You must be signed in to change notification settings - Fork 3
CXP-963: accept literal \n escapes in GitHub App private key PEM #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
46602e5
461000b
b9836f3
b902b3c
17fb437
62eb2f4
2deac91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "encoding/pem" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| cfg "github.com/conductorone/baton-github/pkg/config" | ||
|
|
@@ -39,3 +44,33 @@ func TestAppPrivateKeyPEM(t *testing.T) { | |
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| func TestLoadPrivateKeyFromString(t *testing.T) { | ||
| key, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| require.NoError(t, err) | ||
| der := x509.MarshalPKCS1PrivateKey(key) | ||
| pemBytes := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: der}) | ||
| realNewlines := string(pemBytes) | ||
|
|
||
| shapes := map[string]string{ | ||
| "real newlines": realNewlines, | ||
| "real CRLF": strings.ReplaceAll(realNewlines, "\n", "\r\n"), | ||
| "backslash-n escaped, as the docs instruct": strings.ReplaceAll(realNewlines, "\n", `\n`), | ||
| "backslash-n escaped, trailing space": strings.ReplaceAll(realNewlines, "\n", `\n`) + " ", | ||
| "backslash-r-backslash-n escaped (CRLF file)": strings.ReplaceAll(realNewlines, "\n", `\r\n`), | ||
| "backslash-r escaped (CR-only file)": strings.ReplaceAll(realNewlines, "\n", `\r`), | ||
| } | ||
|
Comment on lines
+55
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: every shape here is built from a PKCS#1 |
||
|
|
||
| for name, shape := range shapes { | ||
| t.Run("parses a PEM with "+name, func(t *testing.T) { | ||
| got, err := loadPrivateKeyFromString(shape) | ||
| require.NoError(t, err) | ||
| require.Equal(t, key.D, got.D) | ||
| }) | ||
| } | ||
|
|
||
| t.Run("errors on garbage input", func(t *testing.T) { | ||
| _, err := loadPrivateKeyFromString("not a pem") | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion: This map entry is one space short of gofmt alignment — its value starts at column 46 while every other entry (including the longest key on line 61) aligns at column 47.
gofmt -wfixes it; worth doing so the file doesn't trip a formatting gate inverify.