Repository navigation
Expand file tree
/
Copy pathsource_test.go
More file actions
206 lines (191 loc) · 8.41 KB
/
Copy pathsource_test.go
File metadata and controls
206 lines (191 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package certs_test
import (
"bytes"
"context"
"fmt"
"os"
"strconv"
"strings"
"testing"
"github.com/git-pkgs/certs"
)
func sourceFixture(t testing.TB, name string) []byte {
t.Helper()
data, err := os.ReadFile("testdata/" + name)
if err != nil {
t.Fatal(err)
}
return data
}
func TestGeneratedSourceCertificates(t *testing.T) {
for _, tc := range []struct {
file, fingerprint, entryType, marker string
}{
{"node-roots.h", "1793927a0614549789adce2f8f34f7f0b66d0f3ae3a3b84d21ec15dbba4fadc7", "c-string-pem", `"-----BEGIN`},
{"rust-roots.rs", "018e13f0772532cf809bd1b17281867283fc48c6e13be9c69812854a490c1b05", "rust-byte-der", "CertificateDer::from_slice"},
} {
t.Run(tc.file, func(t *testing.T) {
data := sourceFixture(t, tc.file)
result := scan(t, "renamed.data", data, certs.Options{})
if !result.Complete || len(result.Certificates) != 1 || len(result.Collections) != 1 {
t.Fatalf("%+v", result)
}
collection := result.Collections[0]
occurrence := collection.Occurrences[0]
if collection.Format != "source" || collection.SHA256 != sha(data) || result.SHA256 != sha(data) {
t.Fatalf("file identity: %+v", collection)
}
if occurrence.SHA256 != tc.fingerprint || occurrence.EntryType != tc.entryType || occurrence.Offset == nil || *occurrence.Offset != int64(bytes.Index(data, []byte(tc.marker))) {
t.Fatalf("occurrence: %+v", occurrence)
}
})
}
}
func TestSourceCertificatesInRenamedArchiveMembers(t *testing.T) {
archive := zipArchive(t, member{"vendor/a.data", sourceFixture(t, "node-roots.h")}, member{"vendor/b.data", sourceFixture(t, "rust-roots.rs")})
result := scan(t, "package.zip", archive, certs.Options{})
if !result.Complete || len(result.Certificates) != 2 || len(result.Collections) != 2 {
t.Fatalf("%+v", result)
}
for i, collection := range result.Collections {
if len(collection.Location) != 2 || collection.Location[1].Entry != i+1 {
t.Fatal(collection.Location)
}
}
limited := scan(t, "package.zip", archive, certs.Options{MaxCertificates: 1})
if limited.Complete || len(limited.Certificates) != 1 || !hasDiagnostic(limited, "certificate_limit") {
t.Fatalf("limit: %+v", limited)
}
}
func TestSourceIgnoresCommentedAndQuotedConstructors(t *testing.T) {
rust := sourceFixture(t, "rust-roots.rs")
node := sourceFixture(t, "node-roots.h")
data := append([]byte("/* outer /* nested */\n"), rust...)
data = append(data, []byte("*/\n")...)
for _, line := range bytes.Split(node, []byte{'\n'}) {
data = append(data, append([]byte("// "), line...)...)
data = append(data, '\n')
}
data = append(data, []byte(`"CertificateDer::from_slice(b\"not a certificate\")"`)...)
result := scan(t, "notes.txt", data, certs.Options{})
if !result.Complete || len(result.Certificates) != 0 || len(result.Collections) != 0 {
t.Fatalf("%+v", result)
}
}
func TestMalformedSourceRecoversLaterCertificate(t *testing.T) {
valid := sourceFixture(t, "rust-roots.rs")
for _, bad := range []string{
`CertificateDer::from_slice(b"\xZZ"),`,
`CertificateDer::from_slice(b"\u1234"),`,
`CertificateDer::from_slice(b"\x30")`,
`CertificateDer::from_slice(b"\x30",`,
"CertificateDer::from_slice(b\"unterminated\n",
`"-----BEGIN CERTIFICATE-----\n" "invalid\q",`,
`"-----BEGIN CERTIFICATE-----\ninvalid\n-----END CERTIFICATE-----",`,
} {
t.Run(bad, func(t *testing.T) {
data := append([]byte(bad+"\n"), valid...)
result := scan(t, "renamed.data", data, certs.Options{})
if result.Complete || len(result.Certificates) != 1 || len(result.Collections) != 1 || len(result.Diagnostics) == 0 {
t.Fatalf("%+v", result)
}
})
}
}
func TestRustCertificateAPICallsAreNotEncodedCollections(t *testing.T) {
data := []byte(`CertificateDer::from_slice(cert); CertificateDer::from_slice(include_bytes!("roots.der"));`)
result := scan(t, "source.rs", data, certs.Options{})
if !result.Complete || len(result.Certificates) != 0 || len(result.Collections) != 0 {
t.Fatalf("%+v", result)
}
}
func TestSourceAndPlainPEMShareIdentity(t *testing.T) {
der := certificate(t, 1)
var source strings.Builder
source.WriteString("CertificateDer::from_slice(b\"")
for _, b := range der {
fmt.Fprintf(&source, `\x%02x`, b)
}
source.WriteString("\");\n")
data := append([]byte(source.String()), pemCertificate(der)...)
result := scan(t, "mixed.data", data, certs.Options{})
if !result.Complete || len(result.Certificates) != 1 || len(result.Collections) != 1 || len(result.Collections[0].Occurrences) != 2 {
t.Fatalf("%+v", result)
}
plain := scan(t, "root.pem", pemCertificate(der), certs.Options{})
if result.Collections[0].SetSHA256 != plain.Collections[0].SetSHA256 || result.Certificates[0].SPKISHA256 != plain.Certificates[0].SPKISHA256 {
t.Fatal("decoded identities differ")
}
}
func TestQuotedSourceRetainsLaterBlockAfterMalformedPEM(t *testing.T) {
bad := "-----BEGIN CERTIFICATE-----\ninvalid\n-----END CERTIFICATE-----\n"
good := string(pemCertificate(certificate(t, 1)))
data := []byte(strconv.Quote(bad) + "\n" + strconv.Quote(good))
result := scan(t, "renamed.data", data, certs.Options{})
if result.Complete || len(result.Certificates) != 1 || !hasDiagnostic(result, "malformed_certificate") {
t.Fatalf("%+v", result)
}
}
func TestUnsupportedCStringEscapeIsNotInterpretedAsRust(t *testing.T) {
data := []byte(`"-----BEGIN CERTIFICATE-----\n\x41B\n-----END CERTIFICATE-----"`)
result := scan(t, "renamed.data", data, certs.Options{})
if result.Complete || len(result.Certificates) != 0 || !hasDiagnostic(result, "unsupported_source") {
t.Fatalf("%+v", result)
}
}
func TestStaticTemplatePEMBundleInArchive(t *testing.T) {
first, second := certificate(t, 1), certificate(t, 2)
body := string(pemCertificate(first)) + strings.TrimSpace(string(pemCertificate(second)))
source := []byte("\"use strict\";\nconst RedisCloudCA = `" + body + "`;\nexports.default = {ca: RedisCloudCA};\n")
result := scan(t, "package.zip", zipArchive(t, member{"package/built/TLSProfiles.js", source}), certs.Options{})
if !result.Complete || len(result.Certificates) != 2 || len(result.Collections) != 1 || len(result.Diagnostics) != 0 {
t.Fatalf("%+v", result)
}
collection := result.Collections[0]
if collection.Format != "source" || collection.SHA256 != sha(source) || len(collection.Occurrences) != 2 {
t.Fatalf("collection: %+v", collection)
}
plain := scan(t, "bundle.pem", []byte(body), certs.Options{})
if collection.SetSHA256 != plain.Collections[0].SetSHA256 {
t.Fatal("template changed certificate identities")
}
for _, occurrence := range collection.Occurrences {
if occurrence.EntryType != "js-template-pem" || occurrence.Offset == nil || *occurrence.Offset != int64(bytes.IndexByte(source, '`')) {
t.Fatalf("occurrence: %+v", occurrence)
}
}
}
func TestUnsupportedTemplatesDoNotLeakPlainPEM(t *testing.T) {
good := string(pemCertificate(certificate(t, 1)))
for _, body := range []string{good + "${value}", "${value}" + good, good + `\n`, good + `\` + "`", strings.ReplaceAll(good, "\n", `\n`)} {
source := []byte("const ca = `" + body + "`;\n" + good)
result := scan(t, "source.js", source, certs.Options{})
if result.Complete || len(result.Certificates) != 1 || len(result.Collections) != 1 || len(result.Collections[0].Occurrences) != 1 || !hasDiagnostic(result, "unsupported_source") {
t.Fatalf("body %q: %+v", body, result)
}
}
result := scan(t, "source.js", []byte("const ca = `"+good), certs.Options{})
if result.Complete || len(result.Certificates) != 0 || !hasDiagnostic(result, "unsupported_source") {
t.Fatalf("unterminated: %+v", result)
}
}
func TestTemplatePEMAndPlainDuplicateKeepBothOccurrences(t *testing.T) {
good := string(pemCertificate(certificate(t, 1)))
data := []byte("const other = `unrelated text`;\nconst ca = `" + good + "`;\n" + good)
result := scan(t, "source.js", data, certs.Options{})
if !result.Complete || len(result.Certificates) != 1 || len(result.Collections) != 1 || len(result.Collections[0].Occurrences) != 2 {
t.Fatalf("%+v", result)
}
}
func FuzzSourceCertificates(f *testing.F) {
f.Add(sourceFixture(f, "node-roots.h"))
f.Add(sourceFixture(f, "rust-roots.rs"))
f.Add([]byte("const ca = `" + string(sourceFixture(f, "ec-acc.pem")) + "`;"))
f.Add([]byte(`CertificateDer::from_slice(b"\x`))
f.Fuzz(func(t *testing.T, data []byte) {
result, err := certs.Scan(context.Background(), "input", bytes.NewReader(data), certs.Options{MaxFileBytes: 1 << 20, MaxCertificates: 100, MaxDiagnostics: 10})
if err != nil || result == nil {
t.Fatalf("scan: %v", err)
}
})
}