Skip to content

Commit

Permalink
Fix: do not sandbox safe icon URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Aug 18, 2023
1 parent 1642675 commit 1c01594
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/safe-apps/SafeAppIconCard/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { _isSafeSrc } from '.'

describe('SafeAppIconCard', () => {
it('should detect unsafe src', () => {
expect(_isSafeSrc('https://google.com/test.jpg')).toBe(false)
expect(_isSafeSrc('data:image/png;base64,')).toBe(false)
})

it('should detect safe src', () => {
expect(_isSafeSrc('https://safe-transaction-assets.safe.global/contracts/logos/0x34CfAC646f3.png')).toBe(true)
expect(_isSafeSrc('https://safe-transaction-assets.staging.5afe.dev/contracts/logos/0x34CfAC.png')).toBe(true)
expect(_isSafeSrc('/images/transactions/incoming.svg')).toBe(true)
})
})
21 changes: 21 additions & 0 deletions src/components/safe-apps/SafeAppIconCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ImageFallback from '@/components/common/ImageFallback'
import { type ReactElement, memo } from 'react'

const APP_LOGO_FALLBACK_IMAGE = `/images/apps/app-placeholder.svg`
Expand All @@ -16,6 +17,22 @@ const getIframeContent = (url: string, width: number, height: number, fallback:
`
}

export const _isSafeSrc = (src: string) => {
const allowedHosts = ['.safe.global', '.5afe.dev']
const isRelative = src.startsWith('/')

let hostname = ''
if (!isRelative) {
try {
hostname = new URL(src).hostname
} catch (e) {
return false
}
}

return isRelative || allowedHosts.some((host) => hostname.endsWith(host))
}

const SafeAppIconCard = ({
src,
alt,
Expand All @@ -29,6 +46,10 @@ const SafeAppIconCard = ({
height?: number
fallback?: string
}): ReactElement => {
if (_isSafeSrc(src)) {
return <ImageFallback src={src} alt={alt} width={width} height={height} fallbackSrc={fallback} />
}

return (
<iframe
title={alt}
Expand Down

0 comments on commit 1c01594

Please sign in to comment.