Skip to content

Commit

Permalink
fix: CP-9331 add avacloud to whitelist (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava authored Oct 10, 2024
1 parent 30e67e3 commit 29bdbdd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ describe('background/services/accounts/handlers/avalanche_getAddressesInRange.ts
return handler.handleAuthenticated(request);
};

const getPayload = (payload) =>
const getPayload = (payload, domain = 'core.app') =>
({
id: '1234',
method: DAppProviderRequest.AVALANCHE_GET_ADDRESSES_IN_RANGE,
site: {
domain: 'core.app',
domain,
tabId: 3,
},
...payload,
Expand Down Expand Up @@ -146,6 +146,21 @@ describe('background/services/accounts/handlers/avalanche_getAddressesInRange.ts
);
});

it('should call `canSkipApproval` with whitelisted domains', async () => {
const EXPOSED_DOMAINS = [
'develop.avacloud-app.pages.dev',
'avacloud.io',
'staging--ava-cloud.avacloud-app.pages.dev',
];
const request = getPayload({ params: [0, 0, 2, 2] });
await handleRequest(buildRpcCall(request));
expect(canSkipApproval).toHaveBeenCalledWith(
'core.app',
3,
EXPOSED_DOMAINS
);
});

it('sets the limit to 0 if not provided', async () => {
const { result } = await handleRequest(
buildRpcCall(getPayload({ params: [0, 0] }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type Params = [
];
import { AccountsService } from '../AccountsService';

const EXPOSED_DOMAINS = [
'develop.avacloud-app.pages.dev',
'avacloud.io',
'staging--ava-cloud.avacloud-app.pages.dev',
];

@injectable()
export class AvalancheGetAddressesInRangeHandler extends DAppRequestHandler<
Params,
Expand Down Expand Up @@ -140,7 +146,13 @@ export class AvalancheGetAddressesInRangeHandler extends DAppRequestHandler<
externalLimit: correctedExternalLimit,
});

if (await canSkipApproval(request.site.domain, request.site.tabId)) {
if (
await canSkipApproval(
request.site.domain,
request.site.tabId,
EXPOSED_DOMAINS
)
) {
return {
...request,
result: addresses,
Expand Down
7 changes: 5 additions & 2 deletions src/background/services/network/utils/getSyncDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const SYNCED_DOMAINS = [
runtime.id,
];

export const isSyncDomain = (domain: string) => {
return SYNCED_DOMAINS.some((syncDomain) => {
export const isSyncDomain = (
domain: string,
exposedDomainList: string[] = []
) => {
return [...SYNCED_DOMAINS, ...exposedDomainList].some((syncDomain) => {
// Match exact domains, but also allow subdomains (i.e. develop.core-web.pages.dev)
return syncDomain === domain || domain.endsWith(`.${syncDomain}`);
});
Expand Down
8 changes: 6 additions & 2 deletions src/utils/canSkipApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { isSyncDomain } from '@src/background/services/network/utils/getSyncDoma
import { isActiveTab } from './isActiveTab';
import { runtime } from 'webextension-polyfill';

export const canSkipApproval = async (domain: string, tabId: number) => {
export const canSkipApproval = async (
domain: string,
tabId: number,
exposedDomainList?: string[]
) => {
return (
isSyncDomain(domain) &&
isSyncDomain(domain, exposedDomainList) &&
// chrome.tabs.get(...) does not see extension popup
(domain === runtime.id || (await isActiveTab(tabId)))
);
Expand Down

0 comments on commit 29bdbdd

Please sign in to comment.