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: perform integrity check on attachment download
thecodrr674c178301a8
diff --git a/apps/web/src/common/attachments.js b/apps/web/src/common/attachments.jsindex a8426aa6a..713639ea9 100644--- a/apps/web/src/common/attachments.js+++ b/apps/web/src/common/attachments.js@@ -7,7 +7,8 @@ export async function downloadAttachment(hash) {const downloadResult = await db.fs.downloadFile(attachment.metadata.hash,attachment.metadata.hash,- attachment.chunkSize+ attachment.chunkSize,+ attachment.metadata);if (!downloadResult) throw new Error("Failed to download file.");diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.jsindex 02c73db72..62c42b52f 100644--- a/apps/web/src/interfaces/fs.js+++ b/apps/web/src/interfaces/fs.js@@ -8,6 +8,7 @@ import { getNNCrypto } from "./nncrypto.stub";import hosts from "notes-core/utils/constants";import { sendAttachmentsProgressEvent } from "notes-core/common";import { saveAs } from "file-saver";+import { showToast } from "../utils/toast";const ABYTES = 17;const CHUNK_SIZE = 512 * 1024;@@ -267,7 +268,8 @@ function reportProgress(ev, { type, hash }) {}async function downloadFile(filename, requestOptions) {- const { url, headers, chunkSize, cancellationToken } = requestOptions;+ const { url, headers, chunkSize, cancellationToken, metadata } =+ requestOptions;if (await streamablefs.exists(filename)) return true;try {@@ -284,6 +286,7 @@ async function downloadFile(filename, requestOptions) {onDownloadProgress: (ev) =>reportProgress(ev, { type: "download", hash: filename }),});+const contentLength =response.headers["content-length"] || response.headers["Content-Length"];if (!isSuccessStatusCode(response.status) || contentLength === "0") {@@ -291,6 +294,13 @@ async function downloadFile(filename, requestOptions) {return false;}+ if (+ metadata &&+ !(await verify(response.data, metadata.hash, metadata.hashType))+ ) {+ throw new Error("File verification failed.");+ }+const distributor = new ChunkDistributor(chunkSize + ABYTES);distributor.fill(new Uint8Array(response.data));distributor.close();@@ -307,12 +317,28 @@ async function downloadFile(filename, requestOptions) {return true;} catch (e) {+ showToast("error", `Could not download file: ${e.message}`);console.error(e);reportProgress(undefined, { type: "download", hash: filename });return false;}}+/**+ *+ * @param {Uint8Array} data+ * @param {string} hash+ * @param {string} hashType+ */+async function verify(data, hash, hashType) {+ switch (hashType) {+ case "xxh64":+ return (await xxhash64(data)) === hash;+ default:+ return false;+ }+}+function exists(filename) {return streamablefs.exists(filename);}