function checkCorsConfig (ipfsd) {
  if (store.get('checkedCorsConfig')) {
    // We've already checked so skip it.
    return
  }

  let config = null

  try {
    config = readConfigFile(ipfsd)
  } catch (err) {
    // This is a best effort check, dont blow up here, that should happen else where.
    // TODO: gracefully handle config errors elsewhere!
    logger.error(`[daemon] checkCorsConfig: error reading config file: ${err.message || err}`)
    return
  }

  if (config.API && config.API.HTTPHeaders && config.API.HTTPHeaders['Access-Control-Allow-Origin']) {
    const allowedOrigins = config.API.HTTPHeaders['Access-Control-Allow-Origin']
    const originsToRemove = ['*', 'webui://-']

    if (Array.isArray(allowedOrigins)) {
      const specificOrigins = allowedOrigins.filter(origin => !originsToRemove.includes(origin))

      if (specificOrigins.length !== allowedOrigins.length) {
        config.API.HTTPHeaders['Access-Control-Allow-Origin'] = specificOrigins

        try {
          writeConfigFile(ipfsd, config)
          store.set('updatedCorsConfig', Date.now())
        } catch (err) {
          logger.error(`[daemon] checkCorsConfig: error writing config file: ${err.message || err}`)
          // don't skip setting checkedCorsConfig so we try again next time time.
          return
        }
      }
    }
  }

  store.set('checkedCorsConfig', true)
}
// TODO: show warning in UI when wildcard is in the allowed origins.
show warning in UI when wildcard is in the allowed origins.
show warning in UI when wildcard is in the allowed origins .
function checkCorsConfig (ipfsd) {
  if (store.get('checkedCorsConfig')) {
    return
  }
  let config = null
  try {
    config = readConfigFile(ipfsd)
  } catch (err) {
    logger.error(`[daemon] checkCorsConfig: error reading config file: ${err.message || err}`)
    return
  }
  if (config.API && config.API.HTTPHeaders && config.API.HTTPHeaders['Access-Control-Allow-Origin']) {
    const allowedOrigins = config.API.HTTPHeaders['Access-Control-Allow-Origin']
    const originsToRemove = ['*', 'webui://-']
    if (Array.isArray(allowedOrigins)) {
      const specificOrigins = allowedOrigins.filter(origin => !originsToRemove.includes(origin))
      if (specificOrigins.length !== allowedOrigins.length) {
        config.API.HTTPHeaders['Access-Control-Allow-Origin'] = specificOrigins
        try {
          writeConfigFile(ipfsd, config)
          store.set('updatedCorsConfig', Date.now())
        } catch (err) {
          logger.error(`[daemon] checkCorsConfig: error writing config file: ${err.message || err}`)
          return
        }
      }
    }
  }
  store.set('checkedCorsConfig', true)
}
function checkCorsConfig ( ipfsd ) { if ( store . get ( 'checkedCorsConfig' ) ) { return } let config = null try { config = readConfigFile ( ipfsd ) } catch ( err ) { logger . error ( ` ${ err . message || err } ` ) return } if ( config . API && config . API . HTTPHeaders && config . API . HTTPHeaders [ 'Access-Control-Allow-Origin' ] ) { const allowedOrigins = config . API . HTTPHeaders [ 'Access-Control-Allow-Origin' ] const originsToRemove = [ '*' , 'webui://-' ] if ( Array . isArray ( allowedOrigins ) ) { const specificOrigins = allowedOrigins . filter ( origin => ! originsToRemove . includes ( origin ) ) if ( specificOrigins . length !== allowedOrigins . length ) { config . API . HTTPHeaders [ 'Access-Control-Allow-Origin' ] = specificOrigins try { writeConfigFile ( ipfsd , config ) store . set ( 'updatedCorsConfig' , Date . now ( ) ) } catch ( err ) { logger . error ( ` ${ err . message || err } ` ) return } } } } store . set ( 'checkedCorsConfig' , true ) }
TODO: show warning in UI when wildcard is in the allowed origins.
TODO : show warning in UI when wildcard is in the allowed origins .
// We've already checked so skip it. // This is a best effort check, dont blow up here, that should happen else where. // TODO: gracefully handle config errors elsewhere! // don't skip setting checkedCorsConfig so we try again next time time.