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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
package contentprocessor
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/headers"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testData = "Hello world!"
func TestFailSetContentTypeAndDisposition(t *testing.T) {
testCaseBody := testData
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := io.WriteString(w, testCaseBody)
assert.NoError(t, err)
})
resp := makeRequest(t, h, testCaseBody, "")
defer func() { _ = resp.Body.Close() }()
require.Equal(t, "", resp.Header.Get(headers.ContentDispositionHeader))
require.Equal(t, "", resp.Header.Get(headers.ContentTypeHeader))
}
func TestSuccessSetContentTypeAndDispositionFeatureEnabled(t *testing.T) {
testCaseBody := testData
resp := makeRequest(t, nil, testCaseBody, "")
defer func() { _ = resp.Body.Close() }()
require.Equal(t, "inline", resp.Header.Get(headers.ContentDispositionHeader))
require.Equal(t, "text/plain; charset=utf-8", resp.Header.Get(headers.ContentTypeHeader))
}
func TestSetProperContentTypeAndDisposition(t *testing.T) {
testCases := []struct {
desc string
contentType string
contentDisposition string
body string
}{
{
desc: "text type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: testData,
},
{
desc: "HTML type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline; filename=blob",
body: "<html><body>Hello world!</body></html>",
},
{
desc: "Javascript within HTML type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline; filename=blob",
body: "<script>alert(\"foo\")</script>",
},
{
desc: "Javascript type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: "alert(\"foo\")",
},
{
desc: "Image type",
contentType: "image/png",
contentDisposition: "inline",
body: testhelper.LoadFile(t, "testdata/image.png"),
},
{
desc: "SVG type",
contentType: "image/svg+xml",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/image.svg"),
},
{
desc: "Partial SVG type",
contentType: "image/svg+xml",
contentDisposition: "attachment",
body: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 330 82\"><title>SVG logo combined with the W3C logo, set horizontally</title><desc>The logo combines three entities displayed horizontall</desc><metadata>",
},
{
desc: "Incomplete SVG start tag",
contentType: "image/svg+xml",
contentDisposition: "attachment",
body: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"",
},
{
desc: "Application type",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/file.pdf"),
},
{
desc: "Application type pdf with inline disposition",
contentType: "application/pdf",
contentDisposition: "inline",
body: testhelper.LoadFile(t, "testdata/file.pdf"),
},
{
desc: "Application executable type",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/file.swf"),
},
{
desc: "Video type",
contentType: "video/mp4",
contentDisposition: "inline",
body: testhelper.LoadFile(t, "testdata/video.mp4"),
},
{
desc: "Audio type",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/audio.mp3"),
},
{
desc: "JSON type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\" } } }",
},
{
desc: "Forged file with png extension but SWF content",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/forgedfile.png"),
},
{
desc: "BMPR file",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/file.bmpr"),
},
{
desc: "STL file",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/file.stl"),
},
{
desc: "RDoc file",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: testhelper.LoadFile(t, "testdata/file.rdoc"),
},
{
desc: "IPYNB file",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: testhelper.LoadFile(t, "testdata/file.ipynb"),
},
{
desc: "Sketch file",
contentType: "application/octet-stream",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/file.sketch"),
},
{
desc: "PDF file with non-ASCII characters in filename",
contentType: "application/octet-stream",
contentDisposition: `attachment; filename="file-ä.pdf"; filename*=UTF-8''file-%c3.pdf`,
body: testhelper.LoadFile(t, "testdata/file-ä.pdf"),
},
{
desc: "Microsoft Word file",
contentType: "application/octet-stream",
contentDisposition: `attachment`,
body: testhelper.LoadFile(t, "testdata/file.docx"),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
resp := makeRequest(t, nil, tc.body, tc.contentDisposition)
defer func() { _ = resp.Body.Close() }()
require.Equal(t, tc.contentType, resp.Header.Get(headers.ContentTypeHeader))
require.Equal(t, tc.contentDisposition, resp.Header.Get(headers.ContentDispositionHeader))
})
}
}
func TestFailOverrideContentType(t *testing.T) {
testCases := []struct {
desc string
overrideFromUpstream string
responseContentType string
body string
}{
{
desc: "Force text/html into text/plain",
responseContentType: "text/plain; charset=utf-8",
overrideFromUpstream: "text/html; charset=utf-8",
body: "<html><body>Hello world!</body></html>",
},
{
desc: "Force application/javascript into text/plain",
responseContentType: "text/plain; charset=utf-8",
overrideFromUpstream: "application/javascript; charset=utf-8",
body: "alert(1);",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// We are pretending to be upstream or an inner layer of the ResponseWriter chain
w.Header().Set(headers.GitlabWorkhorseDetectContentTypeHeader, "true")
w.Header().Set(headers.ContentTypeHeader, tc.overrideFromUpstream)
_, err := io.WriteString(w, tc.body)
assert.NoError(t, err)
})
resp := makeRequest(t, h, tc.body, "")
defer func() { _ = resp.Body.Close() }()
require.Equal(t, tc.responseContentType, resp.Header.Get(headers.ContentTypeHeader))
})
}
}
func TestSuccessOverrideContentDispositionFromInlineToAttachment(t *testing.T) {
testCaseBody := testData
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// We are pretending to be upstream or an inner layer of the ResponseWriter chain
w.Header().Set(headers.ContentDispositionHeader, "attachment")
w.Header().Set(headers.GitlabWorkhorseDetectContentTypeHeader, "true")
_, err := io.WriteString(w, testCaseBody)
assert.NoError(t, err)
})
resp := makeRequest(t, h, testCaseBody, "")
defer func() { _ = resp.Body.Close() }()
require.Equal(t, "attachment", resp.Header.Get(headers.ContentDispositionHeader))
}
func TestInlineContentDispositionForPdfFiles(t *testing.T) {
testCaseBody := testhelper.LoadFile(t, "testdata/file.pdf")
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// We are pretending to be upstream or an inner layer of the ResponseWriter chain
w.Header().Set(headers.ContentDispositionHeader, "inline")
w.Header().Set(headers.GitlabWorkhorseDetectContentTypeHeader, "true")
_, err := io.WriteString(w, testCaseBody)
assert.NoError(t, err)
})
resp := makeRequest(t, h, testCaseBody, "")
defer func() { _ = resp.Body.Close() }()
require.Equal(t, "inline", resp.Header.Get(headers.ContentDispositionHeader))
}
func TestFailOverrideContentDispositionFromAttachmentToInline(t *testing.T) {
testCaseBody := testhelper.LoadFile(t, "testdata/image.svg")
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// We are pretending to be upstream or an inner layer of the ResponseWriter chain
w.Header().Set(headers.ContentDispositionHeader, "inline")
w.Header().Set(headers.GitlabWorkhorseDetectContentTypeHeader, "true")
_, err := io.WriteString(w, testCaseBody)
assert.NoError(t, err)
})
resp := makeRequest(t, h, testCaseBody, "")
defer func() { _ = resp.Body.Close() }()
require.Equal(t, "attachment", resp.Header.Get(headers.ContentDispositionHeader))
}
func TestHeadersDelete(t *testing.T) {
for _, code := range []int{200, 400} {
recorder := httptest.NewRecorder()
rw := &contentDisposition{rw: recorder}
for _, name := range headers.ResponseHeaders {
rw.Header().Set(name, "foobar")
}
rw.WriteHeader(code)
for _, name := range headers.ResponseHeaders {
if header := recorder.Header().Get(name); header != "" {
t.Fatalf("HTTP %d response: expected header to be empty, found %q", code, name)
}
}
}
}
func TestWriteHeadersCalledOnce(t *testing.T) {
recorder := httptest.NewRecorder()
rw := &contentDisposition{rw: recorder}
rw.WriteHeader(400)
require.Equal(t, 400, rw.status)
require.True(t, rw.sentStatus)
rw.WriteHeader(200)
require.Equal(t, 400, rw.status)
}
func makeRequest(t *testing.T, handler http.HandlerFunc, body string, disposition string) *http.Response {
if handler == nil {
handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
// We are pretending to be upstream
w.Header().Set(headers.GitlabWorkhorseDetectContentTypeHeader, "true")
w.Header().Set(headers.ContentDispositionHeader, disposition)
_, err := io.WriteString(w, body)
assert.NoError(t, err)
})
}
req, _ := http.NewRequest("GET", "/", nil)
rw := httptest.NewRecorder()
SetContentHeaders(handler).ServeHTTP(rw, req)
resp := rw.Result()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, body, string(respBody))
return resp
}
|