-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsubdomain_gateway_ipfs_test.go
364 lines (344 loc) · 13.6 KB
/
subdomain_gateway_ipfs_test.go
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package tests
import (
"net/url"
"testing"
"github.com/ipfs/gateway-conformance/tooling"
"github.com/ipfs/gateway-conformance/tooling/car"
. "github.com/ipfs/gateway-conformance/tooling/check"
"github.com/ipfs/gateway-conformance/tooling/helpers"
"github.com/ipfs/gateway-conformance/tooling/specs"
. "github.com/ipfs/gateway-conformance/tooling/test"
)
func TestUnixFSDirectoryListingOnSubdomainGateway(t *testing.T) {
tooling.LogTestGroup(t, GroupUnixFS)
fixture := car.MustOpenUnixfsCar("dir_listing/fixtures.car")
root := fixture.MustGetNode()
file := fixture.MustGetNode("ą", "ę", "file-źł.txt")
// We're going to run the same test against multiple gateways (localhost, and a subdomain gateway)
gatewayURLs := []string{
SubdomainGatewayURL,
SubdomainLocalhostGatewayURL,
}
tests := SugarTests{}
for _, gatewayURL := range gatewayURLs {
u, err := url.Parse(gatewayURL)
if err != nil {
t.Fatal(err)
}
tests = append(tests, SugarTests{
{
Name: "backlink on root CID should be hidden (TODO: cleanup Kubo-specifics)",
Request: Request().
URL(
"{{scheme}}://{{cid}}.ipfs.{{host}}/",
u.Scheme,
root.Cid(),
u.Host,
),
Response: Expect().
BodyWithHint("backlink on root CID should be hidden",
And(
Contains("Index of"),
Not(Contains(`<a href="/">..</a>`)),
)),
},
{
Name: "redirect dir listing to URL with trailing slash",
Request: Request().
URL(
"{{scheme}}://{{cid}}.ipfs.{{host}}/ą/ę",
u.Scheme,
root.Cid(),
u.Host,
),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals(`/%c4%85/%c4%99/`),
),
},
{
Name: "Regular dir listing HTML (TODO: cleanup Kubo-specifics)",
Request: Request().URL(
"{{scheme}}://{{cid}}.ipfs.{{host}}/ą/ę/",
u.Scheme,
root.Cid(),
u.Host,
),
Response: Expect().
Headers(
Header("Etag").Contains(`"DirIndex-`),
).BodyWithHint(`
- backlink on subdirectory should point at parent directory (TODO: kubo-specific)
- breadcrumbs should leverage path-based router mounted on the parent domain (TODO: kubo-specific)
- name column should be a link to content root mounted at subdomain origin
`,
And(
Contains("Index of"),
Contains(
`<a href="/%C4%85/%C4%99/..">..</a>`,
),
Contains(
`/ipfs/<a href="//{{host}}/ipfs/{{cid}}">{{cid}}</a>/<a href="//{{host}}/ipfs/{{cid}}/%C4%85">ą</a>/<a href="//{{host}}/ipfs/{{cid}}/%C4%85/%C4%99">ę</a>`,
u.Host, // We don't have a subdomain here which prevents issues with normalization and cidv0
root.Cid(),
),
Contains(
`<a href="/%C4%85/%C4%99/file-%C5%BA%C5%82.txt">file-źł.txt</a>`,
),
Contains(
`<a class="ipfs-hash" translate="no" href="//{{host}}/ipfs/{{cid}}?filename=file-%25C5%25BA%25C5%2582.txt">`,
u.Host, // We don't have a subdomain here which prevents issues with normalization and cidv0
file.Cid(),
),
)),
},
}...)
}
RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, tests), specs.SubdomainGatewayIPFS)
}
func TestGatewaySubdomains(t *testing.T) {
tooling.LogTestGroup(t, GroupSubdomains)
fixture := car.MustOpenUnixfsCar("subdomain_gateway/fixtures.car")
CIDVal := string(fixture.MustGetRawData("hello-CIDv1")) // hello
DirCID := fixture.MustGetCid("testdirlisting")
CIDv1 := fixture.MustGetCid("hello-CIDv1")
CIDv0 := fixture.MustGetCid("hello-CIDv0")
CIDv0to1 := fixture.MustGetCid("hello-CIDv0to1")
CIDv1_TOO_LONG := fixture.MustGetCid("hello-CIDv1_TOO_LONG")
CIDWikipedia := "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco"
dirWithPercentEncodedFilename := car.MustOpenUnixfsCar("path_gateway_unixfs/dir-with-percent-encoded-filename.car")
dirWithPercentEncodedFilenameCID := dirWithPercentEncodedFilename.MustGetCid()
tests := SugarTests{}
// We're going to run the same test against multiple gateways (localhost, and a subdomain gateway)
gatewayURLs := []string{
SubdomainGatewayURL,
SubdomainLocalhostGatewayURL,
}
for _, gatewayURL := range gatewayURLs {
u, err := url.Parse(gatewayURL)
if err != nil {
t.Fatal(err)
}
tests = append(tests, SugarTests{
{
Name: "request for example.com/ipfs/{CIDv1} redirects to subdomain",
Hint: `
subdomains should not return payload directly,
but redirect to URL with proper origin isolation
`,
Request: Request().URL("{{url}}/ipfs/{{cid}}/", gatewayURL, CIDv1),
Response: Expect().
Status(301).
Headers(
Header("Location").
Hint("request for example.com/ipfs/{CIDv1} returns Location HTTP header for subdomain redirect in browsers").
Contains("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv1, u.Host),
),
},
{
Name: "request for example.com/ipfs/{CIDv1}/{filename with percent encoding} redirects to subdomain",
Request: Request().URL("{{url}}/ipfs/{{cid}}/Portugal%252C+España=Peninsula%20Ibérica.txt", gatewayURL, dirWithPercentEncodedFilenameCID),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("{{scheme}}://{{cid}}.ipfs.{{host}}/Portugal%252C+Espa%C3%B1a=Peninsula%20Ib%C3%A9rica.txt", u.Scheme, dirWithPercentEncodedFilenameCID, u.Host),
),
},
{
Name: "request for example.com/ipfs/{DirCID} redirects to subdomain",
Hint: `
subdomains should not return payload directly,
but redirect to URL with proper origin isolation
`,
Request: Request().URL("{{url}}/ipfs/{{cid}}/", gatewayURL, DirCID),
Response: Expect().
Status(301).
Headers(
Header("Location").
Hint("request for example.com/ipfs/{DirCID} returns Location HTTP header for subdomain redirect in browsers").
Contains("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, DirCID, u.Host),
),
},
{
Name: "request for example.com/ipfs/{CIDv0} redirects to CIDv1 representation in subdomain",
Request: Request().URL("{{url}}/ipfs/{{cid}}/", gatewayURL, CIDv0),
Response: Expect().
Status(301).
Headers(
Header("Location").
Hint("request for example.com/ipfs/{CIDv0to1} returns Location HTTP header for subdomain redirect in browsers").
Contains("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv0to1, u.Host),
),
},
{
Name: "request for {CID}.ipfs.example.com should return expected payload",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}", u.Scheme, CIDv1, u.Host),
Response: Expect().
Status(200).
Body(Contains(CIDVal)),
},
{
Name: "request for {CID}.ipfs.example.com/ipfs/{CID} should return HTTP 404",
Hint: "ensure /ipfs/ namespace is not mounted on subdomain",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/ipfs/{{cid}}", u.Scheme, CIDv1, u.Host),
Response: Expect().
Status(404),
},
{
Name: "request for {CID}.ipfs.example.com/ipfs/file.txt should return data from a file in CID content root",
Hint: "ensure requests to /ipfs/* are not blocked, if content root has such subdirectory",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/ipfs/file.txt", u.Scheme, DirCID, u.Host),
Response: Expect().
Status(200).
Body(Contains("I am a txt file")),
},
{
Name: "valid file and subdirectory paths in directory listing at {cid}.ipfs.example.com",
Hint: "{CID}.ipfs.example.com/sub/dir (Directory Listing)",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, DirCID, u.Host),
Response: Expect().
Status(200).
Body(And(
// TODO: implement html expectations
Contains(`<a href="/hello">hello</a>`),
Contains(`<a href="/ipfs">ipfs</a>`),
)),
},
{
Name: "valid parent directory path in directory listing at {cid}.ipfs.example.com/sub/dir",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/ipfs/ipns/", u.Scheme, DirCID, u.Host),
Response: Expect().
Status(200).
Body(And(
// TODO: implement html expectations
Contains(`<a href="/ipfs/ipns/..">..</a>`),
Contains(`<a href="/ipfs/ipns/bar">bar</a>`),
)),
},
{
Name: "request for deep path resource at {cid}.ipfs.example.com/sub/dir/file",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/ipfs/ipns/bar", u.Scheme, DirCID, u.Host),
Response: Expect().
Status(200).
Body(Contains("text-file-content")),
},
{
Name: "valid breadcrumb links in the header of directory listing at {cid}.ipfs.example.com/sub/dir (TODO: cleanup Kubo-specifics)",
Hint: `
Note 1: we test for sneaky subdir names {cid}.ipfs.example.com/ipfs/ipns/ :^)
Note 2: example.com/ipfs/.. present in HTML will be redirected to subdomain, so this is expected behavior
`,
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/ipfs/ipns/", u.Scheme, DirCID, u.Host),
Response: Expect().
Status(200).
Body(
And(
Contains("Index of"),
Contains(`/ipfs/<a href="//{{host}}/ipfs/{{cid}}">{{cid}}</a>/<a href="//{{host}}/ipfs/{{cid}}/ipfs">ipfs</a>/<a href="//{{host}}/ipfs/{{cid}}/ipfs/ipns">ipns</a>`,
u.Host, DirCID),
),
),
},
{
Name: "request for example.com/ipfs/{CIDv1} produces redirect to {CIDv1}.ipfs.example.com",
Hint: "path requests to the root hostname should redirect to a subdomain URL with proper origin isolation",
Request: Request().URL("{{scheme}}://{{host}}/ipfs/{{cid}}/", u.Scheme, u.Host, CIDv1),
Response: Expect().
Headers(
Header("Location").Equals("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv1, u.Host),
),
},
{
Name: "request for example.com/ipfs/{InvalidCID} produces useful error before redirect",
Hint: "error message should include original CID (and it should be case-sensitive, as we can't assume everyone uses base32)",
Request: Request().URL("{{scheme}}://{{host}}/ipfs/QmInvalidCID", u.Scheme, u.Host),
Response: Expect().
Body(Contains(`invalid path "/ipfs/QmInvalidCID"`)),
},
{
Name: "request for example.com/ipfs/{CIDv0} produces redirect to {CIDv1}.ipfs.example.com",
Request: Request().URL("{{scheme}}://{{host}}/ipfs/{{cid}}/", u.Scheme, u.Host, CIDv0),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv0to1, u.Host),
),
},
{
Name: "request for http://example.com/ipfs/{CID} with X-Forwarded-Proto: https produces redirect to HTTPS URL",
Hint: "Support X-Forwarded-Proto",
Request: Request().URL("{{scheme}}://{{host}}/ipfs/{{cid}}/", u.Scheme, u.Host, CIDv1).
Header("X-Forwarded-Proto", "https"),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("https://{{cid}}.ipfs.{{host}}/", CIDv1, u.Host),
),
},
{
Name: "request for example.com/ipfs/?uri=ipfs%3A%2F%2F.. produces redirect to /ipfs/.. content path",
Hint: "Support ipfs:// in https://developer.mozilla.org/en-US/docs/Web/API/Navigator/registerProtocolHandler",
Request: Request().URL("{{scheme}}://{{host}}/ipfs/", u.Scheme, u.Host).
Query(
"uri", "ipfs://{{host}}/wiki/Diego_Maradona.html", CIDWikipedia,
),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("/ipfs/{{cid}}/wiki/Diego_Maradona.html", CIDWikipedia),
),
},
{
Name: "request for a too long CID at example.com/ipfs/{CIDv1} returns human readable error",
Hint: "router should not redirect to hostnames that could fail due to DNS limits",
Request: Request().URL("{{url}}/ipfs/{{cid}}", gatewayURL, CIDv1_TOO_LONG),
Response: Expect().
Status(400).
Body(Contains("CID incompatible with DNS label length limit of 63")),
},
{
Name: "request for a too long CID at {CIDv1}.ipfs.example.com returns expected payload",
Hint: "direct request should also fail (provides the same UX as router and avoids confusion)",
Request: Request().URL("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv1_TOO_LONG, u.Host),
Response: Expect().
Status(400).
Body(Contains("CID incompatible with DNS label length limit of 63")),
},
// ## ============================================================================
// ## Test support for X-Forwarded-Host
// ## ============================================================================
{
Name: "request for http://fake.domain.com/ipfs/{CID} doesn't match the example.com gateway",
Request: Request().
URL("{{scheme}}://{{domain}}/ipfs/{{cid}}", u.Scheme, "fake.domain.com", CIDv1),
Response: Expect().
Status(200),
},
{
Name: "request for http://fake.domain.com/ipfs/{CID} with X-Forwarded-Host: example.com match the example.com gateway",
Request: Request().
URL("{{scheme}}://{{domain}}/ipfs/{{cid}}", u.Scheme, "fake.domain.com", CIDv1).
Header("X-Forwarded-Host", u.Host),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv1, u.Host),
),
},
{
Name: "request for http://fake.domain.com/ipfs/{CID} with X-Forwarded-Host: example.com and X-Forwarded-Proto: https match the example.com gateway, redirect with https",
Request: Request().
URL("{{scheme}}://{{domain}}/ipfs/{{cid}}", u.Scheme, "fake.domain.com", CIDv1).
Header("X-Forwarded-Host", u.Host).
Header("X-Forwarded-Proto", "https"),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("https://{{cid}}.ipfs.{{host}}/", CIDv1, u.Host),
),
},
}...)
}
RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, tests), specs.SubdomainGatewayIPFS)
}