fix: handle bad access when accessing paths (#2375)

This commit is contained in:
Mo
2023-07-27 13:09:03 -05:00
committed by GitHub
parent bfd2b14264
commit 804a39dabc
10 changed files with 63 additions and 44 deletions

View File

@@ -1,10 +1,7 @@
import { MessageType } from '../test/TestIpcMessage'
import { Store } from './javascripts/Main/Store/Store'
import { StoreKeys } from './javascripts/Main/Store/StoreKeys'
import { Paths, Urls } from './javascripts/Main/Types/Paths'
import { UpdateState } from './javascripts/Main/UpdateManager'
import { handleTestMessage } from './javascripts/Main/Utils/Testing'
import { isTesting } from './javascripts/Main/Utils/Utils'
import { WindowState } from './javascripts/Main/Window'
export class AppState {
@@ -27,12 +24,6 @@ export class AppState {
this.store.set(StoreKeys.LastRunVersion, this.version)
this.updates = new UpdateState(this)
if (isTesting()) {
handleTestMessage(MessageType.AppStateCall, (method, ...args) => {
;(this as any)[method](...args)
})
}
}
public isRunningVersionForFirstTime(): boolean {

View File

@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { app, ipcMain, shell } from 'electron'
import log from 'electron-log'
import fs from 'fs-extra'
@@ -9,8 +10,6 @@ import { Store } from './javascripts/Main/Store/Store'
import { StoreKeys } from './javascripts/Main/Store/StoreKeys'
import { isSnap } from './javascripts/Main/Types/Constants'
import { Paths } from './javascripts/Main/Types/Paths'
import { setupTesting } from './javascripts/Main/Utils/Testing'
import { isTesting } from './javascripts/Main/Utils/Utils'
import { CommandLineArgs } from './javascripts/Shared/CommandLineArgs'
enableExperimentalFeaturesForFileAccessFix()
@@ -39,10 +38,6 @@ if (userDataPathIndex > 0) {
migrateSnapStorage()
}
if (isTesting()) {
setupTesting()
}
log.transports.file.level = 'info'
process.on('uncaughtException', (err) => {
@@ -96,8 +91,12 @@ function migrateSnapStorage() {
fs.moveSync(fullFilePath, path.join(dest, fileName), {
overwrite: false,
})
} catch (error: any) {
console.error(`Migration: error occured while moving ${fullFilePath} to ${dest}:`, error?.message ?? error)
} catch (error) {
console.error(
`Migration: error occured while moving ${fullFilePath} to ${dest}:`,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error as any)?.message ?? error,
)
}
}
@@ -110,18 +109,22 @@ function migrateSnapStorage() {
* Backups location has not been altered by the user. Move it to the
* user documents directory
*/
console.log(`Migration: moving ${store.data.backupsLocation} to ${Paths.documentsDir}`)
const newLocation = path.join(Paths.documentsDir, path.basename(store.data.backupsLocation))
try {
fs.copySync(store.data.backupsLocation, newLocation)
} catch (error: any) {
console.error(
`Migration: error occured while moving ${store.data.backupsLocation} to ${Paths.documentsDir}:`,
error?.message ?? error,
)
const documentsDir = Paths.documentsDir
console.log(`Migration: moving ${store.data.backupsLocation} to ${documentsDir}`)
if (documentsDir) {
const newLocation = path.join(documentsDir, path.basename(store.data.backupsLocation))
try {
fs.copySync(store.data.backupsLocation, newLocation)
} catch (error) {
console.error(
`Migration: error occured while moving ${store.data.backupsLocation} to ${documentsDir}:`,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error as any)?.message ?? error,
)
}
store.set(StoreKeys.LegacyTextBackupsLocation, newLocation)
console.log('Migration: finished moving backups directory.')
}
store.set(StoreKeys.LegacyTextBackupsLocation, newLocation)
console.log('Migration: finished moving backups directory.')
}
}
}

View File

@@ -88,7 +88,7 @@ export class FilesBackupManager implements FileBackupsDevice {
return value === true
}
async getUserDocumentsDirectory(): Promise<string> {
async getUserDocumentsDirectory(): Promise<string | undefined> {
return Paths.documentsDir
}
@@ -103,7 +103,12 @@ export class FilesBackupManager implements FileBackupsDevice {
}
const LegacyTextBackupsDirectory = 'Standard Notes Backups'
return path.join(Paths.homeDir, LegacyTextBackupsDirectory)
const homeDir = Paths.homeDir
if (homeDir) {
return path.join(homeDir, LegacyTextBackupsDirectory)
}
return undefined
}
private getFileBackupsMappingFilePath(backupsLocation: string): string {

View File

@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { compareVersions } from 'compare-versions'
import log from 'electron-log'
import fs from 'fs'

View File

@@ -247,7 +247,7 @@ export class RemoteBridge implements CrossProcessBridge {
return this.fileBackups.migrateLegacyFileBackupsToNewStructure(newPath)
}
getUserDocumentsDirectory(): Promise<string> {
getUserDocumentsDirectory(): Promise<string | undefined> {
return this.fileBackups.getUserDocumentsDirectory()
}

View File

@@ -36,11 +36,19 @@ export const Paths = {
get userDataDir(): string {
return app.getPath('userData')
},
get homeDir(): string {
return app.getPath('home')
get homeDir(): string | undefined {
try {
return app.getPath('home')
} catch (error) {
return undefined
}
},
get documentsDir(): string {
return app.getPath('documents')
get documentsDir(): string | undefined {
try {
return app.getPath('documents')
} catch (error) {
return undefined
}
},
get tempDir(): string {
return app.getPath('temp')

View File

@@ -152,7 +152,7 @@ export class DesktopDevice extends WebOrDesktopDevice implements DesktopDeviceIn
return this.remoteBridge.wasLegacyTextBackupsExplicitlyDisabled()
}
getUserDocumentsDirectory(): Promise<string> {
getUserDocumentsDirectory(): Promise<string | undefined> {
return this.remoteBridge.getUserDocumentsDirectory()
}

View File

@@ -49,7 +49,7 @@ interface PlaintextBackupsMethods {
interface TextBackupsMethods {
getTextBackupsCount(location: string): Promise<number>
saveTextBackupData(location: string, data: string): Promise<void>
getUserDocumentsDirectory(): Promise<string>
getUserDocumentsDirectory(): Promise<string | undefined>
}
interface LegacyBackupsMethods {

View File

@@ -160,14 +160,21 @@ export class FilesBackupService
}
private async automaticallyEnableTextBackupsIfPreferenceNotSet(): Promise<void> {
if (this.storage.getValue(StorageKey.TextBackupsEnabled) == undefined) {
this.storage.setValue(StorageKey.TextBackupsEnabled, true)
const location = await this.device.joinPaths(
await this.device.getUserDocumentsDirectory(),
await this.prependWorkspacePathForPath(TextBackupsDirectoryName),
)
this.storage.setValue(StorageKey.TextBackupsLocation, location)
if (this.storage.getValue(StorageKey.TextBackupsEnabled) != undefined) {
return
}
this.storage.setValue(StorageKey.TextBackupsEnabled, true)
const documentsDir = await this.device.getUserDocumentsDirectory()
if (!documentsDir) {
return
}
const location = await this.device.joinPaths(
documentsDir,
await this.prependWorkspacePathForPath(TextBackupsDirectoryName),
)
this.storage.setValue(StorageKey.TextBackupsLocation, location)
}
openAllDirectoriesContainingBackupFiles(): void {

View File

@@ -170,6 +170,9 @@ export class HomeServerService
let location = await this.getHomeServerDataLocation()
if (!location) {
const documentsDirectory = await this.desktopDevice.getUserDocumentsDirectory()
if (!documentsDirectory) {
return
}
location = `${documentsDirectory}/${this.HOME_SERVER_DATA_DIRECTORY_NAME}`
}