Skip to content

Commit

Permalink
fix(pinning): paths with URL-unsafe characters
Browse files Browse the repository at this point in the history
Closes #303
  • Loading branch information
lidel committed Nov 6, 2017
1 parent 2390a2d commit e8b4c9e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
7 changes: 6 additions & 1 deletion add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ function uploadResultHandler (err, result) {
// Copying URLs
// -------------------------------------------------------------------

function safeIpfsPath (urlOrPath) {
// better safe than sorry: https://github.com/ipfs/ipfs-companion/issues/303
return decodeURIComponent(urlOrPath.replace(/^.*(\/ip(f|n)s\/.+)$/, '$1'))
}

async function findUrlForContext (context) {
if (context) {
if (context.linkUrl) {
Expand All @@ -526,7 +531,7 @@ async function findUrlForContext (context) {

async function copyCanonicalAddress (context) {
const url = await findUrlForContext(context)
const rawIpfsAddress = url.replace(/^.+(\/ip(f|n)s\/.+)/, '$1')
const rawIpfsAddress = safeIpfsPath(url)
copyTextToClipboard(rawIpfsAddress)
notify('notify_copiedCanonicalAddressTitle', rawIpfsAddress)
}
Expand Down
3 changes: 2 additions & 1 deletion add-on/src/popup/browser-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ async function handlePinError (errorMessageKey, error) {
}

async function resolveToIPFS (path) {
const bg = await getBackgroundPage()
path = bg.safeIpfsPath(path) // https://github.com/ipfs/ipfs-companion/issues/303
if (/^\/ipns/.test(path)) {
const bg = await getBackgroundPage()
const response = await bg.ipfs.name.resolve(path, {recursive: true, nocache: false})
return response.Path
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"web-ext": "2.2.2"
},
"dependencies": {
"ipfs-api": "15.0.0",
"ipfs-api": "15.0.1",
"is-ipfs": "0.3.2",
"lru_map": "0.3.3",
"webextension-polyfill": "0.1.2"
Expand Down
28 changes: 28 additions & 0 deletions test/unit/02-safeIpfsPath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'
/* eslint-env webextensions, mocha */
// eslint-disable-next-line no-unused-vars
/* globals sinon, should, state, safeIpfsPath */

// https://github.com/ipfs/ipfs-companion/issues/303
describe('IPFS Path Sanitization', function () {
describe('safeIpfsPath(pathOrUrl) should produce no changes', function () {
it('for URL without special characters', function () {
const path = 'https://ipfs.io/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest'
safeIpfsPath(path).should.equal('/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest')
})
it('for path without special characters', function () {
const path = '/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest'
safeIpfsPath(path).should.equal('/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest')
})
})
describe('safeIpfsPath(pathOrUrl) should normalize', function () {
it('URL with special characters', function () {
const path = 'https://ipfs.io/ipfs/Qmb8wsGZNXt5VXZh1pEmYynjB6Euqpq3HYyeAdw2vScTkQ/1%20-%20Barrel%20-%20Part%201'
safeIpfsPath(path).should.equal('/ipfs/Qmb8wsGZNXt5VXZh1pEmYynjB6Euqpq3HYyeAdw2vScTkQ/1 - Barrel - Part 1')
})
it('path with special characters', function () {
const path = '/ipfs/Qmb8wsGZNXt5VXZh1pEmYynjB6Euqpq3HYyeAdw2vScTkQ/1%20-%20Barrel%20-%20Part%201'
safeIpfsPath(path).should.equal('/ipfs/Qmb8wsGZNXt5VXZh1pEmYynjB6Euqpq3HYyeAdw2vScTkQ/1 - Barrel - Part 1')
})
})
})

0 comments on commit e8b4c9e

Please sign in to comment.