Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: perform dm5 integirty check on att. download

thecodrr321c0135a09b
diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.js
index 62c42b52f..f0cc8b793 100644
--- a/apps/web/src/interfaces/fs.js
+++ b/apps/web/src/interfaces/fs.js
@@ -9,7 +9,9 @@ import hosts from "notes-core/utils/constants";
import { sendAttachmentsProgressEvent } from "notes-core/common";
import { saveAs } from "file-saver";
import { showToast } from "../utils/toast";
+import SparkMD5 from "spark-md5";
+const sparkMD5 = new SparkMD5.ArrayBuffer();
const ABYTES = 17;
const CHUNK_SIZE = 512 * 1024;
const ENCRYPTED_CHUNK_SIZE = CHUNK_SIZE + ABYTES;
@@ -268,8 +270,7 @@ function reportProgress(ev, { type, hash }) {
}
async function downloadFile(filename, requestOptions) {
- const { url, headers, chunkSize, cancellationToken, metadata } =
- requestOptions;
+ const { url, headers, chunkSize, cancellationToken } = requestOptions;
if (await streamablefs.exists(filename)) return true;
try {
@@ -294,11 +295,12 @@ async function downloadFile(filename, requestOptions) {
return false;
}
- if (
- metadata &&
- !(await verify(response.data, metadata.hash, metadata.hashType))
- ) {
- throw new Error("File verification failed.");
+ let etag = response.headers["etag"] || response.headers["ETag"];
+ if (etag) {
+ etag = JSON.parse(etag);
+ if (!verify(response.data, etag, "md5")) {
+ throw new Error("File verification failed.");
+ }
}
const distributor = new ChunkDistributor(chunkSize + ABYTES);
@@ -326,14 +328,14 @@ async function downloadFile(filename, requestOptions) {
/**
*
- * @param {Uint8Array} data
+ * @param {ArrayBuffer} data
* @param {string} hash
- * @param {string} hashType
+ * @param {"md5"} hashType
*/
-async function verify(data, hash, hashType) {
+function verify(data, hash, hashType = "md5") {
switch (hashType) {
- case "xxh64":
- return (await xxhash64(data)) === hash;
+ case "md5":
+ return sparkMD5.append(data).end() === hash;
default:
return false;
}