Skip to content

Build your personal knowledge base with Trilium Notes

fix(core): sync-related issues

Elian Doraned808abd22fa
diff --git a/apps/server/src/crypto_provider.ts b/apps/server/src/crypto_provider.ts
index 6ddeecf9f3..c32f301797 100644
--- a/apps/server/src/crypto_provider.ts
+++ b/apps/server/src/crypto_provider.ts
@@ -26,4 +26,10 @@ export default class NodejsCryptoProvider implements CryptoProvider {
return randtoken.generate(length);
}
+ hmac(secret: string | Uint8Array, value: string | Uint8Array) {
+ const hmac = crypto.createHmac("sha256", Buffer.from(secret.toString(), "ascii"));
+ hmac.update(value.toString());
+ return hmac.digest("base64");
+ }
+
}
diff --git a/apps/server/src/services/utils.ts b/apps/server/src/services/utils.ts
index 38e41b9f42..9998413bd9 100644
--- a/apps/server/src/services/utils.ts
+++ b/apps/server/src/services/utils.ts
@@ -1,4 +1,4 @@
-import { utils as coreUtils } from "@triliumnext/core";
+import { getCrypto,utils as coreUtils } from "@triliumnext/core";
import chardet from "chardet";
import crypto from "crypto";
import { t } from "i18next";
@@ -49,10 +49,8 @@ export function fromBase64(encodedText: string) {
return Buffer.from(encodedText, "base64");
}
-export function hmac(secret: any, value: any) {
- const hmac = crypto.createHmac("sha256", Buffer.from(secret.toString(), "ascii"));
- hmac.update(value.toString());
- return hmac.digest("base64");
+export function hmac(secret: string | Uint8Array, value: string | Uint8Array) {
+ return getCrypto().hmac(secret, value);
}
/**
diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts
index bd48549de3..4f84b1ea01 100644
--- a/packages/trilium-core/src/index.ts
+++ b/packages/trilium-core/src/index.ts
@@ -44,6 +44,7 @@ export { default as bulk_actions } from "./services/bulk_actions";
export { default as hoisted_note } from "./services/hoisted_note";
export { default as special_notes } from "./services/special_notes";
export { default as date_notes } from "./services/date_notes";
+export { getCrypto } from "./services/encryption/crypto";
export { default as attribute_formatter} from "./services/attribute_formatter";
diff --git a/packages/trilium-core/src/services/encryption/crypto.ts b/packages/trilium-core/src/services/encryption/crypto.ts
index 87cd8fb6c0..4f747dd9c6 100644
--- a/packages/trilium-core/src/services/encryption/crypto.ts
+++ b/packages/trilium-core/src/services/encryption/crypto.ts
@@ -10,6 +10,8 @@ export interface CryptoProvider {
randomString(length: number): string;
createCipheriv(algorithm: "aes-128-cbc", key: Uint8Array, iv: Uint8Array): Cipher;
createDecipheriv(algorithm: "aes-128-cbc", key: Uint8Array, iv: Uint8Array): Cipher;
+ hmac(secret: string | Uint8Array, value: string | Uint8Array): string;
+
}
let crypto: CryptoProvider | null = null;
diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.ts
index 1cd56998f6..f3d8d7c79b 100644
--- a/packages/trilium-core/src/services/sync.ts
+++ b/packages/trilium-core/src/services/sync.ts
@@ -14,13 +14,14 @@ import { getSql } from "./sql/index.js";
import syncMutexService from "./sync_mutex.js";
import syncOptions from "./sync_options.js";
import syncUpdateService from "./sync_update.js";
-import { hmac, randomString, timeLimit } from "./utils/index.js";
+import { randomString, timeLimit } from "./utils/index.js";
import ws from "./ws.js";
import getInstanceId from "./instance_id.js";
import request, { CookieJar, ExecOpts } from "./request.js";
import entity_constructor from "../../src/becca/entity_constructor.js";
import becca_loader from "../becca/becca_loader.js";
import * as binary_utils from "./utils/binary.js";
+import { getCrypto } from "./encryption/crypto.js";
let proxyToggle = true;
@@ -119,7 +120,7 @@ async function doLogin(): Promise<SyncContext> {
const timestamp = dateUtils.utcNowDateTime();
const documentSecret = optionService.getOption("documentSecret");
- const hash = hmac(documentSecret, timestamp);
+ const hash = getCrypto().hmac(documentSecret, timestamp);
const syncContext: SyncContext = { cookieJar: {} };
const resp = await syncRequest<SyncResponse>(syncContext, "POST", "/api/login/sync", {
diff --git a/packages/trilium-core/src/services/sync_update.ts b/packages/trilium-core/src/services/sync_update.ts
index f3a52eff22..2d13c8cdbb 100644
--- a/packages/trilium-core/src/services/sync_update.ts
+++ b/packages/trilium-core/src/services/sync_update.ts
@@ -5,7 +5,7 @@ import { getLog } from "./log.js";
import { getSql } from "./sql/index.js";
import ws from "./ws.js";
import { default as eventService } from "./events.js";
-import entity_constructor from "src/becca/entity_constructor.js";
+import entity_constructor from "../becca/entity_constructor.js";
interface UpdateContext {
alreadyErased: number;