-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds async `enable` function that takes optional list of commands to grant access to via user prompt. The goal is to move away from synchronous way of accessing API instance and provide UX incentive to use `window.ipfs.enable` instead. When called without any arguments, command will just return API instance equal to the old `window.ipfs` or throw an error if IPFS Proxy is disabled in Preferences. When called with options object `{ commands: ['id','peers'] }` access rights for specified commands will be validated: - if any of the commands is denied or blocked, function will throw - if any of the commands require user approval, user will be presented with a single prompt dialog that lists all requested permissions and URL that requests them - if user approves, ACLs are saved and future calls will not trigger prompt - if user denies, ACLs are saved and an error is thrown for current and all future executions (unless user removed scope from blacklist) TODO (to be addressed in future commits) - add deprecation warning to API calls executed on `window.ipfs` - improve UX of permission dialog - add ability to return `ipfsx` version fo the API - disable `window.ipfs` injection via manifest in Chromium - stop exposing methods on `window.ipfs` - minimize the size of content script responsible for `window.ipfs` - lazy-init IPFS Proxy client on first call to `window.ipfs.enable()`
- Loading branch information
Showing
18 changed files
with
520 additions
and
94 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
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
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,54 @@ | ||
const { inApiWhitelist, createProxyWhitelistError } = require('./pre-api-whitelist') | ||
const { inNoAclPromptWhitelist, createProxyAclError } = require('./pre-acl') | ||
|
||
// Artificial API command responsible for backend orchestration | ||
// during window.ipfs.enable() | ||
function createEnableCommand (getIpfs, getState, getScope, accessControl, requestAccess) { | ||
return async (opts) => { | ||
const scope = await getScope() | ||
console.log(`[ipfs-companion] received window.ipfs.enable request from ${scope}`, opts) | ||
|
||
// Check if all access to the IPFS node is disabled | ||
if (!getState().ipfsProxy) throw new Error('User disabled access to API proxy in IPFS Companion') | ||
|
||
// NOOP if .enable() was called without any arguments | ||
if (!opts) return | ||
|
||
// Validate and prompt for any missing permissions in bulk | ||
// if a list of needed commands is announced up front | ||
if (opts.commands) { | ||
let missingAcls = [] | ||
let deniedAcls = [] | ||
for (let command of opts.commands) { | ||
// Fail fast if command is not allowed to be proxied at all | ||
if (!inApiWhitelist(command)) { | ||
throw createProxyWhitelistError(command) | ||
} | ||
// Get the current access flag to decide if it should be added | ||
// to the list of permissions to be prompted about in the next step | ||
if (!inNoAclPromptWhitelist(command)) { | ||
let access = await accessControl.getAccess(scope, command) | ||
if (!access) { | ||
missingAcls.push(command) | ||
} else if (access.allow !== true) { | ||
deniedAcls.push(command) | ||
} | ||
} | ||
} | ||
// Fail fast if user already denied any of requested permissions | ||
if (deniedAcls.length) { | ||
throw createProxyAclError(scope, deniedAcls) | ||
} | ||
// Display a single prompt with all missing permissions | ||
if (missingAcls.length) { | ||
const { allow, wildcard } = await requestAccess(scope, missingAcls) | ||
let access = await accessControl.setAccess(scope, wildcard ? '*' : missingAcls, allow) | ||
if (!access.allow) { | ||
throw createProxyAclError(scope, missingAcls) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = createEnableCommand |
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
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
Oops, something went wrong.