sokko-g/tests/toolbox-share-crypto.test.mjs
Shinuwa bec9ea5b2f
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 20m39s
Validate and deploy Sokko G / deploy (push) Successful in 12s
Send a toolbox through an encrypted link
Sharing a toolbox meant exporting a JSON file and passing it around by
hand. A toolbox can now be sent from the site while staying local first:
the browser compresses and encrypts the v2 export with AES-GCM, and the
key lives only in the link fragment, so the server keeps for at most
24 hours a copy it cannot read.

- Add the /api/shares service: upload, metadata, download and revoke,
  with files in SHARES_DIR, a purge every 10 minutes, and single-use
  links (the default) deleted only once fully downloaded.
- Cap the server load: 50 MiB per share, total storage, free disk
  space, daily volume, concurrent and stalled uploads, reads per link,
  plus a wide per-connection cap kept in memory without storing IPs.
- Limit each browser to 2 shares and 100 MiB per day and 2 active
  links, and show the existing link of a toolbox already shared.
- Add the send dialog (card and game drawer) and the /receive page,
  which previews or directly imports through the existing import path.
- Move shared HTTP helpers to services/http.js for both APIs.
- Document the format, delivery, server layout and privacy wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:26:05 +01:00

51 lines
2.5 KiB
JavaScript

// Rôle : vérifie que le chiffrement d'un envoi de toolbox est réversible avec la bonne clé seulement.
import assert from "node:assert/strict";
import { test } from "node:test";
import { decryptSharePayload, encryptSharePayload, isShareKey, ShareCryptoError } from "../website/src/features/toolboxes/sharing/shareCrypto.js";
const sample = {
version: 2,
toolbox: { id: "tbx1", name: "Monster Hunter Wilds — été", modules: [{ id: "mod1", type: "checklist", title: "Quêtes" }] },
modules: { mod1: { sections: [{ id: "sec1", title: "Rang élevé", items: [] }] } }
};
async function rejectsWith(promise, code) {
await assert.rejects(promise, (error) => error instanceof ShareCryptoError && error.code === code);
}
test("an encrypted share decrypts back to the same payload", async () => {
const { bytes, key } = await encryptSharePayload(sample);
assert.ok(isShareKey(key));
assert.equal(new TextDecoder().decode(bytes.subarray(0, 4)), "SKS1");
assert.equal(new TextDecoder().decode(bytes).includes("Monster"), false);
assert.deepEqual(await decryptSharePayload(bytes, key), sample);
assert.deepEqual(await decryptSharePayload(bytes.buffer, key), sample);
});
test("each share uses its own key and IV", async () => {
const first = await encryptSharePayload(sample);
const second = await encryptSharePayload(sample);
assert.notEqual(first.key, second.key);
assert.notDeepEqual(first.bytes.subarray(4, 16), second.bytes.subarray(4, 16));
});
test("a wrong, missing or malformed key is refused", async () => {
const { bytes } = await encryptSharePayload(sample);
const other = await encryptSharePayload(sample);
await rejectsWith(decryptSharePayload(bytes, other.key), "SHARE_KEY_INVALID");
await rejectsWith(decryptSharePayload(bytes, ""), "SHARE_KEY_INVALID");
await rejectsWith(decryptSharePayload(bytes, "abc"), "SHARE_KEY_INVALID");
await rejectsWith(decryptSharePayload(bytes, "not+base64url/"), "SHARE_KEY_INVALID");
assert.equal(isShareKey(other.key.slice(1)), false);
});
test("tampered content or an unknown format is refused", async () => {
const { bytes, key } = await encryptSharePayload(sample);
const tampered = bytes.slice();
tampered[tampered.length - 1] ^= 1;
await rejectsWith(decryptSharePayload(tampered, key), "SHARE_KEY_INVALID");
const header = bytes.slice();
header[0] = 0;
await rejectsWith(decryptSharePayload(header, key), "SHARE_FORMAT");
await rejectsWith(decryptSharePayload(new Uint8Array(10), key), "SHARE_FORMAT");
});