Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function normalizedIpfsPath (urlOrPath) {
let result = urlOrPath
// Convert CID-in-subdomain URL to /ipns// path
if (IsIpfs.subdomain(urlOrPath)) {
result = subdomainToIpfsPath(urlOrPath)
}
// Drop everything before the IPFS path
result = result.replace(/^.*(\/ip(f|n)s\/.+)$/, '$1')
// Remove Unescape special characters
// https://github.com/ipfs/ipfs-companion/issues/303
result = decodeURIComponent(result)
// Return a valid IPFS path or null otherwise
return IsIpfs.path(result) ? result : null
}
exports.normalizedIpfsPath = normalizedIpfsPath
ipfsOrIpnsSubdomain (url) {
return IsIpfs.subdomain(url)
},
// Test if actions such as 'copy URL', 'pin/unpin' should be enabled for the URL
resolveToPublicSubdomainUrl (url, optionalGatewayUrl) {
// if non-subdomain return as-is
if (!IsIpfs.subdomain(url)) return url
const gateway = optionalGatewayUrl || getState().pubSubdomainGwURL
return redirectSubdomainGateway(url, gateway)
},
function isSafeToRedirect (request, runtime) {
// Do not redirect if URL includes opt-out hint
if (request.url.includes('x-ipfs-companion-no-redirect')) {
return false
}
// For now we do not redirect if cid-in-subdomain is used
// as it would break origin-based security perimeter
if (IsIpfs.subdomain(request.url)) {
return false
}
// Ignore XHR requests for which redirect would fail due to CORS bug in Firefox
// See: https://github.com/ipfs-shipyard/ipfs-companion/issues/436
if (runtime.requiresXHRCORSfix && request.type === 'xmlhttprequest' && !request.responseHeaders) {
// Sidenote on XHR Origin: Firefox 60 uses request.originUrl, Chrome 63 uses request.initiator
if (request.originUrl) {
const sourceOrigin = new URL(request.originUrl).origin
const targetOrigin = new URL(request.url).origin
if (sourceOrigin !== targetOrigin) {
log('Delaying redirect of CORS XHR until onHeadersReceived due to https://github.com/ipfs-shipyard/ipfs-companion/issues/436 :', request.url)
onHeadersReceivedRedirect.add(request.requestId)
return false
}
}
resolveToPublicUrl (urlOrPath, optionalGatewayUrl) {
const input = urlOrPath
// CID-in-subdomain is good as-is
if (IsIpfs.subdomain(input)) return input
// IPFS Paths should be attached to the public gateway
const ipfsPath = normalizedIpfsPath(input)
const gateway = optionalGatewayUrl || getState().pubGwURLString
if (ipfsPath) return pathAtHttpGateway(ipfsPath, gateway)
// Return original URL (eg. DNSLink domains) or null if not an URL
return input.startsWith('http') ? input : null
},
// Resolve URL or path to subdomain gateway
isIpfsPageActionsContext (url) {
return Boolean(url && !url.startsWith(getState().apiURLString) && (
IsIpfs.url(url) ||
IsIpfs.subdomain(url) ||
dnslinkResolver.cachedDnslink(new URL(url).hostname)
))
},