Skip to content

Commit e8dc4e4

Browse files
Fix Market and NFT Icons
1 parent 9337aab commit e8dc4e4

File tree

7 files changed

+50
-33
lines changed

7 files changed

+50
-33
lines changed

browser/ui/webui/brave_wallet/market/market_ui.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
#include <string>
99

10+
#include "brave/browser/ui/webui/untrusted_sanitized_image_source.h"
1011
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
1112
#include "brave/components/constants/webui_url_constants.h"
1213
#include "brave/components/l10n/common/localization_util.h"
1314
#include "brave/components/market_display/resources/grit/market_display_generated_map.h"
15+
#include "chrome/browser/profiles/profile.h"
1416
#include "chrome/grit/browser_resources.h"
1517
#include "chrome/grit/generated_resources.h"
1618
#include "components/grit/brave_components_resources.h"
19+
#include "content/public/browser/url_data_source.h"
1720
#include "content/public/browser/web_contents.h"
1821
#include "content/public/browser/web_ui_data_source.h"
1922
#include "ui/webui/resources/grit/webui_resources.h"
@@ -55,7 +58,7 @@ UntrustedMarketUI::UntrustedMarketUI(content::WebUI* web_ui)
5558
untrusted_source->OverrideContentSecurityPolicy(
5659
network::mojom::CSPDirectiveName::ImgSrc,
5760
std::string("img-src 'self' https://assets.cgproxy.brave.com "
58-
"chrome-untrusted://resources;"));
61+
"chrome-untrusted://resources chrome-untrusted://image;"));
5962

6063
untrusted_source->AddResourcePath("load_time_data_deprecated.js",
6164
IDR_WEBUI_JS_LOAD_TIME_DATA_DEPRECATED_JS);
@@ -67,6 +70,10 @@ UntrustedMarketUI::UntrustedMarketUI(content::WebUI* web_ui)
6770
untrusted_source->AddString("braveWalletNftBridgeUrl", kUntrustedNftURL);
6871
untrusted_source->AddString("braveWalletMarketUiBridgeUrl",
6972
kUntrustedMarketURL);
73+
74+
Profile* profile = Profile::FromWebUI(web_ui);
75+
content::URLDataSource::Add(
76+
profile, std::make_unique<UntrustedSanitizedImageSource>(profile));
7077
}
7178

7279
UntrustedMarketUI::~UntrustedMarketUI() = default;

browser/ui/webui/brave_wallet/nft/nft_ui.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
#include <string>
99

10+
#include "brave/browser/ui/webui/untrusted_sanitized_image_source.h"
1011
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
1112
#include "brave/components/constants/webui_url_constants.h"
1213
#include "brave/components/l10n/common/localization_util.h"
1314
#include "brave/components/nft_display/resources/grit/nft_display_generated_map.h"
15+
#include "chrome/browser/profiles/profile.h"
1416
#include "chrome/grit/browser_resources.h"
1517
#include "chrome/grit/generated_resources.h"
1618
#include "components/grit/brave_components_resources.h"
19+
#include "content/public/browser/url_data_source.h"
1720
#include "content/public/browser/web_contents.h"
1821
#include "content/public/browser/web_ui_data_source.h"
1922
#include "ui/webui/resources/grit/webui_resources.h"
@@ -59,7 +62,12 @@ UntrustedNftUI::UntrustedNftUI(content::WebUI* web_ui)
5962
kUntrustedMarketURL);
6063
untrusted_source->OverrideContentSecurityPolicy(
6164
network::mojom::CSPDirectiveName::ImgSrc,
62-
std::string("img-src 'self' https: data:;"));
65+
std::string("img-src 'self' chrome-untrusted://resources "
66+
"chrome-untrusted://image;"));
67+
68+
Profile* profile = Profile::FromWebUI(web_ui);
69+
content::URLDataSource::Add(
70+
profile, std::make_unique<UntrustedSanitizedImageSource>(profile));
6371
}
6472

6573
UntrustedNftUI::~UntrustedNftUI() = default;

