@@ -14,6 +14,7 @@ import (
1414 "math/big"
1515 "net/http"
1616 "net/http/httptest"
17+ "strings"
1718 "testing"
1819 "time"
1920
@@ -248,6 +249,76 @@ func TestFailBackupArtifact(t *testing.T) {
248249 })
249250}
250251
252+ // unrelatedCAPEM mints a CA that signed nothing an httptest server serves:
253+ // every httptest server shares one certificate.
254+ func unrelatedCAPEM (t * testing.T ) []byte {
255+ t .Helper ()
256+ key , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
257+ if err != nil {
258+ t .Fatal (err )
259+ }
260+ tmpl := & x509.Certificate {SerialNumber : big .NewInt (1 ), Subject : pkix.Name {CommonName : "unrelated CA" }, NotBefore : time .Now (), NotAfter : time .Now ().Add (time .Hour ), IsCA : true , BasicConstraintsValid : true }
261+ der , err := x509 .CreateCertificate (rand .Reader , tmpl , tmpl , & key .PublicKey , key )
262+ if err != nil {
263+ t .Fatal (err )
264+ }
265+ return pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : der })
266+ }
267+
268+ // TestMinioObjectLister_EnforcesTheCA pins that a configured CA is the
269+ // trust the client verifies with, whether alone or on top of the system
270+ // roots: an endpoint whose certificate it did not sign is refused.
271+ func TestMinioObjectLister_EnforcesTheCA (t * testing.T ) {
272+ srv := httptest .NewTLSServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
273+ w .Header ().Set ("Content-Length" , "1" )
274+ w .Header ().Set ("ETag" , `"abc"` )
275+ w .Header ().Set ("Last-Modified" , "Wed, 21 Oct 2026 10:00:00 GMT" )
276+ }))
277+ defer srv .Close ()
278+ // minio-go retries a TLS failure with backoff; a refusal needs no more
279+ // than the first attempt.
280+ stat := func (ca []byte , caOnly bool ) error {
281+ l , err := newMinioObjectLister (s3Connection {
282+ Endpoint : strings .TrimPrefix (srv .URL , "https://" ), Secure : true ,
283+ AccessKey : "AKID" , SecretKey : "secret" , Region : "us-east-1" ,
284+ CAPEM : ca , CAOnly : caOnly ,
285+ })
286+ if err != nil {
287+ t .Fatalf ("newMinioObjectLister: %v" , err )
288+ }
289+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
290+ defer cancel ()
291+ _ , err = l .StatObject (ctx , "cozy-backups" , "k" )
292+ return err
293+ }
294+ signer := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : srv .Certificate ().Raw })
295+ for _ , caOnly := range []bool {true , false } {
296+ if err := stat (signer , caOnly ); err != nil {
297+ t .Errorf ("CAOnly=%v: the CA that signed the endpoint: %v" , caOnly , err )
298+ }
299+ if err := stat (unrelatedCAPEM (t ), caOnly ); err == nil {
300+ t .Errorf ("CAOnly=%v: StatObject succeeded against a certificate the CA did not sign, want a TLS error" , caOnly )
301+ }
302+ }
303+ }
304+
305+ // TestMinioObjectLister_StatWithoutSize pins that a HEAD answer without
306+ // Content-Length, which minio-go turns into size -1, is not a size.
307+ func TestMinioObjectLister_StatWithoutSize (t * testing.T ) {
308+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
309+ w .Header ().Set ("ETag" , `"abc"` )
310+ w .Header ().Set ("Last-Modified" , "Wed, 21 Oct 2026 10:00:00 GMT" )
311+ }))
312+ defer srv .Close ()
313+ l , err := newMinioObjectLister (s3Connection {Endpoint : strings .TrimPrefix (srv .URL , "http://" ), AccessKey : "AKID" , SecretKey : "secret" , Region : "us-east-1" })
314+ if err != nil {
315+ t .Fatalf ("newMinioObjectLister: %v" , err )
316+ }
317+ if size , err := l .StatObject (context .Background (), "cozy-backups" , "k" ); err == nil {
318+ t .Errorf ("StatObject = %d, nil; want an error for a response without Content-Length" , size )
319+ }
320+ }
321+
251322// TestEndpointTrustPool_CAOnly tells apart "the CA on top of the system
252323// roots" and "the CA alone". The system pool holds the endpoint's
253324// certificate, and the configured CA is an unrelated one, so the endpoint
@@ -258,18 +329,7 @@ func TestEndpointTrustPool_CAOnly(t *testing.T) {
258329 system := x509 .NewCertPool ()
259330 system .AddCert (endpoint .Certificate ())
260331
261- // Every httptest server shares one certificate, so the unrelated CA is
262- // minted here.
263- key , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
264- if err != nil {
265- t .Fatal (err )
266- }
267- tmpl := & x509.Certificate {SerialNumber : big .NewInt (1 ), Subject : pkix.Name {CommonName : "unrelated CA" }, NotBefore : time .Now (), NotAfter : time .Now ().Add (time .Hour ), IsCA : true , BasicConstraintsValid : true }
268- der , err := x509 .CreateCertificate (rand .Reader , tmpl , tmpl , & key .PublicKey , key )
269- if err != nil {
270- t .Fatal (err )
271- }
272- unrelatedCA := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : der })
332+ unrelatedCA := unrelatedCAPEM (t )
273333
274334 verifies := func (caOnly bool ) bool {
275335 pool , err := endpointTrustPool (s3Connection {CAPEM : unrelatedCA , CAOnly : caOnly }, system .Clone ())
0 commit comments