Skip to content

Commit e9a34a7

Browse files
committed
wip: refactor redirectToGateway logic
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
1 parent 1d922de commit e9a34a7

14 files changed

+289
-262
lines changed

add-on/_locales/en/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@
304304
"description": "An option description on the Preferences screen (option_dnslinkDataPreload_description)"
305305
},
306306
"option_dnslinkRedirect_warning": {
307-
"message": "Redirecting to a path-based gateway breaks Origin-based security isolation of DNSLink websites. Make sure you understand related risks.",
307+
"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.",
308308
"description": "A warning on the Preferences screen, displayed when URL does not belong to Secure Context (option_customGatewayUrl_warning)"
309309
},
310310
"option_noIntegrationsHostnames_title": {

add-on/src/lib/dnslink.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const IsIpfs = require('is-ipfs')
99
const LRU = require('lru-cache')
1010
const { default: PQueue } = require('p-queue')
1111
const { offlinePeerCount } = require('./state')
12-
const { pathAtHttpGateway } = require('./ipfs-path')
12+
const { sameGateway, pathAtHttpGateway } = require('./ipfs-path')
1313

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

@@ -47,11 +47,11 @@ module.exports = function createDnslinkResolver (getState) {
4747
return state.dnslinkPolicy &&
4848
requestUrl.startsWith('http') &&
4949
!IsIpfs.url(requestUrl) &&
50-
!requestUrl.startsWith(state.apiURLString) &&
51-
!requestUrl.startsWith(state.gwURLString)
50+
!sameGateway(requestUrl, state.apiURL) &&
51+
!sameGateway(requestUrl, state.gwURL)
5252
},
5353

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

add-on/src/lib/http-proxy.js

-1
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,4 @@ async function registerSubdomainProxyChromium (enable, proxyHost) {
116116
}
117117
}
118118

119-
120119
module.exports.registerSubdomainProxy = registerSubdomainProxy

add-on/src/lib/ipfs-companion.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ module.exports = async function init () {
191191
// console.log((sender.tab ? 'Message from a content script:' + sender.tab.url : 'Message from the extension'), request)
192192
if (request.pubGwUrlForIpfsOrIpnsPath) {
193193
const path = request.pubGwUrlForIpfsOrIpnsPath
194-
const result = ipfsPathValidator.validIpfsOrIpnsPath(path) ? ipfsPathValidator.resolveToPublicUrl(path, state.pubGwURLString) : null
194+
const { validIpfsOrIpns, resolveToPublicUrl } = ipfsPathValidator
195+
const result = validIpfsOrIpns(path) ? resolveToPublicUrl(path) : null
195196
return Promise.resolve({ pubGwUrlForIpfsOrIpnsPath: result })
196197
}
197198
}

add-on/src/lib/ipfs-import.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const browser = require('webextension-polyfill')
66
const { redirectOptOutHint } = require('./ipfs-request')
77

88
function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime, copier) {
9+
const { resolveToPublicUrl } = ipfsPathValidator
910
const ipfsImportHandler = {
1011
formatImportDirectory (path) {
1112
path = path.replace(/\/$|$/, '/')
@@ -72,7 +73,7 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime,
7273
return new Promise((resolve, reject) => {
7374
const http = new XMLHttpRequest()
7475
// Make sure preload request is excluded from global redirect
75-
const preloadUrl = ipfsPathValidator.resolveToPublicUrl(`${path}#${redirectOptOutHint}`, state.pubGwURLString)
76+
const preloadUrl = resolveToPublicUrl(`${path}#${redirectOptOutHint}`)
7677
http.open('HEAD', preloadUrl)
7778
http.onreadystatechange = function () {
7879
if (this.readyState === this.DONE) {
@@ -100,8 +101,7 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime,
100101
// share wrapping dir
101102
path = `/ipfs/${root.hash}/`
102103
}
103-
const state = getState()
104-
const url = ipfsPathValidator.resolveToPublicUrl(path, state.pubGwURLString)
104+
const url = resolveToPublicUrl(path)
105105
await copier.copyTextToClipboard(url)
106106
},
107107
async preloadFilesAtPublicGateway (files) {

0 commit comments

Comments
 (0)