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

Commit

Permalink
feat: allow /ipns/webui.ipfs.io on api port
Browse files Browse the repository at this point in the history
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 <lidel@lidel.org>
  • Loading branch information
lidel committed Jul 19, 2019
1 parent 5156a47 commit 8c9bed3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/http/api/routes/webui.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
'use strict'

const Joi = require('@hapi/joi')
const Boom = require('@hapi/boom')
const resources = require('../../gateway/resources')

const webuiPath = '/ipfs/QmQNHd1suZTktPRhP7DD4nKWG46ZRSxkwHocycHVrK3dYW'

const failAction = (request, h, err) => {
// match go-ipfs and return 404 without any details if path validation failed
if (err.name === 'ValidationError') throw Boom.notFound()
return err
}

module.exports = [
{
method: '*',
path: '/webui',
handler (request, h) {
return h.redirect(webuiPath)
}
},
{
method: '*',
path: '/ipfs/{path*}',
options: {
handler: resources.gateway.handler,
validate: {
params: {
path: Joi.string().required()
}
path: Joi.string().regex(new RegExp(webuiPath.replace('/ipfs/', '^'))).required()
},
failAction
},
response: {
ranges: false // disable built-in support, handler does it manually
Expand All @@ -24,9 +41,25 @@ module.exports = [
},
{
method: '*',
path: '/webui',
handler (request, h) {
return h.redirect('/ipfs/QmQNHd1suZTktPRhP7DD4nKWG46ZRSxkwHocycHVrK3dYW')
path: '/ipns/{path*}',
options: {
handler: resources.gateway.handler,
validate: {
params: {
path: Joi.alternatives().try(
// be careful here, someone could register webui.ipfs.io.evil.com
Joi.string().regex(/^webui\.ipfs\.io\//), // ends with '/''
Joi.string().regex(/^webui\.ipfs\.io$/) // redirect will add '/'
).required()
},
failAction
},
response: {
ranges: false // disable built-in support, handler does it manually
},
ext: {
onPostHandler: { method: resources.gateway.afterHandler }
}
}
}
]
66 changes: 66 additions & 0 deletions test/http-api/inject/webui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect

module.exports = (http) => {
describe('Web UI', function () {
let api

before(() => {
api = http.api._httpApi._apiServers[0]
})

it('allow /webui', async () => {
const res = await api.inject({
method: 'GET',
url: '/webui'
})
// it should return a redirect
expect(res.statusCode).to.equal(302)
expect(res.headers.location).to.exist()
})

it('disallow /ipfs/ paths that are not webui', async () => {
const res = await api.inject({
method: 'GET',
url: '/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' // empty dir
})
expect(res.statusCode).to.equal(404)
})

it('disallow /ipns/ paths that are not webui', async () => {
const res = await api.inject({
method: 'GET',
url: '/ipns/ipfs.io' // empty dir
})
expect(res.statusCode).to.equal(404)
})

/* DNSLink + fetching actual webui is too slow to include in the test :'-(
it('/ipns/webui.ipfs.io', async () => {
const res = await api.inject({
method: 'GET',
url: '/ipns/webui.ipfs.io'
})
expect(res.statusCode).to.equal(302)
expect(res.headers.location).to.exist()
})
it('/ipns/webui.ipfs.io/', async () => {
const res = await api.inject({
method: 'GET',
url: '/ipns/webui.ipfs.io/'
})
expect(res.statusCode).to.equal(200)
})
it('/ipns/ipfs.io/', async () => {
const res = await api.inject({
method: 'GET',
url: '/ipns/ipfs.io/'
})
expect(res.statusCode).to.equal(404)
})
*/
})
}

0 comments on commit 8c9bed3

Please sign in to comment.