Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 8c9bed3

Browse files
committed
feat: allow /ipns/webui.ipfs.io on api port
We want to allow user to try out the latest versions of the webui before they are officially released with js-ipfs. Context: ipfs/ipfs-companion#736 go-ipfs counterpart: ipfs/kubo#6530 License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 5156a47 commit 8c9bed3

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

src/http/api/routes/webui.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
'use strict'
22

33
const Joi = require('@hapi/joi')
4+
const Boom = require('@hapi/boom')
45
const resources = require('../../gateway/resources')
56

7+
const webuiPath = '/ipfs/QmQNHd1suZTktPRhP7DD4nKWG46ZRSxkwHocycHVrK3dYW'
8+
9+
const failAction = (request, h, err) => {
10+
// match go-ipfs and return 404 without any details if path validation failed
11+
if (err.name === 'ValidationError') throw Boom.notFound()
12+
return err
13+
}
14+
615
module.exports = [
16+
{
17+
method: '*',
18+
path: '/webui',
19+
handler (request, h) {
20+
return h.redirect(webuiPath)
21+
}
22+
},
723
{
824
method: '*',
925
path: '/ipfs/{path*}',
1026
options: {
1127
handler: resources.gateway.handler,
1228
validate: {
1329
params: {
14-
path: Joi.string().required()
15-
}
30+
path: Joi.string().regex(new RegExp(webuiPath.replace('/ipfs/', '^'))).required()
31+
},
32+
failAction
1633
},
1734
response: {
1835
ranges: false // disable built-in support, handler does it manually
@@ -24,9 +41,25 @@ module.exports = [
2441
},
2542
{
2643
method: '*',
27-
path: '/webui',
28-
handler (request, h) {
29-
return h.redirect('/ipfs/QmQNHd1suZTktPRhP7DD4nKWG46ZRSxkwHocycHVrK3dYW')
44+
path: '/ipns/{path*}',
45+
options: {
46+
handler: resources.gateway.handler,
47+
validate: {
48+
params: {
49+
path: Joi.alternatives().try(
50+
// be careful here, someone could register webui.ipfs.io.evil.com
51+
Joi.string().regex(/^webui\.ipfs\.io\//), // ends with '/''
52+
Joi.string().regex(/^webui\.ipfs\.io$/) // redirect will add '/'
53+
).required()
54+
},
55+
failAction
56+
},
57+
response: {
58+
ranges: false // disable built-in support, handler does it manually
59+
},
60+
ext: {
61+
onPostHandler: { method: resources.gateway.afterHandler }
62+
}
3063
}
3164
}
3265
]

test/http-api/inject/webui.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
6+
module.exports = (http) => {
7+
describe('Web UI', function () {
8+
let api
9+
10+
before(() => {
11+
api = http.api._httpApi._apiServers[0]
12+
})
13+
14+
it('allow /webui', async () => {
15+
const res = await api.inject({
16+
method: 'GET',
17+
url: '/webui'
18+
})
19+
// it should return a redirect
20+
expect(res.statusCode).to.equal(302)
21+
expect(res.headers.location).to.exist()
22+
})
23+
24+
it('disallow /ipfs/ paths that are not webui', async () => {
25+
const res = await api.inject({
26+
method: 'GET',
27+
url: '/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' // empty dir
28+
})
29+
expect(res.statusCode).to.equal(404)
30+
})
31+
32+
it('disallow /ipns/ paths that are not webui', async () => {
33+
const res = await api.inject({
34+
method: 'GET',
35+
url: '/ipns/ipfs.io' // empty dir
36+
})
37+
expect(res.statusCode).to.equal(404)
38+
})
39+
40+
/* DNSLink + fetching actual webui is too slow to include in the test :'-(
41+
it('/ipns/webui.ipfs.io', async () => {
42+
const res = await api.inject({
43+
method: 'GET',
44+
url: '/ipns/webui.ipfs.io'
45+
})
46+
expect(res.statusCode).to.equal(302)
47+
expect(res.headers.location).to.exist()
48+
})
49+
50+
it('/ipns/webui.ipfs.io/', async () => {
51+
const res = await api.inject({
52+
method: 'GET',
53+
url: '/ipns/webui.ipfs.io/'
54+
})
55+
expect(res.statusCode).to.equal(200)
56+
})
57+
it('/ipns/ipfs.io/', async () => {
58+
const res = await api.inject({
59+
method: 'GET',
60+
url: '/ipns/ipfs.io/'
61+
})
62+
expect(res.statusCode).to.equal(404)
63+
})
64+
*/
65+
})
66+
}

0 commit comments

Comments
 (0)