Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only prompt for permission check once per origin #1395

Merged
merged 4 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion main/api/origins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import store from '../store'
const dev = process.env.NODE_ENV === 'development'

const activeExtensionChecks: Record<string, Promise<boolean>> = {}
const activePermissionChecks: Record<string, Promise<Permission | undefined>> = {}
const extensionPrefixes = {
firefox: 'moz-extension',
safari: 'safari-web-extension'
Expand Down Expand Up @@ -82,7 +83,11 @@ async function requestExtensionPermission(extension: FrameExtension) {
async function requestPermission(address: Address, fullPayload: RPCRequestPayload) {
const { _origin: originId, ...payload } = fullPayload

return new Promise<Permission | undefined>((resolve) => {
if (originId in activePermissionChecks) {
return activePermissionChecks[originId]
}

const result = new Promise<Permission | undefined>((resolve) => {
const request: AccessRequest = {
payload,
handlerId: originId,
Expand All @@ -95,9 +100,14 @@ async function requestPermission(address: Address, fullPayload: RPCRequestPayloa
const { name: originName } = store('main.origins', originId)
const permission = storeApi.getPermission(address, originName)

delete activePermissionChecks[originId]
resolve(permission)
})
})

activePermissionChecks[originId] = result

return result
}

export function updateOrigin(
Expand Down
67 changes: 56 additions & 11 deletions test/main/api/origins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ describe('#isTrusted', () => {

beforeEach(() => {
store.set('main.origins', frameTestOriginId, { name: 'test.frame.eth' })
store.set('main.permissions', {})
})

describe('extension requests', () => {
Expand Down Expand Up @@ -377,10 +378,48 @@ describe('#isTrusted', () => {
}
})

cb()
setTimeout(cb, 1000)
})

return expect(isTrusted(payload)).resolves
const runTest = isTrusted(payload)

jest.runAllTimers()

return expect(runTest).resolves
})

it('sends a response to all permission requests once the user trusts the origin', async () => {
const address = '0xDAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5'
const payload1 = { method: 'wallet_getEthereumAccounts', _origin: frameTestOriginId }
const payload2 = { method: 'eth_accounts', _origin: frameTestOriginId }

accounts.current.mockReturnValue({ address })

accounts.addRequest.mockImplementationOnce((request, cb) => {
setTimeout(() => {
// simulate user accepting the request after both RPC requests are received
store.set('main.permissions', address, {
[frameTestOriginId]: {
origin: 'test.frame.eth',
provider: true
}
})

cb()
}, 1000)
})

const runTest = Promise.all([isTrusted(payload1), isTrusted(payload2)]).then(
([isPayload1Trusted, isPayload2Trusted]) => {
expect(accounts.addRequest).toHaveBeenCalledTimes(1)
expect(isPayload1Trusted).toBe(true)
expect(isPayload2Trusted).toBe(true)
}
)

jest.runAllTimers()

return runTest
})

const userActions = [
Expand All @@ -398,17 +437,23 @@ describe('#isTrusted', () => {

// simulate user acting on request
accounts.addRequest.mockImplementationOnce((request, cb) => {
store.set('main.permissions', address, {
'c004cc87-bfa3-50f5-812f-3d70dd8f82c6': {
origin: 'test.frame.eth',
provider: permissionGranted
}
})

cb()
setTimeout(() => {
store.set('main.permissions', address, {
'c004cc87-bfa3-50f5-812f-3d70dd8f82c6': {
origin: 'test.frame.eth',
provider: permissionGranted
}
})

cb()
}, 1000)
})

return expect(isTrusted(payload)).resolves.toBe(permissionGranted)
const runTest = isTrusted(payload)

jest.runAllTimers()

return expect(runTest).resolves.toBe(permissionGranted)
})
})
})
Expand Down