Skip to content

Commit

Permalink
Fix Mixed Content Warning when using localhost
Browse files Browse the repository at this point in the history
Closes #328
  • Loading branch information
lidel committed Dec 13, 2017
1 parent 86e77b3 commit 70624a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function createRequestModifier (getState, dnsLink, ipfsPathValidator) {
}

// skip all local requests
if (request.url.startsWith('http://127.0.0.1:') || request.url.startsWith('http://localhost:')) {
if (request.url.startsWith('http://127.0.0.1:') || request.url.startsWith('http://localhost:') || request.url.startsWith('http://[::1]:')) {
return
}

Expand Down
9 changes: 4 additions & 5 deletions add-on/src/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const optionDefaults = Object.freeze({
preloadAtPublicGateway: true,
catchUnhandledProtocols: true,
displayNotifications: true,
customGatewayUrl: 'http://localhost:8080',
ipfsApiUrl: 'http://localhost:5001',
customGatewayUrl: 'http://127.0.0.1:8080',
ipfsApiUrl: 'http://127.0.0.1:5001',
ipfsApiPollMs: 3000
})

Expand Down Expand Up @@ -45,10 +45,9 @@ function storeMissingOptions (read, defaults, storage) {
exports.storeMissingOptions = storeMissingOptions

function normalizeGatewayURL (url) {
// https://github.com/ipfs/ipfs-companion/issues/291
// https://github.com/ipfs/ipfs-companion/issues/328
return url
.replace('/127.0.0.1:', '/localhost:')
.replace('/gateway.ipfs.io', '/ipfs.io')
.replace('/localhost:', '/127.0.0.1:')
}

exports.normalizeGatewayURL = normalizeGatewayURL
20 changes: 13 additions & 7 deletions test/functional/lib/ipfs-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('modifyRequest', function () {
peerCount: 1,
redirect: true,
catchUnhandledProtocols: true,
gwURLString: 'http://localhost:8080'
gwURLString: 'http://127.0.0.1:8080'
})
const getState = () => state
dnsLink = createDnsLink(getState)
Expand All @@ -36,7 +36,7 @@ describe('modifyRequest', function () {
describe('request for a path matching /ipfs/{CIDv0}', function () {
it('should be served from custom gateway if redirect is enabled', function () {
const request = url2request('https://ipfs.io/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest')
expect(modifyRequest(request).redirectUrl).to.equal('http://localhost:8080/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest')
expect(modifyRequest(request).redirectUrl).to.equal('http://127.0.0.1:8080/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest')
})
it('should be left untouched if redirect is disabled', function () {
state.redirect = false
Expand All @@ -57,12 +57,12 @@ describe('modifyRequest', function () {
dnsLink.readDnslinkFromTxtRecord = sinon.stub().withArgs(fqdn).returns('/ipfs/Qmazvovg6Sic3m9igZMKoAPjkiVZsvbWWc8ZvgjjK1qMss')
// pretend API is online and we can do dns lookups with it
state.peerCount = 1
expect(modifyRequest(request).redirectUrl).to.equal('http://localhost:8080/ipns/ipfs.git.sexy/index.html?argTest#hashTest')
expect(modifyRequest(request).redirectUrl).to.equal('http://127.0.0.1:8080/ipns/ipfs.git.sexy/index.html?argTest#hashTest')
})
it('should be served from custom gateway if {path} starts with a valid CID', function () {
const request = url2request('https://ipfs.io/ipns/QmSWnBwMKZ28tcgMFdihD8XS7p6QzdRSGf71cCybaETSsU/index.html?argTest#hashTest')
dnsLink.readDnslinkFromTxtRecord = sinon.stub().returns(false)
expect(modifyRequest(request).redirectUrl).to.equal('http://localhost:8080/ipns/QmSWnBwMKZ28tcgMFdihD8XS7p6QzdRSGf71cCybaETSsU/index.html?argTest#hashTest')
expect(modifyRequest(request).redirectUrl).to.equal('http://127.0.0.1:8080/ipns/QmSWnBwMKZ28tcgMFdihD8XS7p6QzdRSGf71cCybaETSsU/index.html?argTest#hashTest')
})
it('should be left untouched if redirect is disabled', function () {
state.redirect = false
Expand Down Expand Up @@ -218,18 +218,24 @@ describe('modifyRequest', function () {

describe('request for IPFS path at a localhost', function () {
// we do not touch local requests, as it may interfere with other nodes running at the same machine
// or could produce false-positives such as redirection from 127.0.0.1:5001/ipfs/path to localhost:8080/ipfs/path
// or could produce false-positives such as redirection from 127.0.0.1:5001/ipfs/path to 127.0.0.1:8080/ipfs/path
it('should be left untouched if 127.0.0.1 is used', function () {
state.redirect = false
state.redirect = true
const request = url2request('http://127.0.0.1:5001/ipfs/QmPhnvn747LqwPYMJmQVorMaGbMSgA7mRRoyyZYz3DoZRQ/')
expect(modifyRequest(request)).to.equal(undefined)
})
it('should be left untouched if localhost is used', function () {
// https://github.com/ipfs/ipfs-companion/issues/291
state.redirect = false
state.redirect = true
const request = url2request('http://localhost:5001/ipfs/QmPhnvn747LqwPYMJmQVorMaGbMSgA7mRRoyyZYz3DoZRQ/')
expect(modifyRequest(request)).to.equal(undefined)
})
it('should be left untouched if [::1] is used', function () {
// https://github.com/ipfs/ipfs-companion/issues/291
state.redirect = true
const request = url2request('http://[::1]:5001/ipfs/QmPhnvn747LqwPYMJmQVorMaGbMSgA7mRRoyyZYz3DoZRQ/')
expect(modifyRequest(request)).to.equal(undefined)
})
})

after(() => {
Expand Down

0 comments on commit 70624a8

Please sign in to comment.