2020-01-29 18:14:33 +01:00
|
|
|
import { cloneDeep } from 'lodash'
|
2021-01-20 17:13:14 +01:00
|
|
|
import { CAVEAT_NAMES } from '../../../../shared/constants/permissions'
|
2020-01-27 23:42:03 +01:00
|
|
|
import {
|
|
|
|
HISTORY_STORE_KEY,
|
|
|
|
LOG_IGNORE_METHODS,
|
2020-03-16 18:13:22 +01:00
|
|
|
LOG_LIMIT,
|
|
|
|
LOG_METHOD_TYPES,
|
|
|
|
LOG_STORE_KEY,
|
2020-01-27 23:42:03 +01:00
|
|
|
WALLET_PREFIX,
|
|
|
|
} from './enums'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller with middleware for logging requests and responses to restricted
|
|
|
|
* and permissions-related methods.
|
|
|
|
*/
|
|
|
|
export default class PermissionsLogController {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor({ restrictedMethods, store }) {
|
2020-01-27 23:42:03 +01:00
|
|
|
this.restrictedMethods = restrictedMethods
|
|
|
|
this.store = store
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the activity log.
|
|
|
|
*
|
2020-03-16 18:13:22 +01:00
|
|
|
* @returns {Array<Object>} The activity log.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getActivityLog() {
|
2020-01-27 23:42:03 +01:00
|
|
|
return this.store.getState()[LOG_STORE_KEY] || []
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the activity log.
|
|
|
|
*
|
|
|
|
* @param {Array<Object>} logs - The new activity log array.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
updateActivityLog(logs) {
|
2020-01-27 23:42:03 +01:00
|
|
|
this.store.updateState({ [LOG_STORE_KEY]: logs })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the permissions history log.
|
|
|
|
*
|
2020-03-16 18:13:22 +01:00
|
|
|
* @returns {Object} The permissions history log.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getHistory() {
|
2020-01-27 23:42:03 +01:00
|
|
|
return this.store.getState()[HISTORY_STORE_KEY] || {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the permissions history log.
|
|
|
|
*
|
|
|
|
* @param {Object} history - The new permissions history log object.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
updateHistory(history) {
|
2020-01-27 23:42:03 +01:00
|
|
|
this.store.updateState({ [HISTORY_STORE_KEY]: history })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the exposed account history for the given origin.
|
|
|
|
* Sets the 'last seen' time to Date.now() for the given accounts.
|
2020-12-08 20:48:47 +01:00
|
|
|
* Returns if the accounts array is empty.
|
2020-01-27 23:42:03 +01:00
|
|
|
*
|
|
|
|
* @param {string} origin - The origin that the accounts are exposed to.
|
|
|
|
* @param {Array<string>} accounts - The accounts.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
updateAccountsHistory(origin, accounts) {
|
2020-01-27 23:42:03 +01:00
|
|
|
if (accounts.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountToTimeMap = getAccountToTimeMap(accounts, Date.now())
|
|
|
|
|
|
|
|
this.commitNewHistory(origin, {
|
|
|
|
eth_accounts: {
|
|
|
|
accounts: accountToTimeMap,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-16 18:13:22 +01:00
|
|
|
* Create a permissions log middleware. Records permissions activity and history:
|
|
|
|
*
|
|
|
|
* Activity: requests and responses for restricted and most wallet_ methods.
|
|
|
|
*
|
|
|
|
* History: for each origin, the last time a permission was granted, including
|
|
|
|
* which accounts were exposed, if any.
|
2020-01-27 23:42:03 +01:00
|
|
|
*
|
2020-03-16 18:13:22 +01:00
|
|
|
* @returns {JsonRpcEngineMiddleware} The permissions log middleware.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
createMiddleware() {
|
2020-01-27 23:42:03 +01:00
|
|
|
return (req, res, next, _end) => {
|
2020-03-16 18:13:22 +01:00
|
|
|
let activityEntry, requestedMethods
|
|
|
|
const { origin, method } = req
|
2020-01-27 23:42:03 +01:00
|
|
|
const isInternal = method.startsWith(WALLET_PREFIX)
|
|
|
|
|
|
|
|
// we only log certain methods
|
|
|
|
if (
|
|
|
|
!LOG_IGNORE_METHODS.includes(method) &&
|
|
|
|
(isInternal || this.restrictedMethods.includes(method))
|
|
|
|
) {
|
2020-03-16 18:13:22 +01:00
|
|
|
activityEntry = this.logRequest(req, isInternal)
|
2020-01-27 23:42:03 +01:00
|
|
|
|
|
|
|
if (method === `${WALLET_PREFIX}requestPermissions`) {
|
2020-03-16 18:13:22 +01:00
|
|
|
// get the corresponding methods from the requested permissions so
|
|
|
|
// that we can record permissions history
|
2020-01-27 23:42:03 +01:00
|
|
|
requestedMethods = this.getRequestedMethods(req)
|
|
|
|
}
|
|
|
|
} else if (method === 'eth_requestAccounts') {
|
|
|
|
// eth_requestAccounts is a special case; we need to extract the accounts
|
|
|
|
// from it
|
2020-03-16 18:13:22 +01:00
|
|
|
activityEntry = this.logRequest(req, isInternal)
|
2020-08-19 18:27:05 +02:00
|
|
|
requestedMethods = ['eth_accounts']
|
2020-01-27 23:42:03 +01:00
|
|
|
} else {
|
|
|
|
// no-op
|
2020-08-12 21:06:57 +02:00
|
|
|
next()
|
|
|
|
return
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// call next with a return handler for capturing the response
|
2020-02-15 21:34:12 +01:00
|
|
|
next((cb) => {
|
2020-01-27 23:42:03 +01:00
|
|
|
const time = Date.now()
|
2020-03-16 18:13:22 +01:00
|
|
|
this.logResponse(activityEntry, res, time)
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
if (requestedMethods && !res.error && res.result) {
|
2020-01-27 23:42:03 +01:00
|
|
|
// any permissions or accounts changes will be recorded on the response,
|
|
|
|
// so we only log permissions history here
|
|
|
|
this.logPermissionsHistory(
|
2020-11-03 00:41:28 +01:00
|
|
|
requestedMethods,
|
|
|
|
origin,
|
|
|
|
res.result,
|
|
|
|
time,
|
2020-01-27 23:42:03 +01:00
|
|
|
method === 'eth_requestAccounts',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and commits an activity log entry, without response data.
|
|
|
|
*
|
|
|
|
* @param {Object} request - The request object.
|
|
|
|
* @param {boolean} isInternal - Whether the request is internal.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
logRequest(request, isInternal) {
|
2020-01-27 23:42:03 +01:00
|
|
|
const activityEntry = {
|
|
|
|
id: request.id,
|
|
|
|
method: request.method,
|
2020-11-03 00:41:28 +01:00
|
|
|
methodType: isInternal
|
|
|
|
? LOG_METHOD_TYPES.internal
|
|
|
|
: LOG_METHOD_TYPES.restricted,
|
2020-01-27 23:42:03 +01:00
|
|
|
origin: request.origin,
|
2020-03-16 18:13:22 +01:00
|
|
|
request: cloneDeep(request),
|
2020-01-27 23:42:03 +01:00
|
|
|
requestTime: Date.now(),
|
|
|
|
response: null,
|
|
|
|
responseTime: null,
|
|
|
|
success: null,
|
|
|
|
}
|
|
|
|
this.commitNewActivity(activityEntry)
|
2020-03-16 18:13:22 +01:00
|
|
|
return activityEntry
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-16 18:13:22 +01:00
|
|
|
* Adds response data to an existing activity log entry.
|
|
|
|
* Entry assumed already committed (i.e., in the log).
|
2020-01-27 23:42:03 +01:00
|
|
|
*
|
2020-03-16 18:13:22 +01:00
|
|
|
* @param {Object} entry - The entry to add a response to.
|
2020-01-27 23:42:03 +01:00
|
|
|
* @param {Object} response - The response object.
|
|
|
|
* @param {number} time - Output from Date.now()
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
logResponse(entry, response, time) {
|
2020-03-16 18:13:22 +01:00
|
|
|
if (!entry || !response) {
|
2020-01-27 23:42:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
entry.response = cloneDeep(response)
|
2020-01-27 23:42:03 +01:00
|
|
|
entry.responseTime = time
|
|
|
|
entry.success = !response.error
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit a new entry to the activity log.
|
|
|
|
* Removes the oldest entry from the log if it exceeds the log limit.
|
|
|
|
*
|
|
|
|
* @param {Object} entry - The activity log entry.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
commitNewActivity(entry) {
|
2020-01-27 23:42:03 +01:00
|
|
|
const logs = this.getActivityLog()
|
|
|
|
|
|
|
|
// add new entry to end of log
|
|
|
|
logs.push(entry)
|
|
|
|
|
|
|
|
// remove oldest log if exceeding size limit
|
|
|
|
if (logs.length > LOG_LIMIT) {
|
|
|
|
logs.shift()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateActivityLog(logs)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new permissions history log entries, if any, and commit them.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} requestedMethods - The method names corresponding to the requested permissions.
|
|
|
|
* @param {string} origin - The origin of the permissions request.
|
|
|
|
* @param {Array<IOcapLdCapability} result - The permissions request response.result.
|
|
|
|
* @param {string} time - The time of the request, i.e. Date.now().
|
|
|
|
* @param {boolean} isEthRequestAccounts - Whether the permissions request was 'eth_requestAccounts'.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
logPermissionsHistory(
|
|
|
|
requestedMethods,
|
|
|
|
origin,
|
|
|
|
result,
|
|
|
|
time,
|
|
|
|
isEthRequestAccounts,
|
2020-03-16 18:13:22 +01:00
|
|
|
) {
|
2020-01-27 23:42:03 +01:00
|
|
|
let accounts, newEntries
|
|
|
|
|
|
|
|
if (isEthRequestAccounts) {
|
|
|
|
accounts = result
|
|
|
|
const accountToTimeMap = getAccountToTimeMap(accounts, time)
|
|
|
|
|
|
|
|
newEntries = {
|
2020-11-03 00:41:28 +01:00
|
|
|
eth_accounts: {
|
2020-01-27 23:42:03 +01:00
|
|
|
accounts: accountToTimeMap,
|
|
|
|
lastApproved: time,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Records new "lastApproved" times for the granted permissions, if any.
|
|
|
|
// Special handling for eth_accounts, in order to record the time the
|
|
|
|
// accounts were last seen or approved by the origin.
|
|
|
|
newEntries = result
|
2020-03-16 18:13:22 +01:00
|
|
|
.map((perm) => {
|
|
|
|
if (perm.parentCapability === 'eth_accounts') {
|
|
|
|
accounts = this.getAccountsFromPermission(perm)
|
|
|
|
}
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
return perm.parentCapability
|
|
|
|
})
|
|
|
|
.reduce((acc, method) => {
|
|
|
|
// all approved permissions will be included in the response,
|
|
|
|
// not just the newly requested ones
|
|
|
|
if (requestedMethods.includes(method)) {
|
|
|
|
if (method === 'eth_accounts') {
|
|
|
|
const accountToTimeMap = getAccountToTimeMap(accounts, time)
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
acc[method] = {
|
|
|
|
lastApproved: time,
|
|
|
|
accounts: accountToTimeMap,
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
2020-03-16 18:13:22 +01:00
|
|
|
} else {
|
|
|
|
acc[method] = { lastApproved: time }
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
2020-01-27 23:42:03 +01:00
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
return acc
|
|
|
|
}, {})
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(newEntries).length > 0) {
|
|
|
|
this.commitNewHistory(origin, newEntries)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit new entries to the permissions history log.
|
|
|
|
* Merges the history for the given origin, overwriting existing entries
|
|
|
|
* with the same key (permission name).
|
|
|
|
*
|
|
|
|
* @param {string} origin - The requesting origin.
|
|
|
|
* @param {Object} newEntries - The new entries to commit.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
commitNewHistory(origin, newEntries) {
|
2020-01-27 23:42:03 +01:00
|
|
|
// a simple merge updates most permissions
|
|
|
|
const history = this.getHistory()
|
|
|
|
const newOriginHistory = {
|
|
|
|
...history[origin],
|
|
|
|
...newEntries,
|
|
|
|
}
|
|
|
|
|
|
|
|
// eth_accounts requires special handling, because of information
|
|
|
|
// we store about the accounts
|
2020-11-03 00:41:28 +01:00
|
|
|
const existingEthAccountsEntry =
|
2020-08-19 18:27:05 +02:00
|
|
|
history[origin] && history[origin].eth_accounts
|
|
|
|
const newEthAccountsEntry = newEntries.eth_accounts
|
2020-03-16 18:13:22 +01:00
|
|
|
|
2020-01-27 23:42:03 +01:00
|
|
|
if (existingEthAccountsEntry && newEthAccountsEntry) {
|
|
|
|
// we may intend to update just the accounts, not the permission
|
|
|
|
// itself
|
2020-11-03 00:41:28 +01:00
|
|
|
const lastApproved =
|
2020-01-27 23:42:03 +01:00
|
|
|
newEthAccountsEntry.lastApproved ||
|
|
|
|
existingEthAccountsEntry.lastApproved
|
|
|
|
|
|
|
|
// merge old and new eth_accounts history entries
|
2020-08-19 18:27:05 +02:00
|
|
|
newOriginHistory.eth_accounts = {
|
2020-01-27 23:42:03 +01:00
|
|
|
lastApproved,
|
|
|
|
accounts: {
|
|
|
|
...existingEthAccountsEntry.accounts,
|
|
|
|
...newEthAccountsEntry.accounts,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
history[origin] = newOriginHistory
|
|
|
|
|
|
|
|
this.updateHistory(history)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all requested methods from a permissions request.
|
|
|
|
*
|
|
|
|
* @param {Object} request - The request object.
|
2020-03-16 18:13:22 +01:00
|
|
|
* @returns {Array<string>} The names of the requested permissions.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getRequestedMethods(request) {
|
2020-01-27 23:42:03 +01:00
|
|
|
if (
|
|
|
|
!request.params ||
|
2020-02-12 17:17:13 +01:00
|
|
|
!request.params[0] ||
|
2020-01-27 23:42:03 +01:00
|
|
|
typeof request.params[0] !== 'object' ||
|
|
|
|
Array.isArray(request.params[0])
|
|
|
|
) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return Object.keys(request.params[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the permitted accounts from an eth_accounts permissions object.
|
|
|
|
* Returns an empty array if the permission is not eth_accounts.
|
|
|
|
*
|
|
|
|
* @param {Object} perm - The permissions object.
|
2020-03-16 18:13:22 +01:00
|
|
|
* @returns {Array<string>} The permitted accounts.
|
2020-01-27 23:42:03 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getAccountsFromPermission(perm) {
|
2020-01-27 23:42:03 +01:00
|
|
|
if (perm.parentCapability !== 'eth_accounts' || !perm.caveats) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
const accounts = new Set()
|
2020-01-27 23:42:03 +01:00
|
|
|
for (const caveat of perm.caveats) {
|
|
|
|
if (
|
|
|
|
caveat.name === CAVEAT_NAMES.exposedAccounts &&
|
|
|
|
Array.isArray(caveat.value)
|
|
|
|
) {
|
|
|
|
for (const value of caveat.value) {
|
2020-03-16 18:13:22 +01:00
|
|
|
accounts.add(value)
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
return [...accounts]
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper functions
|
|
|
|
|
2020-03-16 18:13:22 +01:00
|
|
|
/**
|
|
|
|
* Get a map from account addresses to the given time.
|
|
|
|
*
|
|
|
|
* @param {Array<string>} accounts - An array of addresses.
|
|
|
|
* @param {number} time - A time, e.g. Date.now().
|
|
|
|
* @returns {Object} A string:number map of addresses to time.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function getAccountToTimeMap(accounts, time) {
|
|
|
|
return accounts.reduce((acc, account) => ({ ...acc, [account]: time }), {})
|
2020-01-27 23:42:03 +01:00
|
|
|
}
|