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

Successive SQL operations on two encrypted databases are resulting in weird behavior #13

Open
HydroCarbons opened this issue Feb 27, 2018 · 1 comment

Comments

@HydroCarbons
Copy link

HydroCarbons commented Feb 27, 2018

Successive SQL operations (querying and creating, inserting & querying) two encrypted databases are resulting in weird behavior

PACKAGE.JSON

{
  "name": "multi-db-issue-with-sqlite",
  "version": "1.0.0",
  "description": "",
  "main": "multi-db-issue.js",
  "keywords": [],
  "author": "HydroCarbons",
  "license": "ISC",
  "dependencies": {
    "sqlite-cipher": "^0.3.5"
  }
}

CODE (multi-db-issue.js)

const fs = require('fs');
var sqlite = require('sqlite-cipher');

function simulateWorkflow(databaseFilePath, databasePassword, fcbOnCompletion) {

  var bFileExist = false;
  if (fs.existsSync(databaseFilePath)) {
    bFileExist = true;
    console.log(databaseFilePath + " exist");
  }

  var c;
  c = sqlite.connect(databaseFilePath, databasePassword, 'aes-256-ctr');

  if(bFileExist == false ) {
    console.log(databaseFilePath + " is a new database");
    try {
        var createData = c.run( "CREATE TABLE `_internal` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `client_name` TEXT UNIQUE ); ");
        console.log("Creation : " + JSON.stringify(createData));
        if(createData.error) {
          return;
        }
        var insertData = c.run( "INSERT INTO `_internal` (`client_name`) VALUES (\'" + databaseFilePath + "\'); ");
        console.log("Insertion : " + JSON.stringify(insertData));
        if(insertData.error) {
          return;
        }
        var rows = c.run( "SELECT * FROM `_internal`");
        console.log("DB(" + databaseFilePath + ") query result => " + JSON.stringify(rows) );
        c.close();
        c = null;
    } catch(e1) {
      console.log("SQL Exception : " + e1);
      c.close();
      c = null;
    }

  }
  else {
  // Query contents of _internal table in case DB file is present
    try {
      var rows = c.run( "SELECT * FROM `_internal`");
      console.log("DB(" + databaseFilePath + ") query result => " + JSON.stringify(rows) );
      c.close();
      c = null;
    } catch(e1) {
      console.log("SQL Exception : " + e1);
      c.close();
      c = null;
    }
  }
}
//
// Precondition: x1.db (password: 'pass') is already present with '_internal' db and one entry (client_name = x1)
//
simulateWorkflow('x1.db', 'pass');
simulateWorkflow('x2.db', '4_jS3XEW{D#E##(@!JD');

OUTPUT

[First execution]

  **HydroCarbons**  

 node multi-db-issue.js 
x1.db exist
DB(x1.db) query result => [{"id":1,"client_name":"x1.db"}]
x2.db is a new database
Creation : {"error":{}}

[Expected]

  • No errors in creating DB

[Second execution]

  **HydroCarbons**  

 node multi-db-issue.js 
x1.db exist
DB(x1.db) query result => [{"id":1,"client_name":"x1.db"}]
x2.db exist
DB(x2.db) query result => [{"id":1,"client_name":"x1.db"}]

[Expected]

  • DB(x2.db) query result => [{"id":1,"client_name":"x2.db"}]
@HydroCarbons HydroCarbons changed the title Successive SQL operations (querying and creating, inserting & querying) two encrypted databases are resulting in weird behavior Successive SQL operations on two encrypted databases are resulting in weird behavior Feb 27, 2018
@HydroCarbons
Copy link
Author

Pre-created x1.db (password: pass)

  • Place it in parallel to multi-db-issue.js
    x1.db.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant