winters27_obsidian-byoc/tests/encryptOpenSSL.test.ts
winters27 203238db84 chore(lint): mirror ReviewBot config + fix OAuth DefinePlugin regression
Add eslint.bot.config.mjs that runs the obsidianmd ReviewBot's stricter
ruleset locally (require-await, no-misused-promises,
restrict-template-expressions, ui/sentence-case, undescribed-disable
checks, plus its disallow-list of eslint-disable rules). Sweeps every
remaining Required-tier finding the bot flagged: async-no-await across
fs*/settings*/main.ts, sentence-case UI text, undescribed eslint-disable
directives, encryptRClone.worker.ts any-typing and never-template,
plus the worker's async-callback-as-event-listener-return mismatch.

Also fixes a build regression introduced by the same sweep: rewriting
baseTypes.ts from `globalThis.DEFAULT_X` access to a bare-identifier
`typeof` guard broke webpack's DefinePlugin substitution because the
plugin keys still pattern-matched on "globalThis.X". Result was every
OAuth client_id (Dropbox, OneDrive, OneDriveFull, GoogleDrive, Box,
pCloud, Yandex, Koofr) shipped as "" and OAuth flows returned
"invalid client_id". DefinePlugin keys now match the bare identifier.
Verified by grepping the deployed main.js for the pCloud client_id
literal post-build.
2026-04-27 06:30:32 -07:00

132 lines
4.5 KiB
TypeScript

import { strict as assert } from "assert";
import * as fs from "fs";
import * as path from "path";
import {
decryptArrayBuffer,
decryptBase32ToString,
encryptArrayBuffer,
encryptStringToBase32,
encryptStringToBase64url,
getSizeFromEncToOrig,
getSizeFromOrigToEnc,
} from "../src/encryptOpenSSL";
import { base64ToBase64url, bufferToArrayBuffer } from "../src/misc";
describe("Encryption OpenSSL tests", () => {
beforeEach(() => {
global.window = {
crypto: require("crypto").webcrypto,
} as any;
(global as any).activeWindow = global.window;
});
it("should encrypt string", async () => {
const k = "dkjdhkfhdkjgsdklxxd";
const password = "hey";
assert.notEqual(await encryptStringToBase32(k, password), k);
});
it("should encrypt string and return different results each time", async () => {
const k = "dkjdhkfhdkjgsdklxxd";
const password = "hey";
const res1 = await encryptStringToBase32(k, password);
const res2 = await encryptStringToBase32(k, password);
assert.notEqual(res1, res2);
});
it("should raise error using different password", async () => {
const k = "secret text";
const password = "hey";
const password2 = "hey2";
const enc = await encryptStringToBase32(k, password);
await assert.rejects(decryptBase32ToString(enc, password2));
});
it("should encrypt and decrypt string and get the same result returned", async () => {
const k = "jfkkjkjbce7983ycdeknkkjckooAIUHIDIBIE((*BII)njD/d/dd/d/sjxhux";
const password = "hfiuibec989###oiu982bj1`";
const enc = await encryptStringToBase32(k, password);
// console.log(enc);
const dec = await decryptBase32ToString(enc, password);
// console.log(dec);
assert.equal(dec, k);
});
it("should encrypt text file and get the same result as openssl", async () => {
const fileContent = (
await fs.readFileSync(
path.join(__dirname, "static_assets", "sometext.txt")
)
).toString("utf-8").replace(/\r\n/g, "\n");
const password = "somepassword";
const saltHex = "8302F586FAB491EC";
const enc = await encryptStringToBase64url(
fileContent,
password,
undefined,
saltHex
);
// two command returns same result:
// cat ./sometext.txt | openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -base64 -pass pass:somepassword
// openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -base64 -pass pass:somepassword -in ./sometext.txt
const opensslBase64Res =
"U2FsdGVkX1+DAvWG+rSR7BPXMnlvSSVGMdjsx7kE1CTH+28P+yAZRdDGgFWMGkMd";
// we output base32, so we need some transformation
const opensslBase64urlRes = base64ToBase64url(opensslBase64Res);
assert.equal(enc, opensslBase64urlRes);
});
it("should encrypt and decrypt binary ArrayBuffer symmetrically", async () => {
const fileArrBuf = bufferToArrayBuffer(
Buffer.alloc(132, 0x42)
) as ArrayBuffer;
const password = "somepassword";
const enc = await encryptArrayBuffer(fileArrBuf, password);
const dec = await decryptArrayBuffer(enc, password);
assert.deepEqual(Buffer.from(dec), Buffer.from(fileArrBuf));
});
it("should encrypt binary ArrayBuffer not deterministically", async () => {
const fileArrBuf = bufferToArrayBuffer(
Buffer.alloc(132, 0x42)
) as ArrayBuffer;
const password = "somepassword";
const res1 = await encryptArrayBuffer(fileArrBuf, password);
const res2 = await encryptArrayBuffer(fileArrBuf, password);
assert.ok(!Buffer.from(res1).equals(Buffer.from(res2)));
});
it("should get size from origin to encrypted correctly", () => {
assert.throws(() => getSizeFromOrigToEnc(-1));
assert.throws(() => getSizeFromOrigToEnc(0.5));
assert.equal(getSizeFromOrigToEnc(0), 32);
assert.equal(getSizeFromOrigToEnc(15), 32);
assert.equal(getSizeFromOrigToEnc(16), 48);
assert.equal(getSizeFromOrigToEnc(31), 48);
assert.equal(getSizeFromOrigToEnc(32), 64);
assert.equal(getSizeFromOrigToEnc(14787203), 14787232);
});
it("should get size from encrypted to origin correctly", () => {
assert.throws(() => getSizeFromEncToOrig(-1));
assert.throws(() => getSizeFromEncToOrig(30));
assert.deepEqual(getSizeFromEncToOrig(32), {
minSize: 0,
maxSize: 15,
});
assert.deepEqual(getSizeFromEncToOrig(48), {
minSize: 16,
maxSize: 31,
});
assert.throws(() => getSizeFromEncToOrig(14787231));
const { minSize, maxSize } = getSizeFromEncToOrig(14787232);
assert.ok(minSize <= 14787203 && 14787203 <= maxSize);
});
});