Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try {
let noIntegrationsHostnames = state.noIntegrationsHostnames
// if we are on /ipns/fqdn.tld/ then use hostname from DNSLink
const fqdn = state.currentDnslinkFqdn || state.currentFqdn
if (noIntegrationsHostnames.includes(fqdn)) {
noIntegrationsHostnames = noIntegrationsHostnames.filter(host => !host.endsWith(fqdn))
} else {
noIntegrationsHostnames.push(fqdn)
}
// console.dir('toggleSiteIntegrations', state)
await browser.storage.local.set({ noIntegrationsHostnames })
// TODO: remove below? does it still make sense in "integrations toggle" context?
// Reload the current tab to apply updated redirect preference
if (!state.currentDnslinkFqdn || !IsIpfs.ipnsUrl(state.currentTab.url)) {
// No DNSLink, reload URL as-is
await browser.tabs.reload(state.currentTab.id)
} else {
// DNSLinked websites require URL change
// from http?://gateway.tld/ipns/{fqdn}/some/path
// to http://{fqdn}/some/path
// (defaulting to http: https websites will have HSTS or a redirect)
const originalUrl = state.currentTab.url.replace(/^.*\/ipns\//, 'http://')
await browser.tabs.update(state.currentTab.id, {
// FF only: loadReplace: true,
url: originalUrl
})
}
} catch (error) {
console.error(`Unable to update integrations state due to ${error}`)
emitter.emit('render')
function validIpnsPath (path, dnsLink) {
if (IsIpfs.ipnsPath(path)) {
// we may have false-positives here, so we do additional checks below
const ipnsRoot = path.match(/^\/ipns\/([^/]+)/)[1]
// console.log('==> IPNS root', ipnsRoot)
// first check if root is a regular CID
if (IsIpfs.cid(ipnsRoot)) {
// console.log('==> IPNS is a valid CID', ipnsRoot)
return true
}
// then see if there is an DNSLink entry for 'ipnsRoot' hostname
// TODO: use dnslink cache only
if (dnsLink.readAndCacheDnslink(ipnsRoot)) {
// console.log('==> IPNS for FQDN with valid dnslink: ', ipnsRoot)
return true
}
}
return false
}
if (domain.startsWith('_dnslink.')) {
// The supplied domain contains a _dnslink component
// Check the non-_dnslink domain
dnslinkRecord = await resolveDnslink(domain.replace('_dnslink.', ''))
} else {
// Check the _dnslink subdomain
const _dnslinkDomain = `_dnslink.${domain}`
// If this throws then we propagate the error
dnslinkRecord = await resolveDnslink(_dnslinkDomain)
}
}
const result = dnslinkRecord.replace('dnslink=', '')
const domainOrCID = result.split('/')[2]
const isIPFSCID = isIPFS.cid(domainOrCID)
if (isIPFSCID || !depth) {
return result
}
return recursiveResolveDnslink(domainOrCID, depth - 1)
}
handleAddClick: function () {
// First check in db to see if name and hash are unique
var file = {};
file.name = this.state.name;
file.description = this.state.description;
file._id = this.state.hash;
file.type = FILE;
file.category = this.state.category;
console.log(file);
if (!file.name || !file.description) {
this.setState({
message: 'Files must have a name and a description'
});
return;
}
if (!multihash(file._id)) {
this.setState({
message: 'Not a valid multihash'
});
return;
}
searchNameOrHash(file.name, file._id)
.then(result => {
console.log('search result', result.length > 0);
if (result.length === 0) {
this.props.postFile(file, this.props.router);
} else {
this.setState({
message: 'A file with this name or hash already exists on the server'
});
}
});
async function handleSharePush(dispatch, getState, payload) {
const { from, hash } = payload
const state: Store = getState()
const contactList: ContactList = state.contactList
const contact = contactList.findContactInDirectory(from)
if(!contact) {
console.log('Got a share notification from unknow contact ' + from)
return
}
if(!isIpfs.multihash(hash)) {
throw 'invalid hash'
}
// Send ACK
const profile = getState().profile
const data = protocol.shareAck(profile, hash)
dispatch(pubsub.send(contact.sharesPubsubTopic, data))
const shareList: ShareList = state.shareList
const storedShare: ?Share = shareList.findByHash(hash)
if(storedShare) {
console.log('Got a share notification that we already knew: ' + storedShare.title)
return
}
apiProvider = 'https://ipfs.io/'
}
// js-ipfs-api does not provide method for fetching this
// TODO: revisit after https://github.com/ipfs/js-ipfs-api/issues/501 is addressed
// TODO: consider worst-case-scenario fallback to https://developers.google.com/speed/public-dns/docs/dns-over-https
const apiCall = `${apiProvider}api/v0/dns/${fqdn}?r=true`
const xhr = new XMLHttpRequest() // older XHR API us used because window.fetch appends Origin which causes error 403 in go-ipfs
// synchronous mode with small timeout
// (it is okay, because we do it only once, then it is cached and read via readAndCacheDnslink)
xhr.open('GET', apiCall, false)
xhr.setRequestHeader('Accept', 'application/json')
xhr.send(null)
if (xhr.status === 200) {
const dnslink = JSON.parse(xhr.responseText).Path
// console.log('readDnslinkFromTxtRecord', readDnslinkFromTxtRecord)
if (!IsIpfs.path(dnslink)) {
throw new Error(`dnslink for '${fqdn}' is not a valid IPFS path: '${dnslink}'`)
}
return dnslink
} else if (xhr.status === 500) {
// go-ipfs returns 500 if host has no dnslink or an error occurred
// TODO: find/fill an upstream bug to make this more intuitive
return false
} else {
throw new Error(xhr.statusText)
}
},
findDNSLinkHostname (url) {
const { hostname, pathname } = new URL(url)
// check //foo.tld/ipns/
if (IsIpfs.ipnsPath(pathname)) {
// we may have false-positives here, so we do additional checks below
const ipnsRoot = pathname.match(/^\/ipns\/([^/]+)/)[1]
// console.log('findDNSLinkHostname ==> inspecting IPNS root', ipnsRoot)
// Ignore PeerIDs, match DNSLink only
if (!IsIpfs.cid(ipnsRoot) && dnslinkResolver.readAndCacheDnslink(ipnsRoot)) {
// console.log('findDNSLinkHostname ==> found DNSLink for FQDN in url.pathname: ', ipnsRoot)
return ipnsRoot
}
}
// check ///foo/bar
if (dnslinkResolver.readAndCacheDnslink(hostname)) {
// console.log('findDNSLinkHostname ==> found DNSLink for url.hostname', hostname)
return hostname
}
}
const resolve = async (name, opts) => {
opts = opts || {}
if (!isIpfs.path(name)) {
throw new Error('invalid argument ' + name)
}
if (isIpfs.ipnsPath(name)) {
name = await ipfs.name.resolve(name, opts)
}
const [, , hash, ...rest] = name.split('/') // ['', 'ipfs', 'hash', ...path]
const cid = new CID(hash)
// nothing to resolve return the input
if (rest.length === 0) {
return `/ipfs/${cidToString(cid, { base: opts.cidBase })}`
}
const path = rest.join('/')
const results = ipfs._ipld.resolve(cid, path)
let value = cid
let remainderPath = path
function validIpnsPath (path, dnsLink) {
if (IsIpfs.ipnsPath(path)) {
// we may have false-positives here, so we do additional checks below
const ipnsRoot = path.match(/^\/ipns\/([^/]+)/)[1]
// console.log('==> IPNS root', ipnsRoot)
// first check if root is a regular CID
if (IsIpfs.cid(ipnsRoot)) {
// console.log('==> IPNS is a valid CID', ipnsRoot)
return true
}
// then see if there is an DNSLink entry for 'ipnsRoot' hostname
// TODO: use dnslink cache only
if (dnsLink.readAndCacheDnslink(ipnsRoot)) {
// console.log('==> IPNS for FQDN with valid dnslink: ', ipnsRoot)
return true
}
}
return false
async resolveToImmutableIpfsPath (urlOrPath) {
const path = ipfsPathValidator.resolveToIpfsPath(urlOrPath)
// Fail fast if no IPFS Path
if (!path) return null
// Resolve /ipns/ → /ipfs/
if (IsIpfs.ipnsPath(path)) {
const labels = path.split('/')
// We resolve /ipns/ as value in DNSLink cache may be out of date
const ipnsRoot = `/ipns/${labels[2]}`
// js-ipfs v0.34 does not support DNSLinks in ipfs.name.resolve: https://github.com/ipfs/js-ipfs/issues/1918
// TODO: remove ipfsNameResolveWithDnslinkFallback when js-ipfs implements DNSLink support in ipfs.name.resolve
const ipfsNameResolveWithDnslinkFallback = async (resolve) => {
try {
return await resolve()
} catch (err) {
const fqdn = ipnsRoot.replace(/^.*\/ipns\/([^/]+).*/, '$1')
if (err.message === 'Non-base58 character' && isFQDN(fqdn)) {
// js-ipfs without dnslink support, fallback to the value read from DNSLink
const dnslink = dnslinkResolver.readAndCacheDnslink(fqdn)
if (dnslink) {
// swap problematic /ipns/{fqdn} with /ipfs/{cid} and retry lookup