A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
fix: check attachment hash before insertion
thecodrr2fa455fc55bd
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 4e9e2f642..3a244350f 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -23,13 +23,14 @@"esbuild": "^0.12.15","eventsource": "^1.0.7","fast-sort": "^2.1.1",+ "file-saver": "^2.0.5","framer-motion": "^3.3.0","hash-wasm": "^4.9.0","highlight.js": "^11.1.0","hotkeys-js": "^3.8.3","immer": "^8.0.1","just-debounce": "^1.1.0",- "localforage": "^1.7.3",+ "localforage": "^1.10.0","localforage-getitems": "https://github.com/thecodrr/localForage-getItems.git","notes-core": "npm:@streetwriters/notesnook-core@latest","print-js": "^1.6.0",@@ -43,9 +44,11 @@"react-scripts": "^4.0.3","react-scroll-sync": "^0.9.0","react-scrollbars-custom": "^4.0.25",+ "react-toggle": "^4.1.2","react-virtualized-auto-sizer": "^1.0.4","react-virtuoso": "^1.9.3","rebass": "^4.0.7",+ "streamsaver": "^2.0.5","timeago-react": "^3.0.2","tinymce": "5.8.1","uzip": "^0.20201231.0",@@ -59,6 +62,7 @@"@types/node-fetch": "^2.5.10","@types/quill": "^2.0.5","babel-eslint": "^10.1.0",+ "babel-plugin-transform-class-properties": "^6.24.1","chalk": "^4.1.0","dotenv": "^10.0.0","env-cmd": "^10.1.0",diff --git a/apps/web/src/components/editor/plugins/picker.js b/apps/web/src/components/editor/plugins/picker.jsindex 8982dd426..e3e7d4abf 100644--- a/apps/web/src/components/editor/plugins/picker.js+++ b/apps/web/src/components/editor/plugins/picker.js@@ -33,25 +33,33 @@ async function insertFile(editor) {})();async function pickFile() {+ const key = await getEncryptionKey();+const selectedFile = await showFilePicker({ acceptedFileTypes: "*/*" });if (!selectedFile) return;- const key = await getEncryptionKey();const buffer = await selectedFile.arrayBuffer();- const output = await fs.writeEncrypted(null, {- data: new Uint8Array(buffer),- type: "buffer",- key,- });+ const { hash, type: hashType } = await fs.hashBuffer(buffer);++ const output = db.attachments.exists(hash)+ ? {}+ : await fs.writeEncrypted(null, {+ data: new Uint8Array(buffer),+ type: "buffer",+ key,+ hash,+ });await db.attachments.add({...output,+ hash,+ hashType,filename: selectedFile.name,type: selectedFile.type,});return {- hash: output.hash,+ hash,filename: selectedFile.name,type: selectedFile.type,size: selectedFile.size,@@ -59,26 +67,33 @@ async function pickFile() {}async function pickImage() {+ const key = await getEncryptionKey();+const selectedImage = await showFilePicker({ acceptedFileTypes: "image/*" });if (!selectedImage) return;const { dataurl, buffer } = await compressImage(selectedImage, "buffer");- const key = await getEncryptionKey();+ const { hash, type: hashType } = await fs.hashBuffer(buffer);- const output = await fs.writeEncrypted(null, {- data: new Uint8Array(buffer),- type: "buffer",- key,- });+ const output = db.attachments.exists(hash)+ ? {}+ : await fs.writeEncrypted(null, {+ data: new Uint8Array(buffer),+ type: "buffer",+ key,+ hash,+ });await db.attachments.add({...output,+ hash,+ hashType,filename: selectedImage.name,type: selectedImage.type,});return {- hash: output.hash,+ hash,filename: selectedImage.name,type: selectedImage.type,size: selectedImage.size,@@ -87,6 +102,7 @@ async function pickImage() {}async function getEncryptionKey() {+ return { password: "helloworld" };const key = await db.user.getEncryptionKey();if (!key) throw new Error("No encryption key found. Are you logged in?");return key; // { password: "helloworld" };diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.jsindex b1312ab27..2787c5f86 100644--- a/apps/web/src/interfaces/fs.js+++ b/apps/web/src/interfaces/fs.js@@ -24,14 +24,12 @@ fs.hasItem = async function (key) {* 3. We encrypt the Uint8Array* 4. We save the encrypted Uint8Array*/-async function writeEncrypted(filename, { data, type, key }) {- const saveAsBuffer = localforage.supports(localforage.INDEXEDDB);-- if (type === "base64") data = new Uint8Array(Buffer.from(data, "base64"));- const { hash, type: hashType } = await hashBuffer(data);+async function writeEncrypted(filename, { data, type, key, hash }) {if (!filename) filename = hash;+ if (await fs.hasItem(filename)) return {};- if (await fs.hasItem(filename)) return { hash, hashType };+ const saveAsBuffer = localforage.supports(localforage.INDEXEDDB);+ if (type === "base64") data = new Uint8Array(Buffer.from(data, "base64"));const output = saveAsBuffer? await crypto.encryptBinary(key, data, "buffer")@@ -39,8 +37,6 @@ async function writeEncrypted(filename, { data, type, key }) {await fs.setItem(filename, output.cipher);return {- hash,- hashType,iv: output.iv,length: output.length,salt: output.salt,@@ -48,6 +44,11 @@ async function writeEncrypted(filename, { data, type, key }) {};}+/**+ *+ * @param {import("hash-wasm/dist/lib/util").IDataType} data+ * @returns+ */async function hashBuffer(data) {return {hash: await xxhash3(data),@@ -154,6 +155,7 @@ const FS = {downloadFile: cancellable(downloadFile),deleteFile,exists,+ hashBuffer,};export default FS;@@ -162,8 +164,8 @@ function isSuccessStatusCode(statusCode) {}function cancellable(operation) {- const source = axios.CancelToken.source();return function (filename, requestOptions) {+ const source = axios.CancelToken.source();requestOptions.cancellationToken = source.token;return {execute: () => operation(filename, requestOptions),