components/brave_wallet_ui/components/asset-name-and-icon/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export const AssetNameAndIcon = (props: Props) => {
2525
return (
2626
<StyledWrapper>
2727
<AssetIcon
28-
// This usage is good, but it's breaking because it looks like it's being loaded from chrome-untrusted://market-display frame
29-
// the image is coming from https://assets.cgproxy.brave.com
30-
src={`chrome://image?url=${encodeURIComponent(assetLogo)}&staticEncode=true16`}
28+
src={`chrome-untrusted://image?url=${encodeURIComponent(
29+
assetLogo
30+
)}&staticEncode=true`}
3131
loading='lazy'
3232
/>
3333
<NameAndSymbolWrapper>

components/brave_wallet_ui/components/shared/create-placeholder-icon/index.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import { BraveWallet } from '../../../constants/types'
1313

1414
// Utils
1515
import {
16-
stripERC20TokenImageURL,
1716
isRemoteImageURL,
1817
isValidIconExtension,
1918
isDataURL,
20-
isComponentInStorybook,
19+
isComponentInStorybook
2120
} from '../../../utils/string-utils'
2221
import { isNativeAsset } from '../../../utils/asset-utils'
2322

@@ -73,34 +72,34 @@ export function withPlaceholderIcon<
7372
const nativeAssetLogo =
7473
isNative && asset ? makeNativeAssetLogo(asset.symbol, asset.chainId) : ''
7574

76-
const tokenImageURL = stripERC20TokenImageURL(
77-
nativeAssetLogo || asset?.logo || ''
78-
)
79-
const isRemoteURL = isRemoteImageURL(tokenImageURL)
75+
const initialTokenImageURL = nativeAssetLogo || asset?.logo || ''
76+
77+
const isRemoteURL = isRemoteImageURL(initialTokenImageURL)
8078

8179
const isNonFungibleToken = asset?.isNft || asset?.isErc721
8280

81+
const tokenImageURL =
82+
isRemoteURL || isNonFungibleToken || isNative
83+
? initialTokenImageURL
84+
: `chrome://erc-token-images/${encodeURIComponent(
85+
initialTokenImageURL
86+
)}`
87+
8388
// memos + computed
8489
const isValidIcon = React.useMemo(() => {
8590
if (isStorybook) {
8691
return !!asset?.logo
8792
}
8893

89-
const isDataUri = isDataURL(asset?.logo)
94+
const isDataUri = isDataURL(tokenImageURL)
9095

9196
if (isRemoteURL || isDataUri) {
92-
return tokenImageURL?.includes('data:image/') ||
93-
isNonFungibleToken
97+
return tokenImageURL?.includes('data:image/') || isNonFungibleToken
9498
? true
95-
: isValidIconExtension(new URL(asset?.logo || '').pathname)
99+
: isValidIconExtension(new URL(tokenImageURL).pathname)
96100
}
97101
return false
98-
}, [
99-
asset?.logo,
100-
isRemoteURL,
101-
tokenImageURL,
102-
isNonFungibleToken
103-
])
102+
}, [asset?.logo, isRemoteURL, tokenImageURL, isNonFungibleToken])
104103

105104
const needsPlaceholder =
106105
(tokenImageURL === '' || !isValidIcon) && nativeAssetLogo === ''
@@ -117,7 +116,9 @@ export function withPlaceholderIcon<
117116

118117
const remoteImage = React.useMemo(() => {
119118
if (isRemoteURL) {
120-
return isStorybook ? tokenImageURL || '' : `chrome://image?url=${encodeURIComponent(tokenImageURL)}&staticEncode=true`
119+
return `chrome://image?url=${encodeURIComponent(
120+
tokenImageURL
121+
)}&staticEncode=true`
121122
}
122123
return ''
123124
}, [isRemoteURL, tokenImageURL])
@@ -127,7 +128,7 @@ export function withPlaceholderIcon<
127128
return null
128129
}
129130

130-
const icon = nativeAssetLogo || (isRemoteURL ? remoteImage : asset?.logo)
131+
const icon = nativeAssetLogo || (isRemoteURL ? remoteImage : tokenImageURL)
131132

132133
if (needsPlaceholder || !icon) {
133134
return (

components/brave_wallet_ui/components/shared/nft-icon/nft-icon.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export const NftIcon = (props: NftIconProps) => {
5555
const nftIframeUrl = React.useMemo(() => {
5656
const urlParams = new URLSearchParams({
5757
displayMode: 'icon',
58-
// appears to still be broken. Seems this is being used in brave://wallet/crypto/accounts/<account-id>/nfts to display nft images
59-
// seems to be not working because we're trying to load from chrome-untrusted://nft-display frame which might not be allowed even with the CSP set correctly
60-
icon: `chrome://image?url=${encodeURIComponent(remoteImage)}&staticEncode=true&test=8`
58+
icon: `chrome-untrusted://image?url=${encodeURIComponent(
59+
remoteImage
60+
)}&staticEncode=true`
6161
})
6262
return `chrome-untrusted://nft-display?${urlParams.toString()}`
6363
}, [remoteImage])

components/brave_wallet_ui/nft/components/nft-details/nft-screen.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,7 @@ export const NftScreen = ({ selectedAsset, tokenNetwork }: Props) => {
233233
spamTokenBalancesRegistry
234234
) && (
235235
<>
236-
<Alert
237-
type='info'
238-
>
239-
{getLocale('braveWalletUnownedNftAlert')}
240-
</Alert>
236+
<Alert type='info'>{getLocale('braveWalletUnownedNftAlert')}</Alert>
241237
<VerticalSpace space='24px' />
242238
</>
243239
)}
@@ -249,7 +245,9 @@ export const NftScreen = ({ selectedAsset, tokenNetwork }: Props) => {
249245
<NftMultimedia
250246
as='img'
251247
visible={!isFetchingNFTMetadata}
252-
src={`chrome://image?url=${encodeURIComponent(nftMetadata?.imageURL || '')}&staticEncode=true9`}
248+
src={`chrome-untrusted://image?url=${encodeURIComponent(
249+
nftMetadata?.imageURL || ''
250+
)}&staticEncode=true`}
253251
/>
254252
) : (
255253
<NftMultimedia

components/brave_wallet_ui/nft/nft_script.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ const imageSrc = stripChromeImageURL(imageUrl) ?? Placeholder
3737
function render() {
3838
const imgEl = document.getElementById('nft-image') as HTMLImageElement
3939
// update img src
40-
imgEl.src = displayMode === 'details' && nftMetadata ? mediaUrl : imageSrc
40+
imgEl.src =
41+
displayMode === 'details' && nftMetadata
42+
? `chrome-untrusted://image?url=${encodeURIComponent(mediaUrl)}`
43+
: imageSrc
4144
}
4245

4346
document.addEventListener('DOMContentLoaded', render)

0 commit comments

Comments
 (0)