Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http_fs_test.go « httpfs « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7885dbf206df490dd38c3856972116ae4e87892f (plain)
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
package httpfs

import (
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"testing"
	"time"

	"gitlab.com/gitlab-org/gitlab-pages/internal/httptransport"

	"github.com/stretchr/testify/require"
)

func TestFSOpen(t *testing.T) {
	wd, err := os.Getwd()
	require.NoError(t, err)

	tests := map[string]struct {
		allowedPaths    []string
		chrootPath      string
		fileName        string
		expectedContent string
		expectedErrMsg  string
	}{
		"file_allowed_in_file_path": {
			allowedPaths:    []string{wd + "/testdata"},
			fileName:        wd + "/testdata/file1.txt",
			expectedContent: "file1.txt\n",
		},
		"file_allowed_in_file_path_subdir": {
			allowedPaths:    []string{wd + "/testdata"},
			fileName:        wd + "/testdata/subdir/file2.txt",
			expectedContent: "subdir/file2.txt\n",
		},
		"file_not_in_allowed_path": {
			allowedPaths:   []string{wd + "/testdata/subdir"},
			fileName:       wd + "/testdata/file1.txt",
			expectedErrMsg: os.ErrPermission.Error(),
		},
		"file_does_not_exist": {
			allowedPaths:   []string{wd + "/testdata"},
			fileName:       wd + "/testdata/unknown.txt",
			expectedErrMsg: "no such file or directory",
		},
		"relative_path_not_allowed": {
			allowedPaths:   []string{"testdata"},
			fileName:       "testdata/file1.txt",
			expectedErrMsg: os.ErrPermission.Error(),
		},
		"dot_dot_in_file_resolved": {
			allowedPaths:    []string{wd + "/testdata"},
			fileName:        wd + "/../httpfs/testdata/file1.txt",
			expectedContent: "file1.txt\n",
		},
		"dot_dot_in_file_resolved_not_allowed": {
			allowedPaths:   []string{wd + "/testdata/subdir"},
			fileName:       wd + "/../httpfs/testdata/file1.txt",
			expectedErrMsg: os.ErrPermission.Error(),
		},
		"chroot_path_not_found_when_not_in_real_chroot": {
			allowedPaths:   []string{wd + "/testdata"},
			fileName:       wd + "/testdata/file1.txt",
			expectedErrMsg: "no such file or directory",
			chrootPath:     wd + "/testdata",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			p, err := NewFileSystemPath(test.allowedPaths, test.chrootPath)
			require.NoError(t, err)

			got, err := p.Open(test.fileName)
			if test.expectedErrMsg != "" {
				require.Error(t, err)
				require.Contains(t, err.Error(), test.expectedErrMsg)
				return
			}

			require.NoError(t, err)

			content, err := ioutil.ReadAll(got)
			require.NoError(t, err)

			require.Equal(t, test.expectedContent, string(content))
		})
	}
}

func TestFileSystemPathCanServeHTTP(t *testing.T) {
	wd, err := os.Getwd()
	require.NoError(t, err)

	tests := map[string]struct {
		path               string
		chrootPath         string
		fileName           string
		escapeURL          bool
		expectedStatusCode int
		expectedContent    string
	}{
		"file_exists_in_path": {
			path:               wd + "/testdata",
			fileName:           "file1.txt",
			expectedStatusCode: http.StatusOK,
			expectedContent:    "file1.txt\n",
		},
		"file_exists_in_sub_dir_path": {
			path:               wd + "/testdata",
			fileName:           "subdir/file2.txt",
			expectedStatusCode: http.StatusOK,
			expectedContent:    "subdir/file2.txt\n",
		},
		"file_not_allowed_in_path": {
			path:               wd + "/testdata/subdir",
			fileName:           "../file1.txt",
			expectedStatusCode: http.StatusForbidden,
			expectedContent:    "403 Forbidden\n",
		},
		"file_does_not_exist": {
			path:               wd + "/testdata",
			fileName:           "unknown.txt",
			expectedStatusCode: http.StatusNotFound,
			expectedContent:    "404 page not found\n",
		},
		"escaped_url_is_invalid": {
			path:               wd + "/testdata",
			fileName:           "file1.txt",
			escapeURL:          true,
			expectedStatusCode: http.StatusForbidden,
			expectedContent:    "403 Forbidden\n",
		},
		"dot_dot_in_URL": {
			path:               wd + "/testdata",
			fileName:           "../testdata/file1.txt",
			expectedStatusCode: http.StatusOK,
			expectedContent:    "file1.txt\n",
		},
		"dot_dot_in_URL_outside_of_allowed_path": {
			path:               wd + "/testdata",
			fileName:           "../file1.txt",
			expectedStatusCode: http.StatusForbidden,
			expectedContent:    "403 Forbidden\n",
		},
		"chroot_path_fails_in_unit_test_not_found_when_not_in_real_chroot": {
			path:               wd + "/testdata",
			fileName:           "file1.txt",
			chrootPath:         wd + "/testdata",
			expectedStatusCode: http.StatusNotFound,
			expectedContent:    "404 page not found\n",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			transport := httptransport.NewTransport()
			fs, err := NewFileSystemPath([]string{test.path}, test.chrootPath)
			require.NoError(t, err)

			transport.RegisterProtocol("file", http.NewFileTransport(fs))

			client := &http.Client{
				Transport: transport,
				Timeout:   time.Second,
			}

			reqURL := "file://" + test.path + "/" + test.fileName
			if test.escapeURL {
				reqURL = url.PathEscape(reqURL)
			}

			req, err := http.NewRequest("GET", reqURL, nil)
			require.NoError(t, err)

			res, err := client.Do(req)
			require.NoError(t, err)
			defer res.Body.Close()

			require.Equal(t, test.expectedStatusCode, res.StatusCode)
			content, err := ioutil.ReadAll(res.Body)
			require.NoError(t, err)

			require.Equal(t, test.expectedContent, string(content))
		})
	}
}