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: offset out of bounds on decrypt stream
thecodrrae350d96cd08
diff --git a/apps/web/packages/nncrypto/index.ts b/apps/web/packages/nncrypto/index.tsindex e2275bc77..70853e184 100644--- a/apps/web/packages/nncrypto/index.ts+++ b/apps/web/packages/nncrypto/index.ts@@ -1,4 +1,5 @@import { ready } from "libsodium-wrappers";+import { Chunk } from "streamablefs/dist/src/types";import Decryption from "./src/decryption";import Encryption from "./src/encryption";import { INNCrypto, IStreamable } from "./src/interfaces";@@ -65,7 +66,12 @@ export class NNCrypto implements INNCrypto {if (!chunk) break;const { data, final } = chunk;- await stream.write(encryptionStream.write(data, final));++ const encryptedChunk: Chunk = {+ data: encryptionStream.write(data, final),+ final,+ };+ await stream.write(encryptedChunk);if (final) break;}@@ -84,7 +90,11 @@ export class NNCrypto implements INNCrypto {if (!chunk) break;const { data, final } = chunk;- await stream.write(decryptionStream.read(data));+ const decryptedChunk: Chunk = {+ data: decryptionStream.read(data),+ final,+ };+ await stream.write(decryptedChunk);if (final) break;}diff --git a/apps/web/packages/nncrypto/src/interfaces.ts b/apps/web/packages/nncrypto/src/interfaces.tsindex 44bbddaf6..832d165c2 100644--- a/apps/web/packages/nncrypto/src/interfaces.ts+++ b/apps/web/packages/nncrypto/src/interfaces.ts@@ -9,7 +9,7 @@ import {export interface IStreamable {read(): Promise<Chunk | undefined>;- write(chunk: Uint8Array | undefined): Promise<void>;+ write(chunk: Chunk | undefined): Promise<void>;}export interface INNCrypto {diff --git a/apps/web/packages/nncryptoworker/index.ts b/apps/web/packages/nncryptoworker/index.tsindex 9d67bdc30..8cbd6f117 100644--- a/apps/web/packages/nncryptoworker/index.ts+++ b/apps/web/packages/nncryptoworker/index.ts@@ -83,7 +83,14 @@ export class NNCryptoWorker implements INNCrypto {if (!this.workermodule) throw new Error("Worker module is not ready.");if (!this.worker) throw new Error("Worker is not ready.");- const eventListener = await this.createWorkerStream(streamId, stream);+ const eventListener = await this.createWorkerStream(+ streamId,+ stream,+ () => {+ if (this.worker)+ this.worker.removeEventListener("message", eventListener);+ }+ );this.worker.addEventListener("message", eventListener);const iv = await this.workermodule.createEncryptionStream(streamId, key);this.worker.removeEventListener("message", eventListener);@@ -101,29 +108,43 @@ export class NNCryptoWorker implements INNCrypto {if (!this.workermodule) throw new Error("Worker module is not ready.");if (!this.worker) throw new Error("Worker is not ready.");- const eventListener = await this.createWorkerStream(streamId, stream);+ const eventListener = await this.createWorkerStream(+ streamId,+ stream,+ () => {+ if (this.worker)+ this.worker.removeEventListener("message", eventListener);+ }+ );this.worker.addEventListener("message", eventListener);await this.workermodule.createDecryptionStream(streamId, iv, key);- await stream.write(undefined);this.worker.removeEventListener("message", eventListener);}private async createWorkerStream(streamId: string,- stream: IStreamable+ stream: IStreamable,+ done: () => void): Promise<EventListenerObject> {const readEventType = `${streamId}:read`;const writeEventType = `${streamId}:write`;+ let finished = false;return {handleEvent: async (ev: MessageEvent) => {+ if (finished) return;+const { type } = ev.data;if (type === readEventType) {const chunk = await stream.read();if (!chunk || !this.worker) return;this.worker.postMessage({ type, data: chunk }, [chunk.data.buffer]);} else if (type === writeEventType) {- const { data } = ev.data as Chunk;- await stream.write(data);+ const chunk = ev.data.data as Chunk;+ await stream.write(chunk);+ if (chunk.final) {+ finished = true;+ done();+ }}},};diff --git a/apps/web/packages/nncryptoworker/src/workerstream.ts b/apps/web/packages/nncryptoworker/src/workerstream.tsindex a2bd0d99f..b76ac43fe 100644--- a/apps/web/packages/nncryptoworker/src/workerstream.ts+++ b/apps/web/packages/nncryptoworker/src/workerstream.ts@@ -23,8 +23,8 @@ export default class WorkerStream/*** @param {Uint8Array} chunk*/- async write(chunk: Uint8Array): Promise<void> {- postMessage({ type: `${this.id}:write`, data: chunk }, [chunk.buffer]);+ async write(chunk: Chunk): Promise<void> {+ postMessage({ type: `${this.id}:write`, data: chunk }, [chunk.data.buffer]);}}diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.jsindex 13b193e94..1493c4ff9 100644--- a/apps/web/src/interfaces/fs.js+++ b/apps/web/src/interfaces/fs.js@@ -52,13 +52,13 @@ async function writeEncryptedFile(file, key, hash) {data: chunk,};},- write: (chunk) => {- encrypted += chunk.length - ABYTES;+ write: async (chunk) => {+ encrypted += chunk.data.length - ABYTES;reportProgress({ total: file.size, loaded: encrypted },{ type: "encrypt", hash });- return fileHandle.write(chunk);+ await fileHandle.write(chunk.data);},},file.name@@ -134,8 +134,7 @@ async function readEncrypted(filename, key, cipherData) {}const reader = fileHandle.getReader();- const ENCRYPTED_SIZE = fileHandle.file.size + fileHandle.file.chunks * ABYTES;- const plainText = new Uint8Array(ENCRYPTED_SIZE);+ const plainText = new Uint8Array(fileHandle.file.size);let offset = 0;const crypto = await getNNCrypto();@@ -148,10 +147,8 @@ async function readEncrypted(filename, key, cipherData) {return value;},write: async (chunk) => {- if (!chunk) return;-plainText.set(chunk, offset);- offset += chunk.length;+ offset += chunk.data.length;},},filename@@ -327,8 +324,7 @@ async function saveFile(filename, { key, iv, name, type }) {return value;},write: async (chunk) => {- if (!chunk) return;- else blobParts.push(chunk);+ blobParts.push(chunk.data);},},filename