Skip to content

Commit

Permalink
fix(proxy): fail fast on unsupported flag
Browse files Browse the repository at this point in the history
Some flags do not make sense over IPFS proxy so we throw early with
meaningful error message.
  • Loading branch information
lidel committed Jan 15, 2019
1 parent ac150f2 commit a28da88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion add-on/src/lib/ipfs-proxy/pre-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
const COMMAND_WHITELIST = Object.freeze(require('./command-whitelist.json'))

// Creates a "pre" function that is called prior to calling a real function
// on the IPFS instance. It will throw if access is denied due to API not being whitelisted
// on the IPFS instance. It will throw if access is denied
// due to API not being whitelisted of arguments not being supported
function createPreCommand (permission) {
return async (...args) => {
if (!inCommandWhitelist(permission)) {
throw createCommandWhitelistError(permission)
}
if (['add', 'files.add'].includes(permission)) {
// Fail fast: nocopy does not work over proxy
if (args.some(arg => typeof arg === 'object' && arg.nocopy)) {
throw new Error(`ipfs.${permission} with 'nocopy' flag is not supported by IPFS Proxy`)
}
}
return args
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/functional/lib/ipfs-proxy/pre-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ describe('lib/ipfs-proxy/pre-command', () => {
expect(() => { if (error) throw error }).to.throw(`Access to '${permission}' commands over IPFS Proxy is globally blocked`)
})

it('should throw early if flag is not supported', async () => {
const permission = 'add'
const preApiWhitelist = createPreCommand(permission)
const flag = 'nocopy'
const opts = {}
opts[flag] = true

let error

try {
await preApiWhitelist('foo', opts)
} catch (err) {
error = err
}

expect(() => { if (error) throw error }).to.throw(`ipfs.${permission} with '${flag}' flag is not supported by IPFS Proxy`)
})

it('should have a well-formed Error if denied', async () => {
const permission = 'config.show'
const preApiWhitelist = createPreCommand(permission)
Expand Down

0 comments on commit a28da88

Please sign in to comment.