Skip to content

Commit

Permalink
Electron fix encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
jepiqueau committed Jul 1, 2023
1 parent 789b8f9 commit a304d8f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 59 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 5.0.5-1 (2023-07-01)

### Chore

- Update to @capacitor/core@5.0.5
- Update to @capacitor/ios@5.0.5
- Update to @capacitor/android@5.0.5

### Bug Fixes

- Electron fix encryption

# 5.0.4 (2023-06-29)

### Removed Features
Expand Down
Binary file modified android/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
22 changes: 20 additions & 2 deletions electron/src/electron-utils/utilsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class UtilsFile {
this.isEncryption = this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
? this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
: false;
console.log(`$$$$$$ this.isEncryption: ${this.isEncryption} $$$$$$`);
this.osType = this.Os.type();
switch (this.osType) {
case 'Darwin':
Expand Down Expand Up @@ -84,7 +83,6 @@ export class UtilsFile {
* @returns
*/
public getIsEncryption(): boolean {
console.log(`>>>>>> getIsEncryption this.isEncryption: ${this.isEncryption} >>>>>>`);
return this.isEncryption;
}
/**
Expand Down Expand Up @@ -598,6 +596,26 @@ export class UtilsFile {
fileStream.on('finish', resolve);
});
}
public readFileAsPromise(path: string, options: {start: number, end: number}): Promise<string> {
return new Promise((resolve, reject) => {
const fileStream = this.NodeFs.createReadStream(path, options);

const chunks: any[] = [];
fileStream.on('data', (data: any) => {
chunks.push(data);
});

fileStream.on('close', () => {
resolve(chunks.toString());
});

fileStream.on('error', (err:any) => {
const msg = err.message ? err.message : err;
reject(msg);
});
});
}

/**
* CreateFolderIfNotExists
* Create directory
Expand Down
20 changes: 7 additions & 13 deletions electron/src/electron-utils/utilsSQLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,13 +947,13 @@ export class UtilsSQLite {
throw new Error(`${msg} ${errmsg}`);
}
}
public isDatabaseEncrypted(dbName: string): boolean {
public async isDatabaseEncrypted(dbName: string): Promise<boolean> {
const msg = "isDatabaseEncrypted";
try {
const isExists: boolean = this.fileUtil.isFileExists(dbName);
if(isExists) {
const filePath = this.fileUtil.getFilePath(dbName + 'SQLite.db');
return this.isDBEncrypted(filePath);
const filePath = this.fileUtil.getFilePath(dbName);
return await this.isDBEncrypted(filePath);
} else {
throw new Error(`${msg}: Database ${dbName} does not exist`);
}
Expand All @@ -964,16 +964,11 @@ export class UtilsSQLite {
}

}
public isDBEncrypted(filePath: string): boolean {
public async isDBEncrypted(filePath: string): Promise<boolean> {
try {
const mDB = this.openOrCreateDatabase(
filePath,
"",
true,
);
this.getVersion(mDB);
this.closeDB(mDB);
return false;
const retStr = await this.fileUtil.readFileAsPromise(filePath, {start:0, end:12});
if (retStr === 'SQLite format') return false;
else return true;
} catch (error) {
return true;
}
Expand Down Expand Up @@ -1032,5 +1027,4 @@ export class UtilsSQLite {
}
return retStmt;
}

}
16 changes: 2 additions & 14 deletions electron/src/electron-utils/utilsSecret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ export class UtilsSecret {
public setEncryptSecret(passphrase: string): void {
try {
let oldpassphrase = this.getPassphrase();
console.log(`>>>> setEncryptSecret oldpassphrase: ${oldpassphrase}`)
if(oldpassphrase.length > 0 ) {
throw new Error(`setEncryptSecret: passphrase already stored`);
} else {
oldpassphrase = this.globalUtil != null ? this.globalUtil.secret : "";
if(oldpassphrase.length <= 0 ) {
throw new Error(`setEncryptSecret: globalUtil is null`);
}
console.log(`>>>> setEncryptSecret oldpassphrase: ${oldpassphrase}, passphrase ${passphrase}`)
// check if some databases were encrypted with the initial secret 'sqlite secret'
this.changeDatabaseSecret(oldpassphrase, passphrase).then(() => {

Expand Down Expand Up @@ -82,20 +80,13 @@ export class UtilsSecret {
}
private async changeDatabaseSecret(oldpassphrase: string, newpassphrase: string): Promise<void> {
try {
console.log(`>>> changeDatabaseSecret oldpassphrase: ${oldpassphrase}`);
console.log(`>>> changeDatabaseSecret newpassphrase: ${newpassphrase}`);
// get the database folder
const pathDatabase = this.fileUtil.getDatabasesPath();
console.log(`>>> changeDatabaseSecret pathDatabase: ${pathDatabase}`);
// get the list of databases
const files: string[] = await this.fileUtil.getFileList(pathDatabase);
console.log(`>>> changeDatabaseSecret files: ${files}`);
files.forEach(dbName => {
console.log(`>>> changeDatabaseSecret dbName: ${dbName}`);
files.forEach(async dbName => {
const filePath = this.fileUtil.getFilePath(dbName);
console.log(`>>> changeDatabaseSecret filePath: ${filePath}`);
const isEncrypt: boolean = this.sqliteUtil.isDBEncrypted(filePath);
console.log(`>>> changeDatabaseSecret isEncrypt: ${isEncrypt}`);
const isEncrypt: boolean = await this.sqliteUtil.isDBEncrypted(filePath);
if(isEncrypt) {

this.sqliteUtil.changePassword(filePath, oldpassphrase, newpassphrase);
Expand All @@ -108,7 +99,6 @@ export class UtilsSecret {
}
public getPassphrase(): string {
const data = this.storage.getSync('userData');
console.log(`>>>>>> data: `, data)
const keys = Object.keys(data);
if(data == null || keys.length <= 0) return "";
if (Object.keys(data).includes('passphrase')) {
Expand All @@ -120,15 +110,13 @@ export class UtilsSecret {
public setPassphrase(passphrase: string): void {
const data = this.storage.getSync('userData');
data.passphrase = passphrase;
console.log(`>>>>>> set data: `, data)
this.storage.set('userData', data,function (error:any) {
if(error) throw new Error(`setPassphrase: ${error.message}`);
});
}
public removePassphrase(): void {
const data = this.storage.getSync('userData');
delete data.passphrase;
console.log(`>>>>>> set data: `, data)
this.storage.set('userData', data,function (error:any) {
if(error) throw new Error(`removePassphrase: ${error.message}`);
});
Expand Down
10 changes: 1 addition & 9 deletions electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
if (!optionKeys.includes('database')) {
throw new Error('Must provide a database name');
}

const dbName: string = options.database;
const version: number = options.version ? options.version : 1;

Expand Down Expand Up @@ -112,13 +111,11 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
const readonly: boolean = options.readonly ? options.readonly : false;
const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
const database = this.getDatabaseConnectionOrThrowError(connName);
console.log(`@@@@@ connName: ${connName}`);

try {
if (database.isDBOpen()) {
// close the database
database.dbClose();
console.log(`@@@@@ after database.dbClose()`);
}
} catch (err) {
throw new Error(
Expand Down Expand Up @@ -236,7 +233,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
throw new Error(`Execute: ${msg}`);
}
try {
console.log(`in index.ts statements`)
const executeResult: number = database.executeSQL(
statements,
transaction,
Expand Down Expand Up @@ -738,7 +734,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {

// get the list of databases
const files: string[] = await this.fileUtil.getFileList(pathDatabase);

if (files.length > 0) {
return { values: files };
} else {
Expand Down Expand Up @@ -833,7 +828,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
throw new Error(`setEncryptionSecret: passphrase already in store`);
}
await this.closeAllConnections();
console.log(`setEncryptionSecret after closeAllConnections`);
this.secretUtil.setEncryptSecret(passphrase);
return;
} catch(err) {
Expand All @@ -852,8 +846,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
if(oldpassphrase.length <= 0) {
throw new Error(`changeEncryptionSecret: You must give the oldpassphrase`);
}
console.log(`##### oldsecret: ${oldsecret}`)
console.log(`##### oldpassphrase: ${oldpassphrase}`)
if(oldpassphrase !== oldsecret) {
throw new Error(`changeEncryptionSecret: the given oldpassphrase is wrong`);
}
Expand Down Expand Up @@ -896,7 +888,7 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
): Promise<capSQLiteResult> {
const dbName: string = this.getOptionValue(options, 'database');
try {
const isEncrypt: boolean = this.sqliteUtil.isDatabaseEncrypted(dbName + 'SQLite.db');
const isEncrypt: boolean = await this.sqliteUtil.isDatabaseEncrypted(dbName + 'SQLite.db');
return {result: isEncrypt};
} catch(err) {
throw new Error(`isDatabaseEncrypted: ${err}`);
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
"prepublishOnly": "npm run build && npm run build-electron && npm run docgen"
},
"devDependencies": {
"@capacitor/android": "^5.0.0",
"@capacitor/cli": "^5.0.0",
"@capacitor/core": "^5.0.0",
"@capacitor/android": "^5.0.5",
"@capacitor/cli": "^5.0.5",
"@capacitor/core": "^5.0.5",
"@capacitor/docgen": "^0.0.17",
"@capacitor/ios": "^5.0.0",
"@capacitor/ios": "^5.0.5",
"@ionic/eslint-config": "^0.3.0",
"@ionic/prettier-config": "^1.0.1",
"@ionic/swiftlint-config": "^1.1.2",
Expand Down
5 changes: 4 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,10 @@ export class SQLiteConnection implements ISQLiteConnection {
async getDatabaseList(): Promise<capSQLiteValues> {
try {
const res = await this.sqlite.getDatabaseList();
return Promise.resolve(res);
const values: string[] = res.values;
values.sort();
const ret = {values: values};
return Promise.resolve(ret);
} catch (err) {
return Promise.reject(err);
}
Expand Down

0 comments on commit a304d8f

Please sign in to comment.