Skip to content

Commit bb861a3

Browse files
authored
fix(file-preview): safeSubresourceGwUrl (#2253)
this aims to sanitize localhost src url for embedded image/audio/video to avoid mixed-content warning in latest chrome-based browsers Rationale: #2246 (comment)
1 parent 809c55a commit bb861a3

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/files/file-preview/FilePreview.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ const Preview = (props) => {
6060
<Drag {...props}>
6161
{/* eslint-disable-next-line jsx-a11y/media-has-caption */}
6262
<audio width='100%' controls>
63-
<source src={src} />
63+
<source src={safeSubresourceGwUrl(src)} />
6464
</audio>
6565
</Drag>
6666
)
6767
case 'pdf':
6868
return (
6969
<Drag {...props}>
70-
<object className="FilePreviewPDF w-100" data={src} type='application/pdf'>
70+
<object className="FilePreviewPDF w-100" data={safeSubresourceGwUrl(src)} type='application/pdf'>
7171
{t('noPDFSupport')}
7272
<a href={src} download target='_blank' rel='noopener noreferrer' className='underline-hover navy-muted'>{t('downloadPDF')}</a>
7373
</object>
@@ -78,14 +78,14 @@ const Preview = (props) => {
7878
<Drag {...props}>
7979
{/* eslint-disable-next-line jsx-a11y/media-has-caption */}
8080
<video controls className={className}>
81-
<source src={src} />
81+
<source src={safeSubresourceGwUrl(src)} />
8282
</video>
8383
</Drag>
8484
)
8585
case 'image':
8686
return (
8787
<Drag {...props}>
88-
<img className={className} alt={name} src={src} />
88+
<img className={className} alt={name} src={safeSubresourceGwUrl(src)} />
8989
</Drag>
9090
)
9191
default: {
@@ -154,3 +154,19 @@ export default connect(
154154
'selectPublicGateway',
155155
withTranslation('files')(Preview)
156156
)
157+
158+
// Potential fix for mixed-content error when redirecting to localhost subdomain
159+
// from https://github.com/ipfs/ipfs-webui/issues/2246#issuecomment-2322192398
160+
// We do it here and not in src/bundles/config.js because we dont want IPLD
161+
// explorer to open links in path gateway, localhost is desired there.
162+
//
163+
// Context: localhost in Kubo is a subdomain gateway, so http://locahost:8080/ipfs/cid will
164+
// redirect to http://cid.ipfs.localhost:8080 – perhaps subdomains are not
165+
// interpreted as secure context correctly and that triggers forced upgrade to
166+
// https. switching to IP should help.
167+
function safeSubresourceGwUrl (url) {
168+
if (url.startsWith('http://localhost:')) {
169+
return url.replace('http://localhost:', 'http://127.0.0.1:')
170+
}
171+
return url
172+
}

0 commit comments

Comments
 (0)