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

[WIP] feat: allow /ipns/webui.ipfs.io on api port #2281

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
*/
})
}