From f29dc409f8345ae0898e1b66cc11c3cb43f9d3b2 Mon Sep 17 00:00:00 2001 From: Garrett Date: Thu, 21 Sep 2023 13:55:51 -0700 Subject: [PATCH 1/5] Encode using crypto api --- src/renderer/src/electron/index.js | 3 ++ src/renderer/src/progress/index.js | 54 ++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/electron/index.js b/src/renderer/src/electron/index.js index a20b50acc..cbf850110 100644 --- a/src/renderer/src/electron/index.js +++ b/src/renderer/src/electron/index.js @@ -11,6 +11,7 @@ export let remote = {}; export let app = null; export let path = null; export let log = null; +export let crypto = null if (isElectron) { try { @@ -19,6 +20,8 @@ if (isElectron) { fs = require("fs-extra"); // File System os = require("os"); + crypto = require("crypto") + remote = require("@electron/remote"); app = remote.app; diff --git a/src/renderer/src/progress/index.js b/src/renderer/src/progress/index.js index 8e39c7e97..85af1d9ad 100644 --- a/src/renderer/src/progress/index.js +++ b/src/renderer/src/progress/index.js @@ -7,8 +7,9 @@ import { appDirectory, stubSaveFolderPath, conversionSaveFolderPath, + homeDirectory, } from "../dependencies/simple.js"; -import { fs } from "../electron/index.js"; +import { fs, crypto } from "../electron/index.js"; import { joinPath, runOnLoad } from "../globals.js"; import { merge } from "../stories/pages/utils.js"; import { updateAppProgress, updateFile } from "./update.js"; @@ -16,16 +17,65 @@ import { updateURLParams } from "../../utils/url.js"; export * from "./update"; +var re = /[0-9A-Fa-f]{6}/g; + +function encode(message) { + if (!crypto) return message + const mykey = crypto.createCipher('aes-128-cbc', homeDirectory); + const mystr = mykey.update(message, 'utf8', 'hex') + return mystr + mykey.final('hex'); +} + +// Try to decode the value +function decode(message) { + + if (!crypto || !/[0-9A-Fa-f]{6}/g.test(message)) return message + + + try { + const mykey = crypto.createDecipher('aes-128-cbc', homeDirectory); + const mystr = mykey.update(message, 'hex', 'utf8') + return mystr + mykey.final('utf8'); + } catch { + return message + } +} + +function drill(o, callback) { + if (o && typeof o === 'object') { + const copy = { ...o } + for (let k in copy) copy[k] = drill(copy[k], callback) + return copy + } else return callback(o) +} + +function encodeObject(o) { + return drill(o, (v) => typeof v === 'string' ? encode(v) : v) +} + +function decodeObject(o) { + return drill(o, (v) => typeof v === 'string' ? decode(v) : v) +} + + + class GlobalAppConfig { path = `${appDirectory}/config.json`; data = {}; constructor() { const exists = fs ? fs.existsSync(this.path) : localStorage[this.path]; - if (exists) this.data = JSON.parse(fs ? fs.readFileSync(this.path) : localStorage.getItem(this.path)); + if (exists) { + const data = JSON.parse(fs ? fs.readFileSync(this.path) : localStorage.getItem(this.path)); + this.data = decodeObject(data) + } } save() { + + console.log('Saving', this.data, encodeObject(this.data)) + + if (fs) fs.writeFileSync(this.path, JSON.stringify(this.data, null, 2)); else localStorage.setItem(this.path, JSON.stringify(this.data)); } From f12cda57f7b72a66d57ebb20b55628b353c04c85 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Sep 2023 20:57:05 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/renderer/src/electron/index.js | 4 +-- src/renderer/src/progress/index.js | 42 +++++++++++++----------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/renderer/src/electron/index.js b/src/renderer/src/electron/index.js index cbf850110..4a9a7326a 100644 --- a/src/renderer/src/electron/index.js +++ b/src/renderer/src/electron/index.js @@ -11,7 +11,7 @@ export let remote = {}; export let app = null; export let path = null; export let log = null; -export let crypto = null +export let crypto = null; if (isElectron) { try { @@ -20,7 +20,7 @@ if (isElectron) { fs = require("fs-extra"); // File System os = require("os"); - crypto = require("crypto") + crypto = require("crypto"); remote = require("@electron/remote"); app = remote.app; diff --git a/src/renderer/src/progress/index.js b/src/renderer/src/progress/index.js index 85af1d9ad..50205ad9f 100644 --- a/src/renderer/src/progress/index.js +++ b/src/renderer/src/progress/index.js @@ -20,45 +20,41 @@ export * from "./update"; var re = /[0-9A-Fa-f]{6}/g; function encode(message) { - if (!crypto) return message - const mykey = crypto.createCipher('aes-128-cbc', homeDirectory); - const mystr = mykey.update(message, 'utf8', 'hex') - return mystr + mykey.final('hex'); + if (!crypto) return message; + const mykey = crypto.createCipher("aes-128-cbc", homeDirectory); + const mystr = mykey.update(message, "utf8", "hex"); + return mystr + mykey.final("hex"); } // Try to decode the value function decode(message) { - - if (!crypto || !/[0-9A-Fa-f]{6}/g.test(message)) return message - + if (!crypto || !/[0-9A-Fa-f]{6}/g.test(message)) return message; try { - const mykey = crypto.createDecipher('aes-128-cbc', homeDirectory); - const mystr = mykey.update(message, 'hex', 'utf8') - return mystr + mykey.final('utf8'); + const mykey = crypto.createDecipher("aes-128-cbc", homeDirectory); + const mystr = mykey.update(message, "hex", "utf8"); + return mystr + mykey.final("utf8"); } catch { - return message + return message; } } function drill(o, callback) { - if (o && typeof o === 'object') { - const copy = { ...o } - for (let k in copy) copy[k] = drill(copy[k], callback) - return copy - } else return callback(o) + if (o && typeof o === "object") { + const copy = { ...o }; + for (let k in copy) copy[k] = drill(copy[k], callback); + return copy; + } else return callback(o); } function encodeObject(o) { - return drill(o, (v) => typeof v === 'string' ? encode(v) : v) + return drill(o, (v) => (typeof v === "string" ? encode(v) : v)); } function decodeObject(o) { - return drill(o, (v) => typeof v === 'string' ? decode(v) : v) + return drill(o, (v) => (typeof v === "string" ? decode(v) : v)); } - - class GlobalAppConfig { path = `${appDirectory}/config.json`; data = {}; @@ -67,14 +63,12 @@ class GlobalAppConfig { const exists = fs ? fs.existsSync(this.path) : localStorage[this.path]; if (exists) { const data = JSON.parse(fs ? fs.readFileSync(this.path) : localStorage.getItem(this.path)); - this.data = decodeObject(data) + this.data = decodeObject(data); } } save() { - - console.log('Saving', this.data, encodeObject(this.data)) - + console.log("Saving", this.data, encodeObject(this.data)); if (fs) fs.writeFileSync(this.path, JSON.stringify(this.data, null, 2)); else localStorage.setItem(this.path, JSON.stringify(this.data)); From eaac60edb2491a9ef76e1b3f2622ac6699a21991 Mon Sep 17 00:00:00 2001 From: Garrett Date: Mon, 25 Sep 2023 14:27:03 -0700 Subject: [PATCH 3/5] Use encoded --- src/renderer/src/progress/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/progress/index.js b/src/renderer/src/progress/index.js index b34d71995..85d3e711f 100644 --- a/src/renderer/src/progress/index.js +++ b/src/renderer/src/progress/index.js @@ -70,10 +70,10 @@ class GlobalAppConfig { } save() { - console.log("Saving", this.data, encodeObject(this.data)); + const encoded = encodeObject(this.data) - if (fs) fs.writeFileSync(this.path, JSON.stringify(this.data, null, 2)); - else localStorage.setItem(this.path, JSON.stringify(this.data)); + if (fs) fs.writeFileSync(this.path, JSON.stringify(encoded, null, 2)); + else localStorage.setItem(this.path, JSON.stringify(encoded)); } } From b2df37f004c8c08f28fc3bb762a67395362332b5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 21:27:19 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/renderer/src/progress/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/src/progress/index.js b/src/renderer/src/progress/index.js index 85d3e711f..2bb2be3bc 100644 --- a/src/renderer/src/progress/index.js +++ b/src/renderer/src/progress/index.js @@ -70,7 +70,7 @@ class GlobalAppConfig { } save() { - const encoded = encodeObject(this.data) + const encoded = encodeObject(this.data); if (fs) fs.writeFileSync(this.path, JSON.stringify(encoded, null, 2)); else localStorage.setItem(this.path, JSON.stringify(encoded)); From 01e841c0f6c05c9f16710d71f59c946e90dc76b9 Mon Sep 17 00:00:00 2001 From: Garrett Date: Wed, 27 Sep 2023 11:45:01 -0700 Subject: [PATCH 5/5] Fix updating saved state --- src/renderer/src/stories/pages/Page.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/src/stories/pages/Page.js b/src/renderer/src/stories/pages/Page.js index 0b49f0a5e..2a44e2200 100644 --- a/src/renderer/src/stories/pages/Page.js +++ b/src/renderer/src/stories/pages/Page.js @@ -100,7 +100,7 @@ export class Page extends LitElement { save = async (overrides, runBeforeSave = true) => { if (runBeforeSave) await this.beforeSave(); save(this, overrides); - this.info.states.saved = true; + if ("states" in this.info) this.info.states.saved = true; this.unsavedUpdates = false; };