Skip to content

Commit eb66dfa

Browse files
committed
refactor: use HTTP proxy for subdomains only on Firefox
Chromium 80 already hardcodes localhost to loopback IPs, and we don't want to add unnecessary permission as it may cause us fail review at Chrome Web Store. I also renamed the setting as its about overall Subdomain support on localhost, even when HTTP proxy is not used.
1 parent 3e6708b commit eb66dfa

14 files changed

+73
-52
lines changed

add-on/_locales/en/messages.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@
279279
"message": "Redirect requests for IPFS resources to the Custom gateway",
280280
"description": "An option description on the Preferences screen (option_useCustomGateway_description)"
281281
},
282-
"option_useSubdomainProxy_title": {
283-
"message": "Use Subdomain Proxy",
284-
"description": "An option title on the Preferences screen (option_useSubdomainProxy_title)"
282+
"option_useSubdomains_title": {
283+
"message": "Use Subdomains",
284+
"description": "An option title on the Preferences screen (option_useSubdomains_title)"
285285
},
286-
"option_useSubdomainProxy_description": {
287-
"message": "Use Custom Gateway as HTTP Proxy to enable Origin isolation per content root at *.ipfs.localhost",
288-
"description": "An option description on the Preferences screen (option_useSubdomainProxy_description)"
286+
"option_useSubdomains_description": {
287+
"message": "Isolate content roots from each other by loading them from subdomains at *.localhost and creating a unique Origin for each CID, IPNS or DNSLink record. Requires a local go-ipfs 0.5.0 or later.",
288+
"description": "An option description on the Preferences screen (option_useSubdomains_description)"
289289
},
290290
"option_dnslinkRedirect_title": {
291291
"message": "Load websites from Custom Gateway",

add-on/manifest.chromium.json

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
{
22
"minimum_chrome_version": "72",
3+
"permissions": [
4+
"<all_urls>",
5+
"idle",
6+
"tabs",
7+
"notifications",
8+
"storage",
9+
"unlimitedStorage",
10+
"contextMenus",
11+
"clipboardWrite",
12+
"webNavigation",
13+
"webRequest",
14+
"webRequestBlocking"
15+
],
316
"incognito": "not_allowed"
417
}

add-on/manifest.common.json

-14
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@
1111
"38": "icons/png/ipfs-logo-on_38.png",
1212
"128": "icons/png/ipfs-logo-on_128.png"
1313
},
14-
"permissions": [
15-
"<all_urls>",
16-
"idle",
17-
"tabs",
18-
"notifications",
19-
"storage",
20-
"unlimitedStorage",
21-
"contextMenus",
22-
"clipboardWrite",
23-
"proxy",
24-
"webNavigation",
25-
"webRequest",
26-
"webRequestBlocking"
27-
],
2814
"background": {
2915
"page": "dist/background/background.html"
3016
},

add-on/manifest.firefox.json

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
"default_title": "__MSG_pageAction_titleNonIpfs__",
1919
"default_popup": "dist/popup/page-action/index.html"
2020
},
21+
"permissions": [
22+
"<all_urls>",
23+
"idle",
24+
"tabs",
25+
"notifications",
26+
"proxy",
27+
"storage",
28+
"unlimitedStorage",
29+
"contextMenus",
30+
"clipboardWrite",
31+
"webNavigation",
32+
"webRequest",
33+
"webRequestBlocking"
34+
],
2135
"content_scripts": [ ],
2236
"protocol_handlers": [
2337
{

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ log.error = debug('ipfs-companion:http-proxy:error')
2626
//
2727
// State in Q2 2020:
2828
// - Chromium hardcodes `localhost` name to point at local IP and proxy is not
29-
// really necessary, but we do it just to be safe.
29+
// really necessary. The code is here (inactivE) in case we need it in the future.
3030
// - Firefox requires proxy to avoid DNS lookup, but there is an open issue
3131
// that will remove that need at some point:
3232
// https://bugzilla.mozilla.org/show_bug.cgi?id=1220810
3333
async function registerSubdomainProxy (getState, runtime, notify) {
34+
// At the moment only firefox requires proxy registration
35+
if (!runtime.isFirefox) return
36+
3437
try {
35-
const { active, useSubdomainProxy, gwURLString } = getState()
36-
const enable = active && useSubdomainProxy
38+
const { active, useSubdomains, gwURLString } = getState()
39+
const enable = active && useSubdomains
3740

3841
// HTTP Proxy feature is exposed on the gateway port
3942
// Just ensure we use localhost IP to remove any dependency on DNS
@@ -44,8 +47,9 @@ async function registerSubdomainProxy (getState, runtime, notify) {
4447
return await registerSubdomainProxyFirefox(enable, hostname, port)
4548
}
4649

47-
// at this point we asume Chromium
48-
return await registerSubdomainProxyChromium(enable, hostname, port)
50+
// At this point we would asume Chromium, but its not needed atm
51+
// Uncomment below if ever needed (+ add 'proxy' permission to manifest.json)
52+
// return await registerSubdomainProxyChromium(enable, hostname, port)
4953
} catch (err) {
5054
// registerSubdomainProxy is just a failsafe, not necessary in most cases,
5155
// so we should not break init when it fails.
@@ -97,6 +101,10 @@ async function registerSubdomainProxyFirefox (enable, hostname, port) {
97101
log('disabled HTTP proxy for *.localhost')
98102
}
99103

104+
/*
105+
* Chromium 80 does not need proxy, so below is not used.
106+
* Uncomment below if ever needed (+ add 'proxy' permission to manifest.json)
107+
100108
// Helpers for converting callback chrome.* API to promises
101109
const cb = (resolve, reject) => (result) => {
102110
const err = chrome.runtime.lastError
@@ -142,5 +150,6 @@ async function registerSubdomainProxyChromium (enable, hostname, port) {
142150
log('disabled HTTP proxy for *.localhost')
143151
}
144152
}
153+
*/
145154

146155
module.exports.registerSubdomainProxy = registerSubdomainProxy

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -637,18 +637,17 @@ module.exports = async function init () {
637637
case 'useCustomGateway':
638638
state.redirect = change.newValue
639639
break
640-
case 'useSubdomainProxy':
640+
case 'useSubdomains':
641641
state[key] = change.newValue
642-
// Normalize hostname if enabled
643-
if (state.useSubdomainProxy) {
644-
await browser.storage.local.set({
645-
// We need to update the hostname in customGatewayUrl because:
646-
// 127.0.0.1 - path gateway
647-
// localhost - subdomain gateway
648-
// and we need to use the latter
649-
customGatewayUrl: guiURLString(state.gwURLString, { useLocalhostName: true })
642+
await browser.storage.local.set({
643+
// We need to update the hostname in customGatewayUrl because:
644+
// 127.0.0.1 - path gateway
645+
// localhost - subdomain gateway
646+
// and we need to use the latter
647+
customGatewayUrl: guiURLString(state.gwURLString, {
648+
useLocalhostName: state.useSubdomains
650649
})
651-
}
650+
})
652651
// Finally, update proxy settings based on the state
653652
await registerSubdomainProxy(getState, runtime)
654653
break

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
148148
// to the local gateway and replace raw IP with 'localhost' hostname to
149149
// take advantage of subdomain redirect provided by go-ipfs >= 0.5
150150
if (state.redirect && request.type === 'main_frame' && sameGateway(request.url, state.gwURL)) {
151-
const redirectUrl = safeURL(request.url, { useLocalhostName: state.useSubdomainProxy }).toString()
151+
const redirectUrl = safeURL(request.url, { useLocalhostName: state.useSubdomains }).toString()
152152
if (redirectUrl !== request.url) return { redirectUrl }
153153
}
154154

add-on/src/lib/options.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports.optionDefaults = Object.freeze({
1313
publicGatewayUrl: 'https://ipfs.io',
1414
publicSubdomainGatewayUrl: 'https://dweb.link',
1515
useCustomGateway: true,
16-
useSubdomainProxy: true,
16+
useSubdomains: true,
1717
noIntegrationsHostnames: [],
1818
automaticMode: true,
1919
linkify: false,
@@ -169,8 +169,8 @@ exports.migrateOptions = async (storage) => {
169169
// migrate old default 127.0.0.1 to localhost hostname
170170
const { customGatewayUrl: gwUrl } = await storage.get('customGatewayUrl')
171171
if (gwUrl && (localhostIpUrl(gwUrl) || localhostNameUrl(gwUrl))) {
172-
const { useSubdomainProxy } = await storage.get('useSubdomainProxy')
173-
const newUrl = guiURLString(gwUrl, { useLocalhostName: useSubdomainProxy })
172+
const { useSubdomains } = await storage.get('useSubdomains')
173+
const newUrl = guiURLString(gwUrl, { useLocalhostName: useSubdomains })
174174
if (gwUrl !== newUrl) {
175175
await storage.set({ customGatewayUrl: newUrl })
176176
}

add-on/src/lib/state.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function initState (options, overrides) {
2525
state.apiURL = safeURL(options.ipfsApiUrl, { useLocalhostName: false }) // go-ipfs returns 403 if IP is beautified to 'localhost'
2626
state.apiURLString = state.apiURL.toString()
2727
delete state.ipfsApiUrl
28-
state.gwURL = safeURL(options.customGatewayUrl, { useLocalhostName: state.useSubdomainProxy })
28+
state.gwURL = safeURL(options.customGatewayUrl, { useLocalhostName: state.useSubdomains })
2929
state.gwURLString = state.gwURL.toString()
3030
delete state.customGatewayUrl
3131
state.dnslinkPolicy = String(options.dnslinkPolicy) === 'false' ? false : options.dnslinkPolicy

add-on/src/options/forms/gateways-form.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ function gatewaysForm ({
1515
ipfsNodeType,
1616
customGatewayUrl,
1717
useCustomGateway,
18-
useSubdomainProxy,
18+
useSubdomains,
1919
noIntegrationsHostnames,
2020
publicGatewayUrl,
2121
publicSubdomainGatewayUrl,
2222
onOptionChange
2323
}) {
24-
const onCustomGatewayUrlChange = onOptionChange('customGatewayUrl', (url) => guiURLString(url, { useLocalhostName: useSubdomainProxy }))
24+
const onCustomGatewayUrlChange = onOptionChange('customGatewayUrl', (url) => guiURLString(url, { useLocalhostName: useSubdomains }))
2525
const onUseCustomGatewayChange = onOptionChange('useCustomGateway')
26-
const onUseSubdomainProxyChange = onOptionChange('useSubdomainProxy')
26+
const onUseSubdomainProxyChange = onOptionChange('useSubdomains')
2727
const onPublicGatewayUrlChange = onOptionChange('publicGatewayUrl', guiURLString)
2828
const onPublicSubdomainGatewayUrlChange = onOptionChange('publicSubdomainGatewayUrl', guiURLString)
2929
const onNoIntegrationsHostnamesChange = onOptionChange('noIntegrationsHostnames', hostTextToArray)
@@ -113,18 +113,18 @@ function gatewaysForm ({
113113
` : null}
114114
${supportRedirectToCustomGateway ? html`
115115
<div>
116-
<label for="useSubdomainProxy">
116+
<label for="useSubdomains">
117117
<dl>
118-
<dt>${browser.i18n.getMessage('option_useSubdomainProxy_title')}</dt>
118+
<dt>${browser.i18n.getMessage('option_useSubdomains_title')}</dt>
119119
<dd>
120-
${browser.i18n.getMessage('option_useSubdomainProxy_description')}
120+
${browser.i18n.getMessage('option_useSubdomains_description')}
121121
<p><a href="https://docs.ipfs.io/guides/guides/addressing/#subdomain-gateway" target="_blank">
122122
${browser.i18n.getMessage('option_legend_readMore')}
123123
</a></p>
124124
</dd>
125125
</dl>
126126
</label>
127-
<div>${switchToggle({ id: 'useSubdomainProxy', checked: useSubdomainProxy, onchange: onUseSubdomainProxyChange })}</div>
127+
<div>${switchToggle({ id: 'useSubdomains', checked: useSubdomains, onchange: onUseSubdomainProxyChange })}</div>
128128
</div>
129129
` : null}
130130
${supportRedirectToCustomGateway ? html`

add-on/src/options/page.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = function optionsPage (state, emit) {
6767
ipfsNodeType: state.options.ipfsNodeType,
6868
customGatewayUrl: state.options.customGatewayUrl,
6969
useCustomGateway: state.options.useCustomGateway,
70-
useSubdomainProxy: state.options.useSubdomainProxy,
70+
useSubdomains: state.options.useSubdomains,
7171
publicGatewayUrl: state.options.publicGatewayUrl,
7272
publicSubdomainGatewayUrl: state.options.publicSubdomainGatewayUrl,
7373
noIntegrationsHostnames: state.options.noIntegrationsHostnames,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
"ipfs-postmsg-proxy": "3.1.1",
141141
"ipfsx": "0.17.0",
142142
"is-fqdn": "1.0.1",
143-
"is-ipfs": "https://github.com/ipfs/is-ipfs/tarball/d9e7082587595a5c7a192405342fae18865c33f9/is-ipfs.tar.gz",
143+
"is-ipfs": "https://github.com/ipfs/is-ipfs/tarball/d5717e910d864d738faf39480897342c7cbf065d/is-ipfs.tar.gz",
144144
"is-svg": "4.2.0",
145145
"it-to-stream": "0.1.1",
146146
"lru-cache": "5.1.1",

test/functional/lib/ipfs-request-gateway-redirect.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('modifyRequest.onBeforeRequest:', function () {
364364
})
365365
it('should be redirected to localhost (subdomain in go-ipfs >0.5) if type=main_frame and 127.0.0.1 (path gw) is used un URL', function () {
366366
state.redirect = true
367-
state.useSubdomainProxy = true
367+
state.useSubdomains = true
368368
expect(state.gwURL.hostname).to.equal('localhost')
369369
const cid = 'QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR'
370370
const request = url2request(`http://127.0.0.1:8080/ipfs/${cid}?arg=val#hash`)

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -7931,9 +7931,9 @@ is-ip@^3.1.0:
79317931
dependencies:
79327932
ip-regex "^4.0.0"
79337933

7934-
"is-ipfs@https://github.com/ipfs/is-ipfs/tarball/d9e7082587595a5c7a192405342fae18865c33f9/is-ipfs.tar.gz":
7934+
"is-ipfs@https://github.com/ipfs/is-ipfs/tarball/d5717e910d864d738faf39480897342c7cbf065d/is-ipfs.tar.gz":
79357935
version "0.6.3"
7936-
resolved "https://github.com/ipfs/is-ipfs/tarball/d9e7082587595a5c7a192405342fae18865c33f9/is-ipfs.tar.gz#733850d100cb80d08d251706d4b91ab14b58bd56"
7936+
resolved "https://github.com/ipfs/is-ipfs/tarball/d5717e910d864d738faf39480897342c7cbf065d/is-ipfs.tar.gz#c3f2f4d5423be1bfff4712488c742eb89d5f7ac0"
79377937
dependencies:
79387938
bs58 "^4.0.1"
79397939
cids "~0.7.0"

0 commit comments

Comments
 (0)