| 1 | package lfshttp |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/git-lfs/git-lfs/v3/creds" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | ) |
| 14 | |
| 15 | var testCert = `-----BEGIN CERTIFICATE----- |
| 16 | MIIDyjCCArKgAwIBAgIJAMi9TouXnW+ZMA0GCSqGSIb3DQEBBQUAMEwxCzAJBgNV |
| 17 | BAYTAlVTMRMwEQYDVQQIEwpTb21lLVN0YXRlMRAwDgYDVQQKEwdnaXQtbGZzMRYw |
| 18 | FAYDVQQDEw1naXQtbGZzLmxvY2FsMB4XDTE2MDMwOTEwNTk1NFoXDTI2MDMwNzEw |
| 19 | NTk1NFowTDELMAkGA1UEBhMCVVMxEzARBgNVBAgTClNvbWUtU3RhdGUxEDAOBgNV |
| 20 | BAoTB2dpdC1sZnMxFjAUBgNVBAMTDWdpdC1sZnMubG9jYWwwggEiMA0GCSqGSIb3 |
| 21 | DQEBAQUAA4IBDwAwggEKAoIBAQCXmsI2w44nOsP7n3kL1Lz04U5FMZRErBSXLOE+ |
| 22 | dpd4tMpgrjOncJPD9NapHabsVIOnuVvMDuBbWYwU9PwbN4tjQzch8DRxBju6fCp/ |
| 23 | Pm+QF6p2Ga+NuSHWoVfNFuF2776aF9gSLC0rFnBekD3HCz+h6I5HFgHBvRjeVyAs |
| 24 | PRw471Y28Je609SoYugxaQNzRvahP0Qf43tE74/WN3FTGXy1+iU+uXpfp8KxnsuB |
| 25 | gfj+Wi6mPt8Q2utcA1j82dJ0K8ZbHSbllzmI+N/UuRLsbTUEdeFWYdZ0AlZNd/Vc |
| 26 | PlOSeoExwvOHIuUasT/cLIrEkdXNud2QLg2GpsB6fJi3NEUhAgMBAAGjga4wgasw |
| 27 | HQYDVR0OBBYEFC8oVPRQbekTwfkntgdL7PADXNDbMHwGA1UdIwR1MHOAFC8oVPRQ |
| 28 | bekTwfkntgdL7PADXNDboVCkTjBMMQswCQYDVQQGEwJVUzETMBEGA1UECBMKU29t |
| 29 | ZS1TdGF0ZTEQMA4GA1UEChMHZ2l0LWxmczEWMBQGA1UEAxMNZ2l0LWxmcy5sb2Nh |
| 30 | bIIJAMi9TouXnW+ZMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBACIl |
| 31 | /CBLIhC3drrYme4cGArhWyXIyRpMoy9Z+9Dru8rSuOr/RXR6sbYhlE1iMGg4GsP8 |
| 32 | 4Cj7aIct6Vb9NFv5bGNyFJAmDesm3SZlEcWxU3YBzNPiJXGiUpQHCkp0BH+gvsXc |
| 33 | tb58XoiDZPVqrl0jNfX/nHpHR9c3DaI3Tjx0F/No0ZM6mLQ1cNMikFyEWQ4U0zmW |
| 34 | LvV+vvKuOixRqbcVnB5iTxqMwFG0X3tUql0cftGBgoCoR1+FSBOs0EXLODCck6ql |
| 35 | aW6vZwkA+ccj/pDTx8LBe2lnpatrFeIt6znAUJW3G8r6SFHKVBWHwmESZS4kxhjx |
| 36 | NpW5Hh0w4/5iIetCkJ0= |
| 37 | -----END CERTIFICATE-----` |
| 38 | |
| 39 | var sslCAInfoConfigHostNames = []string{ |
| 40 | "git-lfs.local", |
| 41 | "git-lfs.local/", |
| 42 | } |
| 43 | var sslCAInfoMatchedHostTests = []struct { |
| 44 | hostName string |
| 45 | shouldMatch bool |
| 46 | }{ |
| 47 | {"git-lfs.local", true}, |
| 48 | {"git-lfs.local:8443", false}, |
| 49 | {"wronghost.com", false}, |
| 50 | } |
| 51 | |
| 52 | func clientForHost(c *Client, host string) *http.Client { |
| 53 | u, _ := url.Parse(fmt.Sprintf("https://%v", host)) |
| 54 | client, _ := c.HttpClient(u, creds.BasicAccess) |
| 55 | return client |
| 56 | } |
| 57 | |
| 58 | func TestCertFromSSLCAInfoConfig(t *testing.T) { |
| 59 | tempfile, err := os.CreateTemp("", "testcert") |
| 60 | assert.Nil(t, err, "Error creating temp cert file") |
| 61 | defer os.Remove(tempfile.Name()) |
| 62 | |
| 63 | _, err = tempfile.WriteString(testCert) |
| 64 | assert.Nil(t, err, "Error writing temp cert file") |
| 65 | tempfile.Close() |
| 66 | |
| 67 | // Test http.<url>.sslcainfo |
| 68 | for _, hostName := range sslCAInfoConfigHostNames { |
| 69 | hostKey := fmt.Sprintf("http.https://%v.sslcainfo", hostName) |
| 70 | c := NewClient(NewContext(nil, nil, map[string]string{ |
| 71 | hostKey: tempfile.Name(), |
| 72 | })) |
| 73 | |
| 74 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 75 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 76 | |
| 77 | var shouldOrShouldnt string |
| 78 | if matchedHostTest.shouldMatch { |
| 79 | shouldOrShouldnt = "should" |
| 80 | } else { |
| 81 | shouldOrShouldnt = "should not" |
| 82 | } |
| 83 | |
| 84 | assert.Equal(t, matchedHostTest.shouldMatch, pool != nil, |
| 85 | "Cert lookup for \"%v\" %v have succeeded with \"%v\"", |
| 86 | matchedHostTest.hostName, shouldOrShouldnt, hostKey) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Test http.sslcainfo |
| 91 | c := NewClient(NewContext(nil, nil, map[string]string{ |
| 92 | "http.sslcainfo": tempfile.Name(), |
| 93 | })) |
| 94 | |
| 95 | // Should match any host at all |
| 96 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 97 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 98 | assert.NotNil(t, pool) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestCertFromSSLCAInfoEnv(t *testing.T) { |
| 103 | tempfile, err := os.CreateTemp("", "testcert") |
| 104 | assert.Nil(t, err, "Error creating temp cert file") |
| 105 | defer os.Remove(tempfile.Name()) |
| 106 | |
| 107 | _, err = tempfile.WriteString(testCert) |
| 108 | assert.Nil(t, err, "Error writing temp cert file") |
| 109 | tempfile.Close() |
| 110 | |
| 111 | c := NewClient(NewContext(nil, map[string]string{ |
| 112 | "GIT_SSL_CAINFO": tempfile.Name(), |
| 113 | }, nil)) |
| 114 | |
| 115 | // Should match any host at all |
| 116 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 117 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 118 | assert.NotNil(t, pool) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestCertFromSSLCAInfoEnvIsIgnoredForSchannelBackend(t *testing.T) { |
| 123 | tempfile, err := os.CreateTemp("", "testcert") |
| 124 | assert.Nil(t, err, "Error creating temp cert file") |
| 125 | defer os.Remove(tempfile.Name()) |
| 126 | |
| 127 | _, err = tempfile.WriteString(testCert) |
| 128 | assert.Nil(t, err, "Error writing temp cert file") |
| 129 | tempfile.Close() |
| 130 | |
| 131 | c := NewClient(NewContext(nil, map[string]string{ |
| 132 | "GIT_SSL_CAINFO": tempfile.Name(), |
| 133 | }, map[string]string{ |
| 134 | "http.sslbackend": "schannel", |
| 135 | })) |
| 136 | |
| 137 | // Should match any host at all |
| 138 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 139 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 140 | assert.Nil(t, pool) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestCertFromSSLCAInfoEnvWithSchannelBackend(t *testing.T) { |
| 145 | tempfile, err := os.CreateTemp("", "testcert") |
| 146 | assert.Nil(t, err, "Error creating temp cert file") |
| 147 | defer os.Remove(tempfile.Name()) |
| 148 | |
| 149 | _, err = tempfile.WriteString(testCert) |
| 150 | assert.Nil(t, err, "Error writing temp cert file") |
| 151 | tempfile.Close() |
| 152 | |
| 153 | c := NewClient(NewContext(nil, map[string]string{ |
| 154 | "GIT_SSL_CAINFO": tempfile.Name(), |
| 155 | }, map[string]string{ |
| 156 | "http.sslbackend": "schannel", |
| 157 | "http.schannelusesslcainfo": "1", |
| 158 | })) |
| 159 | |
| 160 | // Should match any host at all |
| 161 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 162 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 163 | assert.NotNil(t, pool) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestCertFromSSLCAPathConfig(t *testing.T) { |
| 168 | tempdir := t.TempDir() |
| 169 | |
| 170 | err := os.WriteFile(filepath.Join(tempdir, "cert1.pem"), []byte(testCert), 0644) |
| 171 | assert.Nil(t, err, "Error creating cert file") |
| 172 | |
| 173 | c := NewClient(NewContext(nil, nil, map[string]string{ |
| 174 | "http.sslcapath": tempdir, |
| 175 | })) |
| 176 | |
| 177 | // Should match any host at all |
| 178 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 179 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 180 | assert.NotNil(t, pool) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func TestCertFromSSLCAPathEnv(t *testing.T) { |
| 185 | tempdir := t.TempDir() |
| 186 | |
| 187 | err := os.WriteFile(filepath.Join(tempdir, "cert1.pem"), []byte(testCert), 0644) |
| 188 | assert.Nil(t, err, "Error creating cert file") |
| 189 | |
| 190 | c := NewClient(NewContext(nil, map[string]string{ |
| 191 | "GIT_SSL_CAPATH": tempdir, |
| 192 | }, nil)) |
| 193 | |
| 194 | // Should match any host at all |
| 195 | for _, matchedHostTest := range sslCAInfoMatchedHostTests { |
| 196 | pool := getRootCAsForHostFromGitconfig(c, matchedHostTest.hostName) |
| 197 | assert.NotNil(t, pool) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestCertVerifyDisabledGlobalEnv(t *testing.T) { |
| 202 | empty := NewClient(nil) |
| 203 | httpClient := clientForHost(empty, "anyhost.com") |
| 204 | tr, ok := httpClient.Transport.(*http.Transport) |
| 205 | if assert.True(t, ok) { |
| 206 | assert.False(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 207 | } |
| 208 | |
| 209 | c := NewClient(NewContext(nil, map[string]string{ |
| 210 | "GIT_SSL_NO_VERIFY": "1", |
| 211 | }, nil)) |
| 212 | |
| 213 | httpClient = clientForHost(c, "anyhost.com") |
| 214 | tr, ok = httpClient.Transport.(*http.Transport) |
| 215 | if assert.True(t, ok) { |
| 216 | assert.True(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestCertVerifyDisabledGlobalConfig(t *testing.T) { |
| 221 | def := NewClient(nil) |
| 222 | httpClient := clientForHost(def, "anyhost.com") |
| 223 | tr, ok := httpClient.Transport.(*http.Transport) |
| 224 | if assert.True(t, ok) { |
| 225 | assert.False(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 226 | } |
| 227 | |
| 228 | c := NewClient(NewContext(nil, nil, map[string]string{ |
| 229 | "http.sslverify": "false", |
| 230 | })) |
| 231 | |
| 232 | httpClient = clientForHost(c, "anyhost.com") |
| 233 | tr, ok = httpClient.Transport.(*http.Transport) |
| 234 | if assert.True(t, ok) { |
| 235 | assert.True(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func TestCertVerifyDisabledHostConfig(t *testing.T) { |
| 240 | def := NewClient(nil) |
| 241 | httpClient := clientForHost(def, "specifichost.com") |
| 242 | tr, ok := httpClient.Transport.(*http.Transport) |
| 243 | if assert.True(t, ok) { |
| 244 | assert.False(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 245 | } |
| 246 | |
| 247 | httpClient = clientForHost(def, "otherhost.com") |
| 248 | tr, ok = httpClient.Transport.(*http.Transport) |
| 249 | if assert.True(t, ok) { |
| 250 | assert.False(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 251 | } |
| 252 | |
| 253 | c := NewClient(NewContext(nil, nil, map[string]string{ |
| 254 | "http.https://specifichost.com/.sslverify": "false", |
| 255 | })) |
| 256 | |
| 257 | httpClient = clientForHost(c, "specifichost.com") |
| 258 | tr, ok = httpClient.Transport.(*http.Transport) |
| 259 | if assert.True(t, ok) { |
| 260 | assert.True(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 261 | } |
| 262 | |
| 263 | httpClient = clientForHost(c, "otherhost.com") |
| 264 | tr, ok = httpClient.Transport.(*http.Transport) |
| 265 | if assert.True(t, ok) { |
| 266 | assert.False(t, tr.TLSClientConfig.InsecureSkipVerify) |
| 267 | } |
| 268 | } |