Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect invalid or corrupted repository #2067

Merged
merged 4 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,9 @@
"startupFailedDialog": {
"title": "IPFS Desktop Startup Has Failed",
"message": "IPFS node has encountered an error and startup could not be completed:"
},
"invalidRepositoryDialog": {
"title": "Invalid IPFS Repository or Configuration File",
"message": "The repository at “{ path }” is invalid. The “config” file must be a valid JSON.\n\nBefore starting IPFS Desktop again, please fix the configuration file or rename the old repository to “.ipfs.backup”."
}
}
45 changes: 44 additions & 1 deletion src/daemon/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { app, BrowserWindow } = require('electron')
const { join } = require('path')
const fs = require('fs-extra')
const { multiaddr } = require('multiaddr')
Expand Down Expand Up @@ -315,12 +316,54 @@ async function checkPorts (ipfsd) {
logger.info('[daemon] ports updated')
}

function checkValidConfig (ipfsd) {
if (!fs.pathExistsSync(ipfsd.path)) {
// If the repository doesn't exist, skip verification.
return true
}

try {
const stats = fs.statSync(ipfsd.path)
if (!stats.isDirectory()) {
throw new Error('IPFS_PATH must be a directory')
}

if (!configExists(ipfsd)) {
// Config is generated automatically if it doesn't exist.
return true
}

// This should catch errors such having no configuration file,
// IPFS_DIR not being a directory, or the configuration file
// being corrupted.
readConfigFile(ipfsd)
return true
} catch (e) {
// Save to error.log
logger.error(e)

// Hide other windows so the user focus in on the dialog
BrowserWindow.getAllWindows().forEach(w => w.hide())

// Show blocking dialog
showDialog({
title: i18n.t('invalidRepositoryDialog.title'),
message: i18n.t('invalidRepositoryDialog.message', { path: ipfsd.path }),
buttons: [i18n.t('quit')]
})

// Only option is to quit
app.quit()
}
}

module.exports = Object.freeze({
configPath,
configExists,
apiFileExists,
rmApiFile,
applyDefaults,
migrateConfig,
checkPorts
checkPorts,
checkValidConfig
})
17 changes: 15 additions & 2 deletions src/daemon/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const i18n = require('i18next')
const { showDialog } = require('../dialogs')
const logger = require('../common/logger')
const { getCustomBinary } = require('../custom-ipfs-binary')
const { applyDefaults, migrateConfig, checkPorts, configExists, rmApiFile, apiFileExists } = require('./config')
const { applyDefaults, migrateConfig, checkPorts, configExists, checkValidConfig, rmApiFile, apiFileExists } = require('./config')
const showMigrationPrompt = require('./migration-prompt')

function cannotConnectDialog (addr) {
Expand Down Expand Up @@ -40,6 +40,10 @@ async function spawn ({ flags, path }) {
args: flags
})

if (!checkValidConfig(ipfsd)) {
throw new Error(`repository at ${ipfsd.path} is invalid`)
}

if (configExists(ipfsd)) {
migrateConfig(ipfsd)
return { ipfsd, isRemote: false }
Expand Down Expand Up @@ -166,7 +170,16 @@ async function startIpfsWithLogs (ipfsd) {
}

module.exports = async function (opts) {
const { ipfsd, isRemote } = await spawn(opts)
let ipfsd, isRemote

try {
const res = await spawn(opts)
ipfsd = res.ipfsd
isRemote = res.isRemote
} catch (err) {
return { err: err.toString() }
}

if (!isRemote) {
await checkPorts(ipfsd)
}
Expand Down