Validate info refs advertisement type · Entire

Validate info refs advertisement type

de0e626→main·

pjbgf·2mo ago·2 files·+81 added/-0 removed

Changes

2

5 unmodified lines

6
7
8
9
10
11
12
110 unmodified lines

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

5 unmodified lines

"crypto/tls"
    "fmt"
    "io"
    "mime"
    "net/http"
    "net/url"

110 unmodified lines

if err := httpError(res); err != nil {
        return nil, err
    }
    wantContentType := fmt.Sprintf("application/x-%s-advertisement", service)
    gotContentType := res.Header.Get("Content-Type")
    gotMediaType := gotContentType
    if gotContentType != "" {
        if mediaType, _, err := mime.ParseMediaType(gotContentType); err == nil {
            gotMediaType = mediaType
        }
    }
    if gotMediaType != wantContentType {
        return nil, fmt.Errorf("unexpected info/refs content-type %q, want %q", gotContentType, wantContentType)
    }
    if conn.FollowInfoRefsRedirect && res.Request != nil && res.Request.URL != nil {
        final := res.Request.URL
        if final.Host != conn.Endpoint.Host || final.Scheme != conn.Endpoint.Scheme {

Minternal/gitproto/smarthttp.go+12

128 unmodified lines

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

128 unmodified lines

}
}

func TestRequestInfoRefsRequiresAdvertisementContentType(t *testing.T) {
    tests := []struct {
        name        string
        contentType string
        wantErr     bool
    }{
        {
            name:    "missing content type",
            wantErr: true,
        },
        {
            name:        "wrong content type",
            contentType: "text/plain",
            wantErr:     true,
        },
        {
            name:        "wrong service advertisement",
            contentType: "application/x-git-receive-pack-advertisement",
            wantErr:     true,
        },
        {
            name:        "expected content type",
            contentType: "application/x-git-upload-pack-advertisement",
        },
        {
            name:        "expected content type with parameter",
            contentType: "application/x-git-upload-pack-advertisement; charset=utf-8",
        },
    }

for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            ep, err := transport.ParseURL("https://example.com/repo.git")
            if err != nil {
                t.Fatalf("parse endpoint: %v", err)
            }
            conn := NewConn(ep, "source", nil, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
                res := &http.Response{
                    StatusCode: http.StatusOK,
                    Request:    req,
                    Body:       io.NopCloser(strings.NewReader("0000")),
                    Header:     make(http.Header),
                }
                if tt.contentType != "" {
                    res.Header.Set("Content-Type", tt.contentType)
                }
                return res, nil
            }))

body, err := RequestInfoRefs(t.Context(), conn, transport.UploadPackService, "")
            if tt.wantErr {
                if err == nil {
                    t.Fatal("expected content type error")
                }
                if !strings.Contains(err.Error(), "unexpected info/refs content-type") {
                    t.Fatalf("error = %v, want content type error", err)
                }
                return
            }
            if err != nil {
                t.Fatalf("RequestInfoRefs: %v", err)
            }
            if got, want := string(body), "0000"; got != want {
                t.Fatalf("body = %q, want %q", got, want)
            }
        })
    }
}

func TestPostRPCStreamContextCanceled(t *testing.T) {
    started := make(chan struct{}, 1)
    ep, err := transport.ParseURL("https://example.com/repo.git")