Skip to content

Commit

Permalink
wip: refactor redirectToGateway logic
Browse files Browse the repository at this point in the history
redirect to gateway logic was path based, this is wip surgical refactor
to unify subdomain and path handling, hopefully decreasing maintenance
buden going forward

NOT READY YET, still some tests to fix
  • Loading branch information
lidel committed Mar 27, 2020
1 parent 1d922de commit e9a34a7
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 262 deletions.
2 changes: 1 addition & 1 deletion add-on/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@
"description": "An option description on the Preferences screen (option_dnslinkDataPreload_description)"
},
"option_dnslinkRedirect_warning": {
"message": "Redirecting to a path-based gateway breaks Origin-based security isolation of DNSLink websites. Make sure you understand related risks.",
"message": "Avoid using this if your IPFS Node does not support *.ipfs.localhost. Redirecting to a path-based gateway breaks Origin-based security isolation of DNSLink websites. Make sure you understand related risks.",
"description": "A warning on the Preferences screen, displayed when URL does not belong to Secure Context (option_customGatewayUrl_warning)"
},
"option_noIntegrationsHostnames_title": {
Expand Down
11 changes: 5 additions & 6 deletions add-on/src/lib/dnslink.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const IsIpfs = require('is-ipfs')
const LRU = require('lru-cache')
const { default: PQueue } = require('p-queue')
const { offlinePeerCount } = require('./state')
const { pathAtHttpGateway } = require('./ipfs-path')
const { sameGateway, pathAtHttpGateway } = require('./ipfs-path')

// TODO: add Preferences toggle to disable redirect of DNSLink websites (while keeping async dnslink lookup)

Expand Down Expand Up @@ -47,11 +47,11 @@ module.exports = function createDnslinkResolver (getState) {
return state.dnslinkPolicy &&
requestUrl.startsWith('http') &&
!IsIpfs.url(requestUrl) &&
!requestUrl.startsWith(state.apiURLString) &&
!requestUrl.startsWith(state.gwURLString)
!sameGateway(requestUrl, state.apiURL) &&
!sameGateway(requestUrl, state.gwURL)
},

dnslinkRedirect (url, dnslink) {
dnslinkAtGateway (url, dnslink) {
if (typeof url === 'string') {
url = new URL(url)
}
Expand All @@ -62,8 +62,7 @@ module.exports = function createDnslinkResolver (getState) {
// - https://github.com/ipfs/ipfs-companion/issues/298
const ipnsPath = dnslinkResolver.convertToIpnsPath(url)
const gateway = state.ipfsNodeType === 'embedded' ? state.pubGwURLString : state.gwURLString
// TODO: redirect to `ipns://` if hasNativeProtocolHandler === true
return { redirectUrl: pathAtHttpGateway(ipnsPath, gateway) }
return pathAtHttpGateway(ipnsPath, gateway)
}
},

Expand Down
1 change: 0 additions & 1 deletion add-on/src/lib/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,4 @@ async function registerSubdomainProxyChromium (enable, proxyHost) {
}
}


module.exports.registerSubdomainProxy = registerSubdomainProxy
3 changes: 2 additions & 1 deletion add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ module.exports = async function init () {
// console.log((sender.tab ? 'Message from a content script:' + sender.tab.url : 'Message from the extension'), request)
if (request.pubGwUrlForIpfsOrIpnsPath) {
const path = request.pubGwUrlForIpfsOrIpnsPath
const result = ipfsPathValidator.validIpfsOrIpnsPath(path) ? ipfsPathValidator.resolveToPublicUrl(path, state.pubGwURLString) : null
const { validIpfsOrIpns, resolveToPublicUrl } = ipfsPathValidator
const result = validIpfsOrIpns(path) ? resolveToPublicUrl(path) : null
return Promise.resolve({ pubGwUrlForIpfsOrIpnsPath: result })
}
}
Expand Down
6 changes: 3 additions & 3 deletions add-on/src/lib/ipfs-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const browser = require('webextension-polyfill')
const { redirectOptOutHint } = require('./ipfs-request')

function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime, copier) {
const { resolveToPublicUrl } = ipfsPathValidator
const ipfsImportHandler = {
formatImportDirectory (path) {
path = path.replace(/\/$|$/, '/')
Expand Down Expand Up @@ -72,7 +73,7 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime,
return new Promise((resolve, reject) => {
const http = new XMLHttpRequest()
// Make sure preload request is excluded from global redirect
const preloadUrl = ipfsPathValidator.resolveToPublicUrl(`${path}#${redirectOptOutHint}`, state.pubGwURLString)
const preloadUrl = resolveToPublicUrl(`${path}#${redirectOptOutHint}`)
http.open('HEAD', preloadUrl)
http.onreadystatechange = function () {
if (this.readyState === this.DONE) {
Expand Down Expand Up @@ -100,8 +101,7 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime,
// share wrapping dir
path = `/ipfs/${root.hash}/`
}
const state = getState()
const url = ipfsPathValidator.resolveToPublicUrl(path, state.pubGwURLString)
const url = resolveToPublicUrl(path)
await copier.copyTextToClipboard(url)
},
async preloadFilesAtPublicGateway (files) {
Expand Down
Loading

0 comments on commit e9a34a7

Please sign in to comment.