-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: introduce global whitelist for window.ipfs
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
"add", | ||
"block", | ||
"cat", | ||
"dag", | ||
"dht", | ||
"dns", | ||
"files", | ||
"get", | ||
"id", | ||
"ls", | ||
"name.resolve", | ||
"object", | ||
"pin", | ||
"pubsub", | ||
"repo", | ||
"stats", | ||
"swarm", | ||
"version" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Some APIs are too sensitive to be exposed to dapps on every website | ||
// We follow a safe security practice of denying everything and allowing access | ||
// to a pre-approved list of known APIs. This way if a new API is added | ||
// it will be blocked by default, until it is explicitly enabled below. | ||
const API_WHITELIST = Object.freeze(require('./api-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 | ||
function createPreApiWhitelist (permission) { | ||
return async (...args) => { | ||
// Fail fast if API or namespace is not explicitly whitelisted | ||
const permRoot = permission.split('.')[0] | ||
if (!(API_WHITELIST.includes(permRoot) || API_WHITELIST.includes(permission))) { | ||
console.log(`[ipfs-companion] Access to ${permission} API over window.ipfs is blocked. If you feel it should be allowed, open an issue at https://github.com/ipfs-shipyard/ipfs-companion/issues/new`) | ||
const err = new Error(`Access to ${permission} API is globally blocked for window.ipfs`) | ||
err.output = { payload: { isIpfsProxyWhitelistError: true, permission } } | ||
throw err | ||
} | ||
|
||
return args | ||
} | ||
} | ||
|
||
module.exports = createPreApiWhitelist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
const { describe, it, before, after } = require('mocha') | ||
const { expect } = require('chai') | ||
const { URL } = require('url') | ||
const createPreApiWhitelist = require('../../../../add-on/src/lib/ipfs-proxy/pre-api-whitelist') | ||
|
||
describe('lib/ipfs-proxy/pre-api-whitelist', () => { | ||
before(() => { | ||
global.URL = URL | ||
}) | ||
|
||
it('should throw early if access was not enabled via global API whitelist', async () => { | ||
const permission = 'config.show' | ||
const preApiWhitelist = createPreApiWhitelist(permission) | ||
|
||
let error | ||
|
||
try { | ||
await preApiWhitelist() | ||
} catch (err) { | ||
error = err | ||
} | ||
|
||
expect(() => { if (error) throw error }).to.throw(`Access to ${permission} API is globally blocked for window.ipfs`) | ||
}) | ||
|
||
after(() => { | ||
delete global.URL | ||
}) | ||
}) |