Skip to content

Commit

Permalink
feat: add whitelisted dapps (#129)
Browse files Browse the repository at this point in the history
* feat: add whitelisted dapps

* fix: remove localhost from whitelisted dapps

* fix: simplify check for forged dapp id
  • Loading branch information
tomicvladan authored Apr 19, 2023
1 parent 12dfca3 commit 1b7a878
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/constants/whitelisted-dapps.ts
Original file line number Diff line number Diff line change
@@ -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' },
]
28 changes: 21 additions & 7 deletions src/services/fdp-storage/fdp-storage.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { whitelistedDapps } from '../../constants/whitelisted-dapps'
import { assertBeeUrl } from '../../messaging/message.asserts'
import { DappId } from '../../model/general.types'

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 1b7a878

Please sign in to comment.