Skip to content

Commit

Permalink
fix: do not prefix /ipfs/ source paths in window.ipfs.files.cp
Browse files Browse the repository at this point in the history
This fixes bug B from
#530
  • Loading branch information
lidel committed Jul 15, 2018
1 parent e19f231 commit e16709c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 15 additions & 1 deletion add-on/src/lib/ipfs-proxy/pre-mfs-scope.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Use path-browserify for consistent behavior between browser and tests on Windows
const Path = require('path-browserify')
const IsIpfs = require('is-ipfs')
const DEFAULT_ROOT_PATH = '/dapps'

// Creates a "pre" function that is called prior to calling a real function
Expand Down Expand Up @@ -42,13 +43,26 @@ const MfsPre = {
// Scope a src/dest tuple to the app path
function createSrcDestPre (getScope, getIpfs, rootPath) {
return async (...args) => {
// console.log('createSrcDestPre.args.before: ' + JSON.stringify(args))
const appPath = await getAppPath(getScope, getIpfs, rootPath)
args[0][0] = Path.join(appPath, safePath(args[0][0]))
// console.log('createSrcDestPre.args.appPath', appPath)
args[0][0] = safeSourcePathPrefix(appPath, args[0][0])
args[0][1] = Path.join(appPath, safePath(args[0][1]))
// console.log('createSrcDestPre.args.after: ' + JSON.stringify(args))
return args
}
}

// Add a prefix to a src path in a safe way
function safeSourcePathPrefix (prefix, path) {
const realPath = safePath(path)
if (IsIpfs.ipfsPath(realPath)) {
// we don't prefix valid /ipfs/ paths (public and immutable, so safe as-is)
return realPath
}
return Path.join(prefix, realPath)
}

// Scope a src path to the app path
function createSrcPre (getScope, getIpfs, rootPath) {
return async (...args) => {
Expand Down
11 changes: 11 additions & 0 deletions test/functional/lib/ipfs-proxy/pre-mfs-scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describe('lib/ipfs-proxy/pre-mfs-scope', () => {
expect(args[0][1]).to.equal('/test-dapps/https/ipfs.io/destination.txt')
})

it('should not scope src path if it is valid /ipfs/ path for files.cp', async () => {
// Bug B from https://github.com/ipfs-shipyard/ipfs-companion/issues/530#issue-341352979
const fnName = 'files.cp'
const getScope = () => 'https://ipfs.io/'
const getIpfs = () => ({ files: { mkdir: () => Promise.resolve() } })
const pre = createPreMfsScope(fnName, getScope, getIpfs, '/test-dapps')
const args = await pre(['/ipfs/bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', '/destination.jpg'])
expect(args[0][0]).to.equal('/ipfs/bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
expect(args[0][1]).to.equal('/test-dapps/https/ipfs.io/destination.jpg')
})

it('should scope src path for files.mkdir', async () => {
const fnName = 'files.mkdir'
const getScope = () => 'https://ipfs.io/'
Expand Down

0 comments on commit e16709c

Please sign in to comment.