-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nil pointer dereference for PEM certificates
According to the documentation [`Decode`](https://pkg.go.dev/encoding/pem#Decode) returns nil if no pem data is found. This is possible if the certificate contains additional data which is not part of the certificate. If this data occurs at the end the code before this commit panics. This commit ensures that such data is ignored. Data between two certificates in the chain is ignored as well. Leading data leads to an error being returned. Closes #359
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package certificate | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestParseCertificates_CertificateChain tries to parse a chain of PEM | ||
// encoded certificates with interspersed and terminating data. | ||
// See: https://github.com/hetznercloud/terraform-provider-hcloud/issues/359 | ||
func TestParseCertificates_CertificateChain(t *testing.T) { | ||
pem1, _, err := testsupport.RandTLSCert("example.com") | ||
assert.NoError(t, err) | ||
cert1, err := parseCertificates(pem1) | ||
assert.NoError(t, err) | ||
|
||
pem2, _, err := testsupport.RandTLSCert("ca.example.com") | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
cert2, err := parseCertificates(pem2) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
// Not really a certificate chain, but enough for our testing purposes | ||
chain := strings.Join([]string{pem1, pem2}, "\nIntermediate data\n") + "Terminating data" | ||
actual, err := parseCertificates(chain) | ||
assert.NoError(t, err) | ||
|
||
if !assert.Len(t, actual, 2) { | ||
return | ||
} | ||
assert.Equal(t, cert1[0], actual[0]) | ||
assert.Equal(t, cert2[0], actual[1]) | ||
} |