diff --git a/src/constants/whitelisted-dapps.ts b/src/constants/whitelisted-dapps.ts new file mode 100644 index 0000000..542bcf9 --- /dev/null +++ b/src/constants/whitelisted-dapps.ts @@ -0,0 +1,15 @@ +// Maps whitelisted dapps to their ENS names +export const whitelistedDapps: Array<{ url: string; dappId: string }> = [ + { url: 'https://fairdrive.dev.fairdatasociety.org', dappId: 'fairdrive-dev' }, + { url: 'https://fairdrive.fairdatasociety.org', dappId: 'fairdrive' }, + { url: 'https://fairdrive.dev.fairdatasociety.org/apps/consents', dappId: 'consents-dev' }, + { url: 'https://fairdrive.fairdatasociety.org/apps/consents', dappId: 'consents' }, + { url: 'https://app.photo.dev.fairdatasociety.org', dappId: 'photo-album-dev' }, + { url: 'https://app.photo.fairdatasociety.org', dappId: 'photo-album' }, + { url: 'https://app.dracula.dev.fairdatasociety.org', dappId: 'dracula-dev' }, + { url: 'https://app.dracula.fairdatasociety.org', dappId: 'dracula' }, + { url: 'https://fairdrive.dev.fairdatasociety.org/apps/dracula', dappId: 'dracula-dev' }, + { url: 'https://fairdrive.fairdatasociety.org/apps/dracula', dappId: 'dracula' }, + { url: 'https://fairdrive.dev.fairdatasociety.org/apps/slidezz', dappId: 'slidezz-dev' }, + { url: 'https://fairdrive.fairdatasociety.org/apps/slidezz', dappId: 'slidezz' }, +] diff --git a/src/services/fdp-storage/fdp-storage.utils.ts b/src/services/fdp-storage/fdp-storage.utils.ts index 9a57367..398c529 100644 --- a/src/services/fdp-storage/fdp-storage.utils.ts +++ b/src/services/fdp-storage/fdp-storage.utils.ts @@ -1,3 +1,4 @@ +import { whitelistedDapps } from '../../constants/whitelisted-dapps' import { assertBeeUrl } from '../../messaging/message.asserts' import { DappId } from '../../model/general.types' @@ -27,18 +28,31 @@ function extractDappIdFromRegex(url: string, regex: RegExp): string | null { return null } -export function dappUrlToId(url: string, beeUrl: string): DappId { - assertBeeUrl(beeUrl) +function extractDappIdFromWhitelistedDapps(url: string): string | undefined { + const { dappId } = whitelistedDapps.find(({ url: dappUrl }) => url.startsWith(dappUrl)) || {} + + return dappId +} - let dappId = extractDappIdFromRegex(url, constructBzzRegex(beeUrl)) +function isDappIdForged(dappId: string, url: string): boolean { + const whitelistedDapp = whitelistedDapps.find(({ dappId: currentDappId }) => currentDappId === dappId) - if (dappId) { - return dappId + if (!whitelistedDapp) { + return false } - dappId = extractDappIdFromRegex(url, constructSubdomainRegex(beeUrl)) + return !url.startsWith(whitelistedDapp.url) +} + +export function dappUrlToId(url: string, beeUrl: string): DappId { + assertBeeUrl(beeUrl) + + const dappId = + extractDappIdFromWhitelistedDapps(url) || + extractDappIdFromRegex(url, constructBzzRegex(beeUrl)) || + extractDappIdFromRegex(url, constructSubdomainRegex(beeUrl)) - if (dappId) { + if (dappId && !isDappIdForged(dappId, url)) { return dappId